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

Buy Stop Error Handling

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

    Buy Stop Error Handling

    Hello,

    I'm trying to write a strategy that places a buy stop market order a tick above the close of a bar after it closes. The problem with that is that sometimes that bar will close at the high of the bar when the buy stop market order is placed or the market will be moving quickly, thus resulting in the "Buy stop or buy stop limit orders can't be placed below the market" error.

    Is there a way to catch/handle this error within my code so that when this happens the strategy dismisses the error and then enters long at market instead?

    Thanks in advance for any help!

    Fiddich

    #2
    Hello fiddich,

    Thanks for your post.

    You would need to be able to see the current price and then use that comparison of your entry level verses the current price to then decide on placing the entry. To see the current price you would have to run your strategy with Calculate.OnEachTick (or OnPriceChange). Reference: https://ninjatrader.com/support/help...?calculate.htm

    When using OnEachTick (or OnPriceChange) mode, Close[0] would actually reference the right edge current price which allows you to write something like:

    if (your conditions to enter)
    {
    if (Close[0] < High[1]+ TickSize) // is the current price less than the proposed entry level?
    {
    // okay to place buy stop here
    }
    else
    {
    // you may want to place other logic here to place the order elsewhere or perhaps use a market order.
    }
    }


    Please note that using OnEachTick (Or OnPriceChange) changes your bar references so that [0] points to the currently forming bar and [1] points to the last complete bar. It also means that your strategy code will execute as many times as there are ticks in the bar and evaluated conditions may become true and false multiple times within a bar. You may want to consider executing some code only once per bar and other code on each tick. Please see a working sample of this here: https://ninjatrader.com/support/foru...ad.php?t=19387

    Alternatively, if you use Calculate.OnBarClose, you could place the stop market order a few ticks higher but you may still get hit with the error in a fast-moving market.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the detailed response Paul.

      So just to confirm, there's no way to catch/handle the error in the event it occurs?

      Comment


        #4
        Hello fiddich,

        Thanks for your reply.

        That is correct. If the order was being rejected by the broker then you could use: https://ninjatrader.com/support/help...orhandling.htm but that would not be the case with what you are reporting.
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Hey fellas -

          I'm also running into this same situation. I thought using EnterLongStopMarket and EnterShortStopMarket would circumvent this error since it's a Market Order but it looks like this is not the case.

          Is there anyway to capture the Order Rejected error and if it happens, just use 'EnterLong' or 'EnterShort'?

          Comment


            #6
            Hello PN720,

            Thanks for your post.

            The issue is that both EnterLongStopMarket and EnterSellStopMarket both require a price value (trigger value to then submit market order) that is legit for the direction you are going relative to the current price. You might try using a larger distance from the current price or review the information in posts 2 & 4.
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Hi, if I just add this into the OnOrderUpdate method, will this work?

              // If a Long Order is rejected, just EnterLong
              if (order.Name == "Long" && order.OrderState == OrderState.Rejected)
              EnterLong("Long");

              // If a Short Order is rejected, just EnterShort
              if (order.Name == "Short" && order.OrderState == OrderState.Rejected)
              EnterShort("Short");

              Comment


                #8
                Also, if that will work, instead of EnterLong and EnterShort, how would I just cancel any pending orders but let the strategy continue working instead of exiting like it does now?

                Comment


                  #9
                  Hello PN720,

                  thanks for your reply.

                  You can certainly test your ideas on the sim101 account.

                  What you will likely experience is this message: A Buy order placed at '[date/time]' has been ignored since the stop price is less than or equal to the close price of the current bar. This is an invalid order and subsequent orders may also be ignored. Please fix your strategy.
                  Paul H.NinjaTrader Customer Service

                  Comment


                    #10
                    Hi again,

                    My strategy had been running fine on Sim money so I only started seeing the error when I switched to Live money. So, your response confuses me a little because as far as I know, the Rejected order status is one that's sent from the Broker so you can't really simulate this. Correct?

                    And by error, I'm talking about this error:

                    Buy order stop price must be above last trade price ExchangeRejectCode = 2061 affected Order: Buy 1 StopMarket @ 7092.75
                    Last edited by PN720; 07-05-2018, 01:17 PM.

                    Comment


                      #11
                      Hello PN720,

                      Thank you for your response.

                      The OrderState.Rejected logic you have in place is correct to check the OrderState as Rejected and if you want to just submit an entry as market at that point then your logic is correct to do so.

                      Order Rejections can still occur on the Sim101 account. You can test this in realtime data on the Sim101 account.

                      Please let me know if you have any questions.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by algospoke, Today, 06:40 PM
                      0 responses
                      4 views
                      0 likes
                      Last Post algospoke  
                      Started by maybeimnotrader, Today, 05:46 PM
                      0 responses
                      7 views
                      0 likes
                      Last Post maybeimnotrader  
                      Started by quantismo, Today, 05:13 PM
                      0 responses
                      6 views
                      0 likes
                      Last Post quantismo  
                      Started by AttiM, 02-14-2024, 05:20 PM
                      8 responses
                      168 views
                      0 likes
                      Last Post jeronymite  
                      Started by cre8able, Today, 04:22 PM
                      0 responses
                      9 views
                      0 likes
                      Last Post cre8able  
                      Working...
                      X