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

export historical data from strategy

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

    export historical data from strategy

    Hello,

    I need that my strategy export historical data to a text file, only for specific periods of time, while running in strategy analyzer. Any idea?

    Is there some code to export data to a text file?

    Thanks

    #2
    This link may helps You to save text to file.


    Most general approaches, I believe are:

    PHP Code:
    StreamWriter Output_File;

    OnStateChange()
    {
        if (
    State == State.Active)
        {                
            
    StreamWriter Output_File= new StreamWriterOutput_Dir +Output_File_Name)            
        }        

        if (
    State == State.Terminated)
        {                
            
    Output_File.Close();            
        }        
    }

    OnBarUpdate()
    {
        if( 
    Data_Selection_Condition_Is_Ok 
        {
             
    output_file.WriteLineTime.ToStringyyyyMMdd HH:mm:ss ) +";"Open[0] +";"High[0] +";"Low[0] +";"Close[0] );
        }



    and

    PHP Code:
    List<stringData_Lines = new List<string>();

    OnStateChange()
    {
        if (
    State == State.Terminated)
        {                
            
    usingStreamWriter output_file = new StreamWriterOutput_Dir +Output_File_Name )) 
            {
                foreach( var 
    line in Data_Lines )                
                    
    output_file.WriteLineline );
            }                            
        }        
    }

    OnBarUpdate()
    {
        if( 
    Data_Selection_Condition_Is_Ok 
        {
             
    Data_Lines.AddTime.ToStringyyyyMMdd HH:mm:ss ) +";"Open[0] +";"High[0] +";"Low[0] +";"Close[0] );
        }

    fx.practic
    NinjaTrader Ecosystem Vendor - fx.practic

    Comment


      #3
      Thank you fx.practic, but I'm using NT 7, I think OnStateChange() is only for NT 8...

      Comment


        #4
        Hello federicoo,

        Thank you for your note.

        You could use streamwriter. Please see the following example,



        Please let us know if you need further assistance.
        Alan P.NinjaTrader Customer Service

        Comment


          #5
          In this example, only Date and OHLC data are acquired, but I need: Time, Last and Vol data too, in the following format: 20180513 141358;70.47;5 (date time;last;vol).

          How can I get this? I try with the example file but getting an error.

          Thanks

          Comment


            #6
            Hello federicoo,

            Date would be the same as time, for the time rather than date and time you could use Time[0].TimeOfDay.

            Close would be the last price if you run the indicator with Calculate set to OnEachTick.
            See Calculate section of our helpguide,



            If you wanted to add volume you would use Volume[0].

            Please see the following sections of our helpguide,





            Please let us know if you need further assistance.
            Alan P.NinjaTrader Customer Service

            Comment


              #7
              Thank you for your response.

              Added to the file format, I am trying to add a condition to the sample file, because I want to write down the data to the text file only for some days/times, for example, I want to export data from the chart for 3 specific dates, between 090000 to 150000. Adding the "if" sentence for this, I'm getting the "object reference not set..." error.

              Any ideas?

              Thanks

              Comment


                #8
                Hello federicoo,

                Thank you for your response.

                Can you provide the full code of the 'if' condition that is generating the error?

                I also recommend taking a look at our time filter condition at the following link: https://ninjatrader.com/support/help...to_limit_t.htm

                I look forward to your response.

                Comment


                  #9
                  I already fix the issue, thank you!

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by fitspressoburnfat, Today, 04:25 AM
                  0 responses
                  2 views
                  0 likes
                  Last Post fitspressoburnfat  
                  Started by Skifree, Today, 03:41 AM
                  1 response
                  4 views
                  0 likes
                  Last Post Skifree
                  by Skifree
                   
                  Started by usazencort, Today, 01:16 AM
                  0 responses
                  1 view
                  0 likes
                  Last Post usazencort  
                  Started by kaywai, 09-01-2023, 08:44 PM
                  5 responses
                  604 views
                  0 likes
                  Last Post NinjaTrader_Jason  
                  Started by xiinteractive, 04-09-2024, 08:08 AM
                  6 responses
                  23 views
                  0 likes
                  Last Post xiinteractive  
                  Working...
                  X