new variable built from 2

 

Hi,

I want to simplify my code.

What do I want?

I want to get 2 variables for the MA20 for each timeframe:

e.g.: MATrendUp_1, MATrendUp_5 .... MATrendUp_43200 (and the same for MATrendDown)

I could copy the same code for each Timframe and Iam done. But what I think of using arrays instead.

From the code below I get several MATrendUp and MATrendDown. But I can't differ between the Timeframes.

So can I construct a variable like [MATrendUp]+[TF] and return it? e.g. MATrendUp_1


Help is appreciated. :-)

void MA20_Array()
{
/////////////////////////////////////////////////////////////////////
string Array_MA20[9]={1, 5, 15, 30, 60, 240, 1440, 10080, 43200};
bool MATrendUp,MATrendDown;
int TF;
double MA_0, MA_1, MA_2, MA_3;
/////////////////////////////////////////////////////////////////////
 
for (int i=0; i <= ArraySize(Array_MA20)-1; i++)
{
   MATrendUp = false;
   MATrendDown = false;
   TF=Array_MA20[i];

   MA_0 = iMA(NULL,TF,20,0,MODE_SMA,PRICE_CLOSE,0);
   MA_1 = iMA(NULL,TF,20,0,MODE_SMA,PRICE_CLOSE,1);
   MA_2 = iMA(NULL,TF,20,0,MODE_SMA,PRICE_CLOSE,2);
   MA_3 = iMA(NULL,TF,20,0,MODE_SMA,PRICE_CLOSE,3);

   if (MA_0 > MA_1 && MA_1 > MA_2 && MA_2 > MA_3)
      MATrendUp = true;

   if (MA_0 < MA_1 && MA_1 < MA_2 && MA_2 < MA_3)
      MATrendDown = true;
} 

LEO.org
  • dict.leo.org
Übersetzung für 'appreciated' in LEOs Englisch ⇔ Deutsch Wörterbuch. Mit Flexionstabellen, Aussprache und vielem mehr.
 

Here are some ideas on how to simplify... not sure if it's what you were asking, but FWIW it's how I would implement it... 

int OnInit()
  {
//---
   CommentTrends();
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   CommentTrends();
  }
//+------------------------------------------------------------------+

enum TREND_DIRECTION
{
   TREND_UP,
   TREND_DN,
   TREND_NA
};

TREND_DIRECTION Trend(const ENUM_TIMEFRAMES timeframe)
{
   const ENUM_TIMEFRAMES p = timeframe;
   if(MA(p,0) > MA(p,1) && MA(p,1) > MA(p,2)&& MA(p,2)> MA(p,3))
      return TREND_UP;
   if(MA(p,0) < MA(p,1) && MA(p,1) < MA(p,2)&& MA(p,2)< MA(p,3))
      return TREND_DN;
   return TREND_NA;
}

double MA(const ENUM_TIMEFRAMES timeframe,const int index)
{
   return iMA(NULL,timeframe,20,0,MODE_SMA,PRICE_CLOSE,index);
}

string TrendToString(const TREND_DIRECTION trend)
{
   if(trend==TREND_UP)return "Trending up";
   if(trend==TREND_DN)return "Trending down";
   return "No trend identified";
}

void CommentTrends()
{
   const static ENUM_TIMEFRAMES tfs[] = {PERIOD_M1,PERIOD_M5,PERIOD_M15,PERIOD_M30,PERIOD_H1};
   string comm="";
   for(int i=0;i<ArraySize(tfs);i++)
      comm+= EnumToString(tfs[i])+" = "+TrendToString(Trend(tfs[i]))+"\n";
   Comment(comm);
}
 

Hi,

thanks to this great code. Although I don't unterstand noch everything, it works.

Had to test it with Print(comm);

 Comment(comm); --> Comment didn't show up.


Not I will work thourgh the code to understand what you've done :-)

 
1stDiablo:

Hi,

thanks to this great code. Although I don't unterstand noch everything, it works.

Had to test it with Print(comm);


Not I will work thourgh the code to understand what you've done :-)


Most of the magic happens in the trend function... from it you can create other fuctions that put it to use... 

TREND_DIRECTION AllPeriodsTrend()
{
   const static ENUM_TIMEFRAMES tfs[] = {PERIOD_M1,PERIOD_M5,PERIOD_M15,PERIOD_M30,PERIOD_H1};
   TREND_DIRECTION trend = Trend(tfs[0]);
   for(int i=1;i<ArraySize(tfs);i++)
      if(Trend(tfs[i])!=trend)
         return TREND_NA;
   return trend;
}