VolumeWatcher





//+------------------------------------------------------------------+
//|                                                VolumeWatcher.mq4 |
//|                                              @2009 Desynced Tech |
//|                                     http://desynced.no-ip.org/fx |
//+------------------------------------------------------------------+
#property copyright "@2009 Desynced Tech"
#property link      "http://desynced.no-ip.org/fx"

//+------------------------------------------------------------------+
//|                                                VolumeWatcher.mq4 |
//|                      Copyright © 2009, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

extern double  lots = 0.1;
datetime lastOrder =0;

int orderTik =0;
int hedgeTik = 0 ;

extern int MAGIC = 55;
int ticket;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+



int closeOrder(int ticket,int orderType, double lts) {

if ( orderType == OP_BUY ) {

   OrderClose(ticket,lts,Bid,3,Green);
   return(0);
}
else {
   OrderClose(ticket, lts, Ask, 3, Green);
    return(0);

}
}


void openHedge(int currentTicket, int currentOrderType,double hLots ) {

   if ( currentOrderType == 0  ) {
         // é um buy vou abrir um sell */
         OrderSend(Symbol(),OP_SELL, hLots, Bid, 3,0 /*Ask+200*Point*/, 0,"hedge", MAGIC, 0, Red);
         
         OrderModify(currentTicket,Ask,0,0,0,Blue);
         /*
                            mysql_connect("192.168.0.3","forex","forex","forex",3306);
                                                                                                                                              insertOrderDb(ticket,Symbol(),Bid,0, 0,MAGIC,"woodyEA-HEDGER",OP_SELL, TimeCurrent());
                                                                                                                                             mysql_close(mysql);
                        
         */
      //   hasHedgeWithStop = true;
             
      }
      else if ( currentOrderType == 1  ) {
      
        OrderSend(Symbol(),OP_BUY, hLots, Ask, 3, 0/*Bid-200*Point*/, 0,"hedge", MAGIC, 0, Red); 
        OrderModify(currentTicket,Bid,0,0,0,Blue);
      //   hasHedgeWithStop = true;
      }



}


