TradeAroundPivot





//+------------------------------------------------------------------+
//|                                             TradeAroundPivot.mq4 |
//|                                                       by Ibrahim |
//|                           http://winning-solution.com/?id=baim78 |
//+------------------------------------------------------------------+
#property copyright "TradeAroundPivot by Ibrahim"
#property link      "http://winning-solution.com/?id=baim78"

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//---- 
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| EU, GU (and most pairs) at 15M TF and above                      |
//| Entry LONG at Support2 of Previous Bar Pivot Points              |
//| Entry SHORT at Resistant2 of Previous Bar Pivot Points           |
//| Exit LONG/SHORT at Pivot of Previous Bar Pivot Points            |
//| Exit Long (opt) Support1 or Heiken Ashi Low of Previous Bars     |
//| Exit Short (opt) Resistant1 or Heiken Ashi High of Previous Bars |
//+------------------------------------------------------------------+

extern bool UseMM=true;
extern double MaximumRisk=0.03;
extern int MaxOpenOrder=2;
extern int DecreaseFactor=3;
extern double Lots=1;
extern double MaxLots=5;
extern int slip = 2;
extern bool PivotNormalize=false;
extern bool EntryReverse=true;
extern bool EntryLevel3=false;
extern bool EntryLevel2=true;
extern bool EntryLevel1=false;
extern bool ExitPivot=true;
extern bool ExitLevel1=false;
extern bool ExitLevel2=false;
extern bool ExitLevel3=false;
extern bool ExitHeikenAshi=false;

int      BusyTry=5, err=0, c=0;
int      MagicNumber = 81918, EntrySignal, ExitSignal;
double   ModeSpread;

#define SIGNAL_NONE 0
#define SIGNAL_BUY   1
#define SIGNAL_SELL  2

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+

