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,
    https://ninjatrader.com/support/foru...79&postcount=7

    Please let us know if you need further assistance.
    Last edited by NinjaTrader_AlanP; 11-30-2017, 12: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 vantojo, Today, 09:32 AM
      1 response
      4 views
      0 likes
      Last Post NinjaTrader_Kate  
      Started by FelipeRXP, Today, 08:40 AM
      2 responses
      9 views
      0 likes
      Last Post FelipeRXP  
      Started by petedian, Today, 08:44 AM
      0 responses
      13 views
      0 likes
      Last Post petedian  
      Started by J0shTrader, Yesterday, 12:29 PM
      1 response
      2 views
      0 likes
      Last Post NinjaTrader_ChrisL  
      Started by bc24fl, Today, 08:07 AM
      1 response
      10 views
      0 likes
      Last Post NinjaTrader_ChrisL  
      Working...
      X