Introduction of Time condition

 

Hi,

i need to insert a time condition on which the EA trade only between 2pm and 5pm (variables that I will change on backtesting).

I've read several thread but cannot find a way to understand to how to manage "time variables" properly.

Any idea on implement it very easily? (an IF Timecurrent > 2pm and <5pm then trade)

Thanks

 
Lobojo:

Hi,

i need to insert a time condition on which the EA trade only between 2pm and 5pm (variables that I will change on backtesting).

I've read several thread but cannot find a way to understand to how to manage "time variables" properly.

Any idea on implement it very easily? (an IF Timecurrent > 2pm and <5pm then trade)

Thanks

This is a recurring topic - have a look at this thread please

https://www.mql5.com/en/forum/458560

Set Limit Hours For An Indicator
Set Limit Hours For An Indicator
  • 2023.12.06
  • www.mql5.com
Hello, I Want To Set an Time Limitation For My Indicator. (MT5) How Can I Do That? For Example, I Want to Start Calculating From 9:00 To 00:00...
 
Check this thread, it is a lot similar in request https://www.mql5.com/en/forum/458882
Hello Everyone!!! I have written MlQL5 code to develop an EA but its not working.
Hello Everyone!!! I have written MlQL5 code to develop an EA but its not working.
  • 2023.12.12
  • www.mql5.com
Basically, when I attach the EA to GBPUSD 1M chart, I want the EA to open a BUY trade at 0.01 lot size at 18:00:00 hours and close it at 18:59:00...
 

Hi, I need some help to understand Time management in my EA

this is part of my EA. I would need to open a trade short or long following a specific condition (X<lastprice or X>lastprice) during specific timehours (3pm to 8pm in this case) opening 1 unique trade per hour.

It almost work, what is not working is the following:

If I insert as starting hour 8am or 9am, it open positions, if (as in the case below) I will put 3pm as starting hour it doesn't open any position, during the whole year, for the backtesting. Cannot really understand why...

The position it opens, I would like to be within the first minute of the hour (ex: at 4.00.01 pm max 4.01.00 pm), if not, to skip the position. but I really don't know how to put this condition (anyway, I'm stucked in the problem above).

Thanks for your help. I'm stuying by myself MQL5 since a week and I've coded helping me with this forum and AI.

#include<Trade\Trade.mqh>
CTrade trade;


// Dichiarazione delle variabili globali
double lotSize = 0.01;
int pendingOrderExpiration = 3600; // Tempo di scadenza dell'ordine pending in secondi (1 ora)
datetime lastOrderTime = 0; // Ora dell'ultima esecuzione dell'ordine
int tradeIntervalSeconds = 3600;

ulong secsStartTime         = 15 * 3600; // dalle 9
ulong secsEndTime         = 20 * 3600;  // alle 16

// Funzione eseguita all'arrivo di ogni tick
void OnTick()
{
    // Ottieni il valore dell'ultima candela oraria
    double lastHourlyHigh = iHigh(Symbol(), PERIOD_H1, 0);
    double lastHourlyLow = iLow(Symbol(), PERIOD_H1, 0);
    
ulong secsSinceMidnight = TimeCurrent()%(24*3600);

    MqlTick Latest_Price; // Structure to get the latest prices
    SymbolInfoTick(_Symbol, Latest_Price); // Assign current prices to structure


    // Ottieni l'ora corrente
    datetime currentTime = TimeCurrent();

//controlla orario funzionamento bot

if (secsSinceMidnight > secsStartTime && secsSinceMidnight < secsEndTime){



    // Controlla se è trascorso almeno un'ora dall'ultima esecuzione dell'ordine
   if ((currentTime - lastOrderTime) >= tradeIntervalSeconds)
    {
        // Verifica le condizioni per aprire una posizione
        if (X > Latest_Price.bid)
        {
             // Apri una posizione long       
        trade.BuyLimit
        (
        lotSize,                          //lotsize
        entryPriceLong,                   //Entry
        _Symbol,                          //Symbol
        stopLossLong,                     //SL
        targetPriceLong,                  //TP
        ORDER_TIME_GTC,                   //Expiration date
        pendingOrderExpiration,           //Expiration time
        "comment"                         //Comment
        );
        
           lastOrderTime = currentTime;
        }
        else if (X < Latest_Price.bid)
        {
            // Apri una posizione short
       trade.SellLimit
        (
        lotSize,                          //lotsize
        entryPriceShort,                   //Entry
        _Symbol,                          //Symbol
        stopLossShort,                     //SL
        targetPriceShort,                  //TP
        ORDER_TIME_GTC,                   //Expiration date
        pendingOrderExpiration,           //Expiration time
        "comment"                         //Comment
        );




            lastOrderTime = currentTime;
        }
    }
}
}
Reason: