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

Getting error when I place a sell order with auto trail loss

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

    Getting error when I place a sell order with auto trail loss

    Hello,
    i am creating a strategy that receives a signal from HalfTrind indicator to buy or sell
    when i receive a signal, i check if there is any other position is open, then i close it and open the new one.

    currently, when i try to open a new order, i got this error message
    Buy stop or buy stop limit orders can't be placed below the market. affected order: BuyToCover 1 stopMarket @1234.00

    i debug the code, and this was the value when i get the erro
    Short High[0]: 4138.25 currentATR: 3.0280371924987 newStop: 4141.2780371925 _stop: 4141.2780371925

    Code:
                                    if (Position.MarketPosition == MarketPosition.Flat)
                                    {
                                        EnterShort("ShortOrder");
                                        var newStop = High[0] + currentATR;
                                        _shortStop = _shortStop == 0 ? newStop : _shortStop;
                                        _shortStop = Math.Min(_shortStop, newStop);
                                        Print("3 " + ShortOrder + " High[0]: " + High[0] + "  currentATR: " + currentATR + "   newStop: " + newStop + "   _stop: " + _shortStop);
                                        ExitShortStopMarket(_shortStop, "Trail Short", ShortOrder);
                                        //SetStopLoss(CalculationMode.Price, _shortStop);
                                    }
                                    else if (Position.MarketPosition == MarketPosition.Long)
                                    {
                                        ExitLong("LongOrder");
                                        if (Low[0] < Position.AveragePrice)
                                        {
                                            EnterShort("ShortOrder");
                                            var newStop = High[0] + currentATR;
                                            _shortStop = _shortStop == 0 ? newStop : _shortStop;
                                            _shortStop = Math.Min(_shortStop, newStop);
                                            Print("4 " + ShortOrder + " High[0]: " + High[0] + "  currentATR: " + currentATR + "   newStop: " + newStop + "   _stop: " + _shortStop);
                                            ExitShortStopMarket(_shortStop, "Trail Short", ShortOrder);
                                            //SetStopLoss(CalculationMode.Price, _shortStop);
                                        }
                                    }​
    does anyone know what is the problem in my code?

    Thanks

    #2
    If your stop market order is across the market, you should use a ExitLongMarket or ExitShortMarket instead, so the stop does not reject.
    Bruce DeVault
    QuantKey Trading Vendor Services
    NinjaTrader Ecosystem Vendor - QuantKey

    Comment


      #3
      Hello Algo Trading,

      Thanks for your post.

      This error message "Buy stop or buy stop limit orders can't be placed below the market" indicates that your strategy is submitting an order to the wrong side of the market.

      Buy stop orders must be placed above the current ask price. Sell stop orders must be placed below the current bid price.

      You should add prints to the strategy to determine why the stop order is being ignored. One line above where the stop order is placed, print the GetCurrentAsk()/GetCurrentBid() and print the price being submitted to the order method.

      Note if this is due to market volatility then there isn't really a way to 100% avoid this occurring, as in volatile markets the market could move so far and fast that this would occur.

      Something you could consider is using GetCurrentBid() and GetCurrentAsk() to offset orders so that they are more likely to land on the correct side of the market.

      See these help guide pages for more information.
      GetCurrentBid(): https://ninjatrader.com/support/help...currentbid.htm
      GetCurrentAsk(): https://ninjatrader.com/support/help...currentask.htm

      You could use RealtimeErrorHandling.IgnoreAllErrors to determine the behavior of a strategy when the strategy places an order that is returned "Rejected" or "UnableToChangeOrder". The default behavior is to stop the strategy, cancel any remaining working orders, and then close any open positions.

      Please note that setting this property value to IgnoreAllErrors can have serious adverse affects on a running strategy unless you have programmed your own order rejection handling in the OnOrderUpdate() method. To do this you could trap the rejected order within OnOrderUpdate() followed by defining your own order rejection handling behavior for the rejected order.

      Please see the example in the help guide link below that demonstrates using RealtimeErrorHandling and trapping a rejected order in OnOrderUpdate().

      RealtimeErrorHandling — https://ninjatrader.com/es/support/h...orhandling.htm
      Last edited by NinjaTrader_BrandonH; 05-21-2023, 05:15 PM.
      Brandon H.NinjaTrader Customer Service

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by StockTrader88, 03-06-2021, 08:58 AM
      45 responses
      3,992 views
      3 likes
      Last Post johntraderuser2  
      Started by TAJTrades, Today, 09:46 AM
      0 responses
      7 views
      0 likes
      Last Post TAJTrades  
      Started by rhyminkevin, Yesterday, 04:58 PM
      5 responses
      62 views
      0 likes
      Last Post dp8282
      by dp8282
       
      Started by realblubb, Today, 09:28 AM
      0 responses
      8 views
      0 likes
      Last Post realblubb  
      Started by AaronKoRn, Yesterday, 09:49 PM
      1 response
      19 views
      0 likes
      Last Post Rikazkhan007  
      Working...
      X