# Looking for High/Low based on Moving Averages.

718

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.

```void OnStart()
{
double UpperLevel=0, LowerLevel=0, FastMA=0, SlowMA=0;
int CurrentBar=1, CurrentDirection=0, LastDirection=0;
bool AllFound=false, StillOnInitialDirection=true, FoundLong=false, FoundShort=false;
while(!AllFound)
{
SlowMA=iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,CurrentBar);
FastMA=iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,CurrentBar);
if(CurrentBar==1)                //set the inital direction
{
if(FastMA>SlowMA)
{
CurrentDirection=1;
LastDirection=1;
}
if(FastMA<SlowMA)
{
CurrentDirection=2;
LastDirection=2;
}
}
if(CurrentBar>1)                 //Work on current direction
{
if(FastMA>SlowMA)
{
CurrentDirection=1;
}
if(FastMA<SlowMA)
{
CurrentDirection=2;
}
}
if(StillOnInitialDirection)      //See if we're out of the current direction
{
if(CurrentDirection!=LastDirection)
{
StillOnInitialDirection=false;
}
}
if(!StillOnInitialDirection)
{

}
}

}
```
15904

 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.
1. ```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++;
}

```

2. 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;   ```
718

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(" ");
}```

Now if I could just find a way to put a [SOLVED] on the title of this topic.  :)

15904

 nondisclosure: Now if I could just find a way to put a [SOLVED] on the title of this topic.  :) Edit the original post.
718

WHRoeder:
 nondisclosure: Now if I could just find a way to put a [SOLVED] on the title of this topic.  :) Edit the original post.

I'd love to, but the only option I get is 'reply'.

Ok, so on this post, I get an edit.  But on the other ones, I just get 'reply'.  Weird.

718

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;
}```
15904

 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.