/************************************************************ *
 *
 *   Fractal Channel EA
 *
 *   (C) _mikkom, http://www.currencylabs.com             
 *
 *************************************************************/
#property copyright "(C) 2009, _mikkom"
#property link      "http://www.currencylabs.com" 

#include <mikko_lib.mqh>

#define MAX_ORDERS 10000
#define MAGIC_COUNT MAX_ORDERS

extern double lots = 1;

int orders = 0;

void init() {
   Print("** INIT **");
}

void start() {

   if(IsTradeAllowed()==false)
       return;

   if(Volume[0] > 1) {
      return;
   }              
   
   double max = iCustom(Symbol(),0,"mikko_fractal_channels",0,0,0);
   double min = iCustom(Symbol(),0,"mikko_fractal_channels",0,1,0);
   double max1 = iCustom(Symbol(),0,"mikko_fractal_channels",0,0,1);
   double min1 = iCustom(Symbol(),0,"mikko_fractal_channels",0,1,1);
        
   orders = openPos();

   double sl, tp;      
   double stopLevel = MarketInfo(Symbol(),MODE_STOPLEVEL)*Point;   

   if(openPos() > 0)
      closeAll();
     
   double l, open;   

   int type;

      if(max != max1 && max > Ask) {
           deleteAllPending(OP_BUYSTOP);
           l = lots;
           open = max;
           type = OP_BUYSTOP;
           
           OrderSend(Symbol(), type, NormalizeDouble(l, Digits), 
                     NormalizeDouble(open, Digits), 2,
                     NormalizeDouble(sl, Digits),  
                     0, "",    
                     magic(1), 0);
         
         }
         
         if(min != min1 && min < Bid) {         

           deleteAllPending(OP_SELLSTOP);

           sl = 0;           
           open = min;  
           l = lots;           
          
           type = OP_SELLSTOP;
           if(open == Bid)
              type = OP_SELL;
        
          //  l = 0.1;
                       
           OrderSend(Symbol(), type, NormalizeDouble(l, Digits), 
                     NormalizeDouble(open, Digits), 2,
                     NormalizeDouble(sl, Digits),  
                     0, "",    
                     magic(1), 0);
         
      }
   
  
}

