When the order is not cancelled, it is possible to have a long limit order and a short limit order at the same time, thus making for some funky ATM orders.
How do I cancel the ATM order at the close of the bar to prevent multiple limit orders going out at the same time? I have looked at all of the ATM sample strategies and I can not seem to get this to work correctly in real time.
Thanks
Code is below:
#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Indicator;
using NinjaTrader.Strategy;
#endregion
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
/// <summary>
///
/// </summary>
[Description("")]
public class ATMTEST1: Strategy
{
#region Variables
private string atmStrategyId = string.Empty;
private string orderId = string.Empty;
private string atmStrategyName = "atmtest";
private int slipTicks = 1; // Default setting for SlipTicks
#endregion
/// <summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
/// </summary>
protected override void Initialize()
{
MaximumBarsLookBack = MaximumBarsLookBack.Infinite;
CalculateOnBarClose = true;
BarsRequired = 10;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
if (Historical)
return;
// Condition set LONG
if (//orderId.Length == 0 && atmStrategyId.Length == 0
GetAtmStrategyMarketPosition(atmStrategyId) == MarketPosition.Flat
&& Low[0] < Low[1]
)
{
atmStrategyId = GetAtmStrategyUniqueId();
orderId = GetAtmStrategyUniqueId();
AtmStrategyCreate(Cbi.OrderAction.Buy, OrderType.Limit, (Close[0] + SlipTicks * TickSize), 0, TimeInForce.Day, orderId, AtmStrategyName, atmStrategyId);
DrawText("Long1"+CurrentBar, "LL " + (Close[0] + SlipTicks * TickSize).ToString(), 0, Low[0] - 8*TickSize, Color.Cyan);
DrawArrowUp(CurrentBar.ToString(),0,Low[0] - 1*TickSize, Color.Cyan);
}
// Condition set SELL
if (//orderId.Length == 0 && atmStrategyId.Length == 0
GetAtmStrategyMarketPosition(atmStrategyId) == MarketPosition.Flat
&& High[0] > High[1]
)
{
atmStrategyId = GetAtmStrategyUniqueId();
orderId = GetAtmStrategyUniqueId();
AtmStrategyCreate(Cbi.OrderAction.Sell, OrderType.Limit, (Close[0] - SlipTicks * TickSize), 0, TimeInForce.Day, orderId, AtmStrategyName, atmStrategyId);
DrawText("Short1"+CurrentBar,"SL " + (Close[0] - SlipTicks * TickSize).ToString(), 0, High[0] + 8*TickSize, Color.Cyan);
DrawArrowDown(CurrentBar.ToString(),0,High[0] + 1*TickSize, Color.Cyan);
}
// 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;
}
}
else if (atmStrategyId.Length > 0 && GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat)
atmStrategyId = string.Empty;
}
#region Properties
[Description("Slippage Ticks for Entry")]
[GridCategory("Parameters")]
public int SlipTicks
{
get { return slipTicks; }
set { slipTicks = Math.Max(1, value); }
}
[Description("Name for the ATM Strategy")]
[GridCategory("Parameters")]
public string AtmStrategyName
{
get { return atmStrategyName; }
set { atmStrategyName = value; }
}
#endregion
}
}

Comment