mt4 Trading Ea
mt4 Trading Ea
//| EstrategiaTradingCompleta.mq4 |
//| Copyright 2024, TuNombre |
//| https://www.tuurl.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, TuNombre"
#property link "https://www.tuurl.com"
#property version "1.00"
#property strict
// Parámetros de entrada
input int shortSMA = 9;
input int longSMA = 50;
input int shortEMA = 12;
input int longEMA = 26;
input int RSIPeriod = 14;
input int RSIOverbought = 70;
input int RSIOversold = 30;
input double stopLossPerc = 2.0;
input double takeProfitPerc = 4.0;
// Variables globales
double shortSMABuffer, longSMABuffer, shortEMABuffer, longEMABuffer;
double MACDMain, MACDSignal, RSIValue;
double BBUpper, BBLower, VWAPValue;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Actualizar indicadores
UpdateIndicators();
//+------------------------------------------------------------------+
//| Actualizar todos los indicadores |
//+------------------------------------------------------------------+
void UpdateIndicators()
{
shortSMABuffer = iMA(NULL, 0, shortSMA, 0, MODE_SMA, PRICE_CLOSE, 0);
longSMABuffer = iMA(NULL, 0, longSMA, 0, MODE_SMA, PRICE_CLOSE, 0);
shortEMABuffer = iMA(NULL, 0, shortEMA, 0, MODE_EMA, PRICE_CLOSE, 0);
longEMABuffer = iMA(NULL, 0, longEMA, 0, MODE_EMA, PRICE_CLOSE, 0);
VWAPValue = CalculateVWAP();
}
//+------------------------------------------------------------------+
//| Calcular VWAP |
//+------------------------------------------------------------------+
double CalculateVWAP()
{
double cumVolume = 0;
double cumVolumePrice = 0;
for(int i = 0; i < 20; i++) // Calculamos VWAP para las últimas 20 velas
{
double typicalPrice = (High[i] + Low[i] + Close[i]) / 3;
cumVolume += Volume[i];
cumVolumePrice += Volume[i] * typicalPrice;
}
//+------------------------------------------------------------------+
//| Comprobar si es una nueva vela |
//+------------------------------------------------------------------+
bool IsNewBar()
{
static datetime lastbar;
datetime curbar = Time[0];
if(lastbar != curbar)
{
lastbar = curbar;
return true;
}
return false;
}
//+------------------------------------------------------------------+
//| Comprobar condición de compra |
//+------------------------------------------------------------------+
bool CheckBuyCondition()
{
bool emaCondition = shortEMABuffer > longEMABuffer;
bool macdCondition = MACDMain > MACDSignal;
bool rsiCondition = RSIValue < RSIOversold;
bool bbCondition = Close[0] > BBLower;
bool vwapCondition = Close[0] > VWAPValue;
//+------------------------------------------------------------------+
//| Comprobar condición de venta |
//+------------------------------------------------------------------+
bool CheckSellCondition()
{
bool emaCondition = shortEMABuffer < longEMABuffer;
bool macdCondition = MACDMain < MACDSignal;
bool rsiCondition = RSIValue > RSIOverbought;
bool bbCondition = Close[0] < BBUpper;
bool vwapCondition = Close[0] < VWAPValue;
//+------------------------------------------------------------------+
//| Abrir orden de compra |
//+------------------------------------------------------------------+
void OpenBuyOrder()
{
double lotSize = 0.01; // Tamaño de lote fijo, podrías implementar cálculo
basado en riesgo
double stopLoss = Ask * (1 - stopLossPerc / 100);
double takeProfit = Ask * (1 + takeProfitPerc / 100);
if(ticket < 0)
{
Print("OrderSend failed with error #", GetLastError());
}
}
//+------------------------------------------------------------------+
//| Cerrar orden de compra |
//+------------------------------------------------------------------+
void CloseBuyOrder()
{
for(int i = OrdersTotal() - 1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderType() == OP_BUY && OrderSymbol() == Symbol())
{
bool result = OrderClose(OrderTicket(), OrderLots(), Bid, 3, clrRed);
if(!result)
{
Print("OrderClose failed with error #", GetLastError());
}
}
}
}
}
//+------------------------------------------------------------------+