void checkProfit(double currentProfit , int currentTicket , int currentOrderType , double currentLots , double hedgeProfit , int hedgeTicket , int  hedgeType , double hedgeLots,  int hedgeLoss, bool hasHedgeWithStop ) {

/*
   double currentProfit = OrderProfit();
   int orderTicket = OrderTicket();
   int orderType = OrderType();
  */ 
  // double hLots = OrderLots();
   double hLots = currentLots*2;
   
  
   
   if ( hedgeTicket > 0 ) {
   
      if ( currentProfit > 0 && hedgeProfit <0  ) {
         // esta na altura de fechar a original e abrir um hedge do hedge  
         Print (" deu para o torto  fechei" ,currentProfit , " " , hedgeProfit);
         
         closeOrder(currentTicket,currentOrderType,currentLots);
         
         openHedge(hedgeTicket, hedgeType , hedgeLots *2);
         
         return(0);
         
         
         
         
      
      }
   
   }
   if ( hedgeTicket == -1 && currentProfit > hedgeLoss && currentProfit < hedgeLoss -100  ) {
      
     // int decision = stochasticAlert();
      
      if ( currentOrderType == 0  ) {
         // é um buy vou abrir um sell */
         OrderSend(Symbol(),OP_SELL, hLots, Bid, 3,0 /*Ask+200*Point*/, 0,"hedge", MAGIC, 0, Red);
         
         OrderModify(currentTicket,Ask,0,0,0,Blue);
         /*
                            mysql_connect("192.168.0.3","forex","forex","forex",3306);
                                                                                                                                              insertOrderDb(ticket,Symbol(),Bid,0, 0,MAGIC,"woodyEA-HEDGER",OP_SELL, TimeCurrent());
                                                                                                                                             mysql_close(mysql);
                        
         */
         hasHedgeWithStop = true;
             
      }
      else if ( currentOrderType == 1  ) {
      
        OrderSend(Symbol(),OP_BUY, hLots, Ask, 3, 0/*Bid-200*Point*/, 0,"hedge", MAGIC, 0, Red); 
        OrderModify(currentTicket,Bid,0,0,0,Blue);
         hasHedgeWithStop = true;
      }
     
      return(0);
   
   }
   else if ( hedgeTicket > 0 && !hasHedgeWithStop) {
   
         // ja tenho um edge e agora que faco com ele 
         bool res = false;
         double point = 0 ;
         if ( hedgeType == OP_BUY ) {
            
            res = OrderModify(hedgeTicket,Ask,OrderOpenPrice()+100*Point,0,0,Blue);
            
            point = MathAbs((Ask-OrderOpenPrice()));
         
         }
         else {
            
           res =  OrderModify(hedgeTicket,Bid,OrderOpenPrice()-100*Point,0,0,Blue);
           
             point = MathAbs((Bid-OrderOpenPrice()));
         }
         if ( res ) {
         
           // Print(" modifiquei no hedge" , hedge);
            
         }
         else {
              // edgeTries++;
              // Print(" modifiquei no hedge" , edgeTries);
         }
   
   }
   else if  ( hedgeTicket > 0 && hasHedgeWithStop ) {
      
      if ( currentProfit + hedgeProfit > 0 ) {
         
               if ( currentProfit + OrderProfit() > 0 )  {
                  closeOrder(hedgeTicket,hedgeType,hedgeLots);
                  closeOrder(currentTicket,currentOrderType,currentLots);
                  
               }
               Print("tenho aqui um ponto de lucro " , currentProfit + hedgeProfit);
         }
   
   
   }
   else if ( hedgeTicket < 0 ) {
   
      //   getNumberTrades();
         
        // if ( OrderTakeProfit() == 0 ) {
         
            OrderSelect(orderTik,SELECT_BY_TICKET,MODE_TRADES);
            if ( OrderType() == OP_BUY ) {
            
             //     Print("aqui");
                if ( OrderTakeProfit() == 0 )   OrderModify(OrderTicket(),Ask,0,OrderOpenPrice()+50*Point,0,Blue);
                  
            }
            else  {
            //Print("aqui");
            if ( OrderTakeProfit() == 0 ) OrderModify(OrderTicket(),Bid,0,Bid-50*Point,0,Blue);
            }
         
         
   //      }
   
   }
   

}



void  getTrades() {


    int total = OrdersTotal();
   int numords = 0;
   
   int tickets[10];
   int ticketsFound = 0; 
     
   for(int cnt=0; cnt<total; cnt++) 
   {        
   OrderSelect(cnt, SELECT_BY_POS);            
   if(OrderSymbol() == Symbol() &&  OrderMagicNumber() == MAGIC) 
   
      tickets[ticketsFound] = OrderTicket();
      ticketsFound++;
   }
   
   if ( ticketsFound == 2 ) {
   
      if ( tickets[0] < tickets[1] ) {
      
         orderTik = tickets[0];
         hedgeTik = tickets[1];
         
        // Print("orignal é " , tickets[0] , " hedge " , tickets[1] ) ;
      
      }
      else {
         //Print("orignal é " , tickets[1] , " hedge " , tickets[0] ) ;
         
         orderTik = tickets[1];
         hedgeTik = tickets[0];
      
      }
   
   
   }
   else if ( ticketsFound == 1  ) {
   
         orderTik = tickets[0];
          
         hedgeTik = -1;
   }
   else if ( ticketsFound < 1 ) {
   
      orderTik = -1;
      hedgeTik = -1;
   }
   else if ( ticketsFound > 2 ) {
   
      Print("FODDASE NAO PODE HAVER MAIS QUE 2 ");
   }
}






