Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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.
    JesseNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by sy2023, 09-15-2023, 08:51 PM
    2 responses
    234 views
    0 likes
    Last Post Crusade12  
    Started by kiss987, 05-16-2018, 02:19 AM
    14 responses
    2,870 views
    0 likes
    Last Post bobperez  
    Started by jalley, 03-22-2021, 12:17 PM
    4 responses
    133 views
    0 likes
    Last Post bobperez  
    Started by HGTrader, Today, 02:13 PM
    6 responses
    13 views
    0 likes
    Last Post HGTrader  
    Started by rbeckmann05, Today, 03:45 PM
    0 responses
    7 views
    0 likes
    Last Post rbeckmann05  
    Working...
    X