Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

This used to work...

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

    This used to work...

    Hi,

    I'm not sure why this is happening. As you can see from this one picture... SOMETIMES the trailing stop I programmed works...and sometimes it looks like it gets me out at the very price I got in at. (actually in the pic, I set the bars back to 10 during a back test...so if you noticed that it wasn't a 5 bar low, you're really good)

    Help.

    What it's SUPPOSED to do is create a natural volatility trailing stop that takes the lowest low of the last 5 bars (subtracts one more tick) and keeps updating this on each close. This allows some pullbacks, but generally grabs the majority of any break outs that might happen during the trade.


    //variables
    private int barsAgo = 5; // Default setting for BarsAgo
    private int plusX = 1; // Default setting for PlusX

    // Initialize
    // (...nada)

    // OnBarUpdate()

    // local variables
    int LoBar = LowestBar(Low,barsAgo);
    int HiBar = HighestBar(High,barsAgo);

    // Exit Long
    if (Position.MarketPosition == MarketPosition.Long)
    {
    {SetStopLoss(CalculationMode.Price, Low[LoBar]-(plusX*TickSize));}
    }

    // Exit Short
    if (Position.MarketPosition == MarketPosition.Short)
    {
    {SetStopLoss(CalculationMode.Price, High[HiBar]+(plusX*TickSize));}
    }
    Attached Files

    #2
    Hello,

    I did try this and as you said it seems to be using a older value when setting the stop loss using the logic provided. It seems this only lies in the transition between historical and realtime when using this type of logic specifically. In general it is best to set the StopLoss before the Entry statement to ensure its values have been updated to current values and the later in the logic you can call this again if it needs changed.

    In this case, what you have seems to work for the most part in realtime data but historically it seems to hold on to the prior value. The exits on the same bar could also be explained as the SetStopLoss is being called after the entry which would likely use old values.

    The solution for this would be instead to just move the SetStopLoss statements before the Entry as this will prime the order with the correct prices, and if the Entry is successful the stop already knows you are long so there is no need to check if you are already long.

    The revised stop would be:

    SetStopLoss(CalculationMode.Price, Low[LoBar]-(plusX*TickSize))
    EnterLong();

    If this needs updated after the fact, you would supply this exact same statement again later in the logic that updates the Stop.

    I look forward to being of further assistance.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by futurenow, 12-06-2021, 05:49 PM
    18 responses
    878 views
    0 likes
    Last Post dj0ntz
    by dj0ntz
     
    Started by nailz420, 05-14-2025, 09:14 AM
    1 response
    117 views
    0 likes
    Last Post NinjaTrader_ChristopherJ  
    Started by NinjaTrader_Brett, 05-12-2025, 03:19 PM
    0 responses
    521 views
    1 like
    Last Post NinjaTrader_Brett  
    Started by domjabs, 05-12-2025, 01:55 PM
    2 responses
    84 views
    0 likes
    Last Post domjabs
    by domjabs
     
    Started by Morning Cup Of Trades, 05-12-2025, 11:50 AM
    1 response
    122 views
    0 likes
    Last Post NinjaTrader_ChristopherJ  
    Working...
    X