Download MetaTrader 5

d’Alembert Forex EA

To add comments, please log in or register
Carry on your private correspondence in your profile. It is secure!
jimbofx7
88
jimbofx7 2014.12.07 23:19 

Hi all,

I try to code an EA that handle with d'alembert progression (for all that don´t know progression d'alembert: http://www.bettingexpert.com/casino/roulette/strategy/d%E2%80%99alembert-system )

The plan is to start with 0.01 lots and SL 13 pips / TP 14 pips.

Each time a trade close with a loss, a new trade open immediately with additional 0.01 lots and after a win trade next trade open immediately with 0.01 lots less than the lotsize before until we arrive at 0.01 lots.


Example:

1. trade      0.01 lots      loss        

2. trade      0.02 lots      loss

3. trade      0.03 lots      loss

4. trade      0.04 lots      loss

5. trade      0.05 lots      win

6. trade      0.04 lots      win

7. trade      0.03 lots      win

8. trade      0.02 lots      win

9. trade      0.01 lots      win


So here is my first code try but it doesn´t work in the startegy tester. :-(

Who can help?


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


//+------------------------------------------------------------------+
//| extern input parameters                                          |
//+------------------------------------------------------------------+


extern int MagicNumber_101=101000; 
extern int SL=13;
extern int TP=14;

double dXPoint=1;


//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----



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

   {
 
double Lotsize = 0.01;

   if (OrdersHistoryTotal()>=1)
   {
      OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS);
      Lotsize = OrderLots();
   }
    
      if (OrderProfit()<0 && OrdersHistoryTotal()>=1)
         {
            Lotsize = Lotsize + 0.01;
         }
      
      if (OrderProfit()>0 && OrdersHistoryTotal()>=1 && Lotsize>=0.02)
         {
            Lotsize = Lotsize - 0.01;
         }
      
      
   if (OrdersHistoryTotal()<1 && Minute()==00)
   
      {  
         OrderSend(Symbol(),OP_BUY,0.01,MarketInfo(Symbol(),MODE_ASK),2,Bid-SL*Point,Bid+TP*Point,"buy",MagicNumber_101,0,CLR_NONE);
      }
 
Sleep(120000);
   
   if (OrdersTotal()==0 && Minute()==00)
      {
         OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS);   
         if (OrderProfit()>0 && OrderType()==OP_BUY)
         {        
            OrderSend(Symbol(),OP_BUY,0.01,MarketInfo(Symbol(),MODE_ASK),2,Bid-SL*Point,Bid+TP*Point,"buy",MagicNumber_101,0,CLR_NONE);
         }
         Sleep(120000);
      }
      
   if (OrdersTotal()==0 && Minute()==00)
      {               
         if (OrderProfit()>0 && OrderType()==OP_SELL)
         {        
            OrderSend(Symbol(),OP_SELL,0.01,MarketInfo(Symbol(),MODE_BID),2,Ask+SL*Point,Ask-TP*Point,"sell",MagicNumber_101,0,CLR_NONE);
         }
         Sleep(120000);
      }
     
   if (OrdersTotal()==0 && Minute()==00)
      {   
         if (OrderProfit()<0 && OrderType()==OP_SELL)
         {        
            OrderSend(Symbol(),OP_BUY,0.01,MarketInfo(Symbol(),MODE_ASK),2,Bid-SL*Point,Bid+TP*Point,"buy",MagicNumber_101,0,CLR_NONE);
         }
         Sleep(120000);
      }

   if (OrdersTotal()==0 && Minute()==00)
      {         
         if (OrderProfit()<0 && OrderType()==OP_BUY)
         {        
            OrderSend(Symbol(),OP_SELL,0.01,MarketInfo(Symbol(),MODE_BID),2,Ask+SL*Point,Ask-TP*Point,"sell",MagicNumber_101,0,CLR_NONE);
         }
         Sleep(120000);
      }
   return(0);   
}
        


Please no comments like "Blow your account away...blabla"

Just sharing a trading strategy.


Regards.

Keith Watford
Moderator
9647
Keith Watford 2014.12.08 00:45  

It is probably best not to assume that OrdersHistory Total-1 is the last closed trade. Check the close time.

Why all the sleeps?

The first 2 minute sleep makes it likely that you will miss the window when Minute equals 0 

   if (OrdersTotal()==0 && Minute()==00)
      {
         OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS);   
         if (OrderProfit()>0 && OrderType()==OP_BUY)
         {        
            OrderSend(Symbol(),OP_BUY,0.01,MarketInfo(Symbol(),MODE_ASK),2,Bid-SL*Point,Bid+TP*Point,"buy",MagicNumber_101,0,CLR_NONE);
         }
         Sleep(120000);
      }

 If minute equals 0 for the first if and conditions are not satisfied, the 2 minute sleep will guarantee that minute will not equal 0 for the following 3 "ifs"

jimbofx7
88
jimbofx7 2014.12.08 08:25  

I just want to avoid more than one open position and had bad experience without sleep-function.

But you are right one sleep-function at the end will be enough.

SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI 2014.12.08 09:02  
jimbofx7:

I just want to avoid more than one open position and had bad experience without sleep-function.

Have a loop to count open positions and have your condition to be done next there. Can be done quite easy.
To add comments, please log in or register