custom RVI function - what does it return?

 

Hi everyone

I recently started writing scripts. I wrote a function that creates a variable that contains a signal based on the RVI, that is 1 = buy, 2 = sell, 0 = no action. This is the function I wrote


double rvifun(){
   double rvifun = 0;
   double rvibasefast;
   double rvisignalfast;
   double rvibaseslow;
   double rvisignalslow;
   
   // initialize signal variable
   int rvisignal;

   
   // current candlestick -1
   rvibasefast     = iRVI(Symbol(),PERIOD_CURRENT,RVIperioden,0,1);
   rvisignalfast   = iRVI(Symbol(),PERIOD_CURRENT,RVIperioden,1,1);
   
   // current candlestick -2
   rvibaseslow     = iRVI(Symbol(),PERIOD_CURRENT,RVIperioden,0,2);
   rvisignalslow   = iRVI(Symbol(),PERIOD_CURRENT,RVIperioden,1,2);
   
   if(rvisignalfast > rvibasefast && rvisignalslow < rvibaseslow && rvisignalfast < 0){
      // if signal line crosses baseline from bottom to top AND signal line is below zero --> buy signal
      // signal 1 means buy
      rvisignal = 1;
   } else if(rvisignalfast < rvibasefast && rvisignalslow > rvibaseslow){
      // if signal line crosses baseline from top to bototom AND signal line is above zero --> sell signal
      // signal 2 means sell
      rvisignal = 2;
   } else {
      // in all other cases: no action
      rvisignal = 0;
   }
  
  Print(rvisignal);
 
  return (rvisignal);
}


In on tick I want to call the function

rvifun(); Print(rvisignal);

And then work with it, like place an order etc. (not solely on rvi but thats not the point)

if(rvisignal == 1){
        // BUY
}

My intention was to outsource the rvi function out of the on tick block, call the function at the beginning and then work with the int variable rvisignal.

But I do not manage to actually return rvisignal! Currently I cannot work with the int variable rvisignal within my main on tick block, even though I coded to return it. What is wrong with the code?

 
  • Returning an "int".

If you want to return an "int", why have you declare your function to return a "double"?

double rvifun() {...}

If you want to return an "int", then declare it to return an "int"?

int rvifun() {...}

  • Use the return value. Don't try to access internal local variables.
int retRviSignal = rvifun();

Print( retRviSignal );

if( retRviSignal == 1 ) {  // BUY }

  • Also, your code seems to be about MQL4 and not MQL5. The MQL4 section is at the end of the forum. In future, please post in the MQL4 and MetaTrader 4 section if it be the case.
 

Thank you and sorry for using wrong section. You are absolutely right, I am using MQL4. Is it possible to move this thread into the right section?

However, I did not quite get everything you wrote.

So in the on Tick (where I call the function) I declare and call the function all in once, like

int retRviSignal = rvifun()


And later, within the function

int rvifun(){


I compute the int variable retRviSignal that I want to return and at the end of the function, I return the fuction itself?

return(rvifun);

Or the variable I want to use?

return(retRviSignal);
 
Sam Flowdux #: I compute the int variable retRviSignal that I want to return and at the end of the function, I return the fuction itself?

No you don't.

   } else {
      // in all other cases: no action
      rvisignal = 0;
   }
  
  Print(rvisignal);
 
  return (rvisignal);
You computed the variable rvisignal and return its value. You don't return a function, the function returned a value. All variables inside the function no longer exist.
Reason: