OnBarUpdate piece of CODE:
else if (Position.MarketPosition != MarketPosition.Flat)
{
switch (Position.MarketPosition)
{
case MarketPosition.Long:
break;
case MarketPosition.Short:
if(entryOrder != null)
Print($"{entryOrder.Name}");
if (entryOrder != null && entryOrder.Name == "ABC_Short")
{
Print($"{entryOrder.Name}");
try
{
double breakevenPrice = breakevenSwingShort.GetFibLevel(0.7);
if (Close[0] <= breakevenPrice)
{
if (stopOrder != null && stopOrder.StopPrice > Position.AveragePrice)
{
stopOrder = ExitShortStopMarket(0, true, stopOrder.Quantity, Position.AveragePrice, "Abc_StopShort", "ABC_Short");
}
}
} catch (Exception)
{
Print("Cos jest nie tak...");
}
}
}
Normally I use your code from custom example of handling orders.
protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError) {
List<String> orders = new List<string> { "LongEntry", "ShortEntry", "BOS_UP_ENTRY", "ABC_Long", "ABC_Short", "BoS1_Long" };
if (orders.Contains(order.Name))
{
entryOrder = order;
if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
{
entryOrder = null;
sumFilled = 0;
}
}
}
protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
{
if (entryOrder != null && entryOrder == execution.Order)
{
if (execution.Order.Name == "ABC_Long" || execution.Order.Name == "ABC_Short" || execution.Order.Name == "BoS1_Long")
{
entryOrder = execution.Order;
Print($"✅ entryOrder ustawione: {entryOrder.Name}, cena: {entryOrder.AverageFillPrice}, ilość: {entryOrder.Quantity}");
}
if (execution.Order.OrderState == OrderState.Filled
|| execution.Order.OrderState == OrderState.PartFilled
|| (execution.Order.OrderState == OrderState.Cancelled
&& execution.Order.Filled > 0))
{
sumFilled += execution.Quantity;
if (execution.Order.OrderState == OrderState.PartFilled)
{
if ((execution.Order.IsLong))
{
// ABC Settings part 1
stopOrder = ExitLongStopMarket(0, true, execution.Order.Filled, StopLossPrice, "ABC_StopLong", "ABC_Long");
targetOrder = ExitLongLimit(0, true, execution.Order.Filled, TakeProfitPrice, "ABC_ProfitLong", "ABC_Long");
// BoS1_Long
stopOrder = ExitLongStopMarket(0, true, execution.Order.Filled, s_bos1_loss, "BoS1_StopLong", "BoS1_Long");
targetOrder = ExitLongLimit(0, true, execution.Order.Filled, s_bos1_profit, "BoS1_ProfitLong", "BoS1_Long");
}
else if ((execution.Order.IsShort))
{
stopOrder = ExitShortStopMarket(0, true, execution.Order.Filled, StopLossPrice, "Abc_StopShort", "ABC_Short");
targetOrder = ExitShortLimit(0, true, execution.Order.Filled, TakeProfitPrice, "Abc_ProfitShort", "ABC_Short");
}
}
else if (execution.Order.OrderState == OrderState.Filled && sumFilled == execution.Order.Filled)
{
if ((execution.Order.IsLong))
{
// ABC Settings part 1
stopOrder = ExitLongStopMarket(0, true, execution.Order.Filled, StopLossPrice, "ABC_StopLong", "ABC_Long");
targetOrder = ExitLongLimit(0, true, execution.Order.Filled, TakeProfitPrice, "ABC_ProfitLong", "ABC_Long");
// BOS 1 Settings
// BoS1_Long
stopOrder = ExitLongStopMarket(0, true, execution.Order.Filled, s_bos1_loss, "BoS1_StopLong", "BoS1_Long");
targetOrder = ExitLongLimit(0, true, execution.Order.Filled, s_bos1_profit, "BoS1_ProfitLong", "BoS1_Long");
}
else if ((execution.Order.IsShort))
{
stopOrder = ExitShortStopMarket(0, true, execution.Order.Filled, StopLossPrice, "Abc_StopShort", "ABC_Short");
targetOrder = ExitShortLimit(0, true, execution.Order.Filled, TakeProfitPrice, "Abc_ProfitShort", "ABC_Short");
}
}
if (execution.Order.OrderState != OrderState.PartFilled && sumFilled == execution.Order.Filled)
{
entryOrder = null;
sumFilled = 0;
}
}
}
if ((stopOrder != null && stopOrder == execution.Order) || (targetOrder != null && targetOrder == execution.Order))
{
if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
{
stopOrder = null;
targetOrder = null;
lastTradeTime = time;
Print($"Trade zakończony (Stop/Tp) o {time}. Wprowadzono cooldown 30 minut.");
}
}
}
Take Profits and stop losses orders work well ! The thing I want to achieve is breakeven on my conditions for a specific signal i onBarUpdate. The issue is the Order entryOrder stays null and I can't handle a name.

Comment