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

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.


    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.


      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,
        You can submit orders to different Bars objects. This allows you the flexibility of submitting orders to different timeframes. Like in live trading, taking entry conditions from a 5min chart means executing your order as soon as possible instead of waiting until the next 5min bar starts building. You can achieve this by


        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,
          You can submit orders to different Bars objects. This allows you the flexibility of submitting orders to different timeframes. Like in live trading, taking entry conditions from a 5min chart means executing your order as soon as possible instead of waiting until the next 5min bar starts building. You can achieve this by


          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 funk10101, Today, 12:02 AM
                  1 response
                  10 views
                  0 likes
                  Last Post NinjaTrader_LuisH  
                  Started by GLFX005, Today, 03:23 AM
                  1 response
                  6 views
                  0 likes
                  Last Post NinjaTrader_Erick  
                  Started by nandhumca, Yesterday, 03:41 PM
                  1 response
                  12 views
                  0 likes
                  Last Post NinjaTrader_Gaby  
                  Started by The_Sec, Yesterday, 03:37 PM
                  1 response
                  11 views
                  0 likes
                  Last Post NinjaTrader_Gaby  
                  Started by vecnopus, Today, 06:15 AM
                  0 responses
                  1 view
                  0 likes
                  Last Post vecnopus  
                  Working...
                  X