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

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:	450
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:	385
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:	330
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.

      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 Barry Milan, Today, 10:35 PM
              1 response
              8 views
              0 likes
              Last Post NinjaTrader_Manfred  
              Started by WeyldFalcon, 12-10-2020, 06:48 PM
              14 responses
              1,428 views
              0 likes
              Last Post Handclap0241  
              Started by DJ888, Yesterday, 06:09 PM
              2 responses
              9 views
              0 likes
              Last Post DJ888
              by DJ888
               
              Started by jeronymite, 04-12-2024, 04:26 PM
              3 responses
              40 views
              0 likes
              Last Post jeronymite  
              Started by bill2023, Today, 08:51 AM
              2 responses
              16 views
              0 likes
              Last Post bill2023  
              Working...
              X