Looping through ENUM

 

Hi All,

 I want to loop through a number of moving averages as part of my trading system.

Input variables are as below

 input ENUM_TIMEFRAMES TimeFrame1 = PERIOD_D1; // First MA to use

input int Period1 =10; // Number of periods for first MA

input ENUM_TIMEFRAMES TimeFrame2 = PERIOD_D1; // Second MA to use

input int Period2 =10; // Number of periods for second MA
 

 My loop is per below

         for(int count =1;count<=NumberofMAtoUse; count++)

            {

            // Get Gradient for all MA's requested

             string LoopTimeFrame = "TimeFrame" + count;

            string LoopDatapoints = "Period" + count;

                        

            Grad = MAGradient(_Symbol,LoopTimeFrame,LoopDatapoints );

 

in essence i want to run through each variable 1 by 1 passing through to my MA Gradient function (below) the period selected in that variable.

 However i get an error (cannot convert enum) on the Looptimeframe string. 

Any thoughts on what i can do to fix this? 

 

 

 double MAGradient(string pSymbol,ENUM_TIMEFRAMES ChartPeriodDefinition,int NumberofDataPoints) 

  {     

      double ma[];

      double Gradient;

      ArraySetAsSeries(ma,true);

      int maHandle = iMA(pSymbol,ChartPeriodDefinition, NumberofDataPoints,0,MODE_EMA,PRICE_CLOSE);

      CopyBuffer(maHandle,0,0,5,ma);

      double MAValuea = ma[0];

      double MAValueb=ma[1];

      if(MAValuea>MAValueb)

         {

         Gradient = 1;

         } 

      if(MAValuea<MAValueb)

         {

         Gradient = -1;

         }       

     return(Gradient); 

  }
 
Andrew_m:

Hi All,

 I want to loop through a number of moving averages as part of my trading system.

...
Do not use ENUM timeframes for looping,
but use Value as on MT4.
For example PERIOD_D1 = 1440;

Then set and access timeframes as an Array.

// for example:
int TF[]={1440,240,60,30,15,5};

// and loop TF[]
for(int x=0; x<6; x++) Grad = MAGradient(_Symbol,TF[x],LoopDatapoints);



 

Forum on trading, automated trading systems and testing trading strategies


Hello,

Please use the SRC button when you post code. Thank you.


This time, I edited it for you.


 
Andrew_m:

Hi All,

 I want to loop through a number of moving averages as part of my trading system.

Input variables are as below

 

 My loop is per below

 input ENUM_TIMEFRAMES TimeFrame1 = PERIOD_D1; // First MA to use

input int Period1 =10; // Number of periods for first MA

input ENUM_TIMEFRAMES TimeFrame2 = PERIOD_D1; // Second MA to use

input int Period2 =10; // Number of periods for second MA

ENUM_TIMEFRAMES inputTF[NumberofMAtoUse];
int inputPeriod[NumberofMAtoUse];

OnInit()
{
 inputTF[0]=TimeFrame1;
 inputTF[1]=TimeFrame2;
 ...
 inputPeriod[0]=Period1;
 inputPeriod[1]=Period2;
 ...
}

...
         for(int count =0;count<NumberofMAtoUse; count++)

            {

            // Get Gradient for all MA's requested

             string LoopTimeFrame = "TimeFrame" + count;

            string LoopDatapoints = "Period" + count;

                        

            Grad = MAGradient(_Symbol, inputTF[count], inputPeriod[count] );
             ...
            }
 

Thanks Roberto

 

Sorry if this is a dumb question but i then get an error in my subsequent function which is expecting an ENUM_TIMEFRAMES input into the MA function. 

 

Any thoughts on what im doing wrong? 

 

double MAGradient(string pSymbol,int ChartPeriodDefinition,int NumberofDataPoints) 
  {     
      double ma[];
      double Gradient;
      ArraySetAsSeries(ma,true);
      int maHandle = iMA(pSymbol,ChartPeriodDefinition, NumberofDataPoints,0,MODE_EMA,PRICE_CLOSE);
      CopyBuffer(maHandle,0,0,5,ma);
      double MAValuea = ma[0];
      double MAValueb=ma[1];
      if(MAValuea>MAValueb)
         {
         Gradient = 1;
         } 
      if(MAValuea<MAValueb)
         {
         Gradient = -1;
         }       
     return(Gradient); 
  }
 
Andrew_m:

Thanks Roberto

 

Sorry if this is a dumb question but i then get an error in my subsequent function which is expecting an ENUM_TIMEFRAMES input into the MA function. 

 

Any thoughts on what im doing wrong? 

 

Please check whether maHandle is not INVALID_HANDLE.

and amount of CopyBuffer is not 0


 
Andrew_m:

Thanks Roberto

 

Sorry if this is a dumb question but i then get an error in my subsequent function which is expecting an ENUM_TIMEFRAMES input into the MA function. 

 

Any thoughts on what im doing wrong? 

 

 

While using Alain's code,

change

double MAGradient(string pSymbol,int ChartPeriodDefinition,int NumberofDataPoints)

back to

double MAGradient(string pSymbol,ENUM_TIMEFRAMES ChartPeriodDefinition,int NumberofDataPoints)
Reason: