DayImpulse





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

datetime lastOrder = 0;
//bool hasHedge = false;
//bool hasHedgeWithStop = false;
extern double lots = 0.4;
extern int takeprofit = 200;
int MAGIC = 57;
int ticket;
int hedgeTik= -1;
int orderTik = -1;
datetime PreviousBar = 0;
//int edgeTries=0;

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

int decision () {

/*
SetIndexLabel(0, "Signal(" + per + ")");
   SetIndexLabel(1, "MA(" + maPeriod + ")");
   SetIndexLabel(2, "Signal2(" + per + ")");
   
   if ( x1 > y1  && y0 > x0 ) trend = -1;// Print ("yuppi sell " , x0 , " " , y0) ;
       if ( y1 > x1 && x0 > y0 ) trend = 1 ; //Print ("yuppi buy " , , x0 , " " , y0) ;
*/
   double sig0 = iCustom(NULL, 0, "dayImpulse2dd",0,0);
         double sig1 = iCustom(NULL, 0, "dayImpulse2dd",0,1);
         double ma0 = iCustom(NULL, 0, "dayImpulse2dd",1,0);
         double ma1 = iCustom(NULL, 0, "dayImpulse2dd",1,1);
         
         double z0 = iCustom(NULL, 0, "dayImpulse2dd",2,0);

   if (  sig0 < 0 && sig1 > 0 && sig1 > ma1 && ma0 > sig0 && z0 == 0 ) {
   
   Print( "sell " , sig0 , " " , sig1 ," ", ma0 , " " , ma1 ,  " " , z0 );
    return(-1);
   }
   if  ( ma1 > sig1 && sig0 > ma0 ) {
      Print("buy ",  sig0 , " " , sig1 ," ", ma0 , " " , ma1 ,  " " , z0 );
     return(1);
    }
   
   return(0);


}

int getNumberTrades()
{   
   int total = OrdersTotal();
   int numords = 0;
      
   for(int cnt=0; cnt<total; cnt++) 
   {        
   OrderSelect(cnt, SELECT_BY_POS);            
   if(OrderSymbol() == Symbol() &&  OrderMagicNumber() == MAGIC) 
      return (OrderTicket());
   }
   return(-1);
}  

void checkTicket (int tik) {
   
   
     int hstTotal=OrdersHistoryTotal();
     if(OrderSelect(tik,SELECT_BY_TICKET,MODE_HISTORY) !=false ) {
    
         lastOrder = OrderCloseTime();
         ticket =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+0.1;
   
   
   if ( hedgeTicket != -1 ) Print(" current " , currentTicket , " " ,currentProfit , " HEDGE" , hedgeTicket , " " , hedgeProfit) ;
  
   
   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+0.1);
         
         return(0);
         
         
         
         
      
      }
   
   }
   if ( hedgeTicket == -1 && currentProfit < hedgeLoss ) {
      
     // 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(),0,0,Blue);
            
            point = MathAbs((Ask-OrderOpenPrice()));
         
         }
         else {
            
           res =  OrderModify(hedgeTicket,Bid,OrderOpenPrice(),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() > 30 )  {
                  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 ) {
         
         
            if ( OrderType() == OP_BUY ) {
            
                  Print("aqui");
                  OrderModify(OrderTicket(),Ask,0,OrderOpenPrice(),0,Blue);
                  
            }
            else  {
            Print("aqui");
            OrderModify(OrderTicket(),Bid,0,OrderOpenPrice(),0,Blue);
            }
         
         
         }
   */
   }
   

}


  
  
  
  
/*
int getHedgeTrades() {


   int z = OrdersTotal();
   
   for(int i=0;i<z;i++) {
   
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if ( OrderSymbol() == Symbol() && OrderMagicNumber() == MAGIC && OrderComment() == "hedge" ) {
         hasHedge = true;
         if ( OrderStopLoss() == OrderOpenPrice() ) hasHedgeWithStop = true;
         return (OrderTicket());
      }
   
   }
   
   hasHedge = false;
   hasHedgeWithStop = false;
   edgeTicket = 0;
   
   
   return(-1);

}
*/



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);

}




}  

