//+------------------------------------------------------------------+
//| 7 Bar Extension Fade.mq4
//| Copyright © 2007, fxid10t@yahoo.com
//| http://www.fxstreet.com/education/
//+------------------------------------------------------------------+

/*Trading Strategy...
Seven Bar Extension Fade  Statistical Significance

Trading Rules

 Look for 7 consecutive bars of weakness (where each bars
  close is below the prior)
 Buy at the open of the next bar
 Place stop at the low of the seventh bar minus 10 pips
 First target is amount risked, move stop on remaining half to
  breakeven
 Second target is three times the amount risked

**Reverse for Short  */

#property copyright "Copyright © 2007, fxid10t@yahoo.com"
#property link      "http://www.fxstreet.com/education/"
#include <stdlib.mqh>

extern double Lots=0.1;
extern int    TradingPeriod=60;

string pairs[] = { "EURUSDm","USDJPYm","GBPUSDm","USDCHFm","EURCHFm","AUDUSDm","USDCADm",
                   "NZDUSDm","EURGBPm","EURJPYm","GBPJPYm","CHFJPYm","GBPCHFm","EURAUDm",
                   "EURCADm","AUDCADm","AUDJPYm","NZDJPYm","AUDNZDm" };
int periods[] = { 1,5,15,30,60,240,1440,10080,43200 };

string   TradeSymbol;
int      TradePeriod;
int      Pair = -1;
int      PPeriod = -1;
int      b1,b2,s1,s2;
double   bSL1,bSL2,sSL1,sSL2,btp1,btp2,stp1,stp2;

int init(){
   if ( IsTesting() ) { if ( ArrayResize(pairs,1) != 0 )  pairs[0] = Symbol();   } 
   if ( IsTesting() ) { if ( ArrayResize(periods,1) != 0 )  periods[0] = Period(); }
return(0);  }

int deinit(){return(0);}

