nondisclosure: I'm looking for a way to find the last high and last low based on moving averages.
| You don't state a problem other than posting irrelevant code. |
int iSlowHigh = CurrentBar; double SlowHigh; while(true){ SlowHigh = iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,iSlowHigh); if(iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,iSlowHigh+1) < SlowHigh) break; iSlowHigh++; } int iSlowLow = iSlowHigh + 1; double SlowLow; while(true){ SlowLow = iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,iSlowLow); if(iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,iSlowLow+1) > iSlowLow) break; iSlowLow++; }
Direction = 1 is meaningless. Self documenting code if(FastMA>SlowMA) { CurrentDirection=1; LastDirection=1;
enum Direction{ Up, Down }; if(FastMA>SlowMA) { CurrentDirection=Up; LastDirection=Up;
Thanks WHRoeder.
Here's my working code
void OnStart() { double UpperLevel=0, LowerLevel=100000, FastMACurrent=0, SlowMACurrent=0, FastMALast=0, SlowMALast=0, DiffCurrent=0, DiffLast=0; int CurrentBar=2, Cross=0; string UpperTime, LowerTime; bool AllFound=false; while(Cross<3) { SlowMALast=iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,CurrentBar-1); FastMALast=iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,CurrentBar-1); SlowMACurrent=iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,CurrentBar); FastMACurrent=iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,CurrentBar); DiffLast=FastMALast-SlowMALast; DiffCurrent=FastMACurrent-SlowMACurrent; //Loop through and look for Cross value. if((DiffCurrent>0 && DiffLast<0) || (DiffCurrent<0 && DiffLast>0)) //There's a cross { Cross++; } if(DiffCurrent>0 && DiffLast>0 && Cross>0) //Going Long { UpperLevel=MathMax(High[CurrentBar],UpperLevel); if(UpperLevel==High[CurrentBar]) {UpperTime=TimeToString(Time[CurrentBar],TIME_DATE|TIME_MINUTES);} } if(DiffCurrent<0 && DiffLast<0 && Cross>0) //Going Short { LowerLevel=MathMin(Low[CurrentBar],LowerLevel); if(LowerLevel==Low[CurrentBar]) {LowerTime=TimeToString(Time[CurrentBar],TIME_DATE|TIME_MINUTES);} } Comment("Bar:",CurrentBar); CurrentBar++; } Comment(" "); Alert("LowerLevel:",LowerLevel,". LowerTime:",LowerTime,"\n","UpperLevel:",UpperLevel,". UpperTime:",UpperTime); }
Now if I could just find a way to put a [SOLVED] on the title of this topic. :)
Well, I hope no one was using this code live, it had problems.
Here's the "fixed" code:
void OnStart() { double UpperLevel=0, LowerLevel=100000, FastMACurrent=0, SlowMACurrent=0, FastMALast=0, SlowMALast=0, DiffCurrent=0, DiffLast=0, Ceiling=0, Floor=0; int CurrentBar=2, Cross=0; bool AllFound=false; while(Cross<3) { SlowMALast=iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,CurrentBar-1); FastMALast=iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,CurrentBar-1); SlowMACurrent=iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,CurrentBar); FastMACurrent=iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,CurrentBar); DiffLast=FastMALast-SlowMALast; DiffCurrent=FastMACurrent-SlowMACurrent; if((DiffCurrent>0 && DiffLast<0) || (DiffCurrent<0 && DiffLast>0)) //There's a cross { Cross++; } if(DiffCurrent>0 && (Cross==1 || Cross==2)) //Going Long { UpperLevel=MathMax(High[CurrentBar],UpperLevel); } if(DiffCurrent<0 && (Cross==1 || Cross==2)) //Going Short { LowerLevel=MathMin(Low[CurrentBar],LowerLevel); } CurrentBar++; } Ceiling=UpperLevel; Floor=LowerLevel; }
nondisclosure: I'd love to, but the only option I get is 'reply'. Ok, so on this post, I get an edit.
| Can edit for only several days. |
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
I'm looking for a way to find the last high and last low based on moving averages. The arrows in the attached image show what I'm looking for.
I don't use the current moving average trend. I look for the last swings. I've drawn the lines that are the last 2 'swings' in the High and Low.
I've started the code (but I think my stroke is acting up). And I'm getting blanks on moving any further.
I'm not interested in drawing the lines. Just getting the values.