Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Entering with Stop order, unmanaged

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

    Entering with Stop order, unmanaged

    There is a forum article I was able to find about this, called
    " difference between enter long by stop limit order and exit long by stop limit order".

    However, I want to do this with unmanaged approach and am getting an error:

    Click image for larger version  Name:	image.png Views:	0 Size:	4.6 KB ID:	1325898

    So obviously I am not doing it correctly. I wrote a function to place​ a limit entry order; that is, for a price below current price (for a buy):

    Code:
    private void PlaceLimitOrder(string dir, double price, int qty, string name="Entry")
    {
        /// If we are already in a limit order, cancel or ignore
        if (entryOrder != null)
        {
            /// If a limit order is already active...
            if (entryOrder.OrderType == OrderType.Limit)
            {
                if ((entryOrder.OrderAction == OrderAction.SellShort  &&  dir == "Short")
                ||  (entryOrder.OrderAction == OrderAction.Buy  &&  dir == "Long"))
                {
                    if (entryOrder.LimitPrice == price)
                    {
                        Log("Already in Pending " + dir + " Limit order @ " + price + " - Doing nothing");
                        lastCaller = "";    // reset so if this method is entered again before other log, reprint the function name
                        return;
                    }
                    else
                    {
                        Log("Already in Pending " + dir + " Limit order, but at diff price (" + entryOrder.LimitPrice +
                               ", not requested price of " + price + ") - Cancelling old " + entryOrder.OrderAction.ToString() +
                               " pending order");
    
                        CancelOrder(entryOrder);
                    }
                }
                else
                    Log("Unexpected: entryOrder != null, but OrderAction check failed");
            }
        }
        Log("Place limit order @ " + price + ": dir = " + dir + ", qty = " + qty + ", name = " + name);
        var action = (dir == "Long") ? OrderAction.Buy : OrderAction.SellShort;
        SubmitOrderUnmanaged(1, action, OrderType.Limit, qty, price, 0, "", "Entry");
        pendingPlaced = true;
        lastCaller = "";    // reset so if this method is entered again before other log, reprint the function name
    }
    ​

    This works fine. But now I want a stop order entry -- I think - for a buy, that would be for a price above current price.
    So I modified the one above to this:

    Code:
    private void PlaceStopOrder(string dir, double price, int qty, string name="Entry")
    {
        /// If we are already in a limit order, cancel or ignore
        if (entryOrder != null)
        {
            /// If a limit order is already active...
            if (entryOrder.OrderType == OrderType.StopLimit)
            {
                if ((entryOrder.OrderAction == OrderAction.SellShort  &&  dir == "Short")
                ||  (entryOrder.OrderAction == OrderAction.Buy  &&  dir == "Long"))
                {
                    if (entryOrder.LimitPrice == price)
                    {
                        Log("Already in Pending " + dir + " Stop order @ " + price + " - Doing nothing");
                        lastCaller = "";    // reset so if this method is entered again before other log, reprint the function name
                        return;
                    }
                    else
                    {
                        Log("Already in Pending " + dir + " Stop order, but at diff price (" + entryOrder.LimitPrice +
                               ", not requested price of " + price + ") - Cancelling old " + entryOrder.OrderAction.ToString() +
                               " pending order");
    
                        CancelOrder(entryOrder);
                    }
                }
                else
                    Log("Unexpected: entryOrder != null, but OrderAction check failed");
            }
        }
        Log("Place stop order @ " + price + ": dir = " + dir + ", qty = " + qty + ", name = " + name);
        var action = (dir == "Long") ? OrderAction.Buy : OrderAction.SellShort;
        SubmitOrderUnmanaged(1, action, OrderType.StopLimit, qty, price, 0, "", "Entry");
        pendingPlaced = true;
        lastCaller = "";    // reset so if this method is entered again before other log, reprint the function name
    }
    ​
    Can someone tell me what I have done wrong?




    #2
    Hello DerkWehler,

    The limit price of a sell stop limit order must be less than the stop price.

    Currently, you are providing a value of 0 for the stop price.

    Were you intending to use a stop market order (OrderType.StopMarket) instead of a stop limit order (OrderType.StopLimit)?

    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      Hello DerkWehler,

      Were you intending to use a stop market order (OrderType.StopMarket) instead of a stop limit order (OrderType.StopLimit)?

      https://ninjatrader.com/support/help...runmanaged.htm

      Chelsea, thanks for the quick reply!

      Umm...probably..? Is a "StopLimit" order only for exiting? I thought stop market was, when specified price it hit, enter a market order (so entry price might be slightly different from specified price, due to spread or fast-moving market).

      I just want to enter a long trade when price comes UP to my level, like one might use for a breakout strategy.

      Comment


        #4
        Hello DerkWehler,

        Any order type can be used to enter or exit.

        A market order can enter or exit a position.
        A limit order can enter or exit a position.
        A stop market order can enter or exit a position.
        A stop limit order can enter or exit a position.

        A stop limit order when the stop price is touched becomes a limit order at the limit price.
        A stop market order becomes a market order when the stop price is touched and fills at market.


        You are using OrderType.StopLimit in your code and you are not using OrderType.StopMarket.
        You are supplying 'price' as the limit price and '0' as the stop price.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          What I wanted to do was, when price comes down to a 25 line (e.g. NQ, 20825), I want it to go 2 ticks past, so 20824.50. and when touched, I [tried to] enter a stop limit at 20825, so that if it comes back up two ticks, it enters long, but if it just keeps going down, I never enter, as I would if I placed a limit order). So apparently I just have the "price" and zero reversed?

          Update:
          I tried reversing them and some trades appeared on historical, but others did not. Then I adjusted my log lines and tried to run it again and NT locked up, so there is still some error.

          Restarted NT and ran again, but still got:

          Click image for larger version  Name:	image.png Views:	0 Size:	4.9 KB ID:	1325959

          That makes sense, but it's supposed to be placing it at 2 ticks below the 25 line, so they should be above price.​

          Here is my current entry function:

          Code:
          private void PlaceStopOrder(string dir, double price, int qty, string name="Entry")
          {
              /// If we are already in a limit order, cancel or ignore
              if (entryOrder != null)
              {
                  /// If a stop order is already active...
                  if (entryOrder.OrderType == OrderType.StopLimit)
                  {
                      if ((entryOrder.OrderAction == OrderAction.SellShort  &&  dir == "Short")
                      ||  (entryOrder.OrderAction == OrderAction.Buy  &&  dir == "Long"))
                      {
                          if (entryOrder.StopPrice == price)
                          {
                              Log("Already in Pending " + dir + " Stop order @ " + price + " - Doing nothing");
                              lastCaller = "";    // reset so if this method is entered again before other log, reprint the function name
                              return;
                          }
                          else
                          {
                              Log("Already in Pending " + dir + " Stop order, but at diff price (" +
                                      entryOrder.StopPrice + ", not requested price of " + price + ") - Cancelling old " +
                                      entryOrder.OrderAction.ToString() + " pending order");
                              CancelOrder(entryOrder);
                          }
                      }
                      else
                          Log("Unexpected: entryOrder != null, but OrderAction check failed");
                  }
              }
              Log("Place stop order @ " + price + ": dir = " + dir + ", qty = " + qty + ", name = " + name);
              var action = (dir == "Long") ? OrderAction.Buy : OrderAction.SellShort;
              SubmitOrderUnmanaged(1, action, OrderType.StopLimit, qty, 0, price, "", "Entry");
              pendingPlaced = true;
              lastCaller = "";    // reset so if this method is entered again before other log, reprint the function name
          }
          Last edited by DerkWehler; 11-27-2024, 03:21 PM.

          Comment


            #6
            Hello DerkWehler,

            "What I wanted to do was, when price comes down to a 25 line (e.g. NQ, 20825), I want it to go 2 ticks past, so 20824.50. and when touched, I [tried to] enter a stop limit at 20825, so that if it comes back up two ticks, it enters long, but if it just keeps going down, I never enter, as I would if I placed a limit order)."

            This doesn't make sense for the error message. The error message is that a sell stop limit has the limit price greater than the stop price.

            You cannot enter a long position with a sell order.


            Further, a sell stop limit order would not allow for the limit price to be greater than the stop price.
            Simulated stop limits are not available in NinjaScript.

            Instead, you would need to use logic.

            When the price is 20824.50 set a bool to know this price was hit.
            When the price is 20825.00 and the bool is true, submit a limit order and set the bool back to false.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_ChelseaB View Post
              When the price is 20824.50 set a bool to know this price was hit.
              When the price is 20825.00 and the bool is true, submit a limit order and set the bool back to false.
              Apologies for my confusion. I come from the forex world where everything is more consistent and symmetrical (e.g. you just do simple buys and sells, not "sell short" and "buy to cover". If you want a pending order, there were just limits and stops, depending on which direction you are trading and current price compared to desired price).


              Previously I was using the PlaceLimitOrder to place the pending buy limit on a given trigger, at the 20825 level (and it was working). Then I decided that, in the case where market blows past the limit downward, and my stop gets hit, that maybe I could avoid the loss by using this new method.

              It occurred to me that there were two ways to implement it; one was to wait until 20824.50 was touched, set a bool, and then if 20825 touched, place a market order.

              The other manner in which to do it was to place a stop order @ 20825 when 20824.50 was touched, and I decided to use that because I would be more likely to be filled right at 20825.

              You seem to be suggesting using the former way, except you said to place a limit order. But if price goes to 20824.50, then back to 20825, I cannot place a limit order right at price; it would need to be below price, so I don't understand that logic. To place a limit my price would have to be at least one tick below, wouldn't it, at 20824.75?

              Comment


                #8
                Hello DerkWehler,

                It depends on if you are trying to enter a short position with a sell order or enter a long position with a buy order.

                A buy limit should be placed below the current ask.
                A sell limit should be placed above the current bid.

                If you are using a sell limit order to enter a short, as discussed in the previous posts, the sell limit order would be placed above the bid.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_ChelseaB View Post
                  Hello DerkWehler,

                  It depends on if you are trying to enter a short position with a sell order or enter a long position with a buy order.

                  A buy limit should be placed below the current ask.
                  A sell limit should be placed above the current bid.

                  If you are using a sell limit order to enter a short, as discussed in the previous posts, the sell limit order would be placed above the bid.
                  If I want to place a buy order above current price, at 20825, I would wait until the ask is < 20825 and the place what kind of order?

                  Using:
                  SubmitOrderUnmanaged(1, action, type, qty, limit, stop, "", "Entry");

                  I am using "1" for the BiP which is tick series. I need to know what 'action', 'type', 'limit' and 'stop' should be to place order correctly.

                  Comment


                    #10
                    Hello DerkWehler,

                    A buy order above the ask would be a limit order.

                    The OrderAction should be OrderAction.Buy.
                    The OrderType should be OrderType.Limit.
                    The LimitPrice should be the price you want to place the limit order above the ask.
                    The StopPrice should be 0 (but can be any number because it will be ignored for a limit order).
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_ChelseaB View Post
                      Hello DerkWehler,

                      A buy order above the ask would be a limit order.

                      The OrderAction should be OrderAction.Buy.
                      The OrderType should be OrderType.Limit.
                      The LimitPrice should be the price you want to place the limit order above the ask.
                      The StopPrice should be 0 (but can be any number because it will be ignored for a limit order).
                      Isn't that exactly the same as I would do if I wanted to place a buy order below the Ask?

                      That is what I was doing before and it worked.

                      Comment


                        #12
                        Hello DerkWehler,

                        "Isn't that exactly the same as I would do if I wanted to place a buy order below the Ask?"

                        Apologies, I re-read what i wrote and realized I mispoke.

                        A buy limit goes below the ask.
                        A sell limit goes above the bid.
                        A buy stop goes above the ask.
                        A sell stop goes below the bid.

                        If the order is a buy order and you want this above the ask use OrderType.StopMarket.
                        If the order is a buy order and you want this below the ask use OrderType.Limit.

                        You can verify the order types by opening chart trader and then right-clicking on the chart above or below the market price.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Originally posted by NinjaTrader_ChelseaB View Post
                          If the order is a buy order and you want this above the ask use OrderType.StopMarket.
                          If the order is a buy order and you want this below the ask use OrderType.Limit.
                          Ok, and don't I then need to send the requested price as the second (price) parameter?

                          Originally posted by NinjaTrader_ChelseaB View Post
                          You can verify the order types by opening chart trader and then right-clicking on the chart above or below the market price.


                          Yes, I see that right clicking on a chart below price has the buy options of Limit and MIT, and clicking above has Stop Limit and Stop Market. I thought I knew what all those meant until I started trying to get it to work programmatically.

                          Why did you recommend StopMarket rather than StopLimit? I presume that StopMarket is the analogous to 'Market If Touched' and StopLimit analogous to 'Limit'.



                          Last edited by DerkWehler; 12-02-2024, 03:32 PM.

                          Comment


                            #14
                            Hello DerkWehler,

                            "Ok, and don't I then need to send the requested price as the second (price) parameter?"

                            For a limit order you will need to supply the LimitPrice. For a stop market order you will need to supply a StopPrice.
                            For a stop limit order you will need to supply the LimitPrice and StopPrice.

                            I suggest a stop market as this would fill at market when touched, and you do not have to specify a limit price that is on the correct side of the stop price.

                            Your code was supplying a stop price of 0 so I assumed you had meant to supply a stop price and not a limit price while using a stop market order.
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              My code in post #1 was backward, yes; code in post #5 had this corrected.

                              Just to be sure I have this right:

                              I am placing a long pending order above current ask. I wait until ask is < 20825, then I place a:

                              StopMarket order for 20825: As soon as ask touches 20825 again, it will enter at market.

                              or

                              StopLimit order for 20825: As soon as ask touches 20825 again, it will enter at 20825.


                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              600 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              347 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              103 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              558 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              558 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X