Announcement

Collapse
No announcement yet.

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 Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    576 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    334 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    101 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    553 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    551 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X