İş tamamlandı
İş Gereklilikleri
#include <Trade\Trade.mqh>
input double LotSize = 0.1;
input int RSIPeriod = 14;
input int EMAPeriod = 14;
input ENUM_TIMEFRAMES TimeFrame = PERIOD_M1;
CTrade trade;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
static double previous_rsi = 0;
static double previous_ema = 0;
// Create handles for RSI and EMA
int handle_rsi = iRSI(_Symbol, TimeFrame, RSIPeriod, PRICE_CLOSE);
int handle_ema = iMA(_Symbol, TimeFrame, EMAPeriod, 0, MODE_EMA, PRICE_CLOSE);
// Declare arrays to store indicator values
double rsi[2];
double ema[2];
// Check if handles are valid
if (handle_rsi < 0 || handle_ema < 0)
{
Print("Error creating indicator handles");
return;
}
// Copy indicator values into arrays
if (CopyBuffer(handle_rsi, 0, 0, 2, rsi) < 0 || CopyBuffer(handle_ema, 0, 0, 2, ema) < 0)
{
Print("Error copying indicator values");
return;
}
// Log current RSI and EMA values for debugging
double current_rsi = rsi[0];
double current_ema = ema[0];
Print("Current RSI: ", current_rsi);
Print("Current EMA: ", current_ema);
// Ensure previous values are set to current if they are not yet initialized
if (previous_rsi == 0 && previous_ema == 0)
{
previous_rsi = current_rsi;
previous_ema = current_ema;
return;
}
// Check if there's an open position
bool isPositionOpen = PositionSelect(_Symbol);
// Entry criteria
if (previous_rsi != 0 && previous_ema != 0)
{
// Close existing position and open the opposite trade
if (previous_rsi > previous_ema && current_rsi < current_ema)
{
// Sell signal
if (isPositionOpen && PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
{
trade.PositionClose(_Symbol);
}
trade.Sell(LotSize, _Symbol, 0, 0, 0, "Sell on RSI cross below EMA");
Print("Sell on RSI cross below EMA");
}
else if (previous_rsi < previous_ema && current_rsi > current_ema)
{
// Buy signal
if (isPositionOpen && PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
{
trade.PositionClose(_Symbol);
}
trade.Buy(LotSize, _Symbol, 0, 0, 0, "Buy on RSI cross above EMA");
Print("Buy on RSI cross above EMA");
}
}
// Update previous RSI and EMA values
previous_rsi = current_rsi;
previous_ema = current_ema;
}
//+------------------------------------------------------------------+