//+------------------------------------------------------------------+
//| TimeTrader.mq5 |
//| Copyright 2026, AI Assistant |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026"
#property link ""
#property version "1.00"
#include <Trade\Trade.mqh>
enum ENUM_TRADE_DIRECTION
{
DIR_BUY, // Покупка (BUY)
DIR_SELL // Продажа (SELL)
};
//--- Настройки торговли
input group "=== Параметры сделки ==="
input ENUM_TRADE_DIRECTION InpTradeType = DIR_BUY; // Направление сделки
input double InpLot = 0.1; // Объем лота
input int InpStopLoss = 200; // Stop Loss в пунктах (0 = выключен)
input int InpTakeProfit = 400; // Take Profit в пунктах (0 = выключен)
input group "=== Время срабатывания (время сервера) ==="
input int InpOpenHour = 10; // Час открытия (0-23)
input int InpOpenMinute = 0; // Минута открытия (0-59)
input int InpCloseHour = 18; // Час закрытия (0-23)
input int InpCloseMinute = 0; // Минута закрытия (0-59)
input group "=== Общие настройки ==="
input ulong InpMagic = 123456; // Magic Number
//--- Глобальные переменные
CTrade trade;
int lastDayChecked = -1;
bool openedToday = false;
bool closedToday = false;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
trade.SetExpertMagicNumber(InpMagic);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
MqlDateTime dt;
TimeCurrent(dt);
// Сброс флагов при наступлении нового дня
if(dt.day != lastDayChecked)
{
lastDayChecked = dt.day;
openedToday = false;
closedToday = false;
}
int currentMinutes = dt.hour * 60 + dt.min;
int openMinutes = InpOpenHour * 60 + InpOpenMinute;
int closeMinutes = InpCloseHour * 60 + InpCloseMinute;
// 1. Открытие сделки
if(!openedToday && currentMinutes >= openMinutes && currentMinutes < closeMinutes)
{
if(!HasOpenPosition())
{
OpenTrade();
}
openedToday = true; // Отмечаем попытку открытия, чтобы не спамить ордерами
}
// 2. Закрытие сделки по времени
if(!closedToday && currentMinutes >= closeMinutes)
{
CloseAllPositions();
closedToday = true;
}
}
//+------------------------------------------------------------------+
//| Проверка наличия позиций советника |
//+------------------------------------------------------------------+
bool HasOpenPosition()
{
for(int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong ticket = PositionGetTicket(i);
if(ticket > 0)
{
if(PositionGetString(POSITION_SYMBOL) == _Symbol &&
PositionGetInteger(POSITION_MAGIC) == InpMagic)
{
return true;
}
}
}
return false;
}
//+------------------------------------------------------------------+
//| Исполнение ордера |
//+------------------------------------------------------------------+
void OpenTrade()
{
double price = 0.0;
double slValue = 0.0;
double tpValue = 0.0;
double point = _Point;
if(InpTradeType == DIR_BUY)
{
price = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
if(InpStopLoss > 0) slValue = price - InpStopLoss * point;
if(InpTakeProfit > 0) tpValue = price + InpTakeProfit * point;
trade.Buy(InpLot, _Symbol, price, slValue, tpValue, "TimeTrader BUY");
}
else if(InpTradeType == DIR_SELL)
{
price = SymbolInfoDouble(_Symbol, SYMBOL_BID);
if(InpStopLoss > 0) slValue = price + InpStopLoss * point;
if(InpTakeProfit > 0) tpValue = price - InpTakeProfit * point;
trade.Sell(InpLot, _Symbol, price, slValue, tpValue, "TimeTrader SELL");
}
}
//+------------------------------------------------------------------+
//| Принудительное закрытие позиций |
//+------------------------------------------------------------------+
void CloseAllPositions()
{
for(int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong ticket = PositionGetTicket(i);
if(ticket > 0)
{
if(PositionGetString(POSITION_SYMBOL) == _Symbol &&
PositionGetInteger(POSITION_MAGIC) == InpMagic)
{
trade.PositionClose(ticket);
}
}
}
}