demBot.v1





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

#define MABULL 1
#define MABEAR -1


//#include <mysql.mqh>

datetime lastOrder =0;
int times =0;
int ticket = 0;

extern int takeprofit = 100;
extern int MAGIC = 35;
extern double lots = 0.1;


int demarkerPeriods = 14;




double tp;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
  
  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 stochasticAlert() {

   double mainLine;
   double prevMainLine;
   double signalLine;
   double prevSignalLine;
      
   for(int a=0;a<=demarkerPeriods ;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 (MABULL);
        }
      if(prevMainLine>prevSignalLine && mainLine<signalLine) {
      
      //   Print("sell " , mainLine , " " , signalLine );
         return (MABEAR);
        }
   }
   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 trailingProfit(int tp, int sl,  int profittarget) {
   
   int tik = getNumberTrades ();

   if ( tik == -1 ) return(0);
/*
     if ( (TimeCurrent() - OrderOpenTime()) > ( 60*60*24 )  && profitCount == 0 &&  OrderProfit() > 0 ) {
   
   
      if ( OrderType() == OP_BUY ) {
               
               OrderClose(OrderTicket(),OrderLots(),Bid,3,Red);
      
      
      }
      else {
         OrderClose(OrderTicket(),OrderLots(),Ask,3,Red);
      
      }
   
   
   
   
   }
   */
   if ( OrderType() == OP_BUY && OrderProfit() > 0 ) {
         
       //  Print(" diff  " ,OrderProfit() , " = > " ,((OrderTakeProfit() - Ask)/Point));
         
         if ( ((OrderTakeProfit() - Ask)/Point) <= profittarget ) {
               OrderModify(tik,OrderOpenPrice(),Bid-sl*Point,Ask+tp*Point,0,Blue);
              
         }
       //  else profitCount++;
   }
   
   if ( OrderType() == OP_SELL && OrderProfit() > 0 ) {
 
            if ( (  (OrderTakeProfit()-Bid)/Point)*(-1) <= profittarget ) {
               OrderModify(tik,OrderOpenPrice(), Ask+sl*Point,Bid-tp*Point,0,Red);
             //  profitCount = 0 ;
            }
            
            //else profitCount++;
   }
   
   if ( (TimeCurrent() - OrderOpenTime()) > ( 60*60*5 ) ){
      
      //Print(OrderProfit());
   
   }

}
  
  
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
  
  /*
 int ratio = MathFloor( AccountBalance() /1000 );
 
 if ( ratio > 0 ) lots = 0.1*(ratio+1);
 */
// Print(lots);


//trailingProfit(40,60,8);

if ( !canTrade()  ) {
         
            Comment("no trade time");
            return(0);
         }
         else Comment("");

 

double markers[20];
ArrayInitialize(markers,0);
double vol = (iMA(NULL,0,14,0,MODE_SMA,PRICE_HIGH,0)-iMA(NULL,0,14,0,MODE_SMA,PRICE_LOW,0))*1000;


//if ( OrdersTotal() > 0 ) Print (" counter " , iDeMarker(NULL,0,14,0));

for(int i=0;i<demarkerPeriods;i++) {
 markers[i] = iDeMarker(NULL,0,demarkerPeriods,i);

}


int x =0, y=0;
bool acima ;
bool abaixo;

for( i=demarkerPeriods-1;i>0;i-- ) {


   
   if ( markers[i] < 0.7 && markers[i-1] > 0.7 ) {
      x = i ;
      abaixo = true;
      continue;
   }
   
   if ( markers[i] > 0.7 && markers[i-1] < 0.7 ) {
      acima = true;
      y = i;
      continue;
   }

}