int decision2() {

int Current = 0;

int PeriodIXO = 15;

      double Buy1_1 = iMA(NULL, 0, 21, 0, MODE_EMA, PRICE_CLOSE, Current + 1);
      double Buy1_2 = iMA(NULL, 0, 200, 0, MODE_EMA, PRICE_CLOSE, Current + 1);
      double Buy2_1 = iMA(NULL, 0, 21, 0, MODE_EMA, PRICE_CLOSE, Current + 0);
      double Buy2_2 = iMA(NULL, 0, 200, 0, MODE_EMA, PRICE_CLOSE, Current + 0);
      double Buy3_1 = iSAR(NULL, 0, 0.005, 0.05, Current + 1);
      double Buy3_2 = iSAR(NULL, 0, 0.005, 0.05, Current + 0);
      double Buy4_1 = iMACD(NULL, 0, 12, 26, 9, PRICE_CLOSE, MODE_SIGNAL, Current + 1);
      double Buy4_2 = iMACD(NULL, 0, 12, 26, 9, PRICE_CLOSE, MODE_SIGNAL, Current + 0);
      double Sell1_1 = iMA(NULL, 0, 21, 0, MODE_EMA, PRICE_CLOSE, Current + 1);
      double Sell1_2 = iMA(NULL, 0, 200, 0, MODE_EMA, PRICE_CLOSE, Current + 1);
      double Sell2_1 = iMA(NULL, 0, 21, 0, MODE_EMA, PRICE_CLOSE, Current + 0);
      double Sell2_2 = iMA(NULL, 0, 200, 0, MODE_EMA, PRICE_CLOSE, Current + 0);
      double Sell3_1 = iSAR(NULL, 0, 0.005, 0.05, Current + 1);
      double Sell3_2 = iSAR(NULL, 0, 0.005, 0.05, Current + 0);
      double Sell4_1 = iMACD(NULL, 0, 12, 26, 9, PRICE_CLOSE, MODE_SIGNAL, Current + 1);
      double Sell4_2 = iMACD(NULL, 0, 12, 26, 9, PRICE_CLOSE, MODE_SIGNAL, Current + 0);
      double diIXO10=iCustom(NULL,0,"I_XO_A_H",PeriodIXO,0,0);
      double diIXO21=iCustom(NULL,0,"I_XO_A_H",PeriodIXO,1,0);
      double d22=(0);


      if (Buy1_1 < Buy1_2 && Buy2_1 >= Buy2_2 && Buy3_1 < Buy3_2 && Buy4_1 < Buy4_2 && diIXO10 > d22) return(1);
      if (Sell1_1 > Sell1_2 && Sell2_1 <= Sell2_2 && Sell3_1 > Sell3_2 && Sell4_1 > Sell4_2 && diIXO21< d22) return(-1);
      
      return (0);


}


 
  bool NewBar()
{
   if(PreviousBar<Time[0])
   {
      PreviousBar = Time[0];
      return(true);
   }
   else
   {
      return(false);
   }
   return(false);    // in case if - else statement is not executed
}




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()
  {

      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() == OrderOpenPrice();
                  
            }
            checkProfit(currentProfit,orderTik,currentOrderType,currentLots,hedgeProfit,hedgeTik,hedgeType,hedgeLots,hedgeLoss , hasHedgeWithStop );

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


/*
      checkTicket(ticket);
      int tik = getNumberTrades() ;
      
      if ( tik != -1 ) {
      
         checkProfit(-100);
         
      /*
            if ( OrderType() == OP_BUY  && decision == -1) {
            
               closeOrder(OrderTicket(),OrderType(),OrderLots());
            }
            
            if ( OrderType() == OP_SELL  && decision == 1 ) {
            
               closeOrder(OrderTicket(),OrderType(),OrderLots());
            }
      
            
      
      
      return(0);
      }
   */  
     if(NewBar() == false ) return(0);
    
      
      int decision = decision2();
      
      
      if ( decision == 1 ) {
              
               
                if ( (TimeCurrent() - lastOrder ) > (60*60*3 ) ) {

                      ticket = OrderSend(Symbol(),OP_BUY, lots, Ask, 3, 0, Ask+takeprofit*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-takeprofit*Point,"", MAGIC, 0, Red); 
               
                        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:

Series array that contains open time of each bar


Indicator Curves created:


Indicators Used:


Moving average indicator
Parabolic Stop and Reverse system
MACD Histogram


Custom Indicators Used:
dayImpulse2dd
I_XO_A_H

Order Management characteristics:
Checks for the total of open orders

Checks for the total of closed orders
It automatically opens orders when conditions are reached
It can change open orders parameters, due to possible stepping strategy
It Closes Orders by itself

Other Features:

BackTest : EURUSD on H1

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

BackTest : EURUSD on H1

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

BackTest : EURUSD on H1

From 2010-03-01 to 2010-03-27 Profit Factor:1.13 Total Net Profit:296.69

BackTest : EURUSD on H1

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

BackTest : EURUSD on H1

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

BackTest : EURUSD on H1

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

BackTest : GBPUSD on H1

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

BackTest : GBPUSD on H1

From 2010-01-01 to 2010-04-16 Profit Factor:0.01 Total Net Profit:-9952.34

BackTest : USDCAD on H1

From 2009-01-01 to 2010-01-01 Profit Factor:1.16 Total Net Profit:8610.33

BackTest : USDCAD on H1

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

BackTest : USDCHF on H1

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

BackTest : USDJPY on H1

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

Request Backtest for DayImpulse


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

Pair: Period: