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

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:	68
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.
    Emily C.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by futtrader, 04-21-2024, 01:50 AM
    6 responses
    57 views
    0 likes
    Last Post futtrader  
    Started by sgordet, Today, 11:48 AM
    0 responses
    2 views
    0 likes
    Last Post sgordet
    by sgordet
     
    Started by Trader146, Today, 11:41 AM
    0 responses
    5 views
    0 likes
    Last Post Trader146  
    Started by jpapa, 04-23-2024, 07:22 AM
    2 responses
    18 views
    0 likes
    Last Post rene69851  
    Started by funk10101, Today, 11:35 AM
    0 responses
    2 views
    0 likes
    Last Post funk10101  
    Working...
    X