//if (iDeMarker(NULL,0,14,0) > 0.7 && iDeMarker(NULL,0,14,1) < 0.7) {
int entrySafety = x -y;
if ( abaixo && acima && stochasticAlert() == MABEAR && (TimeCurrent() - lastOrder) > (60*60*3) &&  entrySafety > 5 && iStochastic(0,0,14,3,3,MODE_SMA,0,MODE_MAIN,0) > 80 && iStochastic(0,0,14,3,3,MODE_SMA,0,MODE_SIGNAL,0) > 80 /* MathFloor(demarkerPeriods/2)*/ ) {

         if ( getNumberTrades() < 0  ){
         
              
               times++;

            //   if (  (TimeCurrent() - lastOrder) > (60*60*1.5) && times > 3 ) {
               
               
              if ( takeprofit > 0 ) {
              
                  tp = Bid-takeprofit*Point;
              }
              else tp = 0;
              Print( " vol entry"  , vol , " entry " , x , " , " , y , " = " , (x -y ) , " st " , iStochastic(0,0,14,3,3,MODE_SMA,0,MODE_MAIN,0) , " " , iStochastic(0,0,14,3,3,MODE_SMA,0,MODE_SIGNAL,0), " " , iDeMarker(NULL,0,demarkerPeriods,0));
   
                        ticket =  OrderSend(Symbol(),OP_SELL, lots, Bid, 3, 0, tp,"", MAGIC, 0, Red); 
               
                        if ( ticket > 0 ) {
                       /*
                        
                            mysql_connect("192.168.0.3","forex","forex","forex",3306);
 									 insertOrderDb(ticket,Symbol(),Bid,0, tp,MAGIC,"demBot",OP_SELL, TimeCurrent());
 									mysql_close(mysql);
                       */
                        
                               lastOrder = TimeCurrent();
                               times =0;
                               
                               return(0);
                        }
             //  }
               

         }
         else {
        
                  if (  (TimeCurrent() - lastOrder) > (60*60*0.7) ) {
                  
                     //OrderClose(ticket,lots,Ask,3);
                  }
         
         }

}

int ticket = getNumberTrades() ;


acima =false;
abaixo = false;

for( i=demarkerPeriods-1;i>0;i-- ) {


   
   if ( markers[i] > 0.3 && markers[i-1] <0.3 ) {
      x = i ;
      abaixo = true;
      continue;
   }
   
   if ( markers[i] < 0.3 && markers[i-1] > 0.3 ) {
      acima = true;
      y = i;
      continue;
   }

}

        
entrySafety = x-y;
if ( stochasticAlert() == MABULL && abaixo && acima && entrySafety > 7   && (TimeCurrent() - lastOrder) > (60*60*2) && iStochastic(0,0,14,3,3,MODE_SMA,0,MODE_MAIN,0) > 20  && iStochastic(0,0,14,3,3,MODE_SMA,0,MODE_MAIN,0) < 39 && iStochastic(0,0,14,3,3,MODE_SMA,0,MODE_SIGNAL,0) > 20 && iDeMarker(NULL,0,demarkerPeriods,0) >= 0.31) {

         

         if ( getNumberTrades() > 0  ) return(0);
         
         
         if ( takeprofit > 0 ) {
              
                  tp = Ask+takeprofit*Point;
         }
         else tp = 0;

        ticket =  OrderSend(Symbol(),OP_BUY, lots, Ask, 3, 0, tp,"", MAGIC, 0, Red); 
        
        if ( ticket > 0 ) {
                        Print( " vol entry"  , vol , " entry " , x , " , " , y , " = " , (x -y ) , " st " ,  iStochastic(0,0,14,3,3,MODE_SMA,0,MODE_MAIN,0) , " " , iStochastic(0,0,14,3,3,MODE_SMA,0,MODE_SIGNAL,0) ," " , iDeMarker(NULL,0,demarkerPeriods,0) );
                        /*
                            mysql_connect("192.168.0.3","forex","forex","forex",3306);
 									 insertOrderDb(ticket,Symbol(),Bid,0, tp,MAGIC,"demBot",OP_BUY, TimeCurrent());
 									mysql_close(mysql);
                        */
                        
                               lastOrder = TimeCurrent();
                               return(0);
                        }
                




}



   return(0);
  }





Sample





Analysis



Market Information Used:



Indicator Curves created:


Indicators Used:

Stochastic oscillator
Moving average indicator
DeMarker indicator


Custom Indicators Used:

Order Management characteristics:
Checks for the total of open orders

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

Other Features:

It issuies visual alerts to the screen