//+--------------------------------------------------------------------------+
//|                                                    FractalTakeOut_v2.mq4 |
//|                                  Copyright © Mar/30/2007, C.O.P.Y.C.A.T. | Copy Over Paste Yippee Code Assembly Technician
//| http://finance.groups.yahoo.com/group/MetaTrader_Experts_and_Indicators/ |
//+--------------------------------------------------------------------------+

//Built for Peter Henry , as described in the thread titled "Simple Fractal trading system" @ http://finance.groups.yahoo.com/group/MetaTrader_Experts_and_Indicators/

#property copyright "Copyright © Mar/30/2007, C.O.P.Y.C.A.T."
#property link "http://finance.groups.yahoo.com/group/MetaTrader_Experts_and_Indicators/"

extern int OpenTradeBuffer = 1;
extern int CloseTradeBuffer = 1;
extern double StopLoss$ = 10.00; // == $0.00 , Not Pips
extern int MagicNumber = 1913;

double HighPrice = 0;
bool HighBreaker = false;

double LowPrice = 0;
bool LowBreaker = false;

int i;
int ticket = 0;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   
   if ( !IsDemo() ) return(0);
   
   double Ma3High = iMA(Symbol(),0,3,0,MODE_SMA,PRICE_HIGH,0);
   double Ma3Low  = iMA(Symbol(),0,3,0,MODE_SMA,PRICE_LOW ,0);
   
   double LastVisableHighFractal = 0, LastVisableLowFractal = 0;
   

   
    // start looking from bar 3 then earlier bars
   
   for (int b=3 ; LastVisableHighFractal  == 0 && b<=Bars ; b++)
   {
     double dblUpperFractal = iFractals(Symbol( ),0,MODE_UPPER, b);
     if (dblUpperFractal > 0.0) LastVisableHighFractal = dblUpperFractal;
   }
   for (b=3 ; LastVisableLowFractal == 0 && b<=Bars ; b++)
   {
     double dblLowerFractal = iFractals(Symbol( ),0,MODE_LOWER, b);
     if (dblLowerFractal > 0.0) LastVisableLowFractal = dblLowerFractal;
   }
   ticket = 0;
   
  
   for ( i = 0; i < OrdersTotal(); i++ )
    {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
     if ( OrderSymbol() == Symbol() && OrderType() == OP_BUY && OrderMagicNumber() == MagicNumber )
      {
       if ( Close[0] >= Ma3High+(CloseTradeBuffer*Point) || OrderProfit() <= (-StopLoss$) )
        {
          OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,White);
        }
      }
     if ( OrderSymbol() == Symbol() && OrderType() == OP_SELL && OrderMagicNumber() == MagicNumber )
      {
       if ( Close[0] <= Ma3Low -(CloseTradeBuffer*Point) || OrderProfit() <= (-StopLoss$) )
        {
          OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,White);
        }
      }
    }
   
   if ( !HighBreaker && High[0] > LastVisableHighFractal ) { HighPrice = LastVisableHighFractal; HighBreaker = true; LowBreaker = false; }
   
   if ( !LowBreaker && Low[0] < LastVisableLowFractal ) { LowPrice = LastVisableLowFractal; HighBreaker = false; LowBreaker = true; }
   
   if ( HighPrice == 0 ) HighBreaker = false;
   if ( LowPrice  == 0 ) LowBreaker  = false;
   
   if ( OrdersTotal() <= 0)
    {
     if ( HighBreaker && Close[0] <= Ma3Low -(OpenTradeBuffer*Point) )
      {
        ticket = OrderSend(Symbol(),OP_BUY,0.10,Ask,0,0,0,"FractalTakeOut_v2"+MagicNumber,MagicNumber,0,Aqua);
       if( ticket < 1 )
        {
          Print("OrderSend failed with error #",GetLastError());
        }
      }
     if ( LowBreaker && Close[0] >= Ma3High +(OpenTradeBuffer*Point) )
      {
        ticket = OrderSend(Symbol(),OP_SELL,0.10,Bid,0,0,0,"FractalTakeOut_v2"+MagicNumber,MagicNumber,0,DeepPink);
       if( ticket < 1 )
        {
          Print("OrderSend failed with error #",GetLastError());
        }
      }
    }
   
   Comment("\n","  Last High Fractal TakeOut Price : ",HighPrice,
           "\n","  Buy Mode : ",HighBreaker,
           "\n",
           "\n","  Last Low Fractal TakeOut Price : ",LowPrice,
           "\n","  Sell Mode : ",LowBreaker);
   
//----
   return(0);
  }
//+------------------------------------------------------------------+