Download MetaTrader 5
To add comments, please log in or register
Publish your article. Millions of traders are looking for good ideas!
Michael
496
Michael 2014.06.15 23:38 

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)
      {
         
      }
   }

}
whroeder1
13644
whroeder1 2014.06.16 16:17  
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;   
Michael
496
Michael 2014.06.19 05:20  

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.  :)

whroeder1
13644
whroeder1 2014.06.19 12:55  
nondisclosure: Now if I could just find a way to put a [SOLVED] on the title of this topic.  :)
Edit the original post.
Michael
496
Michael 2014.06.24 06:32  
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. 

Michael
496
Michael 2014.06.24 06:36  

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;
}
whroeder1
13644
whroeder1 2014.06.24 15:09  
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.
/
To add comments, please log in or register