Code Library / Session/Time
MQL5 Time-Based EA
EA opening trades at specific times.
Language: MQL5 · Category: Session/Time · Tags: time-based, MQL5, EA, session, scheduled trading
Source code
#include <Trade/Trade.mqh>
CTrade trade; input int OpenH=8; input int CloseH=16; input double Lots=0.1;
static bool traded=false;
void OnTick() {
MqlDateTime dt; TimeCurrent(dt);
if(dt.hour==0&&dt.min==0) traded=false;
if(dt.hour==OpenH&&!traded&&PositionsTotal()==0) {
double pt=SymbolInfoDouble(_Symbol,SYMBOL_POINT);
double ask=SymbolInfoDouble(_Symbol,SYMBOL_ASK);
trade.Buy(Lots,_Symbol,ask,ask-30*pt,ask+60*pt,"Time");
traded=true;
}
if(dt.hour==CloseH)
for(int i=PositionsTotal()-1;i>=0;i--) trade.PositionClose(PositionGetTicket(i));
}How to use this snippet
This is a building block, not a finished trading system. Paste it into your own MQL5 project, compile it, and test it on a demo account with your broker's real spreads before relying on it. If MetaEditor reports errors, the compilation error fixer can help.