int start() {

   double R3, R2, R1, P, S1, S2, S3;
   double HAHigh, HALow;
   int total,ticket,cnt;

   ModeSpread = MarketInfo(Symbol(), MODE_SPREAD);
   
//initial data checks
   if(Bars<10) {
       Print("Bars less than 10");
       return(0); 
   } 

//indicator value
   R3 = iCustom(Symbol(),0,"Pivot SR-mod",PivotNormalize,0,0);
   R2 = iCustom(Symbol(),0,"Pivot SR-mod",PivotNormalize,1,0);
   R1 = iCustom(Symbol(),0,"Pivot SR-mod",PivotNormalize,2,0);
   P = iCustom(Symbol(),0,"Pivot SR-mod",PivotNormalize,3,0);
   S1 = iCustom(Symbol(),0,"Pivot SR-mod",PivotNormalize,4,0);
   S2 = iCustom(Symbol(),0,"Pivot SR-mod",PivotNormalize,5,0);
   S3 = iCustom(Symbol(),0,"Pivot SR-mod",PivotNormalize,6,0);

   HAHigh = iCustom(Symbol(),0,"Heiken Ashi-mod",0,1);
   HALow = iCustom(Symbol(),0,"Heiken Ashi-mod",1,1);

//identifying open orders
   total=OrdersTotal();
  
   if(AccountFreeMargin()<(1000*LotsOptimized())) {
      Print("No Money... FreeMargin=",AccountFreeMargin());
      return(0);
   }
   
//signal for entry 
   double EntryBid = Bid + (ModeSpread/2);
   double EntryAsk = Ask - (ModeSpread/2);
   if (EntryReverse) {
      if (EntryLevel1) {
         if (EntryAsk == S1) EntrySignal = SIGNAL_BUY;
         if (EntryBid == R1) EntrySignal = SIGNAL_SELL;
      }
      if (EntryLevel2) {
         if (EntryAsk == S2) EntrySignal = SIGNAL_BUY;
         if (EntryBid == R2) EntrySignal = SIGNAL_SELL;
      }
      if (EntryLevel3) {
         if (EntryAsk == S3) EntrySignal = SIGNAL_BUY;
         if (EntryBid == R3) EntrySignal = SIGNAL_SELL;
      }
   }
   if (!EntryReverse) {
      if (EntryLevel1) {
         if (EntryAsk == R1) EntrySignal = SIGNAL_BUY;
         if (EntryBid == S1) EntrySignal = SIGNAL_SELL;
      }
      if (EntryLevel2) {
         if (EntryAsk == R2) EntrySignal = SIGNAL_BUY;
         if (EntryBid == S2) EntrySignal = SIGNAL_SELL;
      }
      if (EntryLevel3) {
         if (EntryAsk == R3) EntrySignal = SIGNAL_BUY;
         if (EntryBid == S3) EntrySignal = SIGNAL_SELL;
      }
   }
//open long position
   if(EntrySignal==SIGNAL_BUY) {
      EntrySignal = SIGNAL_NONE;      
      ticket=SendOrder(Symbol(), OP_BUY, LotsOptimized(), EntryAsk, slip, 0, 0, "EA-ShootingPips", MagicNumber, 0, Green);
      if (ticket>0) {
         Print("Buy: ", Symbol());
         if (OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("Buy Order Opened:", OrderOpenPrice());
      }
   }

//open short position
   if(EntrySignal==SIGNAL_SELL) {
      EntrySignal = SIGNAL_NONE;      
      ticket=SendOrder(Symbol(), OP_SELL, LotsOptimized(), EntryBid, slip, 0, 0, "EA-ShootingPips", MagicNumber, 0, Red);
      if (ticket>0) {
         Print("Sell: ", Symbol());
         if (OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("Sell Order Opened:",OrderOpenPrice());
      }
   }
   
//signal for exit 
   double ExitBid = Bid - (ModeSpread/2);
   double ExitAsk = Ask + (ModeSpread/2);
   if (ExitPivot) {
      if (ExitBid == P) ExitSignal = SIGNAL_BUY;
      if (ExitAsk == P) ExitSignal = SIGNAL_SELL;
   }
   if (EntryReverse) {
      if (ExitLevel1) {
         if (ExitBid >= S1) ExitSignal = SIGNAL_BUY;
         if (ExitAsk <= R1) ExitSignal = SIGNAL_SELL;
      }
      if (ExitLevel2) {
         if (ExitBid >= S2) ExitSignal = SIGNAL_BUY;
         if (ExitAsk <= R2) ExitSignal = SIGNAL_SELL;
      }
      if (ExitHeikenAshi) {
         if (ExitBid >= HALow) ExitSignal = SIGNAL_BUY;
         if (ExitAsk <= HAHigh) ExitSignal = SIGNAL_SELL;
      }
   }
   if (!EntryReverse) {
      if (ExitLevel2) {
         if (ExitBid >= R2) ExitSignal = SIGNAL_BUY;
         if (ExitAsk <= S2) ExitSignal = SIGNAL_SELL;
      }
      if (ExitLevel3) {
         if (ExitBid >= R3) ExitSignal = SIGNAL_BUY;
         if (ExitAsk <= S3) ExitSignal = SIGNAL_SELL;
      }
      if (ExitHeikenAshi) {
         if (ExitBid >= HAHigh) ExitSignal = SIGNAL_BUY;
         if (ExitAsk <= HALow) ExitSignal = SIGNAL_SELL;
      }
   }

//close
   if (ExitSignal==SIGNAL_BUY) {
      ExitSignal = SIGNAL_NONE;
      CloseOrder(Symbol(), OP_BUY, MagicNumber);
   }
   if (ExitSignal==SIGNAL_SELL) {
      ExitSignal = SIGNAL_NONE;
      CloseOrder(Symbol(), OP_SELL, MagicNumber);
   }

   return(0);
}

double LotsOptimized()
  {
   double   lot_min        = MarketInfo( Symbol(), MODE_MINLOT  );
   double   lot_max        = MarketInfo( Symbol(), MODE_MAXLOT  );
   double   lot_step       = MarketInfo( Symbol(), MODE_LOTSTEP );
   double   freemargin     = AccountFreeMargin();
   int      leverage       = AccountLeverage();
   int      lotsize        = MarketInfo( Symbol(), MODE_LOTSIZE );
   double   lot=Lots;
   int      orders=HistoryTotal();     // history orders total
   int      losses=0;                  // number of losses orders without a break
   
   if (!UseMM) return(Lots);
//---- select lot size
   lot=NormalizeDouble(AccountFreeMargin()/3*MaximumRisk/100.0,1);
//---- calcuulate number of losses orders without a break
   if(DecreaseFactor>0)
     {
      for(int i=orders-1;i>=0;i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; }
         if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue;
         //----
         if(OrderProfit()>0) break;
         if(OrderProfit()<0) losses++;
        }
      if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
     }
//---- return lot size
   if ( lot < lot_min ) lot = lot_min;
   if ( lot > lot_max ) lot = lot_max;

   double needmargin = NormalizeDouble( lotsize / leverage * lot, 2 );
   if ( freemargin < needmargin ) {
       Print( "LotSize: We have no money. Free Margin = ", freemargin ," Need Margin = ",needmargin);
       return(-1);
   }
   
   //max lot
   if (lot>MaxLots) lot = MaxLots;

   return(lot);
  }


bool CheckTrade(string symbol, int op, int magic, double price) {

   int Total = OrdersTotal();
   if (Total > 0) {
      for (int i = 0; i < Total; i ++) {
         if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) 
          && OrderSymbol() == symbol 
          && OrderType() ==op 
          && OrderOpenPrice() == price  
          && OrderMagicNumber() == magic)
         { 
            return(true);
         }
      }
   } else {
      return(false);
   }
}

int SendOrder(string symbol, int op, double lots, double price, int sp, double sl, double tp,
           string comment, int magic, datetime exp, color col) {

   int ticket, Total = OrdersTotal();
   
   if (!CheckTrade(symbol, op, magic, price)) {
      if (Total < MaxOpenOrder) {
         for(c=0;c<BusyTry;c++) {
            ticket=OrderSend(symbol,op,lots,price,sp,0,0,comment,magic,exp,col);
            err=GetLastError();
            if(err==0) {
               break;
            } else {
               if(err==4 || err==137 ||err==146 || err==136) { //Busy errors
                  Sleep(5000);
                  continue;
               } else { //normal error
                  break;
               }  
            }
         }
      }
   }
   return(ticket);
}

bool CloseOrder(string symbol, int op, int magic) {

   for(int i = OrdersTotal() - 1; i >= 0; i--) {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) 
                  && OrderSymbol() == symbol 
                  && OrderMagicNumber() == magic 
                  && OrderType() == op)
         {
         for(c=0;c<BusyTry;c++) {
            if (OrderClose(OrderTicket()
                       , OrderLots()
                       , OrderClosePrice()
                       , MarketInfo(OrderSymbol(), MODE_SPREAD)
                       , Violet)) 
            {
               break;
            } else {
               err=GetLastError();
               if(err==4 || err==137 ||err==146 || err==136) { //Busy errors
                  Sleep(5000);
                  continue;
               } else { //normal error
                  break;
               }  
            }
         }
      }
   }
}



Sample





Analysis



Market Information Used:



Indicator Curves created:


Indicators Used:




Custom Indicators Used:
Pivot SR-mod
Heiken Ashi-mod

Order Management characteristics:
Checks for the total of open orders
It automatically opens orders when conditions are reached
It Closes Orders by itself

Other Features:


BackTest : USDCHF on H1

From 2009-12-01 to 2010-01-01 Profit Factor:0.00 Total Net Profit:0.00

BackTest : EURUSD on H1

From 2009-12-01 to 2010-01-17 Profit Factor:0.00 Total Net Profit:0.00

BackTest : USDCAD on H1

From 2009-12-01 to 2010-01-01 Profit Factor:0.00 Total Net Profit:0.00

BackTest : EURUSD on H1

From 2009-08-01 to 2009-10-01 Profit Factor:0.00 Total Net Profit:0.00

BackTest : GBPUSD on H1

From 2010-01-01 to 2010-02-27 Profit Factor:0.00 Total Net Profit:0.00

BackTest : EURUSD on H1

From 2010-04-01 to 2010-04-30 Profit Factor:0.00 Total Net Profit:0.00

BackTest : EURUSD on H1

From 2010-05-01 to 2010-05-31 Profit Factor:0.00 Total Net Profit:0.00

BackTest : EURUSD on H1

From 2010-06-01 to 2010-06-30 Profit Factor:0.00 Total Net Profit:0.00

Request Backtest for TradeAroundPivot


From : (yyyy/mm/dd) To: (yyyy/mm/dd)

Pair: Period: