indicator to EA

 

i have this indicator, and i want to add it as a function to the EA. With the EA, my CPU is always at 100%, accessing this indicator from multi time frames, and multi pairs. I was thinking of including the indicator into the EA, so that the CPU usage is lower.



Below is the function i have written in the EA, from the indicator. The problem is returning one of three arrays, LR_Line, Sup_line and Res_line. How would the function return the values for all three arrays?

double indicator(string line, bool UseClose, int barsToCount)
{

double LR_line[];
double Sup_line[];
double Res_line[];
// variables
   double a,b,c,
          sumy=0.0,
          sumx=0.0,
          sumxy=0.0,
          sumx2=0.0,
          h=0.0,l=0.0;   
   int x;
   
   // calculate linear regression
   
   for(int i=0; i<barsToCount; i++)
   {
      sumy+=Close[i];
      sumxy+=Close[i]*i;
      sumx+=i;
      sumx2+=i*i;
   }
   
   c=sumx2*barsToCount-sumx*sumx;
   
   if(c==0.0)
   {
      Alert("Error in linear regression!");
      return;
   }
   
   
   // Line equation    
   b=(sumxy*barsToCount-sumx*sumy)/c;
   a=(sumy-sumx*b)/barsToCount;
   
   // Linear regression line in buffer
   for(x=0;x<barsToCount;x++)
      LR_line[x]=a+b*x;
   
   

   
   // Use PRICE_CLOSE for support-resistance
   if (UseClose)
     for(x=0;x<barsToCount;x++)
     {
       if(Close[x]-LR_line[x] > h) h = Close[x]-LR_line[x];
       if(LR_line[x] - Close[x]> l) l = LR_line[x] - Close[x];
     }  
   
   // Use HIGH - LOW
   else
     for(x=0;x<barsToCount;x++)
     {
       if(High[x]-LR_line[x] > h) h = High[x]-LR_line[x];
       if(LR_line[x] - Low[x]> l) l = LR_line[x] - Low[x];
     }
   
   // Drawing support - resistance lines   
   if (h>l)
   {
     for(x=0;x<barsToCount;x++)
     {
       Sup_line[x]=a-h+b*x;
       Res_line[x]=a+h+b*x;
     } 
   }
   else
   {
     for(x=0;x<barsToCount;x++)
     {
       Sup_line[x]=a-l+b*x;
       Res_line[x]=a+l+b*x;
     }
   }
   
   LR_line[x]  = 0.0;
   Sup_line[x] = 0.0;
   Res_line[x] = 0.0;

   return;
}
Files:
 
c0d3 wrote >>

i have this indicator, and i want to add it as a function to the EA. With the EA, my CPU is always at 100%, accessing this indicator from multi time frames, and multi pairs. I was thinking of including the indicator into the EA, so that the CPU usage is lower.

Below is the function i have written in the EA, from the indicator. The problem is returning one of three arrays, LR_Line, Sup_line and Res_line. How would the function return the values for all three arrays?

declare the array for parameter passing

double indicator(string line, bool UseClose, int barsToCount, double& LR_line[], double& Sup_line[],double& Res_line[])

 
ronaldosim:

declare the array for parameter passing

double indicator(string line, bool UseClose, int barsToCount, double& LR_line[], double& Sup_line[],double& Res_line[])

Would it be possible to return all three arrays?

 
c0d3 wrote >>

Would it be possible to return all three arrays?

yes, but on hindsight, u dont need the ampersand ie & since the passing is unidirection;

double indicator(string line, bool UseClose, int barsToCount, double LR_line[], double Sup_line[],double Res_line[])

what u need to take care is the arrayresizing

 
ronaldosim wrote >>

yes, but on hindsight, u dont need the ampersand ie & since the passing is unidirection;

double indicator(string line, bool UseClose, int barsToCount, double LR_line[], double Sup_line[],double Res_line[])

what u need to take care is the arrayresizing

sorry, i woke up early this morning and was stoned ; u do need the "&" as it is bi-direction ; the main module will call the indicator module and the & is needed to have the indicator module return the values of the arrays; so my first post is correct

 
ronaldosim:

sorry, i woke up early this morning and was stoned ; u do need the "&" as it is bi-direction ; the main module will call the indicator module and the & is needed to have the indicator module return the values of the arrays; so my first post is correct

lol, i got it working!


But now, when i compare the last value in any of the (3) arrays, to the value in the indicator, they are off. The calculations are the same, but for some reason the current value in the arrays is different. It is close when the time frame is lower, the higher the time frame, the higher the difference between the indicator values, and the values in the EA. Any ideas?

 
c0d3 wrote >>

lol, i got it working!

But now, when i compare the last value in any of the (3) arrays, to the value in the indicator, they are off. The calculations are the same, but for some reason the current value in the arrays is different. It is close when the time frame is lower, the higher the time frame, the higher the difference between the indicator values, and the values in the EA. Any ideas?

did u do arrayresize?

 
ronaldosim:

did u do arrayresize?

no, i just declera arraysize of [200], that is it.


double LR_line[200];
double Sup_line[200];
double Res_line[200];

 
c0d3 wrote >>

no, i just declera arraysize of [200], that is it.

do this

extern int bars = 50;
double LR_line[];
double Sup_line[];
double Res_line[];

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
ArrayResize(LR_line,bars);
ArrayResize(Sup_line,bars);
ArrayResize(Res_line,bars);
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
indicator("xx",true,bars,LR_line,Sup_line,Res_line);
Comment(DoubleToStr(LR_line[0],Digits)+" "+DoubleToStr(Sup_line[0],
Digits)+" "+DoubleToStr(Res_line[0],Digits));
//----
return(0);
}

 

strongly suggest you read/digest this article series, the last article of which is here Transferring an Indicator Code into an Expert Advisor Code. Conclusion

The conclusion makes for pertinent reading regards your quest for speed...

hth

 
ronaldosim:

do this

extern int bars = 50;
double LR_line[];
double Sup_line[];
double Res_line[];

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
ArrayResize(LR_line,bars);
ArrayResize(Sup_line,bars);
ArrayResize(Res_line,bars);
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
indicator("xx",true,bars,LR_line,Sup_line,Res_line);
Comment(DoubleToStr(LR_line[0],Digits)+" "+DoubleToStr(Sup_line[0],
Digits)+" "+DoubleToStr(Res_line[0],Digits));
//----
return(0);
}

thx, bro, works like a charm, apreciate the help!

Reason: