Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Which method is called only once at the end of the script?

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

    Which method is called only once at the end of the script?

    While doing a back test on a particular instrument, my strategy keeps a count of a certain number of events (in this case, gaps). Once the strategy finishes running on this instrument, I would like to print this data to a file? Which method should I put that code in?


    I've tried putting it in OnStateChange (State==State.Terminated) but it appears I lose the value of my variables. Otherwise I'm not sure where it should go.


    So my question should be, which method gets called only once as soon as a back test completes?


    Thanks!

    #2
    Hello Jeffsg605,

    Thank you for your note.

    You could use streamwriter to save your trade information to a file.

    See the following section of our helpguide.


    If you use Streamwriter in State.Terminated, does it save the information to the file as desired?

    I look forward to your reply.
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Hi Alan,


      Thank you for responding but I think you missed my question. I don't need help writing to a file. As I stated in my initial question:



      "I've tried putting it in OnStateChange (State==State.Terminated) but it appears I lose the value of my variables."


      The value I want to keep track of has already been reset to 0 in State.Terminated. So to repeat my question, which method gets called only once as soon as a backtest completes?


      Thanks.

      Comment


        #4
        Hello Jeffsg605,

        I’ve attached a sample strategy which when you run in the strategy analyzer, upon completion, will print to the output window the value of a variable which has 1 added to it on each OnBarUpdate. So for example 300.

        Instead of printing, you could have this value written to a file.

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

        Comment


          #5
          Thanks, I'll give this a try tonight. For the time being, I got it to work by just checking to see if the date of the backtest is today's date (or the last tradeable day). If it is, then I write my file, otherwise, I continue. This is a little cleaner though, thank you.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by CarlTrading, 03-31-2026, 09:41 PM
          1 response
          43 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by CarlTrading, 04-01-2026, 02:41 AM
          0 responses
          20 views
          0 likes
          Last Post CarlTrading  
          Started by CaptainJack, 03-31-2026, 11:44 PM
          0 responses
          30 views
          1 like
          Last Post CaptainJack  
          Started by CarlTrading, 03-30-2026, 11:51 AM
          0 responses
          48 views
          0 likes
          Last Post CarlTrading  
          Started by CarlTrading, 03-30-2026, 11:48 AM
          0 responses
          38 views
          0 likes
          Last Post CarlTrading  
          Working...
          X