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 Limit Orders Cant Be Placed

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

    Buy Stop Limit Orders Cant Be Placed

    Hi my strategy occacinally gives out this error when Open is off from previous Close. Can this be prevented? otherwise i cant test the strategy.
    Strategy is on eachtick with Isfirsttickofbar. Do we have an example for it how to deal with it?

    Click image for larger version

Name:	image.png
Views:	168
Size:	344.9 KB
ID:	1255772

    #2
    Hello tkaboris,

    This error would depend on how you are submitting the targets, can you provide detail about how you are setting the target prices and what methods you are using to submit them?
    JesseNinjaTrader Customer Service

    Comment


      #3
      tkaboris Perhaps a useful first "sanity" check on the orders is to ensure that all requested order prices adhere to the basic market requirements for all orders:
      • Buy Limit Order: Set price <= current Ask
      • Sell Limit Order: Set price >= current Bid
      • Buy Stop Order: Set price >= current Ask
      • Sell Stop Order: Set price <= current Bid
      Those conditions need to be satisfied when submitted orders are actioned at the exchange. Sometimes, in fast moving markets, the conditions are met when the submit occurs, but by the time they are actioned at the exchange, the price has moved quickly and made them invalid. So, keep the degree of market volatility in mind as you determine the prices, and allow for potential quick price changes.

      From there, further information as NinjaTrader_Jesse has requested would help.

      Thanks.
      Multi-Dimensional Managed Trading
      jeronymite
      NinjaTrader Ecosystem Vendor - Mizpah Software

      Comment


        #4
        Thank you jeronymite
        Thank you for your response.

        in my entry i have
        EnterLong(PositionSize, "TLL");
        and in buy stops
        ExitLongStopMarket(0, true, Position.Quantity, slLongMinMax[1], "SLL", "TLL");
        ExitLongLimit(0, true, Position.Quantity, Position.AveragePrice + (TickSize * tpByATR), "PTL", "TLL");

        Comment


          #5
          Hello tkaboris,

          In this case because you are using variables for the prices you would need to check that those prices are valid and on the correct side of the market before submitting those orders. As jeronymite mentioned each type of order has a specific requirement for general placement.

          Depending on your condition you can either reprice the variable to be valid or if the price is valid you could use it as is. Keep in mind as jeronymite also mentioned in fast moving markets if you price an order very close to the current price you may see this type of rejection due to when the order is received on the exchange. If the orders price is on the wrong side of the market it will always get rejected.
          JesseNinjaTrader Customer Service

          Comment


            #6
            Thank you,
            can you please help me on how to reprice or recalcualte to make sure they are valid? in my case Position.AveragePrice + (TickSize * tpByATR)?
            or maybe create a slippage
            i have those in doubles
            double atrvalue = atrIndicator[0];
            double tpByATR = Math.Round(atrvalue/TickSize);​
            Last edited by tkaboris; 06-13-2023, 07:44 AM.

            Comment


              #7
              Hello tkaboris,

              That depends on the order and the direction of the order. You would have to add conditions depending on the direction of the trade and check that your variables price is less or greater than the current last price or also the items that jeronymite had mentioned.

              Depending on how your condition evaluates you would either reset the variable to a new price of your choosing or leave it as is because it was valid when you checked it. The conditions would have to come after where you defined those variables so you can reset them if needed.

              I would highly suggest to use Print statements to output the prices that you are calculating so you can get a better idea of how far off market you were at the time of the error based on when you submitted the order. That would help to adjust offsets for the specific market you are trading.

              JesseNinjaTrader Customer Service

              Comment


                #8
                Thank you. Would the solution be like this for Longs to make sure i dont get that message again that buy stop limit cant be placed below market? I inserted new addition in bolds. Got variable bid and ask, then created new var tpbyatrrecalulated and assigned newly created var to exitlonglimit




                if ((Position.MarketPosition == MarketPosition.Long && myFreeTradeLong == true) )

                {
                double ask = GetCurrentAsk();
                double bid = GetCurrentBid();

                if (FixedStopLoss)
                {

                ExitLongStopMarket(0, true, Position.Quantity, StopLossTicks, "SLL", "TLL");
                }



                if (UseTPByATR)
                {
                tpByATRRecalculated = Instrument.MasterInstrument.RoundToTickSize(averag eFillPrice + tpByATR * TickSize);

                if(ask > 0)
                {
                ExitLongLimit(0, true, Position.Quantity, Position.AveragePrice + (TickSize * tpByATR), "tpByATRRecalculated", "TLL");
                }
                }



                }​
                Last edited by tkaboris; 06-13-2023, 09:03 AM.

                Comment


                  #9
                  Hello tkaboris,

                  You would need to test that and use Prints to make sure the values you are using are valid for the symbol you are trading. I don't see that you are checking if the calculated price is valid so you likely will still have issues with that. The ask variable will always be above 0 so that condition is just going to always be true.

                  Your average fill price is stationary meaning when you call that logic it may be above or below the current market.You would have to check if the value you calculated it above or below the current market before calling the exit method or updating a orders price.

                  If you are not sure which side of the market an order goes on you can also use the chart traders right click order placement menu, that adjusts the list of available orders depending on where you click above or below the current price.

                  JesseNinjaTrader Customer Service

                  Comment


                    #10
                    Do you have an example of how to reset to a new price? or to check if calculated price is valid?
                    Depending on how your condition evaluates you would either reset the variable to a new price of your choosing

                    Comment


                      #11
                      Hello tkaboris,

                      I am not aware of a specific example for that. To do that you would need to make a price condition checking your variable against the Close or the ask/bid depending on the direction of the order.

                      This is your initial calculation:

                      tpByATRRecalculated = Instrument.MasterInstrument.RoundToTickSize(averag eFillPrice + tpByATR * TickSize);

                      After that line of code you would need to make a condition checking if tpByATRRecalculated is on the right side of the market for the order in question. If its not then inside that condition you would assign a new value to tpByATRRecalculated which is on the correct side of the market and matches what goal you had for that order. That part is up to you on how you wanted to handle that, you can use a larger offset and recalculate the same items or use something completely different. As long as the new price you make is on the right side of the market that helps to avoid the error you are seeing.
                      JesseNinjaTrader Customer Service

                      Comment


                        #12
                        I added prints to print tpByATRRecalculated to both before condition and after order is placed and my tpByATRRecalculated is always 0. Its0 before the order submitted or after..
                        tpByATRRecalculated = Instrument.MasterInstrument.RoundToTickSize(averag eFillPrice + tpByATR * TickSize);

                        Comment


                          #13
                          Hello tkaboris,

                          If that is an always 0 value then somewhere else in your logic there is a problem, you would need to continue to use prints to find out why thats not being set to a value. The line I highlighted uses all variables that you are setting somewhere else in your script so you may need to look in those other areas to find out why thats 0. TickSize is the only variable that is auto populated from the line you have shown, averageFillPrice + tpByATR are two of your own variables.

                          In your original post the error had a price for the order so in that case whatever logic was used to calculate the orders price was calculating a value but it ended up on the wrong side of the market once submitted.



                          JesseNinjaTrader Customer Service

                          Comment


                            #14
                            ok i i placed print in the wrong plase. Right now tpByATRRecalculated is displaying ok.
                            However I am a bit confused. according to the image above or this one, which order failed? is it Take Profit or Stop Loss, it was a long order but there was a gap down
                            I am assuming it was a buy stop which is a Stop Loss. and not Take profit which tpByATRRecalculated refered to.

                            Below I implemented slrecalculated which comes from variable slLongMinMax[1](below previous bar) and also implemented ApproxCompare but not sure if this was the good option.. Is this correct?

                            if ((Position.MarketPosition == MarketPosition.Long && myFreeTradeLong == true) )

                            {
                            double ask = GetCurrentAsk();
                            double bid = GetCurrentBid();
                            double tpByATRRecalculated = Instrument.MasterInstrument.RoundToTickSize(Positi on.AveragePrice + tpByATR * TickSize);
                            double slRecalculated = Instrument.MasterInstrument.RoundToTickSize(Positi on.AveragePrice - slLongMinMax[1] * TickSize);
                            if (FixedStopLoss)
                            {
                            if(sl.ApproxCompare(bid) < 0)
                            {
                            ExitLongStopMarket(0, true, Position.Quantity, slLongMinMax[1], "SLL", "TLL");
                            }
                            }



                            if (UseTPByATR)

                            {


                            if(tpByATRRecalculated.ApproxCompare(ask) > 0)
                            {
                            ExitLongLimit(0, true, Position.Quantity, Position.AveragePrice + (TickSize * tpByATR), "tpByATRRecalculated", "TLL");
                            }
                            }



                            }​



                            Click image for larger version

