I\m trying to code a indicator in MT5 but need some help. Please let me know what is wrong and help me with the correct code.
//±-----------------------------------------------------------------+
//| Custom indicator initialization function |
//±-----------------------------------------------------------------+
int OnInit()
{
//— indicator buffers mapping
//—
return(INIT_SUCCEEDED);
}
//— Define indicator buffers
double Buy_Arrow[];
double Sell_Arrow[];
double Buy_Exit_Symbol[];
double Sell_Exit_Symbol[];
//±-----------------------------------------------------------------+
//| Custom indicator iteration function |
//±-----------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
//—
//— return value of prev_calculated for next call
return(rates_total);
}
//±-----------------------------------------------------------------+
//Supertrend (7,0.9) indicator
int supertrend1_period = 7;
double supertrend1_multiplier = 0.9;
double supertrend1_factor = supertrend1_multiplier + 1.0;
double supertrend1_ATR = iATR(Symbol(), PERIOD_CURRENT, supertrend1_period);
double supertrend1_upper = (iHigh(Symbol(), PERIOD_CURRENT, 1) + iLow(Symbol(), PERIOD_CURRENT, 1)) / 2.0 + supertrend1_multiplier * supertrend1_ATR;
double supertrend1_lower = (iHigh(Symbol(), PERIOD_CURRENT, 1) + iLow(Symbol(), PERIOD_CURRENT, 1)) / 2.0 - supertrend1_multiplier * supertrend1_ATR;
double supertrend1_value = 0.0;
//Supertrend (7,1.8) indicator
int supertrend2_period = 7;
double supertrend2_multiplier = 1.8;
double supertrend2_factor = supertrend2_multiplier + 1.0;
double supertrend2_ATR = iATR(Symbol(), PERIOD_CURRENT, supertrend2_period);
double supertrend2_upper = (iHigh(Symbol(), PERIOD_CURRENT, 1) + iLow(Symbol(), PERIOD_CURRENT, 1)) / 2.0 + supertrend2_multiplier * supertrend2_ATR;
double supertrend2_lower = (iHigh(Symbol(), PERIOD_CURRENT, 1) + iLow(Symbol(), PERIOD_CURRENT, 1)) / 2.0 - supertrend2_multiplier * supertrend2_ATR;
double supertrend2_value = 0.0;
//EMA (9) indicator
int ema_period = 9;
double ema_value = iMA(Symbol(), PERIOD_CURRENT, ema_period, 0, MODE_EMA, PRICE_CLOSE);
//ADX (9) indicator
int adx_period = 9;
//double adx_value = iADX(Symbol(), PERIOD_CURRENT, adx_period, PRICE_CLOSE, MODE_MAIN, 0);
double adx_value = iADX(Symbol(), PERIOD_CURRENT, adx_period);
//RSI (9) indicator
int rsi_period = 9;
//double rsi_value = iRSI(Symbol(), PERIOD_CURRENT, rsi_period, PRICE_CLOSE, 0);
double rsi_value = iRSI(Symbol(), PERIOD_CURRENT, rsi_period, PRICE_CLOSE);
//Buy signal
if(supertrend1_value < Low[1] && supertrend1_upper > High[1] && supertrend2_value < Low[1] && supertrend2_upper > High[1] && ema_value > ema_value[1] && adx_value > 25 && rsi_value > 50)
{
//Place buy order
ArraySetAsSeries(Buy_Arrow, true);
Buy_Arrow[0] = High[0] + (iATR(NULL, 0, 14, 0) * 0.5);
PlotArrow(_Symbol, PERIOD_CURRENT, Buy_Arrow);
SetIndexBuffer(0, Buy_Arrow);
SetIndexArrow(0, 233);
}
//Buy exit signal
if((supertrend1_value > High[1] || supertrend2_value > High[1]) || ema_value < ema_value[1])
{
//Close buy order.
ArraySetAsSeries(Buy_Exit_Symbol, true);
Buy_Exit_Symbol[0] = High[0] + (iATR(NULL, 0, 14, 0) * 1.5);
PlotText(“B Exit”, _Symbol, PERIOD_CURRENT, Buy_Exit_Symbol[0], 0, 0, “Wingdings”, clrWhite, clrGreen);
SetIndexBuffer(2, Buy_Exit_Symbol);
SetIndexStyle(2, DRAW_NONE);
}
//Sell signal
if(supertrend1_value > High[1] && supertrend1_lower < Low[1] && supertrend2_value > High[1] && supertrend2_lower < Low[1] && ema_value < ema_value[1] && adx_value > 25 && rsi_value < 50)
{
//Place sell order
ArraySetAsSeries(Sell_Arrow, true);
Sell_Arrow[0] = Low[0] - (iATR(NULL, 0, 14, 0) * 0.5);
PlotArrow(_Symbol, PERIOD_CURRENT, Sell_Arrow);
SetIndexBuffer(1, Sell_Arrow);
SetIndexArrow(1, 234);
}
//Sell exit signal
if((supertrend1_value < Low[1] || supertrend2_value < Low[1]) || ema_value > ema_value[1])
{
//Close sell order
ArraySetAsSeries(Sell_Exit_Symbol, true);
Sell_Exit_Symbol[0] = Low[0] - (iATR(NULL, 0, 14, 0) * 1.5);
PlotText(“S Exit”, _Symbol, PERIOD_CURRENT, Sell_Exit_Symbol[0], 0, 0, “Wingdings”, clrWhite, clrRed);
SetIndexBuffer(3, Sell_Exit_Symbol);
SetIndexStyle(3, DRAW_NONE);
}