Using regression line to open trade EA

 
Hi,
I am wondering if anyone can help me coding an EA using the GannSwings indicator so that it opens a trade after a regression line is over a certian amount of pips . I have attached the GannSwing indicator. I appreciate any help guys.

Thanks,
Gee

//+------------------------------------------------------------------+
//|                                                   GannSwings.mq4 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
#property indicator_buffers 6
#property indicator_color1 Yellow
#property indicator_color2 Yellow
#property indicator_color3 Blue
#property indicator_color4 Red
#property indicator_color5 Aqua
#property indicator_color6 HotPink
#define version "XV"

extern int kind=1;// òèï òåíäåíöèè, 1- ìàëàÿ, 2 - ïðîìåæóòî÷íàÿ, 3 - îñíîâíàÿ
extern bool ShowHiLo=false;
extern bool ShowOutSideBars=false;
extern bool MoveLastSwing=false; // åñëè ðàâíî true, òî êîí÷èê õâîñòà áóäåò ñäâèãàòüñÿ áëèæå ê íóëåâîìó áàðó äëÿ äâóõ îäèíàêîâûõ áàðîâ ïî High èëè Low
//---- buffers
double HighSwings[];
double LowSwings[];
double HighsBuffer[];
double LowsBuffer[];
double TrendBuffer[];
double UpCloseOutSideBuffers[];
double DownCloseOutSideBuffers[];
double SwingHigh,SwingLow;
int prevSwing,LastSwing,myBars,lowCounter,highCounter , cnt;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   IndicatorBuffers(7);

   SetIndexStyle(0,DRAW_ZIGZAG);
   SetIndexBuffer(0,HighSwings);
   SetIndexEmptyValue(0,0.0);
   SetIndexLabel(0,"HighSwing");

   SetIndexStyle(1,DRAW_ZIGZAG);
   SetIndexBuffer(1,LowSwings);
   SetIndexEmptyValue(1,0.0);
   SetIndexLabel(1,"LowSwing");

   SetIndexBuffer(2,HighsBuffer);
   SetIndexLabel(2,"HighActive");
   SetIndexEmptyValue(2,0.0);

   SetIndexBuffer(3,LowsBuffer);
   SetIndexLabel(3,"LowActive");
   SetIndexEmptyValue(3,0.0);

   if (ShowHiLo)
      {
      SetIndexStyle(2,DRAW_ARROW);
      SetIndexArrow(2,159);
      SetIndexStyle(3,DRAW_ARROW);
      SetIndexArrow(3,159);
      }
   else
      {
      SetIndexStyle(2,DRAW_NONE);
      SetIndexStyle(3,DRAW_NONE);
      }
         
   SetIndexBuffer(4,UpCloseOutSideBuffers);
   SetIndexStyle(4,DRAW_ARROW);
   SetIndexArrow(4,108);
   SetIndexLabel(4,"UpClose");
   SetIndexEmptyValue(4,0.0);

   SetIndexBuffer(5,DownCloseOutSideBuffers);
   SetIndexStyle(5,DRAW_ARROW);
   SetIndexArrow(5,108);
   SetIndexLabel(5,"DownClose");
   SetIndexEmptyValue(5,0.0);

   SetIndexBuffer(6,TrendBuffer);

   for (int i=Bars-1; i>=0; i--)
      {
      HighSwings[i]=0.0;
      LowSwings[i]=0.0;
      HighsBuffer[i]=0.0;
      LowsBuffer[i]=0.0;
      TrendBuffer[i]=0.0;
      UpCloseOutSideBuffers[i]=0.0;
      DownCloseOutSideBuffers[i]=0.0;
      }
   SwingHigh=0.0;
   SwingLow=0.0;
   prevSwing=0;
   LastSwing=0.0;
   myBars=0;
   lowCounter=0;
   highCounter=0;
   Comment("\n","GannSwings ",version," kind=",kind);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
   Comment("\n","\n","            ");
   
//----
   return(0);
  }

