//+------------------------------------------------------------------+
//| SimpleSMA.mq5|
//| Copyright 2023, MetaTrader 5 User |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#include <Trade\Trade.mqh>
CTrade trade;
// Input variables
input double lotSize = 0.1;
int OnInit()
{
return(INIT_SUCCEEDED);
}
void OnTick()
{
double sma20 = iMA(NULL, 0, 20, 0, MODE_SMA, PRICE_CLOSE, 0);
double sma200 = iMA(NULL, 0, 200, 0, MODE_SMA, PRICE_CLOSE, 0);
double lastHigh = iHigh(NULL, 0, 1); // Close of previous candle
double lastLow = iLow(NULL, 0, 1); // Close of previous candle
int totalOrders = OrdersTotal();
// If there is an open order, check its type
bool hasBuyOrder = false;
bool hasSellOrder = false;
for (int i = 0; i < totalOrders; i++)
{
if (OrderSelect(i, SELECT_BY_POS) && OrderSymbol() == _Symbol)
{
if (OrderType() == OP_BUY)
hasBuyOrder = true;
else if (OrderType() == OP_SELL)
hasSellOrder = true;
}
}
// Market conditions and order handling
if (sma200 < lastLow && lastHigh > sma20) // Buy condition
{
if (hasSellOrder)
CloseAllOrders(OP_SELL);
if (!hasBuyOrder)
trade.Buy(lotSize, NULL, NULL, NULL, NULL);
}
else if (sma200 > lastHigh && lastLow < sma20) // Sell condition
{
if (hasBuyOrder)
CloseAllOrders(OP_BUY);
if (!hasSellOrder)
trade.Sell(lotSize, NULL, NULL, NULL, NULL);
}
}
void CloseAllOrders(int orderType)
{
for (int i = OrdersTotal() - 1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderSymbol() == _Symbol)
{
if (OrderType() == orderType)
{
if (orderType == OP_BUY)
trade.PositionClose(_Symbol);
else if (orderType == OP_SELL)
trade.PositionClose(_Symbol);
}
}
}
}
erro no codigo:
'iMA' - wrong parameters count HTF_BUY_01.mq5 19 19
built-in: int iMA(const string,ENUM_TIMEFRAMES,int,int,ENUM_MA_METHOD,int) HTF_BUY_01.mq5 19 19
'iMA' - wrong parameters count HTF_BUY_01.mq5 20 20
built-in: int iMA(const string,ENUM_TIMEFRAMES,int,int,ENUM_MA_METHOD,int) HTF_BUY_01.mq5 20 20
'SELECT_BY_POS' - undeclared identifier HTF_BUY_01.mq5 33 26
'OrderSelect' - wrong parameters count HTF_BUY_01.mq5 33 11
built-in: bool OrderSelect(ulong) HTF_BUY_01.mq5 33 11
'OrderSymbol' - undeclared identifier HTF_BUY_01.mq5 33 44
')' - expression expected HTF_BUY_01.mq5 33 56
implicit conversion from 'unknown' to 'string' HTF_BUY_01.mq5 33 44
'OrderType' - undeclared identifier HTF_BUY_01.mq5 35 14
')' - expression expected HTF_BUY_01.mq5 35 24
'OP_BUY' - undeclared identifier HTF_BUY_01.mq5 35 29
'OrderType' - undeclared identifier HTF_BUY_01.mq5 37 19
')' - expression expected HTF_BUY_01.mq5 37 29
'OP_SELL' - undeclared identifier HTF_BUY_01.mq5 37 34
'OP_SELL' - undeclared identifier HTF_BUY_01.mq5 46 25
'OP_BUY' - undeclared identifier HTF_BUY_01.mq5 53 25
'SELECT_BY_POS' - undeclared identifier HTF_BUY_01.mq5 63 26
'MODE_TRADES' - undeclared identifier HTF_BUY_01.mq5 63 41
'OrderSelect' - wrong parameters count HTF_BUY_01.mq5 63 11
built-in: bool OrderSelect(ulong) HTF_BUY_01.mq5 63 11
'OrderSymbol' - undeclared identifier HTF_BUY_01.mq5 63 57
')' - expression expected HTF_BUY_01.mq5 63 69
implicit conversion from 'unknown' to 'string' HTF_BUY_01.mq5 63 57
'OrderType' - undeclared identifier HTF_BUY_01.mq5 65 14
')' - expression expected HTF_BUY_01.mq5 65 24
'OP_BUY' - undeclared identifier HTF_BUY_01.mq5 67 30
'OP_SELL' - undeclared identifier HTF_BUY_01.mq5 69 35