Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Ninja cannot create strategy after recompilation

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    #16
    Hello rfsettling,

    You are attempting to serialize a custom class, which is out of the scope of what our scripting desk supports. This is so that we can maintain a high level of service for all of our clients.

    There is a way to do what you’re looking to do, and if you’d like I can have someone reach out with a list of third parties who specialize in C# and can help you with this.

    Chelsea did just upload a sample which you may find of interest,


    Please let us know if you need further assistance.
    Last edited by NinjaTrader_AlanP; 11-30-2017, 01:18 PM.
    Alan P.NinjaTrader Customer Service

    Comment


      #17
      Solution found

      For those people who will run across the problem, like me. It can be solved by fixing CopyTo method.

      Code:
       public override void CopyTo(NinjaScript ninjaScript)
              {
                  var ninjaScriptState = ninjaScript.State;
      
                  try
                  {
                      base.CopyTo(ninjaScript);
                  }
                  catch 
                  {
                      //State property must not be copied
                      var aStateProp = ninjaScript.GetType().GetProperty("State");
                      if (aStateProp != null)
                          aStateProp.SetValue(ninjaScript, ninjaScriptState);
      
                      PropertyInfo[] srcFields = this.GetType().GetProperties(
                          BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty);
      
                      PropertyInfo[] destFields = ninjaScript.GetType().GetProperties(
                          BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty);
      
                      foreach (var src in srcFields)
                      {
                          if (src.Name == "State")
                              continue;
      
                          var aXmlIgnore = src.GetCustomAttribute<XmlIgnoreAttribute>();
                          if (aXmlIgnore != null)
                              continue;
                          var dest = destFields.FirstOrDefault(x => x.Name == src.Name);
                          if (dest != null && dest.CanWrite)
                          {
                              var aValue = src.GetValue(this, null);
                              //this is what is missed in Ninja internals
                              if (src.PropertyType==dest.PropertyType)
                                  dest.SetValue(ninjaScript, aValue, null);
                              //Different types. cannot just assign value
                              else
                              {
                                  //copying value via xml serialization
                                  using (var writer = new StringWriter())
                                  {
                                      new XmlSerializer(aValue.GetType()).Serialize(writer, aValue);
      
                                      using (TextReader reader = new StringReader(writer.ToString()))
                                      {
                                          var serializer = new XmlSerializer(dest.PropertyType);
                                          var aNewValue = serializer.Deserialize(reader);
                                          dest.SetValue(ninjaScript, aNewValue, null);
                                      }
                                  }
                              }
      
                          }
                      }
                  }
              }

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Rapine Heihei, Today, 08:19 PM
      1 response
      8 views
      0 likes
      Last Post NinjaTrader_Manfred  
      Started by Rapine Heihei, Today, 08:25 PM
      0 responses
      6 views
      0 likes
      Last Post Rapine Heihei  
      Started by f.saeidi, Today, 08:01 PM
      1 response
      9 views
      0 likes
      Last Post NinjaTrader_Manfred  
      Started by Rapine Heihei, Today, 07:51 PM
      0 responses
      8 views
      0 likes
      Last Post Rapine Heihei  
      Started by frslvr, 04-11-2024, 07:26 AM
      5 responses
      98 views
      1 like
      Last Post caryc123  
      Working...
      X