Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Issue with Trailing Stop

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

    Issue with Trailing Stop

    I have created a simple EMA crossover strategy and trying to use EMA 50 as a trail stop. however, it is not working. Can anyone please tell me what I am doing wrong.
    I am using Two EMA's (Fast EMA= 21, Slow EMA= 50) for crossover condition to place long and short order. My strategy is not using the trail stop code instead only reversing position per crossover condition or if met profit target. Below is my strategy code.
    I also tried to follow the trailstop thread given on this form but not sure what I am doing wrong.
    I appreciate if somebody can help me!!

    privateint fast = 21; // Default setting for Fast
    privateint slow = 50; // Default setting for Slow
    privateint profitTargetTicks = 50;
    #endregion

    protectedoverridevoid Initialize()
    {
    CalculateOnBarClose =
    true;
    }
    protectedoverridevoid OnBarUpdate()
    {
    if (CrossAbove(EMA(Fast), EMA(Slow), 1))
    {
    SetTrailStop(CalculationMode.Price, EMA(
    50)[0]);
    EnterLong(
    1, "Long1");
    SetProfitTarget(CalculationMode.Ticks, profitTargetTicks);
    }
    if (CrossBelow(EMA(Fast), EMA(Slow), 1))
    {
    SetTrailStop(CalculationMode.Price, EMA(
    50)[0]);
    EnterShort(
    1, "Short1");
    SetProfitTarget(CalculationMode.Ticks, profitTargetTicks);
    }

    #2
    Hi shanibaba,

    The trail stop works tick for tick. While you can set the price, the trailing action will always be tick for tick.

    If you want to have the price of the stop be the price of the ema, then you need to detect there is a position, and update the price by calling SetStopLoss on every new bar, or every new tick.
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by charlesugo_1, 05-26-2026, 05:03 PM
    0 responses
    59 views
    0 likes
    Last Post charlesugo_1  
    Started by DannyP96, 05-18-2026, 02:38 PM
    1 response
    143 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 05-11-2026, 05:56 AM
    0 responses
    161 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 05-10-2026, 08:12 PM
    0 responses
    97 views
    0 likes
    Last Post CarlTrading  
    Started by Hwop38, 05-04-2026, 07:02 PM
    0 responses
    283 views
    0 likes
    Last Post Hwop38
    by Hwop38
     
    Working...
    X