How to convert indicator(e.g Regression indicator ) to function that return a value(e.g. regression value)

 
//+------------------------------------------------------------------+

//|                                             Vinin HighLow v1.mq4 |

//|                                        Victor Nicolaev aka Vinin |

//|                                                    vinin@mail.ru |

//+------------------------------------------------------------------+

#property copyright "Victor Nicolaev aka Vinin"

#property link      "mailto: vinin@mail.ru"



#property indicator_chart_window

#property indicator_buffers 3

#property indicator_color1 Yellow

#property indicator_color2 Green

#property indicator_color3 Red



extern int period=34;

extern int price=0;

extern int Shift=0;

extern int Shift=0;



//---- buffers

double BufferGreen[];

double BufferYellow[];

double BufferRed[];

//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

int init() {

   int i;

   for (i=0;i<3;i++) {

      SetIndexStyle(i,DRAW_LINE);

      SetIndexDrawBegin(i,period);

      SetIndexShift(i,Shift);

   }

   SetIndexBuffer(0,BufferYellow);

   SetIndexBuffer(1,BufferGreen);

   SetIndexBuffer(2,BufferRed);

   return(0); }//int init() 

//+------------------------------------------------------------------+

int start() {

   int limit;

   double tmp1,tmp2,tmp3;

   int tmpBar, tmpTime;

   int counted_bars=IndicatorCounted();

   int i, j,k;

   if(counted_bars<0) return(-1);

   if(counted_bars>0) counted_bars--;

   limit=Bars-counted_bars;

   int cmd;

   for (i = limit;i>=0;i--){

      tmp1=iMA(Symbol(),0,period,0,MODE_SMA,price,i);

      tmp2=iMA(Symbol(),0,period,0,MODE_LWMA,price,i);

      tmp3=3.0*tmp2-2.0*tmp1;

      BufferGreen[i] =tmp3;

      BufferYellow[i]=tmp3;

      BufferRed[i]   =tmp3;

//2018-10-1 plan to enhance  Begin enhancement 

if (i==1) // to return the latest  regression value

{        

  Return_value =tmp3;        //2008-10-01 how do I pass the return_value to calling program ?  ***

}

//2018-10-1 plan to enhance  End enhancement 



      if (BufferYellow[i]>BufferYellow[i+1]){

         BufferRed[i]=EMPTY_VALUE;

      } else if (BufferYellow[i]<BufferYellow[i+1]){

         BufferGreen[i] =EMPTY_VALUE;

      } else {

         BufferRed[i]=EMPTY_VALUE;         

         BufferGreen[i] =EMPTY_VALUE;

      }

   }

   return(0); 

}


Hi all,

Good day.

GENERICALLY, how do we modify an indicator to a become a function that **return a value** to calling program ?

so that e.g. from the main program, we can call this EXAMPLE function to get a value e.g. linear-regression-indikator value 

Just using this indicator to learn how to convert to function

In main program e.g. when condition-1 met, I like to call this linear-regression-indikator to get the regression value and get the 

 double Return_val=iCustom("SP-DEC18",0,"linear-regression-indikator",34,0,0);


This is just a example function to be called

Currently the linear-regression-indikator has parameters

extern int period=34;

extern int price=0;

extern int Shift=0;


---===  sample indicator  script attached  ====

..

   int cmd;

   for (i = limit;i>=0;i--){

      tmp1=iMA(Symbol(),0,period,0,MODE_SMA,price,i);

      tmp2=iMA(Symbol(),0,period,0,MODE_LWMA,price,i);

      tmp3=3.0*tmp2-2.0*tmp1;

      BufferGreen[i] =tmp3;

      BufferYellow[i]=tmp3;

      BufferRed[i]   =tmp3;

//2018-10-1 plan to enhance  Begin enhancement 

if (i==1) // to return the latest  regression value

{  

  Return_value =tmp3;        //2008-10-01 how do I pass the return_value to calling program ?  ***

}

//2018-10-1 plan to enhance  End enhancement 


      if (BufferYellow[i]>BufferYellow[i+1]){

         BufferRed[i]=EMPTY_VALUE;

      } else if (BufferYellow[i]<BufferYellow[i+1]){

         BufferGreen[i] =EMPTY_VALUE;

      } else {

         BufferRed[i]=EMPTY_VALUE;         

         BufferGreen[i] =EMPTY_VALUE;

      }

   }

   return(0); 

}


==== Note: Reference for icustom======

https://docs.mql4.com/indicators/icustom 

double  iCustom(
   string       symbol,           // symbol
   int          timeframe,        // timeframe
   string       name,             // path/name of the custom indicator compiled program
   ...                            // custom indicator input parameters (if necessary)
   int          mode,             // line index
   int          shift             // shift
   );


Thank you very much in advance

/

iCustom - Technical Indicators - MQL4 Reference
iCustom - Technical Indicators - MQL4 Reference
  • docs.mql4.com
[in]  Custom indicator compiled program name, relative to the root indicators directory (MQL4/Indicators/). If the indicator is located in subdirectory, for example, in MQL4/Indicators/ The passed parameters and their order must correspond with the declaration order and the type of extern variables of the custom indicator...
 

Forum on trading, automated trading systems and testing trading strategies

When you post code please use the CODE button (Alt-S)!

Use the CODE button

Thank you.

 
Noted. Thank you