Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Optimizer problem: the value of property does not reset.

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

    Optimizer problem: the value of property does not reset.

    Hello,
    i made a sample strategy, please chek this. When the strategy run, it changes the value of AccountCash only once with 50000.

    Reproduce:
    - Change Fast property: Min = 9, Max = 10, Increment = 1
    - Do NOT change AccountCash.
    - Run Optimization

    Because of the settings, it will run twice.

    Result in Output window:

    Initialize: 150000
    Initialize: 150000
    OnStartUp: 150000
    OnBarUpdate: 200000
    OnTermination: 200000
    -------------

    Initialize: 150000
    Initialize: 200000
    OnStartUp: 200000
    OnBarUpdate: 250000
    OnTermination: 250000
    -------------

    It can be seen that at second run the setting value will not be 150000, but 200000 wich is the result of the previous run.

    Tamas
    Attached Files

    #2
    I just realised. Probably the result will not come out with previous mentioned method.

    Please check this:

    - Start NT.
    - Open Strategy Analyzer.
    - Select Instrument (at my end EURUSD).
    - Click Optimizer button.
    - Select sample strategy.
    - Change Fast property: Min = 9, Max = 10, Increment = 1
    - Do NOT change AccountCash.
    - Run Optimization.

    Till this OK. In output window:
    Initialize: 150000
    Initialize: 150000
    Initialize: 150000
    Initialize: 150000
    OnStartUp: 150000
    OnStartUp: 150000
    OnBarUpdate: 200000
    OnBarUpdate: 200000
    OnTermination: 200000
    -------------
    OnTermination: 200000
    -------------
    Initialize: 150000
    - Click Optimizer TAB.
    - Click Trades TAB.

    New rows in output window:
    Initialize: 150000
    Initialize: 150000
    Initialize: 200000
    OnStartUp: 200000
    OnBarUpdate: 250000
    OnTermination: 250000
    - Run Optimization again.

    New rows in output window:
    Initialize: 150000
    Initialize: 150000
    OnStartUp: 150000
    Initialize: 150000
    OnStartUp: 150000
    OnBarUpdate: 200000
    OnBarUpdate: 200000
    OnTermination: 200000
    -------------
    OnTermination: 200000
    -------------
    Initialize: 150000
    Initialize: 200000
    OnStartUp: 200000
    OnBarUpdate: 250000
    OnTermination: 250000

    Tamas

    Comment


      #3
      tamas, I ran your script but increased the number of trials by optimizing the max value from 9 to 13 in 1 step increments. I did not notice the AccountCash "hanging around". It appears as if it was reset correctly for each iteration.
      Code:
      Initialize: 150000
      Initialize: 150000
      OnStartUp: 150000
      Initialize: 150000
      OnStartUp: 150000
      OnBarUpdate: 200000
      OnBarUpdate: 200000
      OnTermination: 200000
      -------------
      
      Initialize: 150000
      OnStartUp: 150000
      OnBarUpdate: 200000
      OnTermination: 200000
      -------------
      
      Initialize: 150000
      OnStartUp: 150000
      OnBarUpdate: 200000
      OnTermination: 200000
      -------------
      
      Initialize: 150000
      OnStartUp: 150000
      OnBarUpdate: 200000
      OnTermination: 200000
      -------------
      
      OnTermination: 200000
      -------------
      
      Initialize: 150000
      EDIT: After clicking the trades tab, the counter did increase further.
      Code:
      Initialize: 150000
      Initialize: 150000
      Initialize: 200000
      OnStartUp: 200000
      OnBarUpdate: 250000
      OnTermination: 250000
      Someone will get back to you tomorrow regarding this issue.
      AustinNinjaTrader Customer Service

      Comment


        #4
        Thanks Austin, I wait.

        Tamas

        Comment


          #5
          tamas,

          When you click on the various tabs you are already post-optimization. Are you saying this actually invokes improper strategy calculations for you? I am not seeing changed results from what I can tell on my end.
          Josh P.NinjaTrader Customer Service

          Comment


            #6
            Hello Josh,

            Sorry for replying slower, but i made another sample, because the problem is complex. I hope it will be more understandable.

            Originally posted by NinjaTrader_Josh View Post
            When you click on the various tabs you are already post-optimization.
            Yes, i know.

            Originally posted by NinjaTrader_Josh View Post
            Are you saying this actually invokes improper strategy calculations for you?
            I think, yes. One side yes, the other side no.



            Strategy:
            • There is a property which simulates how much money you have. AccountCash = 150000
            • Optimalisation on EURUSD. Margin = 2500 and PointValue = 100000
            • If AccountCash = 150000 then the first entry quantity has to be 6000000 --> (150000 / 2500) * 100000
            • Simulate the change of the cash. In this sample strategy add 50000 once.
            • The settings of optimalization can be found in the previous posts.
            What should happen?
            • The first entry quantity has to be 6000000.
            • The second entry quantity has to be 8000000. And the other entries as well.
            Two cases will be during optimalization. (In both cases the controll is the backtest, see below the pictures.)


            1) The "Summary" or "Settings" TAB is active.
            • Optimalisation will run with the right quantity value.
            • It can be seen in output window.(sample_1.png)
            • And the profit is correct. (sample_2.png)
            (Probably the 1 point is not correct, only the problem can not be seen so much on GUI. Because at the top in the grid --> there is 200000 instead of 150000)


            2) Any other TAB is active (eg. "Trades")
            • The optimalization (maybe after the backtest??) will run with not a correct quantity value.
            • It can be seen in output window and on the "Trades" TAB (sample_3.png)
            • The Cum Profit on the "Trades" TAB is not correct. I think beacuse the quantity is not correct. (sample_4.png)
            Summary: it seems that the optimalization run in two steps correctly according to the Output window (sample_4.png). Then backtest will run again because of the opened "Trades" TAB, but this does not run correctly. I think it does not set again the original AccountCash value (150000), but it count with the last calculated value (200000).

            Have a nice day, Tamas
            Attached Files

            Comment


              #7
              tamas,

              Please leave the GridCategory as Parameters for AccountCash and see if you still have the same observations. Thank you.
              Josh P.NinjaTrader Customer Service

              Comment


                #8
                Josh, checked. The probem still exists.

                Tamas
                Attached Files

                Comment


                  #9
                  tamas,

                  Development says that changing parameters while running it can yield such results. Instead if you wanted something to change you should not change the actual parameter itself, but make another private variable inside the script that would hold any changed values and base your logic off of that variable instead.
                  Josh P.NinjaTrader Customer Service

                  Comment


                    #10
                    OK,

                    what development said, i did before as a workaround.

                    Accordingly: workaround = solution

                    Thanks, Tamas

                    Comment


                      #11
                      Hello Josh,

                      i would like to ask one more question connected to backtesting. This is not a bug, i just do not understand how it works, what sequence do the entries and exits and their events run?

                      In the sample code i just wanted to add the result of the last trade (profit) to TempAccountCash (account is simulated by this) after the exit. I would like to count the quantity of the new entry from this.

                      But the result of the last trade is not always avalilable. It seems that the event of exit runs later then the next entry.

                      The problem can be seen in AllTrades.Count. Sometimes it will not be more with one ...

                      Code:
                      LONG
                      OnBarUpdate - ExitShort
                      OnBarUpdate - AllTrades.Count: [COLOR=red][B]0[/B][/COLOR]
                      OnBarUpdate - Long - TempAccountCash: 150000
                      OnBarUpdate - EnterLong
                      -------------------
                      -------------------
                      SHORT
                      OnBarUpdate - ExitLong
                      OnBarUpdate - AllTrades.Count: [B][COLOR=red]0[/COLOR][/B]
                      OnBarUpdate - Short - TempAccountCash: 150000
                      OnBarUpdate - EnterShort
                      -------------------
                      -------------------
                      LONG
                      OnBarUpdate - ExitShort
                      OnBarUpdate - AllTrades.Count: [COLOR=red][B]2[/B][/COLOR]
                      OnBarUpdate - Profit: 359,999999999934
                      OnBarUpdate - Long - TempAccountCash: 150360
                      OnBarUpdate - EnterLong
                      -------------------
                      -------------------
                      SHORT
                      OnBarUpdate - ExitLong
                      OnBarUpdate - AllTrades.Count: [COLOR=red][B]2[/B][/COLOR]
                      OnBarUpdate - Profit: 359,999999999934
                      OnBarUpdate - Short - TempAccountCash: 150720
                      OnBarUpdate - EnterShort
                      -------------------
                      -------------------
                      LONG
                      OnBarUpdate - ExitShort
                      OnBarUpdate - AllTrades.Count: 3
                      OnBarUpdate - Profit: -4149,9353099989
                      OnBarUpdate - Long - TempAccountCash: 146570,064690001
                      OnBarUpdate - EnterLong
                      -------------------
                      ... sometimes it will.

                      Code:
                      LONG
                      OnBarUpdate - ExitShort
                      OnBarUpdate - AllTrades.Count: 185
                      OnBarUpdate - Profit: 2573,03300000093
                      OnBarUpdate - Long - TempAccountCash: 142411,783830009
                      OnBarUpdate - EnterLong
                      -------------------
                      -------------------
                      SHORT
                      OnBarUpdate - ExitLong
                      OnBarUpdate - AllTrades.Count: [COLOR=seagreen][B]185[/B][/COLOR]
                      OnBarUpdate - Profit: 2573,03300000093
                      OnBarUpdate - Short - TempAccountCash: 144984,81683001
                      OnBarUpdate - EnterShort
                      -------------------
                      -------------------
                      LONG
                      OnBarUpdate - ExitShort
                      OnBarUpdate - AllTrades.Count: [COLOR=seagreen][B]186[/B][/COLOR]
                      OnBarUpdate - Profit: -12760,0950399999
                      OnBarUpdate - Long - TempAccountCash: 132224,72179001
                      OnBarUpdate - EnterLong
                      -------------------
                      -------------------
                      SHORT
                      OnBarUpdate - ExitLong
                      OnBarUpdate - AllTrades.Count: [COLOR=seagreen][B]187[/B][/COLOR]
                      OnBarUpdate - Profit: 4987,47712000071
                      OnBarUpdate - Short - TempAccountCash: 137212,198910011
                      OnBarUpdate - EnterShort
                      -------------------
                      -------------------
                      LONG
                      OnBarUpdate - ExitShort
                      OnBarUpdate - AllTrades.Count: [COLOR=seagreen][B]188[/B][/COLOR]
                      OnBarUpdate - Profit: -13962,9283199997
                      OnBarUpdate - Long - TempAccountCash: 123249,270590011
                      OnBarUpdate - EnterLong
                      -------------------
                      I checked OnOrderUpdate and OnExecution method and the sequence of events is the same as above - obviously.

                      I could solve my problem, i just would like to understand how it works and why is it like this? Or what did i forget?

                      Thank you in advance, Tamas
                      Attached Files

                      Comment


                        #12
                        tamas,

                        TradeCollection object is updated after a trade is fully finished. Meaning, only after you receive an OnPositionUpdate() event for that round trip trade.
                        Josh P.NinjaTrader Customer Service

                        Comment


                          #13
                          Originally posted by NinjaTrader_Josh View Post
                          tamas,

                          TradeCollection object is updated after a trade is fully finished. Meaning, only after you receive an OnPositionUpdate() event for that round trip trade.
                          Josh, i understand this. Actually the question is (backtest): if i use this code (eg. SampleMA):

                          Code:
                          [FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] (CrossAbove(SMA(Fast), SMA(Slow), 1))[/SIZE][/FONT]
                          [SIZE=2][FONT=Consolas]{[/FONT][/SIZE]
                          [SIZE=2][FONT=Consolas]ExitShort();[/FONT][/SIZE]
                          [SIZE=2][FONT=Consolas]EnterLong();[/FONT][/SIZE]
                          [SIZE=2][FONT=Consolas]}[/FONT][/SIZE]
                          [/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]else[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] (CrossBelow(SMA(Fast), SMA(Slow), 1))[/SIZE][/FONT]
                          [SIZE=2][FONT=Consolas]{[/FONT][/SIZE]
                          [SIZE=2][FONT=Consolas]ExitLong();[/FONT][/SIZE]
                          [SIZE=2][FONT=Consolas]EnterShort();[/FONT][/SIZE]
                          [SIZE=2][FONT=Consolas]}[/FONT][/SIZE]
                          [/SIZE][/FONT]
                          the events (eg. OnExecution) of exit (ExitLong & ExitShort) will not always run before, than events of entry (EnterLong & EnterShort) wich is after the exit in code. So after the exit and before the next enter the result of the last trade (ProfitCurrency) will not always be available. I do not understand the cause of this. Would you help me to explain?

                          Maybe is this any kind of internal simulation rutine for two parallel orders (exit and entry)?

                          Tamas

                          Comment


                            #14
                            tamas,

                            Firstly, why are you calling Exit() before your Enter()? Do you have a specific use case for this? When you call Enter() with a prior position in the opposite direction it will already automatically close that position for you before getting you into your new direction.
                            Josh P.NinjaTrader Customer Service

                            Comment


                              #15
                              Hello,

                              Originally posted by NinjaTrader_Josh View Post
                              Firstly, why are you calling Exit() before your Enter()? Do you have a specific use case for this?
                              Yes.

                              Originally posted by NinjaTrader_Josh View Post
                              When you call Enter() with a prior position in the opposite direction it will already automatically close that position for you before getting you into your new direction.
                              Yes, I know.
                              So I try to summarize it again.

                              I would like to solve, that the quantity during backtest for all entries has to be calculated based on the results until that point. Variable is needed for this, which is the value of the account. I would like to increase or decrease this after all trades, and count the quantity of the next entry from this. This is simple so far.

                              If I use this,the result of the last trade befor entry will not be available, so I can not change the value of the account and can not count the quantity of the new entry.

                              Code:
                              if (CrossAbove(SMA(Fast), SMA(Slow), 1))
                                EnterLong(CalculatedQuantity);
                              elseif (CrossBelow(SMA(Fast), SMA(Slow), 1))
                                EnterShort(CalculatedQuantity);
                              So i thought that i should use this then:

                              Code:
                              if (CrossAbove(SMA(Fast), SMA(Slow), 1))
                              {
                                ExitShort();
                                EnterLong(CalculatedQuantity);
                              }
                              elseif (CrossBelow(SMA(Fast), SMA(Slow), 1))
                              {
                                ExitLong();
                                EnterShort(CalculatedQuantity);
                              }
                              But i realised (post #11), that in this case also, the result of the exit will not always run previously than the entry. There are cases when yes, and there are when no. If it does not run before, then the result of last trade will also not be available, so I can not change after the exit (but so far before the next entry) the variable which contains the value of the account, and then, from that count the result of the following entry. The question applied to why does it work like this and what is the reason?

                              (Tha problem can be solved in a different way - a bit more complicated, but can be solved - i just would like to undersatnd the cause and the function of NT.)

                              Tamas

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              607 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              353 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              105 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              560 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              561 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X