#include <stdlib.mqh>
//+------------------------------------------------------------------+
//|                                                             .mq4 |
//|                                                                  |
//|                                                                  |
//|                                        Converted by Mql2Mq4 v2.0 |
//|                                            http://yousky.free.fr |
//|                                  Copyright (c) 2006, Yousky Soft |
//+------------------------------------------------------------------+

#property copyright " Copyright © 2004, MVCM"
#property link      " http://www.brilliantfunds.com"

#property indicator_chart_window
#property indicator_color1 White
#property indicator_buffers 1

//+------------------------------------------------------------------+
//| Common External variables                                        |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| External variables                                               |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| Special Convertion Functions                                     |
//+------------------------------------------------------------------+

int LastTradeTime;
double ExtHistoBuffer[];
double ExtHistoBuffer2[];

void DelObject(string name, datetime time, double price, datetime time_precision, double price_precision)
{
  ObjectDelete(name);
}

void SetLoopCount(int loops)
{
}

void SetIndexValue(int shift, double value)
{
  ExtHistoBuffer[shift] = value;
}

void SetIndexValue2(int shift, double value)
{
  ExtHistoBuffer2[shift] = value;
}

double GetIndexValue(int shift)
{
  return(ExtHistoBuffer[shift]);
}

//+------------------------------------------------------------------+
//| End                                                              |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| Initialization                                                   |
//+------------------------------------------------------------------+

int init()
{
   SetIndexStyle(0, DRAW_LINE, STYLE_SOLID);
   SetIndexBuffer(0, ExtHistoBuffer);
   SetIndexStyle(1, DRAW_ARROW, STYLE_SOLID);
   SetIndexBuffer(1, ExtHistoBuffer2);
   return(0);
}
int start()
{
//+------------------------------------------------------------------+
//| Local variables                                                  |
//+------------------------------------------------------------------+
int shift = 0;
int i = 0;
double slope = 0;
int refbar = 0;
double refhi = 0;
double reflo = 0;
double cnt = 0;
double cap = 1;
double prev = 0;
double pr_bar = 0;
double up_cnt = 0;
double dn_cnt = 0;
double pen_bar = 0;
double pen_val = 0;
double prev_valley = 0;
double prev_peak = 0;
double start_mom = 0;
double crt_peak = 0;
double crt_valley = 0;
double tmp = 0;
double prev_mom = 0;
double prev_val = 0;
bool can = false;

                                 /*[[
	Name := GannSwingOwn
	Author := Copyright © 2004, MVCM
	Link := http://www.brilliantfunds.com
	Separate Window := No
	First Color := White
	First Draw Type := Line
	First Symbol := 217
	Use Second Data := No
	Second Color := Black
	Second Draw Type := Symbol
	Second Symbol := 218
]]*/

 //the slope




 //the counters for up/down lines






refbar=Bars-2;
refhi=High[refbar];
reflo=Low[refbar];

//first clea up chart if necessary 
if( GlobalVariableCheck("UpCnt") ) 
   { 
        tmp=GlobalVariableGet("UpCnt");
        for(i=0;i<=tmp;i++){ 
                 DelObject("Green"+i,0,0,0,0);
            } 
            
        tmp=GlobalVariableGet("DnCnt");
        for(i=0;i<=tmp;i++){ 
                 DelObject("Red"+i,0,0,0,0);
            } 
   } 


SetLoopCount(0);
// loop from first bar to current bar (with shift=0)
for(shift =Bars-3;shift >=0 ;shift --){ 
  SetIndexValue2(shift,0);
  refbar=shift+2;
    //Bearish reversal 
    if( Low[shift]<Low[shift+1] && Low[shift+1]<Low[shift+2] ) 
        {            
          if( slope != 2 ) //if bullish and swing goes down
              { 
                   pr_bar=-1;
                   prev=0;                   
                   slope=2;
                   reflo=Low[shift];                                             
                   SetIndexValue(shift,reflo);                   
                   if( shift != pr_bar ) 
                   { 
                       pr_bar=shift;                                                     
                       prev=reflo;
                   } 
               //  continue;
              } 
        }    
    //Consecutive lows    
      if( slope != 1 && Low[shift]<=reflo ) //if bearish and we get a new low
                 { 
                      reflo=Low[shift];                            
                      if( shift != pr_bar && pr_bar != -1 && reflo<=prev ) 
                         SetIndexValue(pr_bar,0);                      
                         
                      SetIndexValue(shift,reflo);                                                                     
                      if( shift != pr_bar ) 
                   		{ 
                       		pr_bar=shift;
                       		prev=reflo;
                   		} 
                      continue;
                 }                                                                     
     
      //Bullish reversal of swing line                              
     if( High[shift]>High[shift+1] && High[shift+1]>High[shift+2] ) 
     { 
     if( slope != 1 ) //if bearish and trend goes up
              { 
                   pr_bar=-1;
                   prev=0;
                   slope=1;
                   refhi=High[shift];                                                                               
                   SetIndexValue(shift,refhi);                                                    
                   if( shift != pr_bar ) 
                   { 
                       pr_bar=shift;                                                     
                       prev=refhi;
                   } 
                //   continue;
              } 
     }     
         	
     //if bullish and we get a new high   
      if( slope != 2 && High[shift]>=refhi ) 
            //high
              { 
                   refhi=High[shift];
                   if( shift != pr_bar && pr_bar != -1 && refhi>=prev ) 
                         SetIndexValue(pr_bar,0);                      
                   SetIndexValue(shift,refhi);                                                         
                   if( shift != pr_bar ) 
                   { 
                       pr_bar=shift;                                                     
                       prev=refhi;
                   } 
                  continue;
              }                               
                     
} 


shift=Bars-1;
tmp=0;
crt_peak=0;
crt_valley=0;
while( tmp == 0
  ) {
      tmp=GetIndexValue(shift);
      shift--;
  } 
if( tmp == High[shift+1] ) crt_peak=tmp
   ; else crt_valley=tmp;

tmp=0;
while( tmp == 0 
  ) {
      tmp=GetIndexValue(shift);
      shift--;
  } 
if( tmp == Low[shift+1] ) crt_valley=tmp ; else crt_peak=tmp;  return(0);
}