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

XML Serialization revisited

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

    #16
    Hi snaphook,

    for simplicity you have 2 function:

    Serialize(int T1target){}

    and int T1Target=Deserialize(){}

    Serialize writes the parameter to some file and Deserialize() reads it back in. For simplicity here the variable is a single int. In reality you would serialize your whole strategy class MyStrategy : Strategy
    By setting the [XmlIgnore] attribute you define which parameters to save.

    You should find examples on XmlSerialize functions either in the forum or on MSDN site or some other C# site.


    Ok.

    Once you have the Serialize and Deserialize which dipend on your parameters


    then put

    Dispose(){
    Serialize()
    ... other stuff
    base.Dispose();
    }

    in your class to automatically save the parameters

    you do then:

    [Description("Target 1in ticks")]
    [Gui.Design.DisplayName("\tT1 Target")]
    [Category("Parameters")]
    public int T1_Target
    {
    get { return t1; }
    set {
    value=Deserialize(); // instead from GUI you read in value from your file
    // this should work as the get function is called later from the GUI
    //(to show in the properties box)
    t1 = Math.Max(1, value);
    }
    }


    be careful with Deserialize,as it might be called when Initialize() is not yet called, so you are restricted concerning access to class variables. check the log etc

    regards
    andreas



    Last edited by zweistein; 04-28-2009, 06:19 AM.

    Comment


      #17
      I am just so thick and I apologize for that. Do you mind holding my hand just a bit longer?

      My Serialize function:
      protected void SerializeSettings()
      {
      Print("Saving Current Settings");//Note to output window
      DWM_AutoTrader dwmat = new DWM_AutoTrader();
      dwmat.TypeEntry = typeEntry;
      dwmat.T1_Target = t1;
      dwmat.T2_Target = t2;
      dwmat.T3_Target = t3;
      .
      .
      .
      }

      My Deserialize function:
      protected void DeserializeSettings()
      {
      Print("Retrieving Settings");//Note to output window
      DWM_AutoTrader dwmat = new DWM_AutoTrader();
      XmlSerializer mySerializer = new xmlSerializer(typeof(DWM_AutoTrader));
      using (FileStream fs = File.OpenRead(@filePath))
      {
      dwmat = (DWM_AutoTrader)mySerializer.Deserialize(fs);

      typeEntry = dwmat.TypeEntry;
      t1 = dwmat.T1_Target;
      t2 = dwmat.T2_Target;
      t3 = dwmat.T3_Target;
      .
      .
      .
      }

      This works great but you must attach the strategy and click OK for DeserializeSettings() to retrieve the saved settings. If I set the t1 properties to that below, I get an error because the DeserializeSettings() function returns void.

      Properties for t1:
      [Description("Target 1. Set to 0 for no target.")]
      [Gui.Design.DisplayName("\tT1 Target")]
      [Category("Parameters")]
      public int T1_Target
      {
      get { return t1; }
      set { value=DeserializeSettings();
      t1 = Math.Max(0, value); }
      }

      Thanks,
      Snap

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by doihaveto13, Today, 12:46 PM
      1 response
      3 views
      0 likes
      Last Post NinjaTrader_Zachary  
      Started by owen5819, 05-11-2024, 02:24 PM
      3 responses
      20 views
      0 likes
      Last Post owen5819  
      Started by Vitamite, Today, 12:48 PM
      0 responses
      5 views
      0 likes
      Last Post Vitamite  
      Started by MrAdamSK, 04-02-2024, 11:25 PM
      3 responses
      41 views
      0 likes
      Last Post NinjaTrader_Zachary  
      Started by lakman184, 03-24-2024, 01:30 PM
      7 responses
      36 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Working...
      X