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 SalmaTrader, 07-07-2026, 10:26 PM
    0 responses
    46 views
    0 likes
    Last Post SalmaTrader  
    Started by CarlTrading, 07-05-2026, 01:16 PM
    0 responses
    22 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 06-17-2026, 10:32 AM
    0 responses
    14 views
    0 likes
    Last Post CaptainJack  
    Started by kinfxhk, 06-17-2026, 04:15 AM
    0 responses
    20 views
    0 likes
    Last Post kinfxhk
    by kinfxhk
     
    Started by kinfxhk, 06-17-2026, 04:06 AM
    0 responses
    22 views
    0 likes
    Last Post kinfxhk
    by kinfxhk
     
    Working...
    X