the following scenario in unmanaged mode:
--------------------------------------------------------------------------------------------------------------------------------------------------------------
protected override void OnBarUpdate()
.......
// BarByBar Trail
......
if (SLL1 != null && (SLL1.OrderState == OrderState.Accepted || SLL1.OrderState == OrderState.Working))
{
ChangeOrder(SLL1, sumexecution, 0, newPrice); // Readjust stoploss level
Print(Time[0] + " *** Stoploss SLL1 move to " + newPrice + " *** by BarByBar Trail" + " *** Qty: " + Position.Quantity + " *** SLL1 Qty: " + SLL1.Quantity + " *** ocoID: " + ocoExit);
}
// Entry Long Limit
.......
if (LE1 == null && IsFirstTickOfBar .......)
{
SubmitOrderUnmanaged(1, OrderAction.Buy, OrderType.Limit, Positionsgrösse1, GetCurrentBid(), 0, ocoEntry, "LongEntry1");
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------
protected override void OnOrderUpdate(...)
// Long Entry
if (order.Name == "LongEntry1")
{
LE1 = order;
if (LE1 != null && LE1 == order)
{
// Reset the long entry object to null if order was cancelled without any fill
if (order.OrderState == OrderState.Cancelled)// && order.Filled == 0)
{
LE1 = null;
sumFilledLE1 = 0;
}
}
}
// Long Stop
else if (order.Name == "StopLossLong1")
{
SLL1 = order;
if (SLL1 != null && SLL1 == order)
{
// Reset the long stop loss object to null if order was cancelled without any fill
if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
{
SLL1 = null;
}
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
protected override void OnExecutionUpdate(....)
// Long SL and TP Position 1
if (LE1 != null && LE1 == execution.Order)
{
if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
{
if (State == State.Historical)
ocoExit = DateTime.Now.ToString() + CurrentBar + " LongExits ";
else
ocoExit = GetAtmStrategyUniqueId() + " LongExits ";
// We sum the quantities of each execution making up the entry order
sumFilledLE1 += execution.Quantity;
// Submit exit orders for partial fills
if (UseTickStop && execution.Order.OrderState == OrderState.PartFilled)
{
SubmitOrderUnmanaged(1, OrderAction.Sell, OrderType.StopMarket, execution.Quantity, 0, execution.Order.AverageFillPrice - InitialStopTick * TickSize, ocoExit, "StopLossLong1");
}
// Update our exit order quantities once orderstate turns to filled and we have seen execution quantities match order quantities
else if (UseTickStop && execution.Order.OrderState == OrderState.Filled && sumFilledLE1 == execution.Order.Filled)
{
SubmitOrderUnmanaged(1, OrderAction.Sell, OrderType.StopMarket, execution.Quantity, 0, execution.Order.AverageFillPrice - InitialStopTick * TickSize, ocoExit, "StopLossLong1");
Print(Time[0] + " *** Set Stoploss SLL1 fullfilled Order @ " + (execution.Order.AverageFillPrice - InitialStopTick * TickSize) + " *** Qty: " + execution.Quantity + " *** " + execution.Order.Filled + " / " + execution.Order.Quantity + " *** ocoID: " + ocoExit);
}
if (execution.Order.OrderState == OrderState.Filled)
{
sumexecution = execution.Order.Filled;
}
// Resets the long entry object to null after the order has been filled
if (execution.Order.OrderState != OrderState.PartFilled && sumFilledLE1 == execution.Order.Quantity)
{
LE1 = null;
sumFilledLE1 = 0;
}
//Reset our stop order and target orders' Order objects after our position is closed.
//Long SL
if (SLL1 != null && SLL1 == execution.Order)
{
if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
{
SLL1 = null;
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
as far as i can see, i am given one order for the partially executed quantities and another order for the last fully executed position. my problem now is that when i use the ChangeOrder() method for the barbabar trailingstop, the last filled position is taken as the basis for this ChangeOrder() and the last remainder of the partially filled order remains as stoploss. since the ChangeOrder() trades a whole position, the partial position is too much and should be deleted. how exactly can i implement this? on the pictures you can see very well what exactly i mean. sorry for my english ;-)
the example for the partial executions i got from the SampleOnOrderUpdate file from here. I have also looked at the other examples:
UnmanagedOCOBracketExample, SampleUnmanagedBracketStrategy, ProfitChaseStopTrailUnmanagedExample ...., but found nothing that could help me. maybe there is someone here who can - i hope so.... it's certainly a little tip for someone who can a just a little program, not like me ;-)
Comment