I have a code as follows.
[FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]protected[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]override[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]void[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif] OnBarUpdate()[/FONT] [FONT=Microsoft Sans Serif]{[/FONT] [FONT=Microsoft Sans Serif][COLOR=#008000][FONT=Microsoft Sans Serif][COLOR=#008000]// Make sure this strategy does not execute against historical data[/COLOR][/FONT] [/COLOR][/FONT][FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]if[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif] (Historical)[/FONT] [FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]return[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif];[/FONT] [FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]if[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif] (orderId.Length == [/FONT][FONT=Microsoft Sans Serif][COLOR=#800080][FONT=Microsoft Sans Serif][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT] [FONT=Microsoft Sans Serif]&& atmStrategyId.Length == [/FONT][FONT=Microsoft Sans Serif][COLOR=#800080][FONT=Microsoft Sans Serif][COLOR=#800080]0[/COLOR][/FONT] [/COLOR][/FONT][FONT=Microsoft Sans Serif]&& FirstTickOfBar[/FONT] [FONT=Microsoft Sans Serif]&& CrossAbove(MAMA cross[/FONT][FONT=Microsoft Sans Serif])[/FONT] [FONT=Microsoft Sans Serif])[/FONT] [FONT=Microsoft Sans Serif]{[/FONT] [FONT=Microsoft Sans Serif]atmStrategyId = GetAtmStrategyUniqueId();[/FONT] [FONT=Microsoft Sans Serif]orderId = GetAtmStrategyUniqueId();[/FONT] [FONT=Microsoft Sans Serif]AtmStrategyCreate(Cbi.OrderAction.Buy, OrderType.Market, [/FONT][FONT=Microsoft Sans Serif][COLOR=#800080][FONT=Microsoft Sans Serif][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif], [/FONT][FONT=Microsoft Sans Serif][COLOR=#800080][FONT=Microsoft Sans Serif][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif], TimeInForce.Day, orderId, [/FONT][FONT=Microsoft Sans Serif][COLOR=#800000][FONT=Microsoft Sans Serif][COLOR=#800000]"AtmStrategyTemplate"[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif], atmStrategyId);[/FONT] [FONT=Microsoft Sans Serif]}[/FONT] [FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]else[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]if[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif] (orderId.Length == [/FONT][FONT=Microsoft Sans Serif][COLOR=#800080][FONT=Microsoft Sans Serif][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT] [FONT=Microsoft Sans Serif]&& atmStrategyId.Length == [/FONT][FONT=Microsoft Sans Serif][COLOR=#800080][FONT=Microsoft Sans Serif][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT] [FONT=Microsoft Sans Serif]&& FirstTickOfBar[/FONT] [FONT=Microsoft Sans Serif]&& CrossBelow(MAMA cross[/FONT][FONT=Microsoft Sans Serif]) )[/FONT] [FONT=Microsoft Sans Serif]{[/FONT] [FONT=Microsoft Sans Serif]atmStrategyId = GetAtmStrategyUniqueId();[/FONT] [FONT=Microsoft Sans Serif]orderId = GetAtmStrategyUniqueId();[/FONT] [FONT=Microsoft Sans Serif]AtmStrategyCreate(Cbi.OrderAction.Sell, OrderType.Market, [/FONT][FONT=Microsoft Sans Serif][COLOR=#800080][FONT=Microsoft Sans Serif][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif], [/FONT][FONT=Microsoft Sans Serif][COLOR=#800080][FONT=Microsoft Sans Serif][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif], TimeInForce.Day, orderId, [/FONT][FONT=Microsoft Sans Serif][COLOR=#800000][FONT=Microsoft Sans Serif][COLOR=#800000]"AtmStrategyTemplate"[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif], atmStrategyId);[/FONT] [FONT=Microsoft Sans Serif]}[/FONT] [FONT=Microsoft Sans Serif][COLOR=#008000][FONT=Microsoft Sans Serif][COLOR=#008000]// Check for a pending entry order[/COLOR][/FONT] [/COLOR][/FONT][FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]if[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif] (orderId.Length > [/FONT][FONT=Microsoft Sans Serif][COLOR=#800080][FONT=Microsoft Sans Serif][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif])[/FONT] [FONT=Microsoft Sans Serif]{[/FONT] [FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]string[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif][] status = GetAtmStrategyEntryOrderStatus(orderId);[/FONT] [FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]if[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif] (status.GetLength([/FONT][FONT=Microsoft Sans Serif][COLOR=#800080][FONT=Microsoft Sans Serif][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif]) > [/FONT][FONT=Microsoft Sans Serif][COLOR=#800080][FONT=Microsoft Sans Serif][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif]) AtmStrategyCancelEntryOrder(atmStrategyId);[/FONT] [FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]if[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif] (status[[/FONT][FONT=Microsoft Sans Serif][COLOR=#800080][FONT=Microsoft Sans Serif][COLOR=#800080]2[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif]] == [/FONT][FONT=Microsoft Sans Serif][COLOR=#800000][FONT=Microsoft Sans Serif][COLOR=#800000]"Filled"[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif] || status[[/FONT][FONT=Microsoft Sans Serif][COLOR=#800080][FONT=Microsoft Sans Serif][COLOR=#800080]2[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif]] == [/FONT][FONT=Microsoft Sans Serif][COLOR=#800000][FONT=Microsoft Sans Serif][COLOR=#800000]"Cancelled"[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif] || status[[/FONT][FONT=Microsoft Sans Serif][COLOR=#800080][FONT=Microsoft Sans Serif][COLOR=#800080]2[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif]] == [/FONT][FONT=Microsoft Sans Serif][COLOR=#800000][FONT=Microsoft Sans Serif][COLOR=#800000]"Rejected"[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif])[/FONT] [FONT=Microsoft Sans Serif]{[/FONT] [FONT=Microsoft Sans Serif]orderId = [/FONT][FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]string[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif].Empty;[/FONT] [FONT=Microsoft Sans Serif]}[/FONT] [FONT=Microsoft Sans Serif]}[/FONT] [FONT=Microsoft Sans Serif][COLOR=#008000][FONT=Microsoft Sans Serif][COLOR=#008000]// If the strategy has terminated reset the strategy id[/COLOR][/FONT] [/COLOR][/FONT][FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]else[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]if[/COLOR][/FONT][/COLOR][/FONT] [FONT=Microsoft Sans Serif](atmStrategyId.Length > [/FONT][FONT=Microsoft Sans Serif][COLOR=#800080][FONT=Microsoft Sans Serif][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif] &&[/FONT] [FONT=Microsoft Sans Serif]GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat[/FONT] [FONT=Microsoft Sans Serif])[/FONT] [FONT=Microsoft Sans Serif]{[/FONT][FONT=Microsoft Sans Serif]atmStrategyId = [/FONT][FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]string[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif].Empty;[/FONT][FONT=Microsoft Sans Serif]}[/FONT] [FONT=Microsoft Sans Serif]}[/FONT] [FONT=Microsoft Sans Serif][COLOR=#008000][FONT=Microsoft Sans Serif][COLOR=#008000]// Close ATM strategy and pending orders[/COLOR][/FONT] [/COLOR][/FONT] [FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]if[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif] ( atmStrategyId.Length > [/FONT][FONT=Microsoft Sans Serif][COLOR=#800080][FONT=Microsoft Sans Serif][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif] &&[/FONT] [FONT=Microsoft Sans Serif]GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Long &&[/FONT] [FONT=Microsoft Sans Serif]CrossBelow(MAMA cross[/FONT][FONT=Microsoft Sans Serif])[/FONT] [FONT=Microsoft Sans Serif]{[/FONT] [FONT=Microsoft Sans Serif]AtmStrategyClose(atmStrategyId);[/FONT] [FONT=Microsoft Sans Serif]atmStrategyId = [/FONT][FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]string[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif].Empty;[/FONT] [FONT=Microsoft Sans Serif]}[/FONT] [FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]if[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif] ( atmStrategyId.Length > [/FONT][FONT=Microsoft Sans Serif][COLOR=#800080][FONT=Microsoft Sans Serif][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif] &&[/FONT] [FONT=Microsoft Sans Serif]GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Short&&[/FONT] [FONT=Microsoft Sans Serif]CrossAbove(MAMA cross)[/FONT] [FONT=Microsoft Sans Serif]{[/FONT] [FONT=Microsoft Sans Serif]AtmStrategyClose(atmStrategyId);[/FONT] [FONT=Microsoft Sans Serif]atmStrategyId = [/FONT][FONT=Microsoft Sans Serif][COLOR=#0000ff][FONT=Microsoft Sans Serif][COLOR=#0000ff]string[/COLOR][/FONT][/COLOR][/FONT][FONT=Microsoft Sans Serif].Empty;[/FONT] [FONT=Microsoft Sans Serif]}[/FONT] [FONT=Microsoft Sans Serif]}[/FONT]
Comment