Не получается заставить Эксперт ждать следующей свечи после закрытия сделки.

 

Я перепробовал уже массу вариантов, которые только приходили в голову и которые находил в сети, но решение так и не получилось. Текущий вариант кода последняя моя попытка заставить Эксперт прекратить оценку условий на вход в позицию до следующей свечи через флаг position_closed. Все равно получается вот такой лог при бэктесте:

 

2024.09.13 04:10:14   stop loss triggered #6 buy 0.01 XAUUSD 2565.250 sl: 2567.660 tp: 2575.250 [#7 sell 0.01 XAUUSD at 2567.660]

2024.09.13 04:10:14   deal #7 sell 0.01 XAUUSD at 2567.480 done (based on order #7)

2024.09.13 04:10:14   deal performed [#7 sell 0.01 XAUUSD at 2567.480]

2024.09.13 04:10:14   order performed sell 0.01 at 2567.480 [#7 sell 0.01 XAUUSD at 2567.660]

2024.09.13 04:10:14   Checking Sell conditions...

2024.09.13 04:10:14   Sell condition failed: MACD Fast EMA is not below Slow EMA

2024.09.13 04:10:14   market buy 0.01 XAUUSD sl: 2558.590 tp: 2577.590 (2567.480 / 2567.590)

2024.09.13 04:10:14   deal #8 buy 0.01 XAUUSD at 2567.590 done (based on order #8)

2024.09.13 04:10:14   deal performed [#8 buy 0.01 XAUUSD at 2567.590]

2024.09.13 04:10:14   order performed buy 0.01 at 2567.590 [#8 buy 0.01 XAUUSD at 2567.590]

2024.09.13 04:10:14   position modified [#8 buy 0.01 XAUUSD 2567.590 sl: 2558.600 tp: 2577.590]

2024.09.13 04:10:14   CTrade::OrderSend: modify position #8 XAUUSD (sl: 2558.600, tp: 2577.590) [done]

 

То есть за одну секунду он закрыл, проверил и открыл. Даже микро паузы не делает, не говоря уже об ожидании следующей свечи. Подскажите, пожалуйста, какое есть решение для этого?

//--- OnTick function: Main logic to check conditions and manage trades
void OnTick() {
    // Get current prices using MqlTick
    SymbolInfoTick(_Symbol, last_tick);

    // Get the values of indicators
    CopyBuffer(macd_handle, 0, 0, 1, macd_value);
    CopyBuffer(macd_handle, 1, 0, 1, macd_signal);
    CopyBuffer(rsi_handle, 0, 0, 1, rsi_value);
    CopyBuffer(ma_handle, 0, 0, 1, ma_value);  // Use CopyBuffer to get MA values
    
    // Ensure we have valid data for indicators
    if (macd_value[0] == EMPTY_VALUE || rsi_value[0] == EMPTY_VALUE || ma_value[0] == EMPTY_VALUE)
        return;

    // Get the time of the current candle
    datetime current_candle_time = iTime(_Symbol, _Period, 0);

    // Reset the flag on a new candle
    if (current_candle_time != last_candle_open_time) {
        position_closed = false;  // Allow evaluations on new candle
        last_candle_open_time = current_candle_time;
    }

    // Skip evaluations if a position was closed on this candle
    if (position_closed) {
        Print("Skipping condition evaluation since position was closed on this candle.");
        return;
    }

    // Evaluate conditions if no position is open
    if (PositionsTotal() == 0) {
        // Check for Sell conditions
        if (CheckSellConditions(last_tick)) {
            OpenSellOrder(last_tick.bid);
        }
    
        // Check for Buy conditions
        if (CheckBuyConditions(last_tick)) {
            OpenBuyOrder(last_tick.ask);
        }
    } else {
        // Manage the open position (Breakeven, Trail Stop, Stop Loss)
        ManageOpenPosition();
    }
}
 
Вопрос снят. Ответ найден в этой ветке
Открытие не более одной сделки на свече
Открытие не более одной сделки на свече
  • 2012.02.01
  • cos17
  • www.mql5.com
Здравствуйте. Подскажите код или мысль - как на одной свече открыть не более одной сделки, если сработал SL, то нужно ждать следующей свечи...