//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
void DownTrendOutSide()
  {
//---- 
 
//1
   if (Close[cnt]<=Open[cnt] && lowCounter==1)
      {
      SwingHigh=High[cnt];
      SwingLow=Low[cnt];
      HighSwings[cnt]=High[cnt];
      LowSwings[cnt]=Low[cnt];
      highCounter=0;
      lowCounter=0;
      LastSwing=cnt;
      }
//2
   if ( Close[cnt]>Open[cnt] && lowCounter==1)
      {
      LowSwings[LastSwing]=0.0;
      SwingLow=Low[cnt];
      SwingHigh=High[cnt];
      HighSwings[cnt]=High[cnt];
      LowSwings[cnt]=Low[cnt];
      highCounter=0;
      lowCounter=0;
      LastSwing=cnt;
      TrendBuffer[cnt]=1.0;
      //Print("breakout &#224;&#239;&#242;&#240;&#229;&#237;&#228; &#247;&#229;&#240;&#229;&#231; &#226;&#237;&#229;&#248;&#237;&#232;&#233; &#225;&#224;&#240;");
      }
//3
   if (Close[cnt]>Open[cnt] && highCounter==kind)
      {
      SwingLow=Low[cnt];
      SwingHigh=High[cnt];
      LowSwings[cnt]=Low[cnt];
      HighSwings[cnt]=High[cnt];
      highCounter=0;
      lowCounter=0;
      LastSwing=cnt;
      TrendBuffer[cnt]=1.0;
      //Print("breakout &#228;&#224;&#243;&#237;&#242;&#240;&#229;&#237;&#228; &#247;&#229;&#240;&#229;&#231; &#226;&#237;&#229;&#248;&#237;&#232;&#233; &#225;&#224;&#240;");
      }

//4
   if (Close[cnt]<=Open[cnt] && highCounter==kind)   
      {
      SwingLow=Low[cnt];
      SwingHigh=High[cnt];
      if (Low[cnt]<LowSwings[LastSwing]) 
         {
         LowSwings[LastSwing]=0.0;
         LowSwings[cnt]=Low[cnt];
         }
      HighSwings[cnt]=High[cnt];
      highCounter=0;
      lowCounter=0;
      LastSwing=cnt;
      //Print("&#208;&#224;&#231;&#226;&#238;&#240;&#238;&#242; &#228;&#224;&#243;&#237;&#242;&#240;&#229;&#237;&#228;&#224; &#247;&#229;&#240;&#229;&#231; &#226;&#237;&#229;&#248;&#237;&#232;&#233; &#225;&#224;&#240;");
      TrendBuffer[cnt]=1.0;
      }



//----
   return;
  }


//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
void UpTrendOutSide()
  {
//---- 
   if ( Close[cnt]>=Open[cnt] && highCounter==1)
   {
   SwingLow=Low[cnt];
   SwingHigh=High[cnt];
   LowSwings[cnt]=Low[cnt];
   HighSwings[cnt]=High[cnt];
   highCounter=0;
   lowCounter=0;
   LastSwing=cnt;
   //Print("&#207;&#240;&#238;&#228;&#238;&#235;&#230;&#229;&#237;&#232;&#229; &#224;&#239;&#242;&#240;&#229;&#237;&#228; &#247;&#229;&#240;&#229;&#231; &#226;&#237;&#229;&#248;&#237;&#232;&#233; &#225;&#224;&#240;");
   }

   if (Close[cnt]< Open[cnt] && highCounter==1)
   {
   HighSwings[LastSwing]=0.0;
   SwingLow=Low[cnt];
   SwingHigh=High[cnt];
   LowSwings[cnt]=Low[cnt];
   HighSwings[cnt]=High[cnt];
   highCounter=0;
   lowCounter=0;
   LastSwing=cnt;
   TrendBuffer[cnt]=-1.0;
   //Print("breakout &#224;&#239;&#242;&#240;&#229;&#237;&#228; &#247;&#229;&#240;&#229;&#231; &#226;&#237;&#229;&#248;&#237;&#232;&#233; &#225;&#224;&#240;");
   }

   if (Close[cnt]>= Open[cnt] && lowCounter==kind)
   {
   SwingLow=Low[cnt];
   SwingHigh=High[cnt];
   LowSwings[cnt]=Low[cnt];
   HighSwings[cnt]=High[cnt];
   highCounter=0;
   lowCounter=0;
   LastSwing=cnt;
   TrendBuffer[cnt]=-1.0;
   //Print("breakout &#224;&#239;&#242;&#240;&#229;&#237;&#228; &#247;&#229;&#240;&#229;&#231; &#226;&#237;&#229;&#248;&#237;&#232;&#233; &#225;&#224;&#240;");
   }

   // TrendBuffer[cnt+1]==1.0 &&  
   if (Close[cnt]<Open[cnt] && lowCounter==kind)   
   {
   SwingLow=Low[cnt];
   SwingHigh=High[cnt];
   if (High[cnt]>HighSwings[LastSwing]) 
      {
      HighSwings[LastSwing]=0.0;
      HighSwings[cnt]=High[cnt];
      }
   LowSwings[cnt]=Low[cnt];
   highCounter=0;
   lowCounter=0;
   LastSwing=cnt;
   //Print("&#208;&#224;&#231;&#226;&#238;&#240;&#238;&#242; &#224;&#239;&#242;&#240;&#229;&#237;&#228;&#224; &#247;&#229;&#240;&#229;&#231; &#226;&#237;&#229;&#248;&#237;&#232;&#233; &#225;&#224;&#240;");
TrendBuffer[cnt]=-1.0;
}
   
//----
   return;
  }