Name:	image.png
Views:	156
Size:	1.08 MB
ID:	1255879

                            Comment


                              #15
                              Hello tkaboris,

                              The error tells you which order failed, it was the StopMarket order. Buy to cover refers to a buy order made to close out an existing short position

                              Below I implemented slrecalculated which comes from variable slLongMinMax[1](below previous bar) and also implemented ApproxCompare but not sure if this was the good option.. Is this correct?
                              You still are not doing any kind of error checking after calculating a value. It doesn't matter that you change this to use slLongMinMax[1] because you still won't know if that is on the correct side of the market. You also are not using or checking your slRecalculated value.

                              Code:
                              if(sl.ApproxCompare(bid) < 0)
                              {


                              This is saying if the sl value and bid are exactly equal with no difference at all, I am not sure what the sl variable is but that is not your calculated value so that would not make sense in this use case to check if the value you calculated is correct. You need to compare the value you pass to your order, that is the value used for the orders price. If that value is on the wrong side of the market you need to recalculate it and use the new price which is valid instead.
                              Last edited by NinjaTrader_Jesse; 06-13-2023, 11:08 AM.
                              JesseNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by ntram, Today, 05:39 PM
                              0 responses
                              2 views
                              0 likes
                              Last Post ntram
                              by ntram
                               
                              Started by giulyko00, Today, 11:49 AM
                              2 responses
                              11 views
                              0 likes
                              Last Post giulyko00  
                              Started by Aviram Y, Today, 06:03 AM
                              2 responses
                              10 views
                              0 likes
                              Last Post Aviram Y  
                              Started by FishTrade, Today, 03:42 PM
                              0 responses
                              1 view
                              0 likes
                              Last Post FishTrade  
                              Started by Richozzy38, Today, 01:06 PM
                              3 responses
                              14 views
                              0 likes
                              Last Post Richozzy38  
                              Working...
                              X