Using some original code from ProfitSniper which I have customised.
I am trying to get this code to work. It works fine with 2 lots or more but gives errors on single lots.
Anyone spot my error?
Edit - mmm... so it appears to be the very first order which will not do single lots. If I do 2 lots, close and then do 1 lot it works
#region Initial Target & StopLoss Snippets.
private void Account_OrderUpdate(object sender, OrderEventArgs orderUpdateArgs)
{
/*
Print("2A. Account_OrderUpdate() with KEYDOWN " + Time[0]);
Print("entryBuyMar****rder != null : " + (entryBuyMar****rder != null));
Print("entryBuyMar****rder == orderUpdateArgs.Order : " + (entryBuyMar****rder == orderUpdateArgs.Order));
Print("orderUpdateArgs.Order.OrderState == OrderState.Filled : " + (orderUpdateArgs.Order.OrderState == OrderState.Filled));
Print("orderUpdateArgs.Order.OrderState == OrderState.Working : " + (orderUpdateArgs.Order.OrderState == OrderState.Working));
Print(" ");
Print("accountPosition == null : " + (accountPosition == null));
Print("profitTargetOrder == nul : " + (profitTargetOrder == null));
Print("stopLossOrder == nul : " + (stopLossOrder == null));
Print(" ");
*/
// LONG Order Target and StopLoss OCO orders
if (entryBuyMar****rder != null && entryBuyMar****rder == orderUpdateArgs.Order && orderUpdateArgs.Order.OrderState == OrderState.Filled)
{
string oco = Guid.NewGuid().ToString("N");
string oco1 = Guid.NewGuid().ToString("N");
submitOrdersArray = new List<Order>();
liveq = orderUpdateArgs.Quantity;
currentPtPrice = orderUpdateArgs.AverageFillPrice + ProfitTargetDistance * TickSize;
currentPtPrice1 = orderUpdateArgs.AverageFillPrice + (ProfitTargetDistance * TickSize*2);
currentSlPrice = orderUpdateArgs.AverageFillPrice - StopLossDistance * TickSize;
currentSlPrice1 = orderUpdateArgs.AverageFillPrice - (StopLossDistance * TickSize)+TickSize;
if(liveq >=2)
{
if(liveq % 2 ==1)
Q0 = (int)(liveq * 0.5 + 0.5);
else
Q0 = (int)(liveq* 0.5);
Q1 = liveq - (int)Q0;
}
if (PrintDetails)
Print(string.Format("{0} | Account_OrderUpdate | placing profit target | currentPtPrice: {1}", orderUpdateArgs.Time, currentPtPrice));
if(liveq >=2)
{
profitTargetOrder1 = myAccount.CreateOrder(orderUpdateArgs.Order.Instrument, OrderAction.Sell, OrderType.Limit, OrderEntry.Automated, TimeInForce.Day, Q1, currentPtPrice1, 0, oco1, "Profit Target", Core.Globals.MaxDate, null);
submitOrdersArray.Add(profitTargetOrder1);
profitTargetOrder = myAccount.CreateOrder(orderUpdateArgs.Order.Instrument, OrderAction.Sell, OrderType.Limit, OrderEntry.Automated, TimeInForce.Day, Q0, currentPtPrice, 0, oco, "Profit Target 1", Core.Globals.MaxDate, null);
submitOrdersArray.Add(profitTargetOrder);
stopLossOrder = myAccount.CreateOrder(orderUpdateArgs.Order.Instrument, OrderAction.Sell, OrderType.StopMarket, OrderEntry.Automated, TimeInForce.Day, Q0, 0, currentSlPrice, oco, "Stop Loss", Core.Globals.MaxDate, null);
submitOrdersArray.Add(stopLossOrder);
stopLossOrder1 = myAccount.CreateOrder(orderUpdateArgs.Order.Instrument, OrderAction.Sell, OrderType.StopMarket, OrderEntry.Automated, TimeInForce.Day, Q1, 0, currentSlPrice1, oco1, "Stop Loss", Core.Globals.MaxDate, null);
submitOrdersArray.Add(stopLossOrder1);
}
else
{
profitTargetOrder = myAccount.CreateOrder(orderUpdateArgs.Order.Instrument, OrderAction.Sell, OrderType.Limit, OrderEntry.Automated, TimeInForce.Day, Q0, currentPtPrice, 0, oco, "Profit Target 1", Core.Globals.MaxDate, null);
submitOrdersArray.Add(profitTargetOrder);
stopLossOrder = myAccount.CreateOrder(orderUpdateArgs.Order.Instrument, OrderAction.Sell, OrderType.StopMarket, OrderEntry.Automated, TimeInForce.Day, Q0, 0, currentSlPrice, oco, "Stop Loss", Core.Globals.MaxDate, null);
submitOrdersArray.Add(stopLossOrder);
}
if (PrintDetails)
Print(string.Format("{0} | Account_OrderUpdate | placing stop loss | currentSlPrice: {1}", orderUpdateArgs.Time, currentSlPrice));
myAccount.Submit(submitOrdersArray);
}// end buyorder
// SHORT Order Target and StopLoss OCO orders
if (entrySellMar****rder != null && entrySellMar****rder == orderUpdateArgs.Order && orderUpdateArgs.Order.OrderState == OrderState.Filled)
{
string oco = Guid.NewGuid().ToString("N");
string oco1 = Guid.NewGuid().ToString("N");
submitOrdersArray = new List<Order>();
liveq = orderUpdateArgs.Quantity;
currentPtPrice = orderUpdateArgs.AverageFillPrice - ProfitTargetDistance * TickSize;
currentPtPrice1 = orderUpdateArgs.AverageFillPrice - (ProfitTargetDistance * TickSize*2);
currentSlPrice = orderUpdateArgs.AverageFillPrice + StopLossDistance * TickSize;
currentSlPrice1 = orderUpdateArgs.AverageFillPrice + (StopLossDistance * TickSize)+TickSize;
if(liveq >=2)
{
if(liveq % 2 ==1)
Q0 = (int)(liveq * 0.5 + 0.5);
else
Q0 = (int)(liveq* 0.5);
Q1 = liveq - (int)Q0;
}
if (PrintDetails)
Print(string.Format("{0} | Account_OrderUpdate | placing profit target | currentPtPrice: {1}", orderUpdateArgs.Time, currentPtPrice));
if(liveq >=2)
{
profitTargetOrder = myAccount.CreateOrder(orderUpdateArgs.Order.Instrument, OrderAction.Buy, OrderType.Limit, OrderEntry.Automated, TimeInForce.Day, Q0, currentPtPrice, 0, oco, "Profit Target", Core.Globals.MaxDate, null);
submitOrdersArray.Add(profitTargetOrder);
profitTargetOrder1 = myAccount.CreateOrder(orderUpdateArgs.Order.Instrument, OrderAction.Buy, OrderType.Limit, OrderEntry.Automated, TimeInForce.Day, Q1, currentPtPrice1, 0, oco1, "Profit Target", Core.Globals.MaxDate, null);
submitOrdersArray.Add(profitTargetOrder1);
stopLossOrder = myAccount.CreateOrder(orderUpdateArgs.Order.Instrument, OrderAction.Buy, OrderType.StopMarket, OrderEntry.Automated, TimeInForce.Day, Q0, 0, currentSlPrice, oco, "Stop Loss", Core.Globals.MaxDate, null);
submitOrdersArray.Add(stopLossOrder);
stopLossOrder1 = myAccount.CreateOrder(orderUpdateArgs.Order.Instrument, OrderAction.Buy, OrderType.StopMarket, OrderEntry.Automated, TimeInForce.Day, Q1, 0, currentSlPrice1, oco1, "Stop Loss", Core.Globals.MaxDate, null);
submitOrdersArray.Add(stopLossOrder1);
}
else
{
profitTargetOrder = myAccount.CreateOrder(orderUpdateArgs.Order.Instrument, OrderAction.Buy, OrderType.Limit, OrderEntry.Automated, TimeInForce.Day, Q0, currentPtPrice, 0, oco, "Profit Target", Core.Globals.MaxDate, null);
submitOrdersArray.Add(profitTargetOrder);
stopLossOrder = myAccount.CreateOrder(orderUpdateArgs.Order.Instrument, OrderAction.Buy, OrderType.StopMarket, OrderEntry.Automated, TimeInForce.Day, Q0, 0, currentSlPrice, oco, "Stop Loss", Core.Globals.MaxDate, null);
submitOrdersArray.Add(stopLossOrder);
}
if (PrintDetails)
Print(string.Format("{0} | Account_OrderUpdate | placing stop loss | currentSlPrice: {1}", orderUpdateArgs.Time, currentSlPrice));
myAccount.Submit(submitOrdersArray);
}
// once the exit orders are closed, reset for a new entry.
else if ((profitTargetOrder != null && (profitTargetOrder.OrderState == OrderState.Filled
|| profitTargetOrder.OrderState == OrderState.Rejected
|| profitTargetOrder.OrderState == OrderState.Cancelled))
||(profitTargetOrder1 != null && (profitTargetOrder1.OrderState == OrderState.Filled
|| profitTargetOrder1.OrderState == OrderState.Rejected
|| profitTargetOrder1.OrderState == OrderState.Cancelled))
|| (stopLossOrder != null && (stopLossOrder.OrderState == OrderState.Filled
|| stopLossOrder.OrderState == OrderState.Rejected
|| stopLossOrder.OrderState == OrderState.Cancelled))
|| (stopLossOrder1 != null && (stopLossOrder1.OrderState == OrderState.Filled
|| stopLossOrder1.OrderState == OrderState.Rejected
|| stopLossOrder1.OrderState == OrderState.Cancelled)))
{
entryBuyMar****rder = null;
entrySellMar****rder = null;
profitTargetOrder =profitTargetOrder1= null;
stopLossOrder=stopLossOrder1= null;
#region DIRECTION LOCK (LONG StopLoss Order Rejection Prevention Move Lock)
currentSlPriceUpOnly = 0;
currentSlPriceDownOnly = 999999;
#endregion
}
}
#endregion

Comment