Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Help for order management

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

    Help for order management

    I am writing strategy and need to do the following (for long)

    note: CalculateOnBarClose = true;

    OnBarUpdate()
    {
    if entry condition is met
    Open ATM strategy order with stop order buy above previous bar high

    if ATM order is not yet filled (for next onbarupdate)
    change ATM order stop price to high of previous bar

    if reverse signal is met, then close ATM order (which has not been filled)
    }

    Could you help to tell me which APIs to call for those actions above?

    Thanks

    #2
    ssg10,

    What you want to do is possible. Please take a look at the SampleAtmStrategy for an idea of how to call an ATM strategy.

    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Josh View Post
      ssg10,

      What you want to do is possible. Please take a look at the SampleAtmStrategy for an idea of how to call an ATM strategy.

      http://www.ninjatrader-support.com/H...egyCreate.html
      Hi Josh,

      In terms of placing an ATM create buy order will the following work?
      {
      atmStrategyId = GetAtmStrategyUniqueId();
      orderId = GetAtmStrategyUniqueId();
      AtmStrategyCreate(Cbi.Action.Buy, OrderType.Limit, High[0] + Prevbarplusticks * TickSize, 0, TimeInForce.Day, orderId, "YM_2x_130110_Long", atmStrategyId);
      }

      When I have tried this on a test it doesnt buy at the high of the previous bar. I do have a priavte int statement that looks like:
      private int prevbarplusticks = 1; // Default setting for Prevbarplusticks

      Can you tell me how I should code to buy at previous bar high +1 please? I had it working perfectly without ATM create buy code but I need that for stop loss management.


      Thanks,

      Gavin.

      Comment


        #4
        Hello,

        I will have someone reply to you on Monday. Thank you for your patience.
        DenNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Ben View Post
          Hello,

          I will have someone reply to you on Monday. Thank you for your patience.
          No problem, thanks Ben.

          Comment


            #6
            MrTicks, not sure I follow your code - for the previous you would need to reference High[1] then + a tick for the entry.

            Comment


              #7
              Originally posted by NinjaTrader_Bertrand View Post
              MrTicks, not sure I follow your code - for the previous you would need to reference High[1] then + a tick for the entry.
              Hi Bertrand - I have just amended the code to use High[1] instead and the orders were still going in wrong. For example, a sell order that shoud have been entered at the low of a previous candle was entered and filled at the open of the previous candle. The same was happening for buy orders, instead of going in at the high of a previous candle they were going in at open.

              Could this be because I had a limit order instead of a stop order with "OrderType.Limit" ?

              AtmStrategyCreate(Cbi.Action.Buy, OrderType.Limit, High[1] + Prevbarplusticks * TickSize, 0, TimeInForce.Day, orderId, "YM_2x_130110_Long", atmStrategyId);

              I have just changed this to OrderType.Stop to see if that sorts it out. I have attached the strategy code below.

              protected override void Initialize()

              {
              CalculateOnBarClose = true;
              TraceOrders = true;
              }


              /// <summary>
              /// Called on each bar update event (incoming tick)
              /// </summary>
              protected override void OnBarUpdate()
              {
              /* HELP DOCUMENTATION REFERENCE: Please see the Help Guide section "Using ATM Strategies"
              Make sure this strategy does not execute against historical data */
              if (Historical)
              return;

              /* Submits an entry limit order at the current low price to initiate an ATM Strategy if both order id and strategy id are in a reset state
              **** YOU MUST HAVE AN ATM STRATEGY TEMPLATE NAMED 'AtmStrategyTemplate' CREATED IN NINJATRADER (SUPERDOM FOR EXAMPLE) FOR THIS TO WORK ****/
              if (orderId.Length == 0 && atmStrategyId.Length == 0
              && Blah<Blah)
              {
              atmStrategyId = GetAtmStrategyUniqueId();
              orderId = GetAtmStrategyUniqueId();
              AtmStrategyCreate(Cbi.Action.Buy, OrderType.Stop, High[1] + Prevbarplusticks * TickSize, 0, TimeInForce.Day, orderId, "YM_2x_130110_Long", atmStrategyId);
              }
              //else
              // AtmStrategyCancelEntryOrder(orderId);

              // Check for a pending entry order
              if (orderId.Length > 0)
              {
              string[] status = GetAtmStrategyEntryOrderStatus(orderId);

              // If the status call can't find the order specified, the return array length will be zero otherwise it will hold elements
              if (status.GetLength(0) > 0)
              {
              // Print out some information about the order to the output window
              Print("The entry order average fill price is: " + status[0]);
              Print("The entry order filled amount is: " + status[1]);
              Print("The entry order order state is: " + status[2]);

              // If the order state is terminal, reset the order id value
              if (status[2] == "Filled" || status[2] == "Cancelled" || status[2] == "Rejected")
              orderId = string.Empty;
              }
              } // If the strategy has terminated reset the strategy id
              else if (atmStrategyId.Length > 0 && GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat)
              atmStrategyId = string.Empty;



              if (orderId.Length == 0 && atmStrategyId.Length == 0
              && BlahBlah> BlahBlah
              )
              {
              atmStrategyId = GetAtmStrategyUniqueId();
              orderId = GetAtmStrategyUniqueId();
              AtmStrategyCreate(Cbi.Action.Sell, OrderType.Stop, Low[1] - Prevbarminusticks * TickSize, 0, TimeInForce.Day, orderId, "YM_2x_130110_Short", atmStrategyId);
              }
              // After 5 bars have passed since entry, exit the position
              //if (BarsSinceEntry() >= 5)
              //{
              // Submit our exit order
              // myExitOrder = ExitLong();
              //}
              //else
              // AtmStrategyCancelEntryOrder(orderId);


              // Check for a pending entry order
              if (orderId.Length > 0)
              {
              string[] status = GetAtmStrategyEntryOrderStatus(orderId);

              // If the status call can't find the order specified, the return array length will be zero otherwise it will hold elements
              if (status.GetLength(0) > 0)
              {
              // If the order state is terminal, reset the order id value
              if (status[2] == "Filled" || status[2] == "Cancelled" || status[2] == "Rejected")
              orderId = string.Empty;
              }
              } // If the strategy has terminated reset the strategy id after doing some profit/loss calculations.
              else if (atmStrategyId.Length > 0 && GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat)
              {
              /* Retrieve the profit or loss from the just-closed ATM strategy and round it to the nearest tick. This only updates after all targets or stops are filled (the position is flat).
              GetAtmStrategyRealizedProfitLoss can return values such as 99.9999999998 or 100.00000000012, which would be rounded to 100. */
              realizedProfitLoss = Instrument.MasterInstrument.Round2TickSize(GetAtmS trategyRealizedProfitLoss(atmStrategyId));

              // Sum the profit to the running total.
              totalPL = totalPL + realizedProfitLoss;

              // Reset the strategy id and unrealized profit/loss variables because the strategy is now terminated.
              atmStrategyId = string.Empty;
              unrealizedProfitLoss = 0;
              }

              // Make sure atmStrategyId contains a value or GetAtmStrategyUnrealizedProfitLoss() will throw an error.
              if (atmStrategyId.Length > 0)
              {
              unrealizedProfitLoss = Instrument.MasterInstrument.Round2TickSize(GetAtmS trategyUnrealizedProfitLoss(atmStrategyId));
              }

              // Concatenate all the information and then draw it onto the chart. '\r\n' basically just means new line.
              string textToDraw = "Unrealized P/L: " + unrealizedProfitLoss.ToString() + "\r\nRealized P/L: " + totalPL.ToString() + "\r\nLast Strategy's P/L: " + realizedProfitLoss.ToString();
              DrawTextFixed("P/L information", textToDraw, TextPosition.BottomRight);



              }

              Comment


                #8
                MrTicks, a regular buy limit order placed above the current market would fill instantly, as it's a marketable limit order then. You would need to place a StopLimit order at the previous bar's high here for this entry.

                Comment


                  #9
                  Originally posted by NinjaTrader_Bertrand View Post
                  MrTicks, a regular buy limit order placed above the current market would fill instantly, as it's a marketable limit order then. You would need to place a StopLimit order at the previous bar's high here for this entry.

                  http://www.ninjatrader-support.com/H...egyCreate.html
                  Thanks Bertrand, appreciate it.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                  0 responses
                  637 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
                  569 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