• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Testing Breakeven Logic

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

    Testing Breakeven Logic

    Hello,

    I am using managed approach and trying to adjust stop loss to breakeven after 20 ticks profit.

    **Method**

    Adjust SetStopLoss to Position.Average price after 20 ticks of profit


    **Problem**
    Because of the OnBarUpdate (every 5 min), sometimes the SetStopLoss is not adjusted to Position.Average becauce 20 ticks of profit occurs on current bar, but the logic to adjust the setstoploss does not excecute until the bar close. By this time price is above (considering the long case) Position.Average which leads to a lost when ideally it should be break even trade. This does not happen all the time, but it does happen.

    See pictures for example. This happen like 8 times out of 600 trades.

    **Solution**
    I am not sure there is a solution because the logic is working, its just the breakeven logic does not execute until after bar close, but its a bit too late cause price is above the Position.Average. See picture Screenshot_67.png to see print out of logic working.

    Do you know if anyway I can fixed this?
    Attached Files
    Last edited by simple_goodoboy; 12-13-2017, 09:48 PM.

    #2
    Hello simple_goodoboy,

    Thank you for your note.

    If you were to run your strategy with Calculate set to OnEachTick or OnPriceChange, your breakeven would update.

    If you still wish to run your strategy on bar close, and option would be to put your breakeven logic in OnMarketData, however this would be CPU intensive, you could add an if statement which would only check this if statement if x time passed between the last check, rather than on any OnMarketData event.
    https://ninjatrader.com/support/help...marketdata.htm

    The approach above would not work with the strategy analyzer however, for testing you would need to test in TickReplay.

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

    Comment


      #3
      Originally posted by NinjaTrader_AlanP View Post
      Hello simple_goodoboy,

      Thank you for your note.

      If you were to run your strategy with Calculate set to OnEachTick or OnPriceChange, your breakeven would update.

      If you still wish to run your strategy on bar close, and option would be to put your breakeven logic in OnMarketData, however this would be CPU intensive, you could add an if statement which would only check this if statement if x time passed between the last check, rather than on any OnMarketData event.
      https://ninjatrader.com/support/help...marketdata.htm

      The approach above would not work with the strategy analyzer however, for testing you would need to test in TickReplay.

      Please let us know if you need further assistance.
      Thank you Alan for responding to me.

      A few questions to make sure I understand

      1. So I can use OnEachTick or OnPriceChange only on the breakeven code only? I can still use onbarupdate for the entry code?

      2. If I use OnEachTick or OnPriceChange, can I still use Strategy Analyzer for back testing?

      3. Using OnEachTick or OnPriceChange in strategy analyzer will increase optimization time cause now strategy needs to update on every tick now, right

      Thank you

      Comment


        #4
        Hello simple_goodoboy,

        If you ran your strategy on OnEachTick or OnPriceChange the stop would be adjusted in real time, you cannot run your strategy with OnEachTick or OnPriceChange and OnBarUpdate.

        If you use OnEachTick or OnPriceChange, you can use the Strategy Analyzer for back testing but would need to also use a secondary tick series, please see,
        https://ninjatrader.com/support/foru...ead.php?t=6652

        Yes, running a strategy with a secondary tick series would increase optimization time as you are increasing data points considerably.

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

        Comment


          #5
          Originally posted by NinjaTrader_AlanP View Post
          Hello simple_goodoboy,

          If you ran your strategy on OnEachTick or OnPriceChange the stop would be adjusted in real time, you cannot run your strategy with OnEachTick or OnPriceChange and OnBarUpdate.

          If you use OnEachTick or OnPriceChange, you can use the Strategy Analyzer for back testing but would need to also use a secondary tick series, please see,
          https://ninjatrader.com/support/foru...ead.php?t=6652

          Yes, running a strategy with a secondary tick series would increase optimization time as you are increasing data points considerably.

          Please let us know if you need further assistance.
          Thank you Alan,

          And just to be sure. NT only offers 1 year of tick data on the secondary (high fill resolution 1 tick) data series for back testing ?

          Thanks

          Comment


            #6
            Hello simple_goodoboy,

            360 days.

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

            Comment


              #7
              Originally posted by NinjaTrader_AlanP View Post
              Hello simple_goodoboy,

              360 days.

              Please let us know if you need further assistance.

              Thanks Alan,

              I understand now.

              One last question please.

              If I use SetTrailStop() (without using SetStopLoss of course) and OnBarUpdate in trail a winner, does the same apply that the trail stop will update on bar close? I believe so.

              Also, if I am running the the strategy in simulation or live mode with SetTrailStop and OnBarUpdate, will strategy stop trading or give error if SetTrailStop price (exit price) is above the open price of the next bar? I am assuming long case here and the open price is lower then where the SetTrailStop

              In my example below, the back test simulation just exited the trade.

              I hope I make sense.

              Basically I am asking what happens when the market price is below the stop loss price. Using the long trade for this example.

              I am reading about this case here https://ninjatrader.com/support/help...gy_methods.htm " Trail stop orders are modified based on the strategies 'Calculate' settings. In the case of 'Calculate' on bar close, when the bar closes the trail stop order modification will occur using the closing price of the bar as the reference price to apply the trail offset. Subsequently if the open price of the next bar is significantly higher or lower then the current close price then there is a possibility that the calculated trail stop price is now an invalid stop price. This is a risk with modifying any stop order closer to the current market price since any modification above/below the current price would be rejected."


              What happens if running strategy in sim or live mode and stop loss order is rejected?
              Thanks
              Last edited by simple_goodoboy; 12-14-2017, 05:17 PM.

              Comment


                #8
                Hello simple_goodoboy,

                I am responding on behalf of my colleague Alan.

                If I use SetTrailStop() (without using SetStopLoss of course) and OnBarUpdate in trail a winner, does the same apply that the trail stop will update on bar close? I believe so.
                SetTrailStop() will automatically update the stop loss to a certain offset from the current market price depending on the Calculate setting. It would not need to be called from OnBarUpdate() unless you want to change the Trail Stop's distance dynamically. I believe the documentation snippet you provided answers this question. If it doesn't could you clarify on your question?

                If you modify a stop order to the other side of the market, a rejection will be received and the strategy will cancel orders and abort. This could occur if the Trail Stop is close to the market and the market moves toward the other side of the order before that order gets received.

                Such cases could be mitigated by increasing the distance the Trail Stop has from the market, or you can add logic to your strategy to react to order rejections like these and place a market order after the rejection is received.

                I'll include some publicly available documentation on handling order rejections in OnOrderUpdate() if you would like to implement such functionality in your strategy.

                RealtimeErrorHandling - https://ninjatrader.com/support/help...orhandling.htm

                OnOrderUpdate() - https://ninjatrader.com/support/help...rderupdate.htm

                Please let me know if you have any additional questions.
                Last edited by NinjaTrader_Jim; 12-15-2017, 12:38 PM.
                JimNinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_Jim View Post
                  Hello simple_goodoboy,

                  I am responding on behalf of my colleague Alan.



                  SetTrailStop() will automatically update the stop loss to a certain offset from the current market price depending on the Calculate setting. It would not need to be called from OnBarUpdate() unless you want to change the Trail Stop's distance dynamically. I believe the documentation snippet you provided answers this question. If it doesn't could you clarify on your question?

                  If you modify a stop order to the other side of the market, a rejection will be received and the strategy will cancel orders and abort. This could occur if the Trail Stop is close to the market and the market moves toward the other side of the order before that order gets received.

                  Such cases could be mitigated by increasing the distance the Trail Stop has from the market, or you can add logic to your strategy to react to order rejections like these and place a market order after the rejection is received.

                  I'll include some publicly available documentation on handling order rejections in OnOrderUpdate() if you would like to implement such functionality in your strategy.

                  RealtimeErrorHandling - https://ninjatrader.com/support/help...orhandling.htm

                  OnOrderUpdate() - https://ninjatrader.com/support/help...rderupdate.htm

                  Please let me know if you have any additional questions.
                  Thank you so much Jim for responding.

                  I have discovered that a few ways to handling the moving of stoploss orders intra-bar is to

                  1. use Tick Data or TickReplay
                  2. Use OnEachTick

                  or 3. If you want to use run part of your strategy with OnBarClose concepts and the stoploss code with OnEachTick, you can use IsThisFirstBarTick.

                  I used this over the weekend and the stoploss movement worked fine because I execute the stoploss movement session of code each tick.

                  Thanks for the rejection order handling information.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Captain_Qwark, Today, 10:55 AM
                  3 responses
                  18 views
                  0 likes
                  Last Post NinjaTrader_PaulH  
                  Started by kmpc1, Today, 11:03 AM
                  1 response
                  13 views
                  0 likes
                  Last Post NinjaTrader_PatrickG  
                  Started by jransom, Today, 09:08 AM
                  1 response
                  19 views
                  0 likes
                  Last Post NinjaTrader_Kate  
                  Started by mtamaku, Today, 09:19 AM
                  0 responses
                  23 views
                  0 likes
                  Last Post mtamaku
                  by mtamaku
                   
                  Started by traderMNQ, Today, 09:00 AM
                  4 responses
                  20 views
                  0 likes
                  Last Post traderMNQ  
                  Working...
                  X