Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trailing stop to the low or high of a bar

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

    Trailing stop to the low or high of a bar

    Hi

    I would like to trail my stop loss to the low or high of the following bar, my setup is the following:

    EnterShort(Convert.ToInt32(Sellqty), "Sell1");
    Stop_Price1 = Position.AveragePrice + (High[2]-Position.AveragePrice);
    SetStopLoss("Sell1", CalculationMode.Price, Stop_Price1, false);

    So my stop loss at the begining of the order is place at the low or high of the second previous bar of the trigger bar

    Once the current bar closes I want that the stop loss trails to the low or high of the next bar, so there´s always 3 bars in between my stop loss and the current price.

    I tried to do this
    EnterShort(Convert.ToInt32(Sellqty), "Sell1");
    Stop_Price1 = Position.AveragePrice + (High[2]-Position.AveragePrice);
    SetStopLoss("Sell1", CalculationMode.Price, Stop_Price1, false);
    Stop_Trigger = (Position.AveragePrice - (Close[-1])) ;



    protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
    {
    // Print some data to the Output window
    if (marketDataUpdate.MarketDataType == MarketDataType.Last)
    {
    if ((Position.MarketPosition == MarketPosition.Long)
    && (Times[0][0].TimeOfDay >= Hora_inicio.TimeOfDay)
    && (Times[0][0].TimeOfDay <= Hora_final.TimeOfDay)
    && (Stop_Price1 != 0)
    && (UseBreakeven == true)
    && (marketDataUpdate.Price >= Stop_Trigger))
    {
    Stop_Price1 = (marketDataUpdate.Price - (marketDataUpdate.Price-Low[2])) ;
    Stop_Trigger = (marketDataUpdate.Price + (Close[0])) ;
    SetStopLoss("Buy1", CalculationMode.Price, Stop_Price1, false);
    Print("Setting Stop ="+Stop_Price1);
    }


    }

    if (marketDataUpdate.MarketDataType == MarketDataType.Last)
    {
    if ((Position.MarketPosition == MarketPosition.Short)
    && (Times[0][0].TimeOfDay >= Hora_inicio.TimeOfDay)
    && (Times[0][0].TimeOfDay <= Hora_final.TimeOfDay)
    && (Stop_Price1 != 0)
    && (UseBreakeven == true)
    && (marketDataUpdate.Price <= Stop_Trigger))
    {
    Stop_Price1 = (marketDataUpdate.Price + (High[2]-marketDataUpdate.Price)) ;
    Stop_Trigger = (marketDataUpdate.Price - (Close[0])) ;
    SetStopLoss("Sell1", CalculationMode.Price, Stop_Price1, false);
    }

    But it didn´t work

    Thank you

    #2
    Hello henryd333,

    Below are links to some examples of custom trailing logic you may find helpful.


    Please note, that set methods like SetStopLoss() must be called before the entry, not after. Once working, set methods can be called again to update the price to a new price.

    Confirm that the price you are using is valid. For sell stops the stop price must be below the current bid. For buy stops the stop price must be above the current ask.

    Use prints and TraceOrders to understand unexpected behavior.


    Save the output to a text file and include this with your post if you would like review the output.
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Mindset, 04-21-2026, 06:46 AM
    0 responses
    88 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by M4ndoo, 04-20-2026, 05:21 PM
    0 responses
    134 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by M4ndoo, 04-19-2026, 05:54 PM
    0 responses
    68 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by cmoran13, 04-16-2026, 01:02 PM
    0 responses
    119 views
    0 likes
    Last Post cmoran13  
    Started by PaulMohn, 04-10-2026, 11:11 AM
    0 responses
    67 views
    0 likes
    Last Post PaulMohn  
    Working...
    X