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 CarlTrading, 03-31-2026, 09:41 PM
    1 response
    81 views
    1 like
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    42 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    64 views
    2 likes
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    66 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    54 views
    0 likes
    Last Post CarlTrading  
    Working...
    X