Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Set SL above previous Bar High for Short in unamanged approach

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

    Set SL above previous Bar High for Short in unamanged approach

    Hi i want to add ability to add SL on previous bar high for short. I created stopAreaShort and put in in its place for shorts, its doesnt set SL one tick above.

    stopAreaShort = High[1] + 1 * TickSize;
    Code:
    
                   #region OnOrderUpdate
    
            protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
              {
                if (orderState == OrderState.Filled && order.Name == "BT")
                {
                    double ask = GetCurrentAsk();
                    double bid = GetCurrentBid();
    
    
                    string oco = Guid.NewGuid().ToString();
    
                    if (order.IsLong)
                    {
                        double tp = Instrument.MasterInstrument.RoundToTickSize(averageFillPrice + ProfitTarget * TickSize);
    
                        if (ProfitTarget > 0 && tp.ApproxCompare(ask) > 0)
                        {
                            targetOrder = SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.Limit, filled, tp, 0, oco, "PT");
                        }
    
                        double sl = Instrument.MasterInstrument.RoundToTickSize(averageFillPrice - StopLoss * TickSize);
    
                        if (StopLoss > 0 && sl.ApproxCompare(bid) < 0)
                        {
                            stopOrder = SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.StopMarket, filled, 0, sl, oco, "SL");
                        }
                    }
    
                    if (order.IsShort)
                    {
                        double tp = Instrument.MasterInstrument.RoundToTickSize(averageFillPrice - ProfitTarget * TickSize);
    
                        if (ProfitTarget > 0 && tp.ApproxCompare(bid) < 0)
                        {
                            targetOrder = SubmitOrderUnmanaged(0, OrderAction.BuyToCover, OrderType.Limit, filled, tp, 0, oco, "PT");
                        }
    
                        double sl = Instrument.MasterInstrument.RoundToTickSize(averageFillPrice + StopLoss * TickSize);
    
    //                    if (StopLoss > 0 && sl.ApproxCompare(ask) > 0)
    //                    {
    //                        stopOrder = SubmitOrderUnmanaged(0, OrderAction.BuyToCover, OrderType.StopMarket, filled, 0, sl, oco, "SL");
    //                    }
                         if (StopLoss <= 0 )
                            {
                            stopOrder = SubmitOrderUnmanaged(0, OrderAction.BuyToCover, OrderType.StopMarket, filled, 0, stopAreaShort, oco, "SL");
                            }
                    }
                }
              }
    
            #endregion​​

    #2
    Actually its working as expected.. Thank you

    Comment


      #3
      Actually often times my Sl is not setting because price moves too fast and it says StopLoss cant be placed above market price, something like that. Is there a way to ensure SL will be placed above below recent high low?

      Comment


        #4
        Hi tkaboris, to handle a rejected order you can use the OnOrderUpdate method and re-submit the order if the order state Rejected is seen. If you are placing a stop order at the bid or ask there is always the risk that an in-flight price change can occur and the order will be rejected once it reaches the exchange.

        Comment


          #5
          I already use onOrderupdate, thats where i place my SL. Do you have an example of how to resumbit the order?

          Comment


            #6
            Hi, There is an example on the RealTimeErrorHandling page:

            https://ninjatrader.com/support/help...meerrorhandlin g.htm

            The section with "if (order.OrderState == OrderState.Rejected)" is the location you would re-submit the order to a legal price level.

            Kind regards,
            -ChrisL

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by NullPointStrategies, Yesterday, 05:17 AM
            0 responses
            55 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            132 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            73 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            45 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            49 views
            0 likes
            Last Post TheRealMorford  
            Working...
            X