• 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

SL/TP handling in backtest and live trading

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

    SL/TP handling in backtest and live trading

    This is a follow up question. Last question: https://ninjatrader.com/support/foru...t-sl-tp-in-nt8

    I am using NT8. 8.0.21.1 64-bit.
    With tick replay backtest.
    1 minute Bar update.

    I used the method suggested from the previous thread to set the TP and SL position and found that SL is missed.

    Code:
    OnBarUpdate()
    {
        SetProfitTarget("Trade_001", CalculationMode.Price, 0.65588);
        SetStopLoss("Trade_001", CalculationMode.Price, 0.65427, false);
        EnterLong(1, "Trade_001");
    }
    Click image for larger version

Name:	Screenshot from 2020-04-09 14-01-16.png
Views:	60
Size:	19.8 KB
ID:	1093639


    I use another method. SL is detected.

    Code:
    OnBarUpdate()
    {
        EnterLong(1, "Trade_001");
    }
    OnMarketData(MarketDataEventArgs marketDataUpdate)
    {
        if (condition okay) // tp or sl
        {
            ExitLong(1);
        }
    }
    Click image for larger version

Name:	Screenshot from 2020-04-09 14-05-12.png
Views:	40
Size:	17.2 KB
ID:	1093640


    Q: What is the official way or the right way to handle this SL/TP setting during backtest and live trading ?
    Q: In both case I set the SL position at 0.65427, which should hit by the following bar, but both case did not simulate at the right position, is it a setting problem ?
    Click image for larger version

Name:	001.png
Views:	41
Size:	11.1 KB
ID:	1093641

    Thank you very much.

    #2
    As the suggestion from document historical fill. I used

    Code:
    if (IsInStrategyAnalyzer)
        AddDataSeries(BarsPeriodType.Tick, 1);


    Q: And then I remove the selection of Tick Replay from Strategy Analyzer. The problem seems solved. But the callback function OnMarketData() is not being call anymore. Is there a way to keep both of them?

    Thank you very much.
    Last edited by Sulfred; 04-09-2020, 07:44 AM.

    Comment


      #3
      Hello Sulfred,

      Thank you for the post.

      Q: What is the official way or the right way to handle this SL/TP setting during backtest and live trading ?
      The way to handle this depends on what you need your strategy to do. If you are using the Set methods you would need to add granularity if you wanted to see fills other than on the close of the bar,however that will also disable tick replay. Tick replay is generally not used for fills because it will not be as accurate, this is commonly just used for logic that requires OnMarketData to be called.

      Q: In both case I set the SL position at 0.65427, which should hit by the following bar, but both case did not simulate at the right position, is it a setting problem ?
      In general there are differences between realtime and backtesting, most common is the granularity as you have been pointing out in this post.
      https://ninjatrader.com/support/help...lightsub=discr
      You could use the playback connection to play forward through the data rather than using a historical test as one way to gain more accuracy.



      Q: And then I remove the selection of Tick Replay from Strategy Analyzer. The problem seems solved. But the callback function OnMarketData() is not being call anymore. Is there a way to keep both of them?
      Unfortunately not, if you specifically need tick replay you would need to use the exit method you had shown instead.

      I look forward to being of further assistance.
      JesseNinjaTrader Customer Service

      Comment


        #4
        Thank you for your reply.

        A small further question.

        Q: If I have a setting Calculate = Calculate.OnBarClose;. Is that means in live trading, the TP and SL will fill after bar close only?

        Comment


          #5
          Hello Sulfred,

          Set methods prep NinjaTrader to submit the target/stop after the entry order comes back as filled. Orders will reside on the broker's order routing servers or the exchange when trading live and they will fill according to market dynamics. The live orders will not fill following the Calculate mode set by your script.

          The Calculate mode will determine how often OnBarUpdate is called. Note also that Set methods will move their order back to price level specified depending on the Calculate mode. For example, let's say we use Calculate.OnBarClose and SetStopLoss. When the Stop order is placed, we move it with Chart Trader. When a bar closes, the strategy moves the stop loss back to the price level specified in the Set method.

          In case you want to be able to move the target/stop without having it move back when using a Set method, you can use Exit orders to exit the position. An example is included below.

          https://ninjatrader.com/support/help...and_onexec.htm

          We look forward to assisting.
          JimNinjaTrader Customer Service

          Comment


            #6
            Thank you for your reply,

            Set methods prep NinjaTrader to submit the target/stop after the entry order comes back as filled. Orders will reside on the broker's order routing servers or the exchange when trading live and they will fill according to market dynamics. The live orders will not fill following the Calculate mode set by your script.
            Okay, thank you

            The Calculate mode will determine how often OnBarUpdate is called. Note also that Set methods will move their order back to price level specified depending on the Calculate mode. For example, let's say we use Calculate.OnBarClose and SetStopLoss. When the Stop order is placed, we move it with Chart Trader. When a bar closes, the strategy moves the stop loss back to the price level specified in the Set method.
            I am sorry, I don't understand what you mean by we move it with Chart Trader. I guess what you mean and would like to use an example, If I use Calculate.OnBarClose and this

            Code:
            SetStopLoss("Trade_001", 1, 0.6333, false);
            EntryLong(1, ""Trade_001");
            When I am in position 1, the StopLoss order will also be sent to the broker? or It will wait for the next bar close?
            And since there is a delay, so you are suggesting https://ninjatrader.com/support/help...and_onexec.htm for a faster protection?

            Comment


              #7
              Hello Sulfred,

              I am sorry, I don't understand what you mean by we move it with Chart Trader. I guess what you mean and would like to use an example, If I use Calculate.OnBarClose and this
              Jim was providing an example of the frequency the stop updates when being called from OnBarUpdate, he provided an example which uses the chart trader but I believe an easier way to describe this relates to your next statement:

              When I am in position 1, the StopLoss order will also be sent to the broker? or It will wait for the next bar close?
              The code you have shown configures the stop loss for immediate submission and submits an entry. The stoploss is submitted when Trade_001 fills and does not wait for the next OnBarUpdate.

              A situation where SetStopLoss does wait for OnBarUpdate is if you already have an active stoploss targetingTrade_001 and then called SetStopLoss again to update its price, it would be tied to OnBarUpdate and will be called at that frequency.

              I look forward to being of further assistance.




              JesseNinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by leodrz, Today, 09:45 PM
              2 responses
              9 views
              0 likes
              Last Post leodrz
              by leodrz
               
              Started by habibalex, Today, 09:30 PM
              0 responses
              6 views
              0 likes
              Last Post habibalex  
              Started by Herrwolf1, Today, 09:07 PM
              0 responses
              5 views
              0 likes
              Last Post Herrwolf1  
              Started by nomadmilo, Today, 08:52 PM
              1 response
              7 views
              0 likes
              Last Post nomadmilo  
              Started by jvanelli, Today, 08:48 PM
              0 responses
              5 views
              0 likes
              Last Post jvanelli  
              Working...
              X