Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Saving trades in optimization

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

    Saving trades in optimization

    I would like to save trades for each iteration in optimization. Of course, I know that you need to provide unique names for files. What is the best way to save transactions from the strategy?
    I tried to use it on State.Terminated but not all the iterations of optimization save results, only for some, I do not know why.

    #2
    Hello Andrew_e,

    Some instances created in optimizations are not backtested but are cloned instances used behind the scenes for different purposes.

    Each instance that is actually backtested and appears in a row in the results, should enter the State.Terminated when completed.

    Do you have prints from a specific iteration that did not write to file?
    Last edited by NinjaTrader_ChelseaB; 05-01-2019, 07:16 AM.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3


      For the purpose of the test, I optimize one variable from 1 to 20. We should get 20 results and that's what we have in the grid.
      In strategy code, I have:


      else if (State == State.Terminated)
      {

      Log("saveTransaction parametr=" + ATR_Period + " trades=" + SystemPerformance.AllTrades.Count, LogLevel.Information);
      saveTransactions();
      }

      However, only 9 results are saved, similarly in the log I have only 9 entries (trades>0).

      2019-05-01 16:05:33:702|1|16|saveTransaction parametr=17 trades=3484
      2019-05-01 16:05:33:813|1|16|saveTransaction parametr=18 trades=3478
      2019-05-01 16:05:33:904|1|16|saveTransaction parametr=19 trades=3363
      2019-05-01 16:05:33:999|1|16|saveTransaction parametr=20 trades=3431
      2019-05-01 16:05:34:120|1|16|saveTransaction parametr=13 trades=3382
      2019-05-01 16:05:34:203|1|16|saveTransaction parametr=14 trades=3391
      2019-05-01 16:05:34:286|1|16|saveTransaction parametr=15 trades=3489
      2019-05-01 16:05:34:378|1|16|saveTransaction parametr=16 trades=3495
      2019-05-01 16:05:34:484|1|16|saveTransaction parametr=2 trades=0
      2019-05-01 16:05:35:265|1|16|saveTransaction parametr=1 trades=0
      2019-05-01 16:05:35:265|1|16|saveTransaction parametr=2 trades=0
      2019-05-01 16:05:36:548|1|16|saveTransaction parametr=20 trades=0
      2019-05-01 16:05:39:139|1|16|saveTransaction parametr=1 trades=3481

      There are no missing 11 iterations of optimization.
      It is interesting to see that there are calls for the variable equal to 2 only for the zero number of transactions
      (this iteration has no zero number of transactions, which can be seen in the grid after optimization).

      Performance Parameters
      4,17 1/0,5/20/10/2/4/4/1/1/2/100000/True (ATR_Period
      2,02 10/0,5/20/10/2/4/4/1/1/2/100000/True (ATR_Period
      2,09 11/0,5/20/10/2/4/4/1/1/2/100000/True (ATR_Period,
      2,07 12/0,5/20/10/2/4/4/1/1/2/100000/True (ATR_Period,
      2,05 13/0,5/20/10/2/4/4/1/1/2/100000/True (ATR_Period,
      2,03 14/0,5/20/10/2/4/4/1/1/2/100000/True (ATR_Period,
      2,12 15/0,5/20/10/2/4/4/1/1/2/100000/True (ATR_Period,
      2,10 16/0,5/20/10/2/4/4/1/1/2/100000/True (ATR_Period,
      2,09 17/0,5/20/10/2/4/4/1/1/2/100000/True (ATR_Period,
      2,07 18/0,5/20/10/2/4/4/1/1/2/100000/True (ATR_Period,
      2,02 19/0,5/20/10/2/4/4/1/1/2/100000/True (ATR_Period,
      2,01 2/0,5/20/10/2/4/4/1/1/2/100000/True (ATR_Period,
      2,06 20/0,5/20/10/2/4/4/1/1/2/100000/True (ATR_Period,
      2,00 3/0,5/20/10/2/4/4/1/1/2/100000/True (ATR_Period,
      1,98 4/0,5/20/10/2/4/4/1/1/2/100000/True (ATR_Period,
      2,02 5/0,5/20/10/2/4/4/1/1/2/100000/True (ATR_Period,
      2,00 6/0,5/20/10/2/4/4/1/1/2/100000/True (ATR_Period,
      2,06 7/0,5/20/10/2/4/4/1/1/2/100000/True (ATR_Period,
      2,04 8/0,5/20/10/2/4/4/1/1/2/100000/True (ATR_Period,
      1,96 9/0,5/20/10/2/4/4/1/1/2/100000/True (ATR_Period,




      Comment


        #4
        Hello Andrew_e,

        I am not able to reproduce this behavior.

        Below is a link to a video of the test.


        Attached is the test script.

        If you follow the exact steps in this video using the test script I have provided are you experiencing different behavior?
        Attached Files
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Thank you for the video. When I run your test script, everything is fine.
          I've rewritten my script to print data in the output window, just like you do, but I still do not get all iterations. Interestingly, as I have noticed, there are always 9, regardless of whether iterations are 10, 20 or 40 iterations. Weird

          For example, this is the result for 40 iterations


          68a8a08c-6d88-4df6-9dea-3c9925a0c9dd | Terminated | TestInput1: 20, TestInput2: 35 | SystemPerformance.AllTrades.Count: 1911
          68ec7de5-17e1-4733-bfc6-b830cf566f25 | Terminated | TestInput1: 20, TestInput2: 36 | SystemPerformance.AllTrades.Count: 1876
          8c297076-d4fb-4b25-8b55-089748f61a8f | Terminated | TestInput1: 20, TestInput2: 37 | SystemPerformance.AllTrades.Count: 1853
          4ffe0932-ac64-4fb5-9cc7-b68dd346fc3f | Terminated | TestInput1: 20, TestInput2: 38 | SystemPerformance.AllTrades.Count: 1820
          aebc77ab-7abb-4e4e-b97f-944df6f27b6f | Terminated | TestInput1: 20, TestInput2: 39 | SystemPerformance.AllTrades.Count: 1780
          662c1f3e-dbde-4034-ab6d-a455e1ca1fb7 | Terminated | TestInput1: 20, TestInput2: 40 | SystemPerformance.AllTrades.Count: 1761
          4f3e3573-1a05-4651-a322-026962d15378 | Terminated | TestInput1: 20, TestInput2: 33 | SystemPerformance.AllTrades.Count: 1975
          d24e221a-a2c6-4c07-8308-a207ef12b648 | Terminated | TestInput1: 20, TestInput2: 34 | SystemPerformance.AllTrades.Count: 1946
          Strategy 'Turtle1/-1': A BuyToCover stop order placed at '09.02.2017 13:00:00' has been ignored since the stop price is less than or equal to the close price of the current bar. This is an invalid order and subsequent orders may also be ignored. Please fix your strategy.
          fad4896d-e9d4-4e92-b8eb-31c3c85a1f1b | Terminated | TestInput1: 20, TestInput2: 20 | SystemPerformance.AllTrades.Count: 2537

          Comment


            #6
            Hello Andrew_e,

            This may indicate an issue with the logic in your script..

            You can use the script I provided as a starting point and add your code to this.
            If at some point the behavior returns, I recommend focusing on the last code that was added.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              The idea behind an optimization is simple. First you have to have a trading system, this may be a simple moving average crossover for example. In almost every system there are some parameters upsers
              Last edited by Canning89; 05-03-2019, 11:42 PM.

              Comment


                #8
                Canning89 the problem raised in this thread is not about understanding the idea of strategy or optimization, the point is that when I make the optimization of the strategy (slightly more complicated than the intersection of averages) for some iterations it is called State.Terminated, and for others it is not.

                Comment


                  #9
                  I found a workaround. The problem disappeared when I set the IsInstantiatedOnEachOptimizationIteration = true parameter; . Previously for speeding up the tests I had it set to false. The problem I understand is in the resetting of variables during optimization. Maybe in the future I will look for variables that cause this, for now I do not have time for it and I will be satisfied with the solution. I treat NT mainly as a tool for strategy prototyping.

                  Comment


                    #10
                    Hello Andrew_e,

                    When IsInstantiatedOnEachOptimizationIteration is set to false, each variable needs to be reset in State.DataLoaded. This brings on a whole new set of questions.

                    Below is a link to the help guide on IsInstantiatedOnEachOptimizationIteration.


                    May I confirm you were using my test script that re-initializes on State.DataLoaded?
                    Are you finding my testing script also experiences the same behavior?
                    Chelsea B.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by NullPointStrategies, Today, 05:17 AM
                    0 responses
                    20 views
                    0 likes
                    Last Post NullPointStrategies  
                    Started by argusthome, 03-08-2026, 10:06 AM
                    0 responses
                    119 views
                    0 likes
                    Last Post argusthome  
                    Started by NabilKhattabi, 03-06-2026, 11:18 AM
                    0 responses
                    63 views
                    0 likes
                    Last Post NabilKhattabi  
                    Started by Deep42, 03-06-2026, 12:28 AM
                    0 responses
                    41 views
                    0 likes
                    Last Post Deep42
                    by Deep42
                     
                    Started by TheRealMorford, 03-05-2026, 06:15 PM
                    0 responses
                    45 views
                    0 likes
                    Last Post TheRealMorford  
                    Working...
                    X