Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Serialize data on disk

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

    Serialize data on disk

    hi,
    i ask information
    how to serialize the data provided to my indicator?

    In programming in visual studio I use the class
    Serializable <T> .... but NinjaTrader does not know how to use it

    Example:

    Last edited by turbofib; 07-15-2016, 03:34 AM.

    #2
    Hello turbofib,

    Thank you for writing in.

    Please take a look at this reference sample on our support forum: http://ninjatrader.com/support/forum...43&postcount=2

    While the example in the reference above pertains to a Brush, you would do the same with a DateTime.

    An example:

    Code:
    [XmlIgnore]
    [Browsable(false)]
    public DateTime MyDateTime
    {get; set; }
    
    [NinjaScriptProperty]
    [Display(Name="", Order=1, GroupName="General",Description="")]
    public string MyDateTimeSerialize
    {
         get { return MyDateTime.ToString(); }
         set { MyDateTime = DateTime.Parse(value); }
    }
    Please, let us know if we may be of further assistance.
    Zachary G.NinjaTrader Customer Service

    Comment


      #3
      hi,,
      i want to save data in file
      ex: Ts.Dat(10/02/2015MorningStar,15/07/2015HighCandle.....other data) and i want to read it in text file..
      it's possibile?

      Serialize and deserialize file..

      Do you have understand this question? i want write in a file
      Last edited by turbofib; 07-15-2016, 10:47 AM.

      Comment


        #4
        Hello turbofib,

        We have a reference sample on our support forum detailing how you can use StreamWriter to write to a text file: http://ninjatrader.com/support/forum...ead.php?t=3475

        We also have a reference for using StreamReader to read from a text file: http://ninjatrader.com/support/forum...ead.php?t=3476
        Zachary G.NinjaTrader Customer Service

        Comment


          #5
          i apply it in chart
          Symbol : ES 09-16 TimeFrame: Daily

          See pics:



          See Manage Attachments..

          But i see problem.....it's not complete ( Printing up to 20160519)
          Attached Files
          Last edited by turbofib; 07-16-2016, 10:58 AM.

          Comment


            #6
            Hello turbofib,

            The sample will print out a line on every new call of OnBarUpdate().

            If all lines have not been outputted, this simply means you do not have enough bars on your chart to call every line of your file.

            You will need to modify the sample if you wish for every line to be outputted.

            What you can do is place the print at the end of the while ((line = sr.ReadLine()) != null) loop. I would suggest using a boolean so the while loop does not execute on every call of OnBarUpdate() or you will just keep getting the same prints over and over again.
            Zachary G.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_ZacharyG View Post
              Hello turbofib,


              What you can do is place the print at the end of the while ((line = sr.ReadLine()) != null) loop. I would suggest using a boolean so the while loop does not execute on every call of OnBarUpdate() or you will just keep getting the same prints over and over again.
              you write :" use sr.ReadLine...but i'm used SampeStreamWriter not SampeStreamReader
              sr.ReadLine don't exist in the following code


              namespace NinjaTrader.NinjaScript.Indicators
              {
              public class SampleStreamWriter : Indicator
              {
              #region Variables
              // This sets the path in which the text file will be created.
              private string path;

              // Creates a StreamWriter and a StreamReader object
              private StreamWriter sw;
              #endregion

              protected override void OnStateChange()
              {
              if(State == State.SetDefaults)
              {
              Calculate = Calculate.OnBarClose;
              Name = "Sample stream writer";
              path = NinjaTrader.Core.Globals.UserDataDir + "MyTestFile.txt";
              }
              // Necessary to call in order to clean up resources used by the StreamWriter object
              else if(State == State.Terminated)
              {
              if (sw != null)
              {
              sw.Dispose();
              sw = null;
              }
              }
              }

              protected override void OnBarUpdate()
              {
              /* The try-catch block is used for error handling.
              In this case it is used to ensure you only have one stream object operating on the same file at any given moment. */
              try
              {
              // If file at 'path' doesn't exist it will create the file. If it does exist it will append the file.
              if (CurrentBar == 0)
              sw = File.AppendText(path);

              // This is the output of all lines. The output format is as follows: Date Open High Low Close
              sw.WriteLine(ToDay(Time[0]) + " " + Open[0] + " " + High[0] + " " + Low[0] + " " + Close[0]);
              }
              catch (Exception)
              {
              // Outputs the error to the log
              Log("You cannot write and read from the same file at the same time. Please remove SampleStreamReader.", NinjaTrader.Cbi.LogLevel.Error);
              throw;
              }
              }
              }
              }

              Comment


                #8
                Another question.....
                you say : "If all lines have not been outputted, this simply means you do not have enough bars on your chart to call every line of your file."
                i try to do this :
                Print("CurrentBar " + CurrentBar + "Date " + ToDay(Time[0]));
                // This is the output of all lines. The output format is as follows: Date Open High Low Close

                sw.WriteLine(CurrentBar + " " + ToDay(Time[0]) + " " + Open[0] + " " + High[0] + " " + Low[0] + " " + Close[0]);
                i see it's different ...(see attached)

                i don't understand what is the mistake
                Attached Files
                Last edited by turbofib; 07-19-2016, 04:15 PM.

                Comment


                  #9
                  Hello turbofib,

                  My previous post was referring to StreamReader. As you are instead referring to the StreamWriter reference script, please disregard what I have stated in my previous post. I apologize for the misunderstanding.

                  I am investigating into this further.
                  Zachary G.NinjaTrader Customer Service

                  Comment


                    #10
                    Hello turbofib,

                    Please modify the script as shown below:

                    Code:
                    protected override void OnBarUpdate()
                    {
                         sw = File.AppendText(path);
                         sw.WriteLine(ToDay(Time[0]) + " " + Open[0] + " " + High[0] + " " + Low[0] + " " + Close[0]);
                         sw.Close();
                    }
                    The sample on our forum will be modified and updated with this change.

                    Please, let us know if we may be of further assistance.
                    Zachary G.NinjaTrader Customer Service

                    Comment


                      #11
                      yet is not correct



                      //
                      // Copyright (C) 2007, NinjaTrader LLC <www.ninjatrader.com>.
                      // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
                      //
                      #region Using declarations
                      using System;
                      using System.Collections.Generic;
                      using System.ComponentModel;
                      using System.ComponentModel.DataAnnotations;
                      using System.Linq;
                      using System.Text;
                      using System.Windows.Media;
                      using System.Xml.Serialization;
                      #endregion

                      // Add this to your declarations to use StreamWriter
                      using System.IO;

                      // This namespace holds all indicators and is required. Do not change it.
                      namespace NinjaTrader.NinjaScript.Indicators
                      {
                      public class SampleStreamWriter : Indicator
                      {
                      #region Variables
                      // This sets the path in which the text file will be created.
                      private string path;

                      // Creates a StreamWriter and a StreamReader object
                      private StreamWriter sw;
                      #endregion

                      protected override void OnStateChange()
                      {
                      if(State == State.SetDefaults)
                      {
                      Calculate = Calculate.OnBarClose;
                      Name = "Sample stream writer";
                      path = NinjaTrader.Core.Globals.UserDataDir + "MyTestFile.txt";
                      }
                      // Necessary to call in order to clean up resources used by the StreamWriter object
                      else if(State == State.Terminated)
                      {
                      if (sw != null)
                      {
                      sw.Dispose();
                      sw = null;
                      }
                      }
                      }

                      protected override void OnBarUpdate()
                      { //ciao
                      /* The try-catch block is used for error handling.
                      In this case it is used to ensure you only have one stream object operating on the same file at any given moment. */
                      /*
                      try
                      {
                      // If file at 'path' doesn't exist it will create the file. If it does exist it will append the file.
                      if (CurrentBar == 0)
                      sw = File.AppendText(path);
                      Print("CurrentBar " + CurrentBar + "Date " + ToDay(Time[0]));
                      // This is the output of all lines. The output format is as follows: Date Open High Low Close
                      sw.WriteLine(CurrentBar + " " + ToDay(Time[0]) + " " + Open[0] + " " + High[0] + " " + Low[0] + " " + Close[0]);
                      }
                      catch (Exception)
                      {
                      // Outputs the error to the log
                      Log("You cannot write and read from the same file at the same time. Please remove SampleStreamReader.", NinjaTrader.Cbi.LogLevel.Error);
                      throw;
                      }
                      */
                      sw = File.AppendText(path);
                      sw.WriteLine(ToDay(Time[0]) + " " + Open[0] + " " + High[0] + " " + Low[0] + " " + Close[0]);
                      sw.Close();
                      }
                      }
                      }

                      #region NinjaScript generated code. Neither change nor remove.

                      namespace NinjaTrader.NinjaScript.Indicators
                      {
                      public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
                      {
                      private SampleStreamWriter[] cacheSampleStreamWriter;
                      public SampleStreamWriter SampleStreamWriter()
                      {
                      return SampleStreamWriter(Input);
                      }

                      public SampleStreamWriter SampleStreamWriter(ISeries<double> input)
                      {
                      if (cacheSampleStreamWriter != null)
                      for (int idx = 0; idx < cacheSampleStreamWriter.Length; idx++)
                      if (cacheSampleStreamWriter[idx] != null && cacheSampleStreamWriter[idx].EqualsInput(input))
                      return cacheSampleStreamWriter[idx];
                      return CacheIndicator<SampleStreamWriter>(new SampleStreamWriter(), input, ref cacheSampleStreamWriter);
                      }
                      }
                      }

                      namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
                      {
                      public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
                      {
                      public Indicators.SampleStreamWriter SampleStreamWriter()
                      {
                      return indicator.SampleStreamWriter(Input);
                      }

                      public Indicators.SampleStreamWriter SampleStreamWriter(ISeries<double> input )
                      {
                      return indicator.SampleStreamWriter(input);
                      }
                      }
                      }

                      namespace NinjaTrader.NinjaScript.Strategies
                      {
                      public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
                      {
                      public Indicators.SampleStreamWriter SampleStreamWriter()
                      {
                      return indicator.SampleStreamWriter(Input);
                      }

                      public Indicators.SampleStreamWriter SampleStreamWriter(ISeries<double> input )
                      {
                      return indicator.SampleStreamWriter(input);
                      }
                      }
                      }

                      #endregion

                      Attached Files
                      Last edited by turbofib; 07-20-2016, 09:34 AM.

                      Comment


                        #12
                        Hello turbofib,

                        Please modify OnBarUpdate() to what I have stated in my previous post. Remove all previous existing code within OnBarUpdate():

                        Originally posted by NinjaTrader_ZacharyG View Post
                        Hello turbofib,

                        Please modify the script as shown below:

                        Code:
                        protected override void OnBarUpdate()
                        {
                             sw = File.AppendText(path);
                             sw.WriteLine(ToDay(Time[0]) + " " + Open[0] + " " + High[0] + " " + Low[0] + " " + Close[0]);
                             sw.Close();
                        }
                        The sample on our forum will be modified and updated with this change.

                        Please, let us know if we may be of further assistance.
                        Zachary G.NinjaTrader Customer Service

                        Comment


                          #13

                          protected override void OnBarUpdate()
                          { //ciao
                          /* The try-catch block is used for error handling.
                          In this case it is used to ensure you only have one stream object operating on the same file at any given moment. */
                          /*
                          try
                          {
                          // If file at 'path' doesn't exist it will create the file. If it does exist it will append the file.
                          if (CurrentBar == 0)
                          sw = File.AppendText(path);
                          Print("CurrentBar " + CurrentBar + "Date " + ToDay(Time[0]));
                          // This is the output of all lines. The output format is as follows: Date Open High Low Close
                          sw.WriteLine(CurrentBar + " " + ToDay(Time[0]) + " " + Open[0] + " " + High[0] + " " + Low[0] + " " + Close[0]);
                          }
                          catch (Exception)
                          {
                          // Outputs the error to the log
                          Log("You cannot write and read from the same file at the same time. Please remove SampleStreamReader.", NinjaTrader.Cbi.LogLevel.Error);
                          throw;
                          }
                          */
                          i have write old code in /* */
                          See code....
                          The present code is Your !!

                          Comment


                            #14
                            Hello turbofib,

                            After editing the code, please ensure you have compiled the code. Additionally, remove the indicator from your chart completely. Close the Indicators window, reopen, and re-add.

                            If it still does not output what you are expecting, please restart NinjaTrader and test.
                            Zachary G.NinjaTrader Customer Service

                            Comment


                              #15
                              i check indicator....i restart ninjatrader but the problem is the same...
                              I try it on 2 different pc......


                              The data up to 20160524
                              Attached Files

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by kevinenergy, 02-17-2023, 12:42 PM
                              115 responses
                              2,699 views
                              1 like
                              Last Post kevinenergy  
                              Started by prdecast, Today, 06:07 AM
                              1 response
                              4 views
                              0 likes
                              Last Post NinjaTrader_LuisH  
                              Started by Christopher_R, Today, 12:29 AM
                              1 response
                              14 views
                              0 likes
                              Last Post NinjaTrader_LuisH  
                              Started by chartchart, 05-19-2021, 04:14 PM
                              3 responses
                              577 views
                              1 like
                              Last Post NinjaTrader_Gaby  
                              Started by bsbisme, Yesterday, 02:08 PM
                              1 response
                              15 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Working...
                              X