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; }
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
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. :-)