0% found this document useful (0 votes)
36 views4 pages

mt4 Trading Ea

trading python2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views4 pages

mt4 Trading Ea

trading python2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

//+------------------------------------------------------------------+

//| 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();

// Comprobar condiciones de trading


if(IsNewBar())
{
// Comprobar condición de compra
if(CheckBuyCondition())
{
OpenBuyOrder();
}
// Comprobar condición de venta
if(CheckSellCondition())
{
CloseBuyOrder();
}
}
}

//+------------------------------------------------------------------+
//| 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);

MACDMain = iMACD(NULL, 0, shortEMA, longEMA, 9, PRICE_CLOSE, MODE_MAIN, 0);


MACDSignal = iMACD(NULL, 0, shortEMA, longEMA, 9, PRICE_CLOSE, MODE_SIGNAL, 0);

RSIValue = iRSI(NULL, 0, RSIPeriod, PRICE_CLOSE, 0);

BBUpper = iBands(NULL, 0, 20, 2, 0, PRICE_CLOSE, MODE_UPPER, 0);


BBLower = iBands(NULL, 0, 20, 2, 0, PRICE_CLOSE, MODE_LOWER, 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;
}

return (cumVolume != 0) ? cumVolumePrice / cumVolume : 0;


}

//+------------------------------------------------------------------+
//| 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;

return (emaCondition || macdCondition || rsiCondition) && bbCondition &&


vwapCondition;
}

//+------------------------------------------------------------------+
//| 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;

return (emaCondition || macdCondition || rsiCondition) && bbCondition &&


vwapCondition;
}

//+------------------------------------------------------------------+
//| 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);

int ticket = OrderSend(Symbol(), OP_BUY, lotSize, Ask, 3, stopLoss, takeProfit,


"Buy Order", 0, 0, clrGreen);

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());
}
}
}
}
}

//+------------------------------------------------------------------+

You might also like