Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Execution system development

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

    Execution system development

    I'd like to develop an execution strategy where I place a buy limit order at the bid for 80% of the time of the bar, if I don't get filled I move it to mid price for another 15% of the time, and just in the 5% of the time left, I will hit the bid.
    Could you tell me with Indexes/Modules should I use to achieve this?

    Thank you

    #2
    Hi stefy,

    To use percentages like that please take a look at the Bars.PercentComplete property. http://www.ninjatrader-support.com/H...tComplete.html
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      I understand the code should go into the OnBarUpdate() Method.

      I did the following:

      double Bid = GetCurrentBid();
      double Ask = GetCurrentAsk();


      if (Bars.PercentComplete < 0.8)
      EnterLongLimit(Bid,
      "LongLimitBid");


      if (Bars.PercentComplete > 0.8
      && Bars.PercentComplete < 0.9)
      EnterLongLimit((Bid + Ask)/
      2, "LongLimitMidPrice");


      if (Bars.PercentComplete > 0.9)
      EnterLong(
      "LongMarket");

      Three questions:

      1) How do I cancel and replace the orders each time I want to change my limit?

      2) When I calculate the Mid Price, does NT automatically round it up?

      3) Is the position of the code in OnBarUpdate() correct?

      Thank you

      Comment


        #4
        Hi stefy,

        You are correct in using OnBarUpdate() for this code snippet. You do not need to explicitly cancel an order. If you do not resubmit the order at every OnBarUpdate() event the order will automatically cancel. If you do resubmit the order but at a different limit price it will modify your order.

        In your case you may want to change your second condition to if (Bars.PercentComplete >= 0.8 && ....). The reason for this is so you have an all inclusive set which will allow for your order to stay alive throughout the whole bar. NinjaTrader will generally round up, but you can also use this to ensure you submit at a valid price level for your strategy. http://www.ninjatrader-support.com/H...2TickSize.html

        So you could do this:
        Code:
        [FONT=Courier New][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] (Bars.PercentComplete >= [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]0.8
        [/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]&& Bars.PercentComplete < [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]0.9[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2])
        EnterLongLimit(Instrument.MasterInstrument.Round2TickSize((Bid + Ask)/[/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]2[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]), [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000]"LongLimitMidPrice"[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]);[/SIZE][/FONT]
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          I tried to develop the strategy as follows:

          if (Bars.PercentComplete < 0.8)
          EnterLongLimit(Bid,
          "LongLimitBid");

          if (Bars.PercentComplete > 0.8
          && Bars.PercentComplete < 0.9)
          EnterLongLimit(Instrument.MasterInstrument.Round2T ickSize((Bid + Ask)/
          2), "LongLimitMidPrice");

          if (Bars.PercentComplete > 0.9)
          EnterLongLimit(Ask,
          "LongAsk");

          but I get in the Log error = order cancelled - reason: 202

          I'm attaching the Log file.

          Thank you
          Attached Files

          Comment


            #6
            stefy,

            To understand why your orders are cancelled you need to use TraceOrders = true as per this tip: http://www.ninjatrader-support.com/v...ead.php?t=3627

            Also, please make an inclusive set of statements that covers 0.8. As my previous post suggests, you need to use a >= or <= somewhere. With your current code, if the bar falls on 80% complete your order will not be resubmitted and will be cancelled.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Now it works.
              I have a question concerning exiting positions: normally I'm supposed to refer to the entry string for the exit position, but I don't know if my position has been filled with LongLimitBid, LongLimitMidPrice or LongAsk, so how am I supposed to refer to them in the ExitLongLimit? Should I use all three of them? In this case, with what syntax?

              Thank you

              Comment


                #8
                You can use all three of them. Or you can just use a flag variable to notify you as to which one was actually triggered. Set the flag variable to take on a unique value for each different entry condition and when you submit exits you can check the flag variable and then you will know which one to take.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  My exit conditions are independent from whichever entry I used, so I just need to make sure any of the three entries is valid for an exit.

                  This is the entry execution strategy for the long:

                  if (Bars.PercentComplete <= 0.8)

                  EnterLongLimit(Bid, "LongLimitBid");


                  if (Bars.PercentComplete > 0.8
                  && Bars.PercentComplete <= 0.9)
                  EnterLongLimit(Instrument.MasterInstrument.Round2T ickSize((Bid + Ask)/2), "LongLimitMidPrice");


                  if (Bars.PercentComplete > 0.9)

                  EnterLongLimit(Ask, "LongAsk");

                  For the Long Exit, I wrote:

                  if (Bars.PercentComplete <= 0.8)
                  ExitLongLimit(Ask, "ExitLongLimitBid");


                  if (Bars.PercentComplete > 0.8
                  && Bars.PercentComplete <= 0.9)
                  ExitLongLimit(Instrument.MasterInstrument.Round2Ti ckSize((Bid + Ask)/2), "ExitLongLimitMidPrice");


                  if (Bars.PercentComplete > 0.9)
                  ExitLongLimit(Bid, "ExitLongLimitAsk");

                  Like this they won't work, since I need to add the string "from entry signal", but how can I do that with 3 signals? I mean, for the first Exit condition, should I just write just:

                  f (Bars.PercentComplete <= 0.8)
                  ExitLongLimit(Ask, "ExitLongLimitBid", "LongLimitBid", "LongLimitMidPrice", "LongAsk");

                  Thank you

                  Comment


                    #10
                    Try using a "" blank fromEntrySignal name. I believe that will work. Let me know if it doesn't.
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      I tried to run the strategy with your suggestion ("" for EntrySignalName), but there is a bigger problem.
                      Let's say I'm using a 30min time frame and I want to buy, so for 80% of the time (24min) I will stay at the Bid price (limit order) and wait if the price comes to me.
                      As per the code below though, NT keeps on changing the limit price at every tick!
                      So, if it starts 1.9820 - 1.9830 (Bid/Ask), it will input EnterLongLimit 1.9820, if then it ticks to 1.9822 - 1.9832, it will input EnterLongLimit 1.9822... but in this way the condition will never be met!
                      I need to input the Limit price once and then keep it constant until I don't move to the next sub-bracket (as you remember, I have 80% of the bar at Bid, 80%-90% at Mid, 90% - end of bar at Ask).

                      Thank you

                      Comment


                        #12
                        stefy,

                        Then change your condition to evaluate to true only once via flag variables. Then on your limit order just use the liveUntilCancelled = true.
                        Josh P.NinjaTrader Customer Service

                        Comment


                          #13
                          So far I did the following (for EnterLong signals):

                          Entry Long Condition


                          {

                          if (Bars.PercentComplete <= 0.8)
                          EnterLongLimit(0, true, Position.Quantity, Bid, "LongLimitBid");


                          if (Bars.PercentComplete > 0.8
                          && Bars.PercentComplete <= 0.9)
                          EnterLongLimit(0, true, Position.Quantity, Instrument.MasterInstrument.Round2TickSize((Bid + Ask)/2), "LongLimitMidPrice");

                          if (Bars.PercentComplete > 0.9)
                          EnterLongLimit(0, true, Position.Quantity, Ask, "LongAsk");
                          }

                          The strategy so far doesn't work (I get "Limit Order cannot be lower than current Bid" and "Order Rejected").

                          I know how to use flag variables, but I don't understand how to use them in this case, to grab the first Bid of the bar (keeping it constant should be achieved via the "LiveUntilCancelled" boolean I understood).

                          Thank you

                          Comment


                            #14
                            Your order is rejected based on the reason you were given. You have to change the price you are submitting at.

                            The way you use a flag variable here is you add it to your conditions:
                            Code:
                            if (Bars.PercentComplete <= 0.8 && traded == false)
                            {
                                 EnterLongLimit(0, true, Position.Quantity, Bid, "LongLimitBid");
                                 traded = true;
                            }
                            You then reset the bool after you are done with this position.
                            Josh P.NinjaTrader Customer Service

                            Comment


                              #15
                              I stated private bool traded = false; in the Variables region, then

                              // Entry Long Condition


                              {

                              if (Bars.PercentComplete <= 0.8
                              && traded == false)
                              {
                              EnterLongLimit(0, true, Position.Quantity, Bid, "LongLimitBid");
                              traded = true;
                              }

                              if (Bars.PercentComplete > 0.8
                              && Bars.PercentComplete <= 0.9
                              && traded == false)
                              {
                              EnterLongLimit(0, true, Position.Quantity, Instrument.MasterInstrument.Round2TickSize((Bid + Ask)/2), "LongLimitMidPrice");
                              traded = true;
                              }

                              if (Bars.PercentComplete > 0.9
                              && traded == false)
                              {
                              EnterLongLimit(0, true, Position.Quantity, Ask, "LongAsk");
                              traded = true;
                              }

                              Same for Short condition, then to exit positions:


                              if (Exit Condition Long)

                              {

                              if (Bars.PercentComplete <= 0.8
                              && traded == true)

                              {
                              ExitLongLimit(0, true, Position.Quantity, Ask, "ExitLongLimitBid", "");
                              traded = false;
                              }

                              if (Bars.PercentComplete > 0.8
                              && Bars.PercentComplete <= 0.9
                              && traded == true)

                              {
                              ExitLongLimit(0, true, Position.Quantity, Instrument.MasterInstrument.Round2TickSize((Bid + Ask)/2), "ExitLongLimitMidPrice", "");
                              traded = false;
                              }

                              if (Bars.PercentComplete > 0.9
                              && traded == true)

                              {
                              ExitLongLimit(0, true, Position.Quantity, Bid, "ExitLongLimitAsk", "");
                              traded = false;
                              }
                              }

                              I used the same structure of Exit Conditions for Stop Losses and Take Profits conditions.

                              The code above doesn't trade. I switch on the strategy and nothing happens. I tried to backtest it and it stopped after few days (over 1 year of history).

                              Thank you

                              Comment

                              Latest Posts

                              Collapse

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