Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Profit targets and Stop losses seem sporadic

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

    Profit targets and Stop losses seem sporadic

    I am running a strategy on a chart with 15 min bars; the strategy has 1 sec granularity coded in. I have it set to calculate every tick. I've posted the logic for setting Profit Targets and Stop Losses below. In backtesting, sometimes the profit target will be reached for each of the two entries on the same bar as entry executed, but only one PT will execute and the other will run back down to stoploss. Any advice on how to code this so it will backtest accurately would be greatly appreciated. Thank you.

    I've also attached a photo that has an example of similar incorrect behavior.The profit target for LongN1 was reached on the first bar after entry, and the price did not really dip below stop loss on the entry bar.Click image for larger version

Name:	MNQ DEC23 (15 Minute) 2023_10_03 (6_21_14 AM).png
Views:	130
Size:	49.3 KB
ID:	1271758

    Code:
    if (IsFirstTickOfBar == true && BarsInProgress == 1)
    
    {
    
    SetProfitTarget("LongN1", CalculationMode.Price, Bollinger1.Middle[1], false);
    
    SetProfitTarget("LongN2", CalculationMode.Price, Bollinger1.Upper[1], false);
    
    SetProfitTarget("ShortN1", CalculationMode.Price, Bollinger1.Middle[1], false);
    
    SetProfitTarget("ShortN2", CalculationMode.Price, Bollinger1.Lower[1], false);
    
    }
    
    if (IsFirstTickOfBar == true && BarsInProgress == 1)// && Position.MarketPosition == MarketPosition.Flat)
    
    {
    
    SetStopLoss("LongN1", CalculationMode.Price, Bollinger2.Lower[1], false);
    
    SetStopLoss("LongN2", CalculationMode.Price, Bollinger2.Lower[1], false);
    
    SetStopLoss("ShortN1", CalculationMode.Price, Bollinger2.Upper[1], false);
    
    SetStopLoss("ShortN2", CalculationMode.Price, Bollinger2.Upper[1], false);
    
    
    
    }
    
    
    
    
    if (IsFirstTickOfBar == true && BarsInProgress == 1 && Position.MarketPosition == MarketPosition.Long
    
    && BarsSinceEntryExecution(1, "LongN2", 0 ) > 0
    
    && MAX(High, BarsSinceEntryExecution(1, "LongN2", 0 ))[1] > Bollinger1.Middle[1])
    
    {
    
    SetStopLoss("LongN2", CalculationMode.Price, Bollinger1.Lower[1], false);
    
    }
    
    
    
    
    if (IsFirstTickOfBar == true && BarsInProgress == 1 && Position.MarketPosition == MarketPosition.Short
    
    && BarsSinceEntryExecution(1, "ShortN2", 0 ) > 0
    
    && MIN(Low, BarsSinceEntryExecution(1, "ShortN2", 0 ))[1] < Bollinger1.Middle[1])
    
    {
    
    SetStopLoss("ShortN2", CalculationMode.Price, Bollinger1.Upper[1], false);
    
    }
    
    
    ​
    Last edited by zrobfrank; 10-06-2023, 07:25 AM.

    #2
    Hello zrobfrank,

    Thank you for your post.

    There are certain discrepancies that are expected between backtest vs. real-time behavior. More information may be found here:


    It is also important to keep the following notes in mind that for the Set() methods, such as SetStopLoss() and SetProfitTarget(), per the help guide:
    • https://ninjatrader.com/support/help...etstoploss.htm
      • "Should you have multiple Bars objects of the same instrument while using SetStopLoss() in your strategy, you should only submit orders for this instrument to the first Bars context of that instrument. This is to ensure your order logic is processed correctly and any necessary order amendments are done properly."
    • https://ninjatrader.com/support/help...ofittarget.htm
      • "Should you have multiple Bars objects of the same instrument while using SetProfitTarget() in your strategy, you should only submit orders for this instrument to the first Bars context of that instrument. This is to ensure your order logic is processed correctly and any necessary order amendments are done properly."
    Since you are adding a 1-second series for granularity, are you also submitting orders to that added series? This could result in unexpected behavior from the SetStopLoss() and SetProfitTarget() orders. In order to get more accurate backtest results and expected behavior from your stop loss and profit target orders, you may want to consider using OnOrderUpdate() and OnExecutionUpdate() to submit protective orders rather than using the Set() methods. We have a reference sample demonstrating how to do this here:


    My colleague has created a post about the discrepancies between backtest and real-time here:


    Depending on what the strategy is doing, one of the ways to get the most accurate backtest results with intrabar actions and granular price data is to submit orders to an added 1-tick data series as well as enable Tick Replay. There is more information about intrabar granularity as well as developing for Tick Replay in that post as well as in the linked help guide pages from that post:

    Please let us know if we may be of further assistance.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Today, 05:17 AM
    0 responses
    50 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    126 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    68 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    42 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    46 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X