Download MetaTrader 5

Watch how to download trading robots for free

Interesting script?
So post a link to it -
let others appraise it

You liked the script? Try it in the MetaTrader 5 terminal

2008.12.17 07:33
Libraries

Universal Expert Advisor scheme - library for MetaTrader 4

| English 日本語 Русский 中文 Español Deutsch Português

Views:
7920
Rating:
votes: 6

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.
Accelerator Oscillator (AC) Accelerator Oscillator (AC)

The Acceleration/Deceleration Indicator (AC) measures acceleration and deceleration of the current driving force.

Average Directional Movement Index (ADX) Average Directional Movement Index (ADX)

The Average Directional Movement Index Indicator (ADX) helps to determine if there is a price trend.

Example of MACD Automated Example of MACD Automated

Code example of MACD automated with advanced money management function.

Pivot Star Pivot Star

Pivot indicator. Standard, Fibonacci, Camarilla, Woody's, Demark. Calculates pivot points from previous bar of chosen period (M5 - MN).