I am developing a strategy including ATM strategy. The main strategy calls for ATM staretgy and opens the orders as desired, however it does not close previous orders when it gets new signal from the main script.I have included code for reset of the ATM strategy.
Can anybody add the code for close of running ATM strategy when a new ATM order is opened by main strategy? I have gone through previous posts but not able to figure out how to close the running ATM strategy on a new opposite signal.
Here is the code.
{ #region Variables //main strategy variables here //ATM variables privatestring atmStrategyId = string.Empty; privatestring orderId = string.Empty; #endregion ///<summary> /// This method is used to configure the strategy and is called once before any strategy method is called. ///</summary> protectedoverridevoid Initialize() { CalculateOnBarClose = true; TraceOrders = true; EntryHandling = EntryHandling.AllEntries; TimeInForce = Cbi.TimeInForce.Day; Add (main strategy indicators) } ///<summary> /// Called on each bar update event (incoming tick) ///</summary> protectedoverridevoid 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 && main strategy conditions for buy { atmStrategyId = GetAtmStrategyUniqueId(); orderId = GetAtmStrategyUniqueId(); AtmStrategyCreate(Cbi.OrderAction.Buy, OrderType.Market, 0, 0, TimeInForce.Day, orderId, "AtmStrategyTemplate", atmStrategyId); } if (orderId.Length == 0 && atmStrategyId.Length == 0 && main strategy conditions for sell { atmStrategyId = GetAtmStrategyUniqueId(); orderId = GetAtmStrategyUniqueId(); AtmStrategyCreate(Cbi.OrderAction.Sell, OrderType.Market, 0, 0, TimeInForce.Day, orderId, "AtmStrategyTemplate", atmStrategyId); } // 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 if (atmStrategyId.Length > 0 && GetAtmStrategyMarketPosition(atmStrategyId) == MarketPosition.Flat) { atmStrategyId = string.Empty; orderId = string.Empty; } }
Comment