int start()
  {
 //double y0 = iCustom(NULL, 0, "Indraws Volume Analysis 3",0,0);
 //double y1 = iCustom(NULL, 0, "Indraws Volume Analysis 3",0,0);
 
orderTik = -1;
      hedgeTik = -1;
      getTrades();
      
      
      if ( orderTik != -1 )  {
      
         double currentProfit;     
         int currentOrderType;
         double currentLots;
         
         
         double hedgeProfit = 0;
         
         int  hedgeType = -1;
         double hedgeLots= 0 ;
         int hedgeLoss = -150;
         bool hasHedgeWithStop= false;
         
         
         //void checkProfit(double currentProfit , int currentTicket , int currentOrderType , double currentLots , double hedgeProfit , int hedgeTicket , int  hedgeType , double hedgeLots,  int hedgeLoss, bool hasHedgeWithStop ) {
           
            OrderSelect(orderTik,SELECT_BY_TICKET,MODE_TRADES);
            
            currentProfit = OrderProfit();
            currentOrderType = OrderType();
            currentLots = OrderLots();
            
            if ( hedgeTik != -1 ) {
               
                  OrderSelect(hedgeTik,SELECT_BY_TICKET,MODE_TRADES);
                  hedgeProfit = OrderProfit();
                  hedgeType = OrderType();
                  hedgeLots = OrderLots();
                  hasHedgeWithStop = OrderStopLoss() != 0;
                  
            }
            checkProfit(currentProfit,orderTik,currentOrderType,currentLots,hedgeProfit,hedgeTik,hedgeType,hedgeLots,hedgeLoss , hasHedgeWithStop );

         
         
        // Print(" tenho ordem Activa ") ;
         
         return(0);
      }

 
 double y0 = iMFI(NULL,0,14,0);
 double y1 =iMFI(NULL,0,14,1);
 
 Comment(Ask - Bid );

 
 int decision = 0;
 if ( y1 > 80 && y0 < 80 ) decision = -1;
 
 if ( y1 < 20 && y0 > 20 ) decision = 1;
 
 
 if ( decision == 1 ) {
              
               
                if ( (TimeCurrent() - lastOrder ) > (60*60*3 ) ) {
                     Print("buy " , y0 , " " , y1);
                      ticket = OrderSend(Symbol(),OP_BUY, lots, Ask, 3, 0, Ask+50*Point,"", MAGIC, 0, Red); 
                      
                      /*
                            mysql_connect("192.168.0.3","forex","forex","forex",3306);
                                                                                                                                              insertOrderDb(ticket,Symbol(),,0, Ask+60*Point,MAGIC,"woodyEA",OP_BUY, TimeCurrent());
                                                                                                                                             mysql_close(mysql);
                        
                      */
                      lastOrder = TimeCurrent();
               }

         }
         
         else if ( decision == -1 ) {
             
             if ( (TimeCurrent() - lastOrder ) > (60*60*3 ) ) {
             ticket =  OrderSend(Symbol(),OP_SELL, lots, Bid, 3, 0, Bid-50*Point,"", MAGIC, 0, Red); 
                           Print("sell " , y0 , " " , y1);
                        if ( ticket > 0 ) {
                        
                         /*
                        
                            mysql_connect("192.168.0.3","forex","forex","forex",3306);
                                                                                                                                              insertOrderDb(ticket,Symbol(),Bid,0, Bid-60*Point,MAGIC,"woodyEA",OP_SELL, TimeCurrent());
                                                                                                                                             mysql_close(mysql);
                      */
                               lastOrder = TimeCurrent();
                            
                        }
            }
         }


 
   
//----
   return(0);
  }
//+------------------------------------------------------------------+





Sample





Analysis



Market Information Used:



Indicator Curves created:


Indicators Used:

Money flow index


Custom Indicators Used:

Order Management characteristics:
It Closes Orders by itself
It automatically opens orders when conditions are reached
It can change open orders parameters, due to possible stepping strategy

Checks for the total of open orders

Other Features:

BackTest : EURUSD on H1

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

BackTest : EURUSD on H1

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

BackTest : EURUSD on H1

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

BackTest : EURUSD on H1

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

BackTest : GBPUSD on H1

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

BackTest : GBPUSD on H1

From 2010-01-01 to 2010-04-16 Profit Factor:0.09 Total Net Profit:-422.42

BackTest : USDCAD on H1

From 2009-01-01 to 2010-01-01 Profit Factor:0.09 Total Net Profit:-1416.02

BackTest : USDCAD on H1

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

BackTest : USDCHF on H1

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

BackTest : USDJPY on H1

From 2009-11-01 to 2009-11-30 Profit Factor:0.04 Total Net Profit:-369.01

Request Backtest for VolumeWatcher


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

Pair: Period: