Voir comment télécharger gratuitement des robots de trading
Retrouvez-nous sur Facebook !
Rejoignez notre page de fans
Un script intéressant ?
Poster un lien vers celui-ci -
laisser les autres l'évaluer
Vous avez aimé le script ? Essayez-le dans le terminal MetaTrader 5
Bibliothèque

Universal Expert Advisor scheme - bibliothèque pour MetaTrader 4

Vues:
13406
Note:
(13)
Publié:
2008.12.17 07:33
Mise à jour:
2016.11.22 07:32
MQL5 Freelance Besoin d'un robot ou d'un indicateur basé sur ce code ? Commandez-le sur Freelance Aller sur Freelance

Time to time I get a new idea about some new strategy to implement and to see results in strategy tester. Before, I still copied my previous EA and deleted all unwanted stuff
and added my new stuff, so the result was new EA. Some time ago I find a better way for me. I developed a template or scheme for most of my future EAs.
This happened when I was thinkink about more strategies in one EA and about switching between them, according to a period (trend, choppy,... ).
I needed EA scheme that would allow me to have whole "action" code together and whole "strategy" code together. I came to a scheme I want to share with you today.

Main structure of this EA is following:

ActionCode - this contains code independent of strategy implemented in this EA
StrategyCode - this contains whole strategy code

Everything, ActionCode part has to decide to do, is asked by calling StrategyCode part, which returns a result, which can be executed again by universal ActionCode part.

ActionCode Part

int   _STRATEGY_NUMBER              = 1;

#define     _OPEN_LONG                    1
#define     _OPEN_SHORT                   2
#define     _CLOSE_LONG                   3
#define     _CLOSE_SHORT                  4
#define     _GET_LONG_STOPLOSS_PRICE      5
#define     _GET_SHORT_STOPLOSS_PRICE     6
#define     _GET_LONG_TAKEPROFIT_PRICE    7
#define     _GET_SHORT_TAKEPROFIT_PRICE   8
#define     _GET_LOTS                     9
#define     _GET_TRAILED_STOPLOSS_PRICE   10
#define     _GET_TRAILED_TAKEPROFIT_PRICE 11
#define     _GET_TRADED_TIMEFRAME         12
#define     _OPEN_PENDING_BUY_STOP        13
#define     _OPEN_PENDING_SELL_STOP       14
#define     _OPEN_PENDING_BUY_LIMIT       15
#define     _OPEN_PENDING_SELL_LIMIT      16
#define     _GET_PENDING_BUY_STOP_PRICE   17
#define     _GET_PENDING_SELL_STOP_PRICE  18
#define     _GET_PENDING_ORDER_EXPIRATION 19

int start()
{
   double            Stoploss          = 0;
   double            TakeProfit        = 0;
   
   if(LastBarTraded())
      return(0);

   if(OrdersTotal() > 0)
   {
      Stoploss = Strategy(_STRATEGY_NUMBER, _GET_TRAILED_STOPLOSS_PRICE);
      TakeProfit = Strategy(_STRATEGY_NUMBER, _GET_TRAILED_TAKEPROFIT_PRICE);
   
      if(Stoploss != 0 || TakeProfit != 0)
         ModifyAllPositions(_MAGICNUMBER, Stoploss, TakeProfit);

      if(Strategy(_STRATEGY_NUMBER, _CLOSE_LONG) == 1)
         CloseAllLongPositions(_MAGICNUMBER);
      if(Strategy(_STRATEGY_NUMBER, _CLOSE_SHORT) == 1)
         CloseAllShortPositions(_MAGICNUMBER);
   }
   

   if(!TradeAllowed(1))
      return(0);

   if(Strategy(_STRATEGY_NUMBER, _OPEN_LONG) == 1)
      OpenPosition(false, Strategy(_STRATEGY_NUMBER, _GET_LOTS), Strategy(_STRATEGY_NUMBER, _GET_LONG_STOPLOSS_PRICE), 
      Strategy(_STRATEGY_NUMBER, _GET_LONG_TAKEPROFIT_PRICE), 3, _MAGICNUMBER);
   if(Strategy(_STRATEGY_NUMBER, _OPEN_SHORT) == 1)
      OpenPosition(true, Strategy(_STRATEGY_NUMBER, _GET_LOTS), Strategy(_STRATEGY_NUMBER, _GET_SHORT_STOPLOSS_PRICE), 
      Strategy(_STRATEGY_NUMBER, _GET_SHORT_TAKEPROFIT_PRICE), 3, _MAGICNUMBER);

   if(Strategy(_STRATEGY_NUMBER, _OPEN_PENDING_BUY_STOP) == 1)
      OpenPendingPosition(false, Strategy(_STRATEGY_NUMBER, _GET_LOTS), Strategy(_STRATEGY_NUMBER, _GET_PENDING_BUY_STOP_PRICE), 
      Strategy(_STRATEGY_NUMBER, _GET_LONG_STOPLOSS_PRICE), Strategy(_STRATEGY_NUMBER, _GET_LONG_TAKEPROFIT_PRICE), 3, _MAGICNUMBER, 
      Strategy(_STRATEGY_NUMBER, _GET_PENDING_ORDER_EXPIRATION));
   if(Strategy(_STRATEGY_NUMBER, _OPEN_PENDING_SELL_STOP) == 1)
      OpenPendingPosition(true, Strategy(_STRATEGY_NUMBER, _GET_LOTS), Strategy(_STRATEGY_NUMBER, _GET_PENDING_SELL_STOP_PRICE), 
      Strategy(_STRATEGY_NUMBER, _GET_SHORT_STOPLOSS_PRICE), Strategy(_STRATEGY_NUMBER, _GET_SHORT_TAKEPROFIT_PRICE), 3, _MAGICNUMBER, 
      Strategy(_STRATEGY_NUMBER, _GET_PENDING_ORDER_EXPIRATION));

   return(0);
}

StrategyCode part

double Strategy(int STRATEGY, int COMMAND)
{
   switch(STRATEGY)
   {
// Short description of strategy
      case 1:
      {
         return(Strategy_001(COMMAND));
      }
// Short description of strategy
      case 2:
      {
         return(Strategy_002(COMMAND));
      }

//...

// Short description of strategy
      case 99:
      {
         return(Strategy_099(COMMAND));
      }
   }

   return(0);
}


double Strategy_001(int COMMAND)
{
   string   _SYMBOL        = Symbol();
   int      _TIMEFRAME     = getStrategyTimeframeByNumber(_STRATEGY_TIMEFRAME);
         
   double   result         = 0;
   
   int      i;

   switch(COMMAND)
   {
      case _OPEN_LONG:
      {
//         break;

//         if(!OpenNewBar())
//            break;

         if(Open long condition is true)
            result = 1;

         break;
      }
      case _OPEN_SHORT:
      {
//         break;

//         if(!OpenNewBar())
//            break;

         if(Open short condition is true)
            result = 1;

         break;
      }
      case _CLOSE_LONG:
      {
//         break;

         if(Close long condition is true)
            result = 1;
            
            break;
      }
      case _CLOSE_SHORT:
      {
//         break;

         if(Close short condition is true)
            result = 1;
            
            break;
      }
      case _GET_LONG_STOPLOSS_PRICE:
      {
//         break;

         result = Long SL price
         
         break;
      }
      case _GET_SHORT_STOPLOSS_PRICE:
      {
//         break;

         result = Short SL price
         
         break;
      }
      case _OPEN_PENDING_BUY_STOP:
      {
         break;
      }
      case _OPEN_PENDING_SELL_STOP:
      {
         break;
      }
      case _GET_PENDING_BUY_STOP_PRICE:
      {
         break;
      }
      case _GET_PENDING_SELL_STOP_PRICE:
      {
         break;
      }
      case _GET_LONG_TAKEPROFIT_PRICE:
      {
         break;
      }
      case _GET_SHORT_TAKEPROFIT_PRICE:
      {
         break;
      }
      case _GET_TRAILED_STOPLOSS_PRICE:
      {
         break;
      }      
      case _GET_TRAILED_TAKEPROFIT_PRICE:
      {
         break;
      }
      case _GET_LOTS:
      {
         result = Lot amount;

         break;
      }
      case _GET_TRADED_TIMEFRAME:
      {
         result = _TIMEFRAME;

         break;
      }
      case _GET_PENDING_ORDER_EXPIRATION:
      {
         break;
      }
   }
      
   return(result);
}

You can see that implementation of new strategy in this scheme is very easy. Just put a new CASE in Strategy() function and add new function Strategy_NNN() with implemented strategy rules.

This scheme does not fit to any strategy, just to ones, which open one position at time and have a standard "workflow".

As an example I attach complete code with few loosing strategy implemented (see attached code)

I hope many of you can be inspired by this and maybe some new ideas can come from you, to improve this concept.
DiNapoli Price Oscillator DiNapoli Price Oscillator

New version of famous and simple indicator created by Joe DiNapoli. It is different than rest of this type indicators, because it could count and display overbought and oversold levels.

Candle Patterns Candle Patterns

Visual and Audio alerts when reversal or continuation candle patterns occur.

21hour 21hour

We place two pending orders at the specified time and delete one of them when another one triggers.

MTrendLine is Very Convenient Autoregulator of Pendings in Reference to the Trend Lines MTrendLine is Very Convenient Autoregulator of Pendings in Reference to the Trend Lines

You often have to move the pending order every hour in reference to the trend line and it embarasses you concerning your time. That's why this EA (semi-automated) was developed, it totally relieves from tracking the trend line and from moving the pending