DoubleStochastic





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

static datetime dttm;
int     MAGICMA = 20050611;

extern double stopLoss=50;
extern double takeProfit=50;
extern double lotes=1;
extern double Risk=0.1;
extern double step=50000;

double trasformMargin(double margin){
  double newMargin;
  double aux=MathFloor(margin/step);
  double marginAux=margin-(step*aux);
  if(marginAux>0){
      newMargin=marginAux;
  }else{
      newMargin=margin;
  }
  return(newMargin);
}
double LotSize(){
   double acountFree=trasformMargin(AccountFreeMargin());   
   double lotMM = MathCeil(acountFree * Risk / 1000)/10;  
   if (lotMM < 0.1) lotMM = lotes;
   if (lotMM > 1.0) lotMM = MathCeil(lotMM);
   if  (lotMM > 50) lotMM = lotes;
   return (lotMM);
} 

int subGenerateMagicNumber(int MagicNumber, string symbol, int timeFrame)
{
int isymbol = 0;
if (symbol == "EURUSD") isymbol = 1;
else if (symbol == "GBPUSD") isymbol = 2;
else if (symbol == "USDJPY") isymbol = 3;
else if (symbol == "USDCHF") isymbol = 4;
else if (symbol == "AUDUSD") isymbol = 5;
else if (symbol == "USDCAD") isymbol = 6;
else if (symbol == "EURGBP") isymbol = 7;
else if (symbol == "EURJPY") isymbol = 8;
else if (symbol == "EURCHF") isymbol = 9;
else if (symbol == "EURAUD") isymbol = 10;
else if (symbol == "EURCAD") isymbol = 11;
else if (symbol == "GBPUSD") isymbol = 12;
else if (symbol == "GBPJPY") isymbol = 13;
else if (symbol == "GBPCHF") isymbol = 14;
else if (symbol == "GBPAUD") isymbol = 15;
else if (symbol == "GBPCAD") isymbol = 16;
else isymbol = 17;
if(isymbol<10) MagicNumber = MagicNumber * 10;
return (StrToInteger(StringConcatenate(MagicNumber, isymbol, timeFrame)));
}
bool isNewBar(){   
  if (Time[0]!=dttm)     //if opening time changed, it means that we have a new candle
  { dttm=Time[0];        //.... so, store its time
    return (true);       //.... and tell back that is a new one
  }
  return (false);        //else, nothing to do, no new candle*/  
}

  void buy(){
   double precio=NormalizeDouble(Ask,Digits);
   int res=OrderSend(Symbol(),OP_BUY,lotes,precio,5,precio-stopLoss*Point,precio+takeProfit*Point,"",MAGICMA,0,Green);   
   if(res<0){
		 Print("OrderSend failed with error #",GetLastError());
		 return(0);
	}	
	//res=OrderSend(Symbol(),OP_SELLSTOP,lotes,precio-stopLoss*Point,5,precio,precio-takeProfit*Point,"",MAGICMA,0,Red);   
   return;
}

void sell(){
   double precio=NormalizeDouble(Bid,Digits);
   int res=OrderSend(Symbol(),OP_SELL,lotes,precio,5,precio+stopLoss*Point,precio-takeProfit*Point,"",MAGICMA,0,Red);
   if(res<0){
	    Print("OrderSend failed with error #",GetLastError());
	    return(0);
	}		         
	//res=OrderSend(Symbol(),OP_BUYSTOP,lotes,precio+stopLoss*Point,5,precio,precio+takeProfit*Point,"",MAGICMA,0,Green);   
   return;
}  
bool WorkingOrder() {
   int l_ord_total_0 = OrdersTotal();
   for (int l_pos_4 = 0; l_pos_4 < l_ord_total_0; l_pos_4++) {
      OrderSelect(l_pos_4, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MAGICMA) return (TRUE);
   }
   return (FALSE);
  }
  
int closeBuy(){
  int total = OrdersTotal();
  for(int i=total-1;i>=0;i--)
  {
    OrderSelect(i, SELECT_BY_POS);
    if(OrderSymbol()==Symbol()){
      int type   = OrderType();
      bool result = false;    
      switch(type){
        //Close opened long positions
        case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
                            break;    
      }          
    }      
  } 
  return(0);
}

int closeSell(){
  int total = OrdersTotal();
  for(int i=total-1;i>=0;i--)
  {
    OrderSelect(i, SELECT_BY_POS);
    if(OrderSymbol()==Symbol()){
      int type   = OrderType();
      bool result = false;    
      switch(type){        
        //Close opened short positions
        case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
                            break;        
      }          
    }      
  } 
  return(0);
}
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   MAGICMA = subGenerateMagicNumber(MAGICMA, Symbol(), Period());
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
   double stoch1 = iStochastic(NULL, 0, 120, 3, 3, MODE_SMA, 0, MODE_MAIN, 0);
   double stoch2 = iStochastic(NULL, 0, 15, 3, 3, MODE_SMA, 0, MODE_MAIN, 0);
   if(!WorkingOrder()){
      //long trade stoch 1 must be 85 or above and stoch 2 must be 40 or less . stoploss 35 pips. exit the trade at 50 pips or stoch 2 reaches 95.
      if(stoch1>=85 && stoch2<=40){
         lotes=LotSize();
         buy();   
      }
     //short trade stoch 1 must be 10 or less and stoch 2 must be 70 or above.exit the trade at50 pips or stoch 2 reaches 10.
      if(stoch1<=10 && stoch2>=70){
         lotes=LotSize();
         sell();   
      }
   }else{
      if(stoch2>=95){
         closeBuy();
      }
      if(stoch2<=10){
         closeSell();
      }
   }
   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:

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

Other Features:


BackTest : EURUSD on H1

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

BackTest : USDJPY on H1

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

BackTest : EURUSD on H1

From 2009-12-01 to 2010-01-17 Profit Factor:0.00 Total Net Profit:-20.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.65 Total Net Profit:-390.17

BackTest : GBPUSD on H1

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

BackTest : USDCAD on H1

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

BackTest : EURUSD on H1

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

BackTest : GBPUSD on H1

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

BackTest : EURUSD on H1

From 2010-04-01 to 2010-04-30 Profit Factor:0.36 Total Net Profit:-35.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 DoubleStochastic


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

Pair: Period: