Why did you post your coding question in the MT5 General section (a catch-all category) instead of the MT5 EA section (non-indicator coding)?
General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
Next time, post in the correct place. I have moved this thread.
when arrays are not set to series, then count_candle will be a very old candle, and it won't serve you to go from 0 to a very old bar
u could do it like so
int last_bar = Bars(_Symbol, _Period) - 1; for (i= last_bar - count_Candle; i<last_bar - 1; i++){ sellBuffer[i] = 0; buyBuffer[i] = 0; bool upper_peak = rsi[i] > rsi[i-1] && rsi[i+1] < rsi[i]; bool lower_peak = rsi[i] < rsi[i-1] && rsi[i+1] > rsi[i]; if (i - 1 > 0) { if (_result[i] > 60 && upper_peak){ sellBuffer[i] = _result[i]; } else if (_result[i] < 40 && lower_peak){ buyBuffer[i] = _result[i]; } } }
now why am I saying last_Bar - 1? that's because the PRICE_CLOSE on the last bar is constantly changing, as it is an unclosed bar. If you would use it, the signals will always repaint. with this logic you will have a sellBuffer and buyBuffer that should signal a trade when they are not equal to 0.
Thanks for your explain about last_bar. I edited the code according to your instructions, but get error "array out of range in 'RSI-findpeak.mq5' (55,20)" . Please help me to correct it
Thank you
//+------------------------------------------------------------------+ //| RSI-findpeak.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" input int Inp_Period = 7; input ENUM_APPLIED_PRICE Inp_Apply = PRICE_CLOSE; int _handle; int count_Candle = 10; int last_bar; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- _handle = iRSI(_Symbol,_Period,Inp_Period,Inp_Apply); if (_handle==INVALID_HANDLE) { Print("Error loading indicator"); return(INIT_FAILED); } //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- double _result[], sellBuffer[],buyBuffer[]; int i; string text=""; if (CopyBuffer(_handle,0,0,count_Candle,_result)<0)//{Print("CopyBufferRSI plus error =",GetLastError());} last_bar=Bars(_Symbol, _Period)-1; for (i=last_bar-count_Candle; i<last_bar - 1; i++){ sellBuffer[i] = 0; buyBuffer[i] = 0; bool upper_peak =_result[i] > _result[i-1] && _result[i+1] < _result[i]; bool lower_peak = _result[i] < _result[i-1] && _result[i+1] > _result[i]; if (i - 1 > 0) { if (_result[i] > 60 && upper_peak){ sellBuffer[i] = _result[i]; } else if (_result[i] < 40 && lower_peak){ buyBuffer[i] = _result[i]; } } text=text+"peak of RSI "+ IntegerToString(i)+ " : " +DoubleToString(sellBuffer[i], Digits())+ " datetime :"+ TimeToString(iTime(_Symbol,_Period,i))+"\n"; Comment(text); } } //+------------------------------------------------------------------+ //| Trade function | //+------------------------------------------------------------------+ void OnTrade() { //--- } //+------------------------------------------------------------------+ //| TradeTransaction function | //+------------------------------------------------------------------+ void OnTradeTransaction(const MqlTradeTransaction& trans, const MqlTradeRequest& request, const MqlTradeResult& result) { //--- } //+------------------------------------------------------------------+
//+------------------------------------------------------------------+ //| RSI-findpeak.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" #define OBJ_PREFIX MQLInfoString(MQL_PROGRAM_NAME) input int Inp_Period = 7; input ENUM_APPLIED_PRICE Inp_Apply = PRICE_CLOSE; int _handle; int count_Candle = 10; int last_bar; int bars; string support_draw; string resistance_draw; int count = 0; double _result[], sellBuffer[],buyBuffer[]; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- _handle = iRSI(_Symbol,_Period,Inp_Period,Inp_Apply); if (_handle==INVALID_HANDLE) { Print("Error loading indicator"); return(INIT_FAILED); } bars = Bars(_Symbol, _Period); ArrayResize(_result, count_Candle); ArrayResize(sellBuffer, count_Candle); ArrayResize(buyBuffer, count_Candle); ArraySetAsSeries(_result, true); ArraySetAsSeries(sellBuffer, true); ArraySetAsSeries(buyBuffer, true); return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { int i; string text=""; if (CopyBuffer(_handle,0,0,count_Candle,_result)<0)//{Print("CopyBufferRSI plus error =",GetLastError());} last_bar = Bars(_Symbol, _Period)-1; int start = MathMax(2, last_bar); for (i = start; i >= 1; i--){ sellBuffer[i] = 0; buyBuffer[i] = 0; bool upper_peak =_result[i] > _result[i-1] && _result[i+1] < _result[i]; bool lower_peak = _result[i] < _result[i-1] && _result[i+1] > _result[i]; if (i-1 > 0){ if (upper_peak){ sellBuffer[i] = _result[i]; draw_downward_arrow(sellBuffer[i]); } else if (lower_peak){ buyBuffer[i] = _result[i]; draw_upward_arrow(buyBuffer[i]); } text=text+"peak of RSI "+ IntegerToString(i)+ " : " +DoubleToString(sellBuffer[i], Digits())+ " datetime :"+ TimeToString(iTime(_Symbol,_Period,i))+"\n"; } Comment(text); } } //+------------------------------------------------------------------+ //| Trade function | //+------------------------------------------------------------------+ void OnTrade() { //--- } //+------------------------------------------------------------------+ //| TradeTransaction function | //+------------------------------------------------------------------+ void OnTradeTransaction(const MqlTradeTransaction& trans, const MqlTradeRequest& request, const MqlTradeResult& result) { //--- } //+------------------------------------------------------------------+ void draw_upward_arrow(double price){ datetime time_to_use = iTime(Symbol(), Period(), 2); support_draw = OBJ_PREFIX + "arr" + IntegerToString(count); ObjectCreate(0, support_draw, OBJ_ARROW, 1, time_to_use, price); ObjectSetInteger(0, support_draw, OBJPROP_COLOR, clrLightGreen); ObjectSetInteger(0, support_draw, OBJPROP_ARROWCODE, 241); ObjectSetInteger(0, support_draw, OBJPROP_WIDTH, 2); } void draw_downward_arrow(double price){ datetime time_to_use = iTime(Symbol(), Period(), 2); resistance_draw = OBJ_PREFIX + "arr" + IntegerToString(count); ObjectCreate(0, resistance_draw, OBJ_ARROW, 1, time_to_use, price); ObjectSetInteger(0, resistance_draw, OBJPROP_COLOR, clrLawnGreen); ObjectSetInteger(0, resistance_draw, OBJPROP_ARROWCODE, 242); ObjectSetInteger(0, resistance_draw, OBJPROP_WIDTH, 2); }
It's better to make what you want to do in an indicator first and then use iCustom in the EA.
Thank you so much

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi
I'm trying to find peaks on RSI indicator by code below, but it's not work. I don't know if finding the peak based on my idea is correct? Please help me to fix it.
Thanks in advance