Converting Indicator to EA

 

Hello, i have been solving this problem for several weeks now ,I need your help. How can i convert the indicator below to EA ? I'm lost when it comes to using OnTIck function.

//--- input params

input int InChPeriod = 150; //Channel Period

int ExChPeriod,rCount;
//---- buffers
double rlBuffer[],upBuffer[],downBuffer[],highBuffer[],lowBuffer[]; 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+  
void OnInit()
  {
//--- check input variables
   int BarsTotal;
   BarsTotal=Bars(_Symbol,PERIOD_CURRENT);
   if(InChPeriod<2)
     {
      ExChPeriod=2;
      printf("Incorrect input value InChPeriod=%d. Indicator will use InChPeriod=%d.",
             InChPeriod,ExChPeriod);
     }
   else if(InChPeriod>=BarsTotal)
     {
      ExChPeriod=BarsTotal-1;
      printf("Total Bars=%d. Incorrect input value InChPeriod=%d. Indicator will use InChPeriod=%d.",
             BarsTotal,InChPeriod,ExChPeriod);
     }
   else ExChPeriod=InChPeriod;
   
   SetIndexBuffer(0,rlBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,upBuffer,INDICATOR_DATA);
   SetIndexBuffer(2,downBuffer,INDICATOR_DATA);
   SetIndexBuffer(3,highBuffer,INDICATOR_DATA);
   SetIndexBuffer(4,lowBuffer,INDICATOR_DATA);
   PlotIndexSetString(0,PLOT_LABEL,"Main Line("+string(ExChPeriod)+")");
   PlotIndexSetString(1,PLOT_LABEL,"Up Line("+string(ExChPeriod)+")");
   PlotIndexSetString(2,PLOT_LABEL,"Down Line("+string(ExChPeriod)+")");
   PlotIndexSetString(3,PLOT_LABEL,"High Line("+string(ExChPeriod)+")");
   PlotIndexSetString(4,PLOT_LABEL,"Low Line("+string(ExChPeriod)+")");
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total, const int prev_calculated, const int begin, const double &price[])
   {
    double sumX,sumY,sumXY,sumX2,a,b,F,S;
    int X;
//--- check for bars count
    if(rates_total<ExChPeriod+1)return(0);
//--- if  new bar set, calculate    
    if(rCount!=rates_total)
      {
       PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,rates_total-ExChPeriod-1);
       PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,rates_total-ExChPeriod-1);
       PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,rates_total-ExChPeriod-1);
       PlotIndexSetInteger(3,PLOT_DRAW_BEGIN,rates_total-ExChPeriod-1);
       PlotIndexSetInteger(4,PLOT_DRAW_BEGIN,rates_total-ExChPeriod-1);
//--- calculate coefficient a and b of equation linear regression 
       F=0.0;
       S=0.0;
       sumX=0.0;
       sumY=0.0;
       sumXY=0.0;
       sumX2=0.0;
       X=0;
       for(int i=rates_total-1-ExChPeriod;i<rates_total-1;i++)
         {
          sumX+=X;
          sumY+=price[i];
          sumXY+=X*price[i];
          sumX2+=MathPow(X,2);
          X++;
         }
       a=(sumX*sumY-ExChPeriod*sumXY)/(MathPow(sumX,2)-ExChPeriod*sumX2);
       b=(sumY-a*sumX)/ExChPeriod;
//--- calculate values of main line and error F
       X=0;
       for(int i=rates_total-1-ExChPeriod;i<rates_total;i++)
         {
          rlBuffer[i]=b+a*X;
          F+=MathPow(price[i]-rlBuffer[i],2);
          X++;
         }
//--- calculate deviation S       
       S=NormalizeDouble(MathSqrt(F/(ExChPeriod+1))/MathCos(MathArctan(a*M_PI/180)*M_PI/180),_Digits);
//--- calculate values of last buffers
       for(int i=rates_total-1-ExChPeriod;i<rates_total;i++)
         {
          upBuffer[i]=rlBuffer[i]+S;
          downBuffer[i]=rlBuffer[i]-S;
          highBuffer[i]=rlBuffer[i]+2*S;
          lowBuffer[i]=rlBuffer[i]-2*S;
         }
 
        rCount=rates_total;
      }
      
    return(rates_total);
   }
 
Mathewstwapalisha Mulwafu: Hello, i have been solving this problem for several weeks now ,I need your help. How can i convert the indicator below to EA ? I'm lost when it comes to using OnTIck function.

Generally you don't convert indicators into EAs. You USE indicators in EAs, by using the iCustom function. There are only some special cases when converting an Indicator into internal code of an EA has benefits. Other than that, it is better to separate the two and just use iCustom to collect the data being generated by the indicator.

Documentation on MQL5: Technical Indicators / iCustom
Documentation on MQL5: Technical Indicators / iCustom
  • www.mql5.com
iCustom - Technical Indicators - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Mathewstwapalisha Mulwafu: How can i convert the indicator below to EA ? I'm lost when it comes to using OnTIck function.
  1. Don't try to do that. There are no buffers, no IndicatorCounted() or prev_calculated. No way to know if older bars have changed or been added (history update.)

    Just get the value(s) of the indicator(s) into EA/indicator (using iCustom) and do what you want with it.
              (MT4) Detailed explanation of iCustom - MQL4 programming forum (2017)

  2. MT4: Learn to code it.
    MT5: Begin learning to code it.

    If you don't learn MQL4/5, there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into your code.

    Or pay (Freelance) someone to code it. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum (2019)

    We're not going to code it for you (although it could happen if you are lucky or the problem is interesting.) We are willing to help you when you post your attempt (using CODE button) and state the nature of your problem.
              No free help (2017)