int start(){
//Select Pair from Array
Pair = (Pair+1) % ArraySize(pairs);
TradeSymbol = pairs[Pair];
   
//Pair Chart Period Setup check loop
for(int p=0;p<9;p++) {
      PPeriod = (PPeriod+1) % ArraySize(periods);
      TradePeriod = periods[PPeriod];


   int cnt, ticket,total=OrdersTotal();
   if(TotalTradesThisSymbol(TradeSymbol)==0) {  b1=0;b2=0;s1=0;s2=0;   }
   if(TotalTradesThisSymbol(TradeSymbol)>0)  {
      for(cnt=0;cnt<total;cnt++) {
         OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
         if(OrderSymbol()==TradeSymbol) {
         if(OrderMagicNumber()==21)  {b1=OrderTicket(); }
         if(OrderMagicNumber()==41)  {b2=OrderTicket(); }
         if(OrderMagicNumber()==11)  {s1=OrderTicket(); }
         if(OrderMagicNumber()==31)  {s2=OrderTicket(); }   }  }  }


//buy setup   
   if(   iClose(TradeSymbol,TradePeriod,1)<iClose(TradeSymbol,TradePeriod,2) &&
         iClose(TradeSymbol,TradePeriod,2)<iClose(TradeSymbol,TradePeriod,3) &&
         iClose(TradeSymbol,TradePeriod,3)<iClose(TradeSymbol,TradePeriod,4) &&
         iClose(TradeSymbol,TradePeriod,4)<iClose(TradeSymbol,TradePeriod,5) &&
         iClose(TradeSymbol,TradePeriod,5)<iClose(TradeSymbol,TradePeriod,6) &&
         iClose(TradeSymbol,TradePeriod,6)<iClose(TradeSymbol,TradePeriod,7) )  {
//buy execution        
      if(b1==0)   {
         bSL1=iLow(TradeSymbol,TradePeriod,1)-(Ask-Bid)-(10*Point);  
         btp1=Ask+(Ask-(iLow(TradeSymbol,TradePeriod,1)-(Ask-Bid)-(10*Point)));
         ticket=OrderSend(TradeSymbol,
                          OP_BUY,
                          Lots,
                          Ask,
                          0,//slippage
                          bSL1,
                          btp1,
                          TradePeriod+" 7 BEF 1st Long",
                          21,
                          0,//timedelete(),
                          Aqua);
                          if(ticket>0)   {
                              if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
                                    {  b1=ticket;  Print(ticket); }
                                 else Print("Error Opening BuyStop Order: ",GetLastError());
                                 return(0);  }  }
      if(b2==0)   {
         bSL2=iLow(TradeSymbol,TradePeriod,1)-(Ask-Bid)-(10*Point);
         btp2=Ask + NormalizeDouble( ((Ask-(iLow(TradeSymbol,TradePeriod,1)-(Ask-Bid)-(10*Point))*3)) , Digits);
         ticket=OrderSend(TradeSymbol,
                          OP_BUY,
                          Lots,
                          Ask,
                          0,//slippage
                          bSL2,
                          btp2,
                          TradePeriod+" 7 BEF 2nd Long",
                          41,
                          0,//timedelete(),
                          Aqua);
                          if(ticket>0)   {
                              if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
                                    {   b2=ticket; Print(ticket); }
                                 else Print("Error Opening BuyStop Order: ",GetLastError());
                                 return(0);  }  }  }
//Sell Setup
   if(   iClose(TradeSymbol,TradePeriod,1)>iClose(TradeSymbol,TradePeriod,2) &&
         iClose(TradeSymbol,TradePeriod,2)>iClose(TradeSymbol,TradePeriod,3) &&
         iClose(TradeSymbol,TradePeriod,3)>iClose(TradeSymbol,TradePeriod,4) &&
         iClose(TradeSymbol,TradePeriod,4)>iClose(TradeSymbol,TradePeriod,5) &&
         iClose(TradeSymbol,TradePeriod,5)>iClose(TradeSymbol,TradePeriod,6) &&
         iClose(TradeSymbol,TradePeriod,6)>iClose(TradeSymbol,TradePeriod,7) )  {
//Sell Execution
      if(s1==0)   {
         sSL1=iHigh(TradeSymbol,TradePeriod,1)+(Ask-Bid)+(10*Point);
         stp1=Bid-(iHigh(TradeSymbol,TradePeriod,1)+(Ask-Bid)+(10*Point)-Bid);
         ticket=OrderSend(TradeSymbol,
                          OP_SELL,
                          Lots,
                          Bid,
                          0,
                          sSL1,
                          stp1,
                          TradePeriod+" 7 BEF 1st Short",
                          11,
                          0,//timedelete(),
                          HotPink);
                          if(ticket>0)   {
                             if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
                                {  s1=ticket;  Print(ticket); }
                                   else Print("Error Opening SellStop Order: ",GetLastError());
                                   return(0);  }  }
      if(s2==0)   {
         sSL2=iHigh(TradeSymbol,TradePeriod,1)+(Ask-Bid)+(10*Point);
         stp2=Bid-NormalizeDouble( (iHigh(TradeSymbol,TradePeriod,1)+(Ask-Bid)+(10*Point)-Bid)*3,Digits);
         ticket=OrderSend(TradeSymbol,
                          OP_SELL,
                          Lots,
                          Bid,
                          0,
                          sSL2,
                          stp2,
                          TradePeriod+" 7 BEF 2nd Short",
                          31,
                          0,//timedelete(),
                          HotPink);
                          if(ticket>0)   {
                              if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
                                    {  s2=ticket;  Print(ticket); }
                                 else Print("Error Opening SellStop Order: ",GetLastError());
                                 return(0);  }  }  }
//Order Modify
   OrderSelect(b1,SELECT_BY_TICKET);   if(OrderCloseTime()>0)  {
      b1=0; SendMail(Symbol()+" "+OrderComment(),"$"+DoubleToStr(OrderProfit(),2));
      OrderSelect(b2,SELECT_BY_TICKET);
      OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+(1*Point),OrderTakeProfit(),0,Blue); }
   OrderSelect(b2,SELECT_BY_TICKET);   if(OrderCloseTime()>0) {b2=0; SendMail(Symbol()+" "+OrderComment(),"$"+DoubleToStr(OrderProfit(),2)); }
   
   OrderSelect(s1,SELECT_BY_TICKET);   if(OrderCloseTime()>0) {
      s1=0; SendMail(Symbol()+" "+OrderComment(),"$"+DoubleToStr(OrderProfit(),2));
      OrderSelect(s2,SELECT_BY_TICKET);
      OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-(1*Point),OrderTakeProfit(),0,Red); }
   OrderSelect(s2,SELECT_BY_TICKET);   if(OrderCloseTime()>0) {s2=0; SendMail(Symbol()+" "+OrderComment(),"$"+DoubleToStr(OrderProfit(),2)); }
//end
Print(TradeSymbol+", "+"Period "+TradePeriod);  }
return(0);}
//+------------------------------------------------------------------+
int TotalTradesThisSymbol(string TradeSymbol) {
   int i, TradesThisSymbol=0;
   for(i=0;i<OrdersTotal();i++)  {
      OrderSelect(i,SELECT_BY_POS);
      if(OrderSymbol()==TradeSymbol &&
        (OrderMagicNumber()==11 ||
         OrderMagicNumber()==21 || 
         OrderMagicNumber()==41 || 
         OrderMagicNumber()==51))   {  TradesThisSymbol++;  }   }
return(TradesThisSymbol);  }//end TotalTradesThisSymbol