how to return an array of copybuffer?

 

Hello,

I am writing a function to get 10 RSI values from copybuffer, how do return the function as an array/pointer?

double RSI[] (int Position, int RSI _Period, int RSI_Apply)
{
   int Handle;
   double Values;
   Handle = iRSI (Symbol(), Period(), RSI _Period, RSI I_Apply);
   CopyBuffer(Handle, 0, Position, 10, Values);
  return(Values);  //<---- what is it here?
}

thank you

Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...
 
Dilwyn Tng Zhuo Yu :

Hello,

I am writing a function to get 10 RSI values from copybuffer, how do return the function as an array/pointer?

thank you

Your function contains a gross error: in MQL5, the indicator handle MUST BE CREATED ONCE! You need to do this in OnInit.

Correct your code, then I can tell you further ...

 
How to start with MQL5
How to start with MQL5
  • 2020.07.05
  • www.mql5.com
This thread discusses MQL5 code examples. There will be examples of how to get data from indicators, how to program advisors...
 
  1.    Handle = iRSI (Symbol(), Period(), RSI _Period, RSI I_Apply);
       CopyBuffer(Handle, 0, Position, 10, Values);

    Perhaps you should read the manual, especially the examples.
       How To Ask Questions The Smart Way. 2004
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

    They all (including iCustom) return a handle (an int.) You get that in OnInit. In OnTick (after the indicator has updated its buffers,) you use the handle, shift and count to get the data.
              Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
              Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
              How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 2020.03.08
              How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 2020.07.05
              How to call indicators in MQL5 - MQL5 Articles 12 March 2010
  2.    double Values;
       CopyBuffer(Handle, 0, Position, 10, Values);
    CopyXXXX requires an array; don't post code that will not even compile. Perhaps you should read the manual.

  3. double RSI[] ( …

    As the documentation clearly says "With the return operator you can't return any arrays, class objects, variables of compound structure type." Perhaps you should read the manual.

    You can pass an array into a function by (non-constant) reference, which allows the function to modify the caller's array.

 

Hello, I am trying to create an RSI indicator of 7 currencies but seems to have issue with the array in first position CopyBuffer (native functiomn)...

   ArrayInitialize(iTsi, EMPTY_VALUE);

   for(x = 0; x < 28; x++)
      if(StringSubstr(symbols[x], 0, 3) == "USD"||
         StringSubstr(symbols[x], 3, 3) == "USD")
        {
         int ii = iBarShift(symbols[x], tf, iTime(_Symbol, tf, _i), true);

         if(ii == -1)
            continue; 
            
         if(!getCopyBuffer(TSI[x], MAIN_LINE, ii, 1, iTsi))
            return DBL_MIN;

         count++;

         if(iTsi[0] != EMPTY_VALUE)
            if(StringSubstr(symbols[x], 0, 3) == "USD")
               res += iTsi[0];
            else
               if(StringSubstr(symbols[x], 3, 3) == "USD")
                  res -= iTsi[0];

        }

   return res / count;
Reason: