SimpleAutoBot/SimpleAutoBot.mq5

86 строки
2,5 КиБ
MQL5
Исходный Постоянная ссылка Обычный вид История

2026-01-31 22:38:12 +00:00
#property strict
//--- Bot Inputs
input double LotSize = 0.01;
input int TakeProfitPips = 150;
input int StopLossPips = 80;
input int Lookback = 14; // Period for detecting high/low
//+------------------------------------------------------------------+
//| Expert initialization |
//+------------------------------------------------------------------+
int OnInit()
{
Print("SimpleAutoBot initialized successfully.");
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert tick function — main trading logic |
//+------------------------------------------------------------------+
void OnTick()
{
// Only one trade at a time
if (PositionsTotal() > 0) return;
double lowest = iLow(_Symbol, PERIOD_CURRENT, iLowest(_Symbol, PERIOD_CURRENT, MODE_LOW, Lookback, 1));
double highest = iHigh(_Symbol, PERIOD_CURRENT, iHighest(_Symbol, PERIOD_CURRENT, MODE_HIGH, Lookback, 1));
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
//--- BUY LOGIC: Price touches recent LOW
if (bid <= lowest)
{
OpenOrder(ORDER_TYPE_BUY, LotSize, ask);
}
//--- SELL LOGIC: Price touches recent HIGH
if (ask >= highest)
{
OpenOrder(ORDER_TYPE_SELL, LotSize, bid);
}
}
//+------------------------------------------------------------------+
//| Function to place orders |
//+------------------------------------------------------------------+
void OpenOrder(ENUM_ORDER_TYPE type, double lots, double price)
{
double sl, tp;
double point = _Point;
double pip = point * 10;
if (type == ORDER_TYPE_BUY)
{
sl = price - StopLossPips * pip;
tp = price + TakeProfitPips * pip;
}
else
{
sl = price + StopLossPips * pip;
tp = price - TakeProfitPips * pip;
}
MqlTradeRequest request;
MqlTradeResult result;
ZeroMemory(request);
ZeroMemory(result);
request.action = TRADE_ACTION_DEAL;
request.symbol = _Symbol;
request.volume = lots;
request.type = type;
request.price = price;
request.sl = sl;
request.tp = tp;
request.deviation = 10;
OrderSend(request, result);
if (result.retcode == 10009 || result.retcode == 10008)
Print("Trade opened: ", type == ORDER_TYPE_BUY ? "BUY" : "SELL");
else
Print("Failed to open trade. Error: ", result.retcode);
}