I have tried it with ExitShortStopMarket and with SetStopLoss.
What am I doing wrong?
protected override void OnBarUpdate()
{
if (PositionAccount != null && PositionAccount.MarketPosition != MarketPosition.Flat) // Es gibt eine offene Position
{
bool hasStopLoss = false;
if (Orders != null && Orders.Count > 0) // Überprüfen, ob Orders nicht null und nicht leer ist
{
foreach (Order order in Orders)
{
if (order.OrderType == OrderType.StopMarket || order.OrderType == OrderType.StopLimit)
{
hasStopLoss = true;
break;
}
}
}
if (!hasStopLoss) // Falls kein Stop-Loss existiert, einen setzen
{
double stopPrice;
if (PositionAccount.MarketPosition == MarketPosition.Long) // Für Long-Position
{
stopPrice = PositionAccount.AveragePrice - (stopLossTicks * TickSize);
}
else // Für Short-Position
{
stopPrice = PositionAccount.AveragePrice + (stopLossTicks * TickSize);
}
// Stop-Loss-Order setzen und überprüfen
Order longStopOrder = ExitLongStopMarket(stopPrice);
Order shortStopOrder = ExitShortStopMarket(stopPrice);
// Überprüfung, ob die Stop-Orders erfolgreich gesetzt wurden
if (longStopOrder != null)
{
Print("Long Stop-Loss-Order Status: " + longStopOrder.OrderState);
if (longStopOrder.OrderState == OrderState.Working)
{
Print("Long Stop-Loss-Order erfolgreich gesetzt.");
}
}
else
{
Print("Fehler beim Setzen der Long Stop-Loss-Order. Status: " + longStopOrder?.OrderState);
}
if (shortStopOrder != null)
{
Print("Short Stop-Loss-Order Status: " + shortStopOrder.OrderState);
if (shortStopOrder.OrderState == OrderState.Working)
{
Print("Short Stop-Loss-Order erfolgreich gesetzt.");
}
}
else
{
Print("Fehler beim Setzen der Short Stop-Loss-Order. Status: " + shortStopOrder?.OrderState);
}
}

Comment