Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Feel like TrailingStop not work

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

    Feel like TrailingStop not work

    I code a strategy but It doesnt work with the trailingstop when I backtest. I dont know why. Any logic or syntax wrong? a another problem is ninja trader strategy dosent work with value < 0,0001 right?

    Click image for larger version  Name:	Screenshot 2023-01-11 221741.png Views:	0 Size:	46.4 KB ID:	1230816


    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 3)
    return;

    // Set 1
    if (CCI1[2] > CCI1[1]
    && CCI1[1] < CCI1[0]
    && CCI1[1] < -100)


    {
    EnterLong(3000, "long");
    c =1;
    SetProfitTarget("long",CalculationMode.Pips, 30);
    SetStopLoss("long", CalculationMode.Pips, 30, false);
    }


    // Set 2
    if (CCI1[2] < CCI1[1]
    && CCI1[1] > CCI1[0]
    && CCI1[1] > 100)

    {
    EnterShort(3000, "short");
    c=1;
    SetProfitTarget("short",CalculationMode.Pips, 30);
    SetStopLoss("short", CalculationMode.Pips, 30, false);
    }




    // Set 3
    if (SystemPerformance.AllTrades.Count > 1)
    {
    Trade lastTrade = SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1];
    if ( lastTrade.ProfitPips <0 && lastTrade.Entry.Name == "short" && c==1)
    {
    EnterLong(3000,"RLong");

    SetTrailStop("RLong",CalculationMode.Pips, 30,false);
    c--;
    }
    if ( lastTrade.ProfitPips <0 && lastTrade.Entry.Name== "long")
    {
    EnterShort(3000,"RShort");

    SetTrailStop("RShort",CalculationMode.Pips, 30,false);
    c--;
    }
    }


    }
    }
    }​​

    #2
    Hello baolinh09,

    Thanks for your post.

    "ninja trader strategy dosent work with value < 0,0001 right?"

    What value are you referring to exactly? Are you referring to an order quantity or trailing stop value?​

    Set methods prep NinjaTrader to submit protective orders for an Entry order and should be called before the Entry method. For example:

    SetTrailStop("RShort",CalculationMode.Pips, 30,false);
    EnterShort(3000,"RShort");


    Should you call this method to dynamically change the trail stop price in the strategy OnBarUpdate() method, you should always reset the trail stop price / offset value when your strategy is flat otherwise, the last price/offset value set will be used to generate your trail stop order on your next open position. This could be seen in the 'Tips' section of the SetTrailStop() help guide page linked ​below.

    SetTrailStop(): https://ninjatrader.com/support/help...ttrailstop.htm

    Please review the help guide document on the differences on real-time vs backtest (historical).
    http://ninjatrader.com/support/helpG...ime_vs_bac.htm

    When backtesting on historical data, only the Open, High, Low, and Close will be available and there will be no intra-bar data. This means actions cannot happen intra-bar, fills cannot happen intra-bar. All prices and actions come from and occur when the bar closes as this is all the information that is known.

    Because of this, OnBarUpdate will only update 'On bar close' as it does not have the intra-bar information necessary for 'On price change' or 'On each tick' and the script will not have the intra-bar information to accurately fill an order at the exact price and time.

    Below is a link to the help guide on Calculate.
    https://ninjatrader.com/support/help.../calculate.htm

    To improve the accuracy of a backtest, you may use Tick Replay along with an added 1-tick series to have logic processed intra-bar and have orders filled intrabar. Tick Replay would be used to have the logic process OnEachTick or OnPriceChange with historical data, but this does not allow for intra-bar order fills. You would need to add a single-tick data series and submit orders to that single-tick data series for a strategy that uses Tick Replay.

    Additional information may be found in this NinjaTrader Forum post: https://ninjatrader.com/support/foru...mance?t=102504

    Ultimately, if a script is not behaving as expected, debugging prints should be added to the script to see exactly how it is behaving.

    Below is a link to a forum post that demonstrates how to use prints to understand behavior.

    https://ninjatrader.com/support/foru...121#post791121

    Please let us know if we may be of further assistance to you.​
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    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
    69 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