Announcement

Collapse
No announcement yet.

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 NullPointStrategies, Today, 05:17 AM
      0 responses
      43 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      124 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      65 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      42 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      46 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X