//+------------------------------------------------------------------+
//| OutSidePart function                                             |
//+------------------------------------------------------------------+
void OutSidePart()
  {
//---- 
   if (TrendBuffer[cnt+1]==1.0)
      {
      UpTrendOutSide();                  
      }//TrendBuffer[cnt+1]==1.0;
   else
      {// TrendBuffer[cnt+1]==-1.0
      DownTrendOutSide();
      }//TrendBuffer[cnt+1]==-1.0
         
//----
   return;
  }

//+------------------------------------------------------------------+
//| nonOutSidePart() function                                        |
//+------------------------------------------------------------------+
void nonOutSidePart()
  {
//---- 
   if (TrendBuffer[cnt+1]==-1.0)
   
      {
      if (lowCounter==1) 
         {
         if (LastSwing!=Bars-1) LowSwings[LastSwing]=0.0; // &#231;&#224;&#242;&#240;&#229;&#236; &#239;&#240;&#229;&#228;&#251;&#228;&#243;&#249;&#232;&#233; &#241;&#226;&#232;&#237;&#227;
         //Print("&#231;&#224;&#242;&#240;&#229;&#236; &#239;&#240;&#229;&#228;&#251;&#228;&#243;&#249;&#232;&#233; Down &#241;&#226;&#232;&#237;&#227;");
         LastSwing=cnt;
         SwingHigh=High[cnt];
         SwingLow=Low[cnt];
         //TrendBuffer[cnt]=-1.0;
         highCounter=0;
         lowCounter=0;
         LowSwings[cnt]=Low[cnt];
         HighsBuffer[cnt]=SwingHigh;
         //Print("&#239;&#240;&#238;&#228;&#238;&#235;&#230;&#229;&#237;&#232;&#229; &#228;&#224;&#243;&#237;&#242;&#240;&#229;&#237;&#228;&#224;");
         }
      else
         {
         if (highCounter==kind)
            {
            //if (LastSwing=!cnt) 
            LastSwing=cnt;
            SwingHigh=High[cnt];
            SwingLow=Low[cnt];
            TrendBuffer[cnt]=1.0;
            highCounter=0;
            lowCounter=0;
            HighSwings[cnt]=High[cnt];
            LowsBuffer[cnt]=SwingLow;
            //Print("&#240;&#224;&#231;&#226;&#238;&#240;&#238;&#242; &#228;&#224;&#243;&#237;&#242;&#240;&#229;&#237;&#228;&#224;");
            }
         else
            {
            if (Low[cnt]==SwingLow && MoveLastSwing)
               {
               LowSwings[LastSwing]=0.0;
               LastSwing=cnt;
               LowSwings[cnt]=Low[cnt];
//                     continue;
               }
            }
         
         }
      
      }//   TrendBuffer[cnt+1]==-1.0
      
   else     //   TrendBuffer[cnt+1]==1.0          
      {             
      if (highCounter==1) 
         {
         if (LastSwing!=Bars-1) HighSwings[LastSwing]=0.0; // &#231;&#224;&#242;&#240;&#229;&#236; &#239;&#240;&#229;&#228;&#251;&#228;&#243;&#249;&#232;&#233; &#241;&#226;&#232;&#237;&#227;
         LastSwing=cnt;
         SwingHigh=High[cnt];
         SwingLow=Low[cnt];
         highCounter=0;
         lowCounter=0;
         HighSwings[cnt]=High[cnt];
         HighsBuffer[cnt]=SwingHigh;
         LowsBuffer[cnt]=SwingLow;
         //Print("&#239;&#240;&#238;&#228;&#238;&#235;&#230;&#229;&#237;&#232;&#229; &#224;&#239;&#242;&#240;&#229;&#237;&#228;&#224;");
         }
      else
         {
         if (lowCounter==kind)
            {
            LastSwing=cnt;
            SwingHigh=High[cnt];
            SwingLow=Low[cnt];
            TrendBuffer[cnt]=-1.0;
            highCounter=0;
            lowCounter=0;
            LowSwings[cnt]=Low[cnt];
            HighsBuffer[cnt]=SwingHigh;
            LowsBuffer[cnt]=SwingLow;
            //Print("&#240;&#224;&#231;&#226;&#238;&#240;&#238;&#242; &#224;&#239;&#242;&#240;&#229;&#237;&#228;&#224;");
            }
         else
            {
            if (High[cnt]==SwingHigh && MoveLastSwing)
               {
               HighSwings[LastSwing]=0.0;
               LastSwing=cnt;
               HighSwings[cnt]=High[cnt];
               }
            }   

         }
      } //TrendBuffer[cnt+1]==1.0           
   


//----
   return;
  }

