Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

#include <Trade/Trade.

mqh>

input int CloseTimeHour = 17;


input int CloseTimeMin = 0;
input int CloseTimeSec = 0;
input double TargetProfit = 100.0; // Example target profit in account currency

CTrade trade;

int OnInit() {
return INIT_SUCCEEDED;
}

void OnDeinit(const int reason) {


}

void OnTick() {
MqlDateTime structTime;
TimeCurrent(structTime);
structTime.hour = CloseTimeHour;
structTime.min = CloseTimeMin;
structTime.sec = CloseTimeSec;
datetime timeClose = StructToTime(structTime);

double profit = AccountInfoDouble(ACCOUNT_EQUITY) -


AccountInfoDouble(ACCOUNT_BALANCE);
bool isCloseTime = TimeCurrent() > timeClose;
bool isProfit = profit >= TargetProfit;

if (isCloseTime || isProfit) {
for (int i = PositionsTotal() - 1; i >= 0; i--) {
ulong posTicket = PositionGetTicket(i);
if (PositionSelectByTicket(posTicket)) {
if (trade.PositionClose(posTicket)) {
if (isCloseTime)
Print(__FUNCTION__, " > Pos #", posTicket, " was closed
because of close time...");
else if (isProfit)
Print(__FUNCTION__, " > Pos #", posTicket, " was closed
because of profit...");
Comment("Server Time: ", TimeCurrent(),
"\nClose Time: ", TimeToString(timeClose, TIME_DATE |
TIME_MINUTES),
"\nProfit: ", DoubleToString(profit, 2), " (Target: ",
DoubleToString(TargetProfit, 2), ")");
}
}
}
}
}

You might also like