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 rtwave, 04-12-2024, 09:30 AM
    5 responses
    37 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by funk10101, Today, 12:02 AM
    1 response
    11 views
    0 likes
    Last Post NinjaTrader_LuisH  
    Started by GLFX005, Today, 03:23 AM
    1 response
    6 views
    0 likes
    Last Post NinjaTrader_Erick  
    Started by nandhumca, Yesterday, 03:41 PM
    1 response
    13 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Started by The_Sec, Yesterday, 03:37 PM
    1 response
    11 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Working...
    X