//+------------------------------------------------------------------+
//| return shift of last swing                                       |
//+------------------------------------------------------------------+
int GetLastSwing()
  {
//---- 
  int point=1;
  while (HighSwings[point]==0.0 && LowSwings[point]==0.0 && point<Bars) point++;
    
//----
   return(point);
  }  
//+------------------------------------------------------------------+
//| Check prev swing                                                 |
//+------------------------------------------------------------------+
void SetPrevSwing()
  {
//---- 
   int LastPoint;
   LastPoint=GetLastSwing();
   HighSwings[0]=0.0;
   SwingHigh=High[LastPoint];
   SwingLow=Low[LastPoint];
   LastSwing=LastPoint;
//----
   return;
  }



//+------------------------------------------------------------------+
//| check Out side bar                                               |
//+------------------------------------------------------------------+
bool isOutSideSwingBar(int shift)
  {
   bool res=false;
//---- 
   res=((High[shift]>SwingHigh)&&(Low[shift]<SwingLow));
   if (res && ShowOutSideBars) 
      {
      if (Close[shift]>Open[shift]) UpCloseOutSideBuffers[shift]=(High[shift]+Low[shift])/2.0;
      else DownCloseOutSideBuffers[shift]=(High[shift]+Low[shift])/2.0;
      }
//----
   return(res);
  }
//+------------------------------------------------------------------+
//| Check new low                                                   |
//+------------------------------------------------------------------+
void SetSwings(int shift)
  {
//---- 

//----
   return;
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
   int limit;
   bool nonOutSide;
   if (counted_bars==0) 
      {
      limit=Bars-1;
      LowsBuffer[limit]=Low[limit];
      LowSwings[limit]=Low[limit];
      SwingHigh=High[limit];
      SwingLow=Low[limit];
      LastSwing=limit;
      TrendBuffer[limit]=-1.0;
      }
   if (counted_bars>0) limit=Bars-counted_bars;  
   if ( (counted_bars<0)|| ((counted_bars>0)&&(Bars-myBars>1)) ) init();
   limit--;
//---- 
   for ( cnt=limit;cnt>0;cnt--)
      {
      //if (cnt==0) SetPrevSwing();
      nonOutSide=!isOutSideSwingBar(cnt);

      if (High[cnt]>SwingHigh) {highCounter++;SwingHigh=High[cnt];HighsBuffer[cnt]=SwingHigh;}
      if (Low[cnt]<SwingLow) {lowCounter++;SwingLow=Low[cnt];LowsBuffer[cnt]=SwingLow;}


      TrendBuffer[cnt]=TrendBuffer[cnt+1];// &#229;&#241;&#235;&#232; &#237;&#229; &#225;&#243;&#228;&#243;&#242; &#232;&#231;&#236;&#229;&#237;&#229;&#237;&#232;&#233; - &#242;&#240;&#229;&#237;&#228; &#241;&#238;&#245;&#240;&#224;&#237;&#232;&#242;&#241;&#255;
      if (nonOutSide)
         {
         nonOutSidePart(); // &#242;&#243;&#242; &#236;&#251; &#240;&#224;&#225;&#238;&#242;&#224;&#229;&#236; &#241; &#238;&#225;&#251;&#247;&#237;&#251;&#236;&#232; &#225;&#224;&#240;&#224;&#236;&#232;          
         }// if (nonOutSide)         
      else // outSideBars
         {
         OutSidePart();
         }// isOutSideSwingBar(cnt)          
      } // &#202;&#206;&#205;&#197;&#214; &#214;&#200;&#202;&#203;&#192;
   myBars=Bars;   
//----
   return(0);
  }
//+------------------------------------------------------------------+
Reason: