Download MetaTrader 5

Help me with this line of code

To add comments, please log in or register
Huido
73
Huido  
I wrote a simple indicator to compare RSI of previous bar with current rsi. But i need to change it to compare moving averages of RSI. please note that i know its doable to calculate moving average of rsi indicator by dragging moving average over rsi and applying it to previous indicators data and I also know there is ready to use indicators for this purpose on codebase but i need to code it. 
Please note to line 62 of code. How can i compare rsi[i+1] with moving average of RSI (IMA(RSI) )? 
I want to call rsi using iCustom and then in line 9, instead of PRICE_CLOSE, use that calculated rsi. 
Please consider that this code seems nonsense for trading, but i want to know how to write  above code. 
  1. int MA_handle;
  2. double MA[];
  3. int RSI_handle;
  4. double RSI[];
  5. double Low[];
  6. int OnInit()
  7.   {  
  8.    MA_handle = iMA(NULL, PERIOD_CURRENT, 14, 0, MODE_SMA, PRICE_CLOSE);
  9.    if(MA_handle < 0)
  10.      {
  11.       Print("The creation of iMA has failed: MA_handle=", INVALID_HANDLE);
  12.       Print("Runtime error = ", GetLastError());
  13.       return(INIT_FAILED);
  14.      }
  15.   
  16.    RSI_handle = iRSI(NULL, PERIOD_CURRENT, 14, PRICE_CLOSE);
  17.    if(RSI_handle < 0)
  18.      {
  19.       Print("The creation of iRSI has failed: RSI_handle=", INVALID_HANDLE);
  20.       Print("Runtime error = ", GetLastError());
  21.       return(INIT_FAILED);
  22.      }
  23.   
  24.    return(INIT_SUCCEEDED);
  25.   }
  26. //+------------------------------------------------------------------+
  27. //| Custom indicator iteration function                              |
  28. //+------------------------------------------------------------------+
  29. int OnCalculate(const int rates_total,
  30.                 const int prev_calculated,
  31.                 const datetime& time[],
  32.                 const double& open[],
  33.                 const double& high[],
  34.                 const double& low[],
  35.                 const double& close[])
  36.   {
  37.    int limit = rates_total - prev_calculated;
  38.    //--- counting from 0 to rates_total
  39.    ArraySetAsSeries(Buffer1, true);
  40.    //--- initial zero
  41.    if(prev_calculated < 1)
  42.      {
  43.       ArrayInitialize(Buffer1, 0);
  44.      }
  45.    else
  46.       limit++;
  47.   
  48.    if(CopyOpen(Symbol(), PERIOD_CURRENT, 0, rates_total, Open) <= 0) return(rates_total);
  49.    ArraySetAsSeries(Open, true);
  50.    if(CopyBuffer(MA_handle, 0, 0, rates_total, MA) <= 0) return(rates_total);
  51.    ArraySetAsSeries(MA, true);
  52.    if(CopyBuffer(RSI_handle, 0, 0, rates_total, RSI) <= 0) return(rates_total);
  53.    ArraySetAsSeries(RSI, true);
  54.    if(CopyLow(Symbol(), PERIOD_CURRENT, 0, rates_total, Low) <= 0) return(rates_total);
  55.    ArraySetAsSeries(Low, true);
  56.    //--- main loop
  57.    for(int i = limit-1; i >= 0; i--)
  58.      {
  59.     
  60.       if(RSI[1+i] > RSI[i])
  61.         {
  62.          Buffer1[i] = Low[i];
  63.         }
  64.       else
  65.         {
  66.          Buffer1[i] = 0;
  67.         }
  68.      }
  69.    return(rates_total);
  70.   }


whroeder1
16836
whroeder1  

Huido:

Please note to line 62 of code. How can i compare open[i+1] with moving average of RSI (IMA(RSI) )? 

I want to call rsi using iCustom and then in line 9, instead of PRICE_CLOSE, use that calculated rsi.
  1. Open is 1.12345 and RSI is in the range zero to 100 What is the point in comparing them?
  2. RSI is not a custom indicator, you can't use iCustom.
  3. To get the MA of a buffer you must calculate it, like the MT4's iMAOnArray.
              Migrating from MQL4 to MQL5 - MQL5 Articles -> № 17. Technical Indicators -> MAOnArrayMQL4
Huido
73
Huido  
whroeder1:
  1. Open is 1.12345 and RSI is in the range zero to 100 What is the point in comparing them?
  2. RSI is not a custom indicator, you can't use iCustom.
  3. To get the MA of a buffer you must calculate it, like the MT4's iMAOnArray.
              Migrating from MQL4 to MQL5 - MQL5 Articles -> № 17. Technical Indicators -> MAOnArrayMQL4

You are right for #1. I was written it wrong. I changed it now to compare previous rsi with current one. 

So you would say  its not possible to have something like this and use it instead of price_close in line 9?

double myrsi=iRSI(NULL,0,14,PRICE_CLOSE,0)
Ralf Graf
2459
Ralf Graf  

I would say, it is not possible.

Both indicators need an array to calculate the proper value.

The only way I see, is to create your own indicator, which is using one of the standard indicators to calculate the value
and put it to an internal array, while the other indicator needs to be rewritten to use this internal array instead of
Open, High, Low or Close.

To add comments, please log in or register