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

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 mattbsea, Today, 05:44 PM
    0 responses
    3 views
    0 likes
    Last Post mattbsea  
    Started by RideMe, 04-07-2024, 04:54 PM
    6 responses
    31 views
    0 likes
    Last Post RideMe
    by RideMe
     
    Started by tkaboris, Today, 05:13 PM
    0 responses
    2 views
    0 likes
    Last Post tkaboris  
    Started by GussJ, 03-04-2020, 03:11 PM
    16 responses
    3,282 views
    0 likes
    Last Post Leafcutter  
    Started by WHICKED, Today, 12:45 PM
    2 responses
    20 views
    0 likes
    Last Post WHICKED
    by WHICKED
     
    Working...
    X