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

/*Trading Strategy...
Seven Day 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;

string pairs[] = { "EURUSDm","USDJPYm","GBPUSDm","USDCHFm","EURCHFm","AUDUSDm","USDCADm",
                   "NZDUSDm","EURGBPm","EURJPYm","GBPJPYm","CHFJPYm","GBPCHFm","EURAUDm",
                   "EURCADm","AUDCADm","AUDJPYm","NZDJPYm","AUDNZDm" };
string   TradeSymbol;
int      Pair = -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(); }
return(0);  }

int deinit(){return(0);}

int start(){
   Pair = (Pair+1) % ArraySize(pairs);
   TradeSymbol = pairs[Pair];

   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,1440,1)<iClose(TradeSymbol,1440,2) &&
         iClose(TradeSymbol,1440,2)<iClose(TradeSymbol,1440,3) &&
         iClose(TradeSymbol,1440,3)<iClose(TradeSymbol,1440,4) &&
         iClose(TradeSymbol,1440,4)<iClose(TradeSymbol,1440,5) &&
         iClose(TradeSymbol,1440,5)<iClose(TradeSymbol,1440,6) &&
         iClose(TradeSymbol,1440,6)<iClose(TradeSymbol,1440,7) )  {
//buy execution        
      if(b1==0)   {
         bSL1=iLow(TradeSymbol,1440,1)-(Ask-Bid)-(10*Point);  
         btp1=Ask+(Ask-(iLow(TradeSymbol,1440,1)-(Ask-Bid)-(10*Point)));
         ticket=OrderSend(TradeSymbol,
                          OP_BUY,
                          Lots,
                          Ask,
                          0,//slippage
                          bSL1,
                          btp1,
                          "7 DEF 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,1440,1)-(Ask-Bid)-(10*Point);
         btp2=Ask + NormalizeDouble( ((Ask-(iLow(TradeSymbol,1440,1)-(Ask-Bid)-(10*Point))*3)) , Digits);
         ticket=OrderSend(TradeSymbol,
                          OP_BUY,
                          Lots,
                          Ask,
                          0,//slippage
                          bSL2,
                          btp2,
                          "7 DEF 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,1440,1)>iClose(TradeSymbol,1440,2) &&
         iClose(TradeSymbol,1440,2)>iClose(TradeSymbol,1440,3) &&
         iClose(TradeSymbol,1440,3)>iClose(TradeSymbol,1440,4) &&
         iClose(TradeSymbol,1440,4)>iClose(TradeSymbol,1440,5) &&
         iClose(TradeSymbol,1440,5)>iClose(TradeSymbol,1440,6) &&
         iClose(TradeSymbol,1440,6)>iClose(TradeSymbol,1440,7) )  {
//Sell Execution
      if(s1==0)   {
         sSL1=iHigh(TradeSymbol,1440,1)+(Ask-Bid)+(10*Point);
         stp1=Bid-(iHigh(TradeSymbol,1440,1)+(Ask-Bid)+(10*Point)-Bid);
         ticket=OrderSend(TradeSymbol,
                          OP_SELL,
                          Lots,
                          Bid,
                          0,
                          sSL1,
                          stp1,
                          "7 DEF 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,1440,1)+(Ask-Bid)+(10*Point);
         stp2=Bid-NormalizeDouble( (iHigh(TradeSymbol,1440,1)+(Ask-Bid)+(10*Point)-Bid)*3,Digits);
         ticket=OrderSend(TradeSymbol,
                          OP_SELL,
                          Lots,
                          Bid,
                          0,
                          sSL2,
                          stp2,
                          "7 DEF 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   
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