WoodyEA





//+------------------------------------------------------------------+
//|                                                      WoodyEA.mq4 |
//|                                              @2009 Desynced Tech |
//|                                     http://desynced.no-ip.org/fx |
//+------------------------------------------------------------------+
#property copyright "@2009 Desynced Tech"
#property link      "http://desynced.no-ip.org/fx"
#include <mysql.mqh>

datetime lastOrder = 0;
bool hasHedge = false;
bool hasHedgeWithStop = false;
extern double lots = 0.1;
int MAGIC = 60;
int ticket;
int edgeTicket= 0;

int edgeTries=0;
datetime PreviousBar;      // record the candle/bar time
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   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
}
  
  
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);

}




}  


datetime checkTicket (int tik) {
   
   
     int hstTotal=OrdersHistoryTotal();
     datetime ret = 0;
     if(OrderSelect(tik,SELECT_BY_TICKET,MODE_HISTORY) !=false ) {
     
        ret = OrderCloseTime();
        return(ret);
             
     }  
      
     return(0); 
}



void checkProfit(int hedgeLoss ) {

   double currentProfit = OrderProfit();
   int orderTicket = OrderTicket();
   int orderType = OrderType();
   
   double hLots = OrderLots();//*2;
   
   int hedge = getHedgeTrades();
   
   if ( hedge == -1 && currentProfit < hedgeLoss ) {
      
      int decision = stochasticAlert();
      
      if ( orderType == 0 && decision == -1 ) {
         // é um buy vou abrir um sell */
         edgeTicket =OrderSend(Symbol(),OP_SELL, hLots, Bid, 3, Ask+200*Point, 0,"hedge", MAGIC, 0, Red);
         /*
                            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);
                        
                      */
         
             hasHedge = true;
             edgeTries = 0;
      }
      else if ( orderType == 1 && decision == 1 ) {
      
        edgeTicket =  OrderSend(Symbol(),OP_BUY, hLots, Ask, 3, Bid-200*Point, 0,"hedge", MAGIC, 0, Red); 
         hasHedge = true;
         edgeTries = 0;
      }
     
      return(0);
   
   }
   if ( hedge > 0 && !hasHedgeWithStop) {
   
         // ja tenho um edge e agora que faco com ele 
         bool res = false;
         double point = 0 ;
         if ( OrderType() == 0 ) {
            
            res = OrderModify(hedge,Ask,OrderOpenPrice(),0,0,Blue);
            
            point = MathAbs((Ask-OrderOpenPrice()));
         
         }
         else {
            
           res =  OrderModify(hedge,Bid,OrderOpenPrice(),0,0,Blue);
           
             point = MathAbs((Bid-OrderOpenPrice()));
         }
         if ( res ) {
         
            Print(" modifiquei no hedge" , hedge);
            hasHedgeWithStop = true;
         }
         else {
               edgeTries++;
               Print(" modifiquei no hedge" , edgeTries);
         }
   
   }
   if  ( hedge > 0 && hasHedgeWithStop ) {
      
      if ( currentProfit + OrderProfit() > 0 ) {
         
               if ( currentProfit + OrderProfit() > 100 )  {
                  closeOrder(OrderTicket(),OrderType(),hLots);
                  closeOrder(orderTicket,orderType,hLots/2);
                  
               }
               Print("tenho aqui um ponto de lucro " , currentProfit + OrderProfit());
         }
   
   
   }
   

}


  
  
  
  

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

}



bool canTrade() {

   //if ( Minute() == 0) return(false);

   if ( Hour() >= 1 && Hour() < 2 || (Hour() == 2 && Minute() < 20 )  ) return (false);
   
   if ( Hour() >= 9 && Hour() < 10 || (Hour() == 10 && Minute() < 20 ) ) return (false);
   
   if ( Hour() >= 13 && Hour() < 14 || ( Hour() == 14 && Minute() < 20 )) return (false);
   
   if ( Hour() == 17 ) return(false);
   
   return (true);

}


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


int decision(bool includeTime) {

         double y1 = iCustom(NULL, 0, "cci_woodies_lnx_v1_1",0,0);
         double y2 = iCustom(NULL, 0, "cci_woodies_lnx_v1_1",1,0);
         double y3 = iCustom(NULL, 0, "cci_woodies_lnx_v1_1",2,0);
         double y4 = iCustom(NULL, 0, "cci_woodies_lnx_v1_1",3,0);
         double y5 = iCustom(NULL, 0, "cci_woodies_lnx_v1_1",5,0);
         double y6 = iCustom(NULL, 0, "cci_woodies_lnx_v1_1",5,1);
         
         double y7 = iCustom(NULL, 0, "cci_woodies_lnx_v1_1",5,2);
         
 
        /* int ratio = MathFloor( AccountBalance() /1000 );
 
          if ( ratio > 0 ) lots = 0.1*(ratio*2);
  */
        if (y1 ==0 && y6 > -10 &&  y6 < 0 && y5 > 0  && y5 > 120 && OrdersTotal() == 0  ) {
        
            if ( includeTime ) {
            
               if ( (TimeCurrent() - lastOrder ) > (60*60*3 ) ) {
                  Print( "buy " , y7 , " " , y6 , " ", y5 ,  " " , y1 );
                  return(1);
               
               }
               else return (0);
            }
            Print( "buy " , y7 , " " , y6 , " ", y5 ,  " " , y1 );
            return (1);
        }
        
        
        if ( y1 == 0 &&  y6 < 0 && y5 > 72 && OrdersTotal() == 0   ) {
        
                  if ( includeTime ) {
            
                          if ( (TimeCurrent() - lastOrder ) > (60*60*3 ) ) {
                                  Print( "sell " , y7 , " " , y6 , " ", y5 , " " , y1 );
                                  return(-1);
               
                            }
                           else return (0);
                  }
            Print( "sell " , y7 , " " , y6 , " ", y5 , " " , y1 );
            return (-1);
         }
        
        return(0);
      


}

int stochasticAlert() {

   double mainLine;
   double prevMainLine;
   double signalLine;
   double prevSignalLine;
      
   for(int a=0;a<=14 ;a++)
   {
      mainLine=iStochastic(0,0,14,3,3,MODE_SMA,0,MODE_MAIN,a);
      prevMainLine=iStochastic(0,0,14,3,3,MODE_SMA,0,MODE_MAIN,a+1);
      signalLine=iStochastic(0,0,14,3,3,MODE_SMA,0,MODE_SIGNAL,a);
      prevSignalLine=iStochastic(0,0,14,3,3,MODE_SMA,0,MODE_SIGNAL,a+1);
 
      if(prevMainLine<prevSignalLine && mainLine>signalLine) {
        // Print("buy " , mainLine , " " , signalLine );
         return (1);
        }
      if(prevMainLine>prevSignalLine && mainLine<signalLine) {
      
      //   Print("sell " , mainLine , " " , signalLine );
         return (-1);
        }
   }
   return(0);




}



int start()
  {

 
         if ( edgeTicket != 0 ) {
            datetime edgeTime = checkTicket(edgeTicket);
         if ( edgeTime != 0 && (TimeCurrent() - edgeTime )< (60*60*2 ) ) return(0);
        
         
         
         }

         int tik = getNumberTrades();
         
         if ( tik != -1 ) {
               checkProfit(-150);
               return(0);
         }


      
         if ( !canTrade()  ) {
         
            Comment("no trade time");
            return(0);
         }
         else Comment("");
         
        if(NewBar() == false ) return(0);
         
        
 
         int decision = decision(true);
        /* int ratio = MathFloor( AccountBalance() /1000 );
 
          if ( ratio > 0 ) lots = 0.1*(ratio*2);
  */
        if ( decision == 1 ) {
              
               
               

                      ticket = OrderSend(Symbol(),OP_BUY, lots, Ask, 3, 0, Ask+60*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 ) {
         
             ticket =  OrderSend(Symbol(),OP_SELL, lots, Bid, 3, 0, Bid-60*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:


Stochastic oscillator


Custom Indicators Used:
cci_woodies_lnx_v1_1

Order Management characteristics:
It Closes Orders by itself
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
Checks for the total of open orders

Other Features:

It issuies visual alerts to the screen