error de compilacion mql5

freygiron

Trader
Jan 11, 2024
1
0
6
42
este codigo me da error por un desvalance de parentesis pero no encuentro solucionarlo:

MQL4:
//+------------------------------------------------------------------+
//|                                                        20200.mq5 |
//|                                  Copyright 2024, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
 
#property strict
 
// Definir las variables globales aquí
int periodosMediaMovilCorta = 20;
int periodosMediaMovilLarga = 200;
double takeProfitPips = 140;
double porcentajeCierre = 0.15;
 
 
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
             // Inicialización del asesor experto
    return(INIT_SUCCEEDED);
 }
 
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
 
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
    // Obtener la información del mercado
    double mediaMovilCorta = iMA(_Symbol, 0, periodosMediaMovilCorta, 0, MODE_SMA, PRICE_CLOSE, 0);
    double mediaMovilLarga = iMA(_Symbol, 0, periodosMediaMovilLarga, 0, MODE_SMA, PRICE_CLOSE, 0);
    double precio = SymbolInfoDouble(_Symbol, SYMBOL_BID);
 
 
    // Comprobar si hay un cruce de medias móviles hacia arriba (posición larga)
    if (mediaMovilCorta > mediaMovilLarga && iMA(_Symbol, 0, periodosMediaMovilCorta, 0, MODE_SMA, PRICE_CLOSE, 1) <= iMA(_Symbol, 0, periodosMediaMovilLarga, 0, MODE_SMA, PRICE_CLOSE, 1))
    {
        // Abrir una posición larga
        OrderSend(_Symbol, OP_BUY, 0.1, precio, 3, 0, 0, "Compra por EA", 0, 0, Green);
    }
 
    // Comprobar si hay un cruce de medias móviles hacia abajo (posición corta)
    if (mediaMovilCorta < mediaMovilLarga && iMA(_Symbol, 0, periodosMediaMovilCorta, 0, MODE_SMA, PRICE_CLOSE, 1) >= iMA(_Symbol, 0, periodosMediaMovilLarga, 0, MODE_SMA, PRICE_CLOSE, 1))
    {
        // Abrir una posición corta
        OrderSend(_Symbol, OP_SELL, 0.1, precio, 3, 0, 0, "Venta por EA", 0, 0, Red);
    }
 
    // Comprobar si hay posiciones abiertas y cerrarlas según las condiciones
    for (int i = OrdersHistoryTotal() - 1; i >= 0; i--)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) && OrderSymbol() == _Symbol)
        {
            if (OrderType() == OP_BUY && (precio - OrderOpenPrice()) >= takeProfitPips * Point)
{
 
    // Cerrar la posición larga
    OrderClose(OrderTicket(), OrderLots(), SymbolInfoDouble(_Symbol, SYMBOL_BID), 3, Green);
 
else if (OrderType() == OP_SELL && (OrderOpenPrice() - precio) >= takeProfitPips * Point)
{
    // Cerrar la posición corta
    OrderClose(OrderTicket(), OrderLots(), SymbolInfoDouble(_Symbol, SYMBOL_ASK), 3, Red);
}
}
}
}
 
Last edited by a moderator:
este codigo me da error por un desvalance de parentesis pero no encuentro solucionarlo:

MQL4:
//+------------------------------------------------------------------+
//|                                                        20200.mq5 |
//|                                  Copyright 2024, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
 
#property strict
 
// Definir las variables globales aquí
int periodosMediaMovilCorta = 20;
int periodosMediaMovilLarga = 200;
double takeProfitPips = 140;
double porcentajeCierre = 0.15;
 
 
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
             // Inicialización del asesor experto
    return(INIT_SUCCEEDED);
 }
 
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
 
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
    // Obtener la información del mercado
    double mediaMovilCorta = iMA(_Symbol, 0, periodosMediaMovilCorta, 0, MODE_SMA, PRICE_CLOSE, 0);
    double mediaMovilLarga = iMA(_Symbol, 0, periodosMediaMovilLarga, 0, MODE_SMA, PRICE_CLOSE, 0);
    double precio = SymbolInfoDouble(_Symbol, SYMBOL_BID);
 
 
    // Comprobar si hay un cruce de medias móviles hacia arriba (posición larga)
    if (mediaMovilCorta > mediaMovilLarga && iMA(_Symbol, 0, periodosMediaMovilCorta, 0, MODE_SMA, PRICE_CLOSE, 1) <= iMA(_Symbol, 0, periodosMediaMovilLarga, 0, MODE_SMA, PRICE_CLOSE, 1))
    {
        // Abrir una posición larga
        OrderSend(_Symbol, OP_BUY, 0.1, precio, 3, 0, 0, "Compra por EA", 0, 0, Green);
    }
 
    // Comprobar si hay un cruce de medias móviles hacia abajo (posición corta)
    if (mediaMovilCorta < mediaMovilLarga && iMA(_Symbol, 0, periodosMediaMovilCorta, 0, MODE_SMA, PRICE_CLOSE, 1) >= iMA(_Symbol, 0, periodosMediaMovilLarga, 0, MODE_SMA, PRICE_CLOSE, 1))
    {
        // Abrir una posición corta
        OrderSend(_Symbol, OP_SELL, 0.1, precio, 3, 0, 0, "Venta por EA", 0, 0, Red);
    }
 
    // Comprobar si hay posiciones abiertas y cerrarlas según las condiciones
    for (int i = OrdersHistoryTotal() - 1; i >= 0; i--)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) && OrderSymbol() == _Symbol)
        {
            if (OrderType() == OP_BUY && (precio - OrderOpenPrice()) >= takeProfitPips * Point)
{
 
    // Cerrar la posición larga
    OrderClose(OrderTicket(), OrderLots(), SymbolInfoDouble(_Symbol, SYMBOL_BID), 3, Green);
 
else if (OrderType() == OP_SELL && (OrderOpenPrice() - precio) >= takeProfitPips * Point)
{
    // Cerrar la posición corta
    OrderClose(OrderTicket(), OrderLots(), SymbolInfoDouble(_Symbol, SYMBOL_ASK), 3, Red);
}
}
}
}
//+------------------------------------------------------------------+
//| EA 20X200 ING GABOPINO.mq5 |
//| Copyright 2026, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
#property copyright "Copyright 2026."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict

#include <Trade/Trade.mqh>

CTrade trade;

input int InpFastMAPeriod = 20;
input int InpSlowMAPeriod = 200;
input double InpLotSize = 0.10;
input double InpTakeProfitPips = 140;
input int InpDeviation = 30;
input ulong InpMagicNumber = 20200;
input double InpClosePercent = 0.15;

int fastMAHandle = INVALID_HANDLE;
int slowMAHandle = INVALID_HANDLE;

datetime lastProcessedBarTime = 0;

double PipValue()
{
if(_Digits == 3 || _Digits == 5)
return _Point * 10.0;

return _Point;
}

bool HasOpenPosition()
{
for(int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong ticket = PositionGetTicket(i);

if(ticket <= 0)
continue;

string symbol = PositionGetString(POSITION_SYMBOL);
ulong magic = (ulong)PositionGetInteger(POSITION_MAGIC);

if(symbol == _Symbol && magic == InpMagicNumber)
return true;
}

return false;
}

void ManageTakeProfitClose()
{
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);

double tpDistance = InpTakeProfitPips * PipValue();

for(int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong ticket = PositionGetTicket(i);

if(ticket <= 0)
continue;

string symbol = PositionGetString(POSITION_SYMBOL);
ulong magic = (ulong)PositionGetInteger(POSITION_MAGIC);

if(symbol != _Symbol || magic != InpMagicNumber)
continue;

long positionType = PositionGetInteger(POSITION_TYPE);
double openPrice = PositionGetDouble(POSITION_PRICE_OPEN);

if(positionType == POSITION_TYPE_BUY)
{
if((bid - openPrice) >= tpDistance)
{
if(trade.PositionClose(ticket))
Print("Buy position closed by manual take profit. Ticket: ", ticket);
else
Print("Buy close error. Retcode: ", trade.ResultRetcode(),
" - ", trade.ResultRetcodeDescription());
}
}
else if(positionType == POSITION_TYPE_SELL)
{
if((openPrice - ask) >= tpDistance)
{
if(trade.PositionClose(ticket))
Print("Sell position closed by manual take profit. Ticket: ", ticket);
else
Print("Sell close error. Retcode: ", trade.ResultRetcode(),
" - ", trade.ResultRetcodeDescription());
}
}
}
}

int OnInit()
{
trade.SetExpertMagicNumber(InpMagicNumber);
trade.SetDeviationInPoints(InpDeviation);

fastMAHandle = iMA(
_Symbol,
PERIOD_CURRENT,
InpFastMAPeriod,
0,
MODE_SMA,
PRICE_CLOSE
);

slowMAHandle = iMA(
_Symbol,
PERIOD_CURRENT,
InpSlowMAPeriod,
0,
MODE_SMA,
PRICE_CLOSE
);

if(fastMAHandle == INVALID_HANDLE)
{
Print("Fast moving average handle creation error. Error: ", GetLastError());
return INIT_FAILED;
}

if(slowMAHandle == INVALID_HANDLE)
{
Print("Slow moving average handle creation error. Error: ", GetLastError());
return INIT_FAILED;
}

Print("EA initialized successfully on ", _Symbol, " / ", EnumToString(_Period));

return INIT_SUCCEEDED;
}

void OnDeinit(const int reason)
{
if(fastMAHandle != INVALID_HANDLE)
IndicatorRelease(fastMAHandle);

if(slowMAHandle != INVALID_HANDLE)
IndicatorRelease(slowMAHandle);

Print("EA deinitialized. Reason: ", reason);
}

void OnTick()
{
ManageTakeProfitClose();

datetime currentBarTime = iTime(_Symbol, PERIOD_CURRENT, 0);

if(currentBarTime == lastProcessedBarTime)
return;

lastProcessedBarTime = currentBarTime;

double fastMA[];
double slowMA[];

ArraySetAsSeries(fastMA, true);
ArraySetAsSeries(slowMA, true);

int copiedFast = CopyBuffer(fastMAHandle, 0, 0, 3, fastMA);
int copiedSlow = CopyBuffer(slowMAHandle, 0, 0, 3, slowMA);

if(copiedFast <= 0)
{
Print("Fast moving average buffer copy error. Error: ", GetLastError());
return;
}

if(copiedSlow <= 0)
{
Print("Slow moving average buffer copy error. Error: ", GetLastError());
return;
}

if(ArraySize(fastMA) < 3 || ArraySize(slowMA) < 3)
return;

double fastMAClosedBar = fastMA[1];
double slowMAClosedBar = slowMA[1];

double fastMAPreviousBar = fastMA[2];
double slowMAPreviousBar = slowMA[2];

bool bullishCross = (
fastMAClosedBar > slowMAClosedBar &&
fastMAPreviousBar <= slowMAPreviousBar
);

bool bearishCross = (
fastMAClosedBar < slowMAClosedBar &&
fastMAPreviousBar >= slowMAPreviousBar
);

if(HasOpenPosition())
return;

if(bullishCross)
{
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);

bool result = trade.Buy(
InpLotSize,
_Symbol,
ask,
0.0,
0.0,
"MA crossover buy"
);

if(result)
{
Print("Buy position opened by bullish crossover. Price: ", ask);
}
else
{
Print("Buy open error. Retcode: ", trade.ResultRetcode(),
" - ", trade.ResultRetcodeDescription());
}
}

if(bearishCross)
{
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);

bool result = trade.Sell(
InpLotSize,
_Symbol,
bid,
0.0,
0.0,
"MA crossover sell"
);

if(result)
{
Print("Sell position opened by bearish crossover. Price: ", bid);
}
else
{
Print("Sell open error. Retcode: ", trade.ResultRetcode(),
" - ", trade.ResultRetcodeDescription());
}
}
} I don’t know if you’re still alive. I hope you are. I hope this can still be of some use to you. Best regards.