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

Serializing Objects

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

    Serializing Objects

    I want to serialize some objects i get when i run a backtest but I am having some problems reading back. I was wondering if there are any good examples of how to do this.

    I created an object and use the constructors along the lines of this link


    I dont get any errors writing or reading but when I try to assign myObject the serialized info I get an "Unable to find assembly" error.

    any idea

    Code:
    TimeSobject newTimeObject = new TimeSobject();
    newTimeObject.barCount= CurrentBar;
    newTimeObject.closePx = 9999;
    Print("serilization srted");
    		Stream stream = File.Open(Cbi.Core.UserDataDir.ToString() + "Cereal.osl", FileMode.Create);
    			
    			    BinaryFormatter bin = new BinaryFormatter();
    			    bin.Serialize(stream, newTimeObject);
    			
    		stream.Close();	
    		Print("serilization complete");
    		
    			newTimeObject = null;
    			
    		stream = File.Open(Cbi.Core.UserDataDir.ToString() + "Cereal.osl", FileMode.Open);
    			Print("serilization 1");
    			    bin = new BinaryFormatter();
    Print("serilization 2");
    			    newTimeObject = (TimeSobject)bin.Deserialize(stream);  //Error here!
    	Print("serilization 3");		
    				stream.Close();	
    			Print("serilization 4");
    			Print(newTimeObject.closePx.ToString() + " is the close price from serilzation");
    And my class

    Code:
    public TimeSobject(SerializationInfo info, StreamingContext ctxt)
    {
        //Get the values from info and assign them to the appropriate properties
        barCount = (int)info.GetValue("barCount", typeof(int));
        closePx = (double)info.GetValue("ClosePX", typeof(double));
    }
            
    //Serialization function.
    public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
    {
        //You can use any custom name for your name-value pair. But make sure you
        // read the values with the same name. For ex:- If you write closePx as "ClosePX"
        // then you should read the same with "ClosePX"
        info.AddValue("barCount", barCount);
        info.AddValue("ClosePX", closePx);
    }

    #2
    Hello eurostoxx trader,

    Thank you for your post.

    Would you be able to provide the full strategy .cs file to test on our end? If so, please attach the .cs file for the strategy to your response. Or if you wish please send it to support[at]ninjatrader[dot]com with 'ATTN: Patrick - 1022204' in the subject line.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by mmckinnm, Today, 01:34 PM
    0 responses
    1 view
    0 likes
    Last Post mmckinnm  
    Started by f.saeidi, Today, 01:32 PM
    0 responses
    1 view
    0 likes
    Last Post f.saeidi  
    Started by traderqz, Today, 12:06 AM
    9 responses
    16 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Started by kevinenergy, 02-17-2023, 12:42 PM
    117 responses
    2,766 views
    1 like
    Last Post jculp
    by jculp
     
    Started by Mongo, Today, 11:05 AM
    5 responses
    15 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Working...
    X