5 Useful Tips When Programming a Metatrader Expert Advisor (EA)

/tips-when-programming-metatrader-expert

  • As an automated Forex trader who is not a programmer and uses Metatrader 4 Expert Advisors (EAs) I`d like to share my experience with creating strategy codes and how we can make our live easier during real time execution of the trades. The article is designed to help those who seek ways to have a better working EA which is easy for monitoring, backtesting and optimizing. Here are my tips:

    Making all Calculations Once per bar

    The rule applies to all inputs including all entry and exit conditions of the trades. For example, if a given Expert Advisor requires RSI indicator to go above 30 for a short position to be initiated I`d prefer the code to check for this condition to be met after the last bar is completed and if I trade on daily candles the entries will be when the new day begins. So the Expert Advisor checks only once per bar and doesn’t constantly make calculations on every tick.

    Another example would be if the trading logic includes exiting every trade on a trailing stop. Regardless of its concept, the Metatrader 4 Expert should be programmed so that the stop is moved once per bar – usually when the new bar begins.

    How does this concept help us? Well, there are 3 main advantages. 1) We are avoiding possible errors by lessening the calculations the code is making to just one per bar and 2) the Expert Advisor is much more reliable for backtesting because since in MT4 Tester the ticks are made by the platform itself and they are not real. Thus we eliminate a possible disappearance between backtesting and real time results. By observing only closed bars in the code any real time results can be duplicated on backtest. And 3) advantage is the fact that the EA could be backtested in Tester using control points and open options which saves us a lot of computing time.

    Setting up a Timeframe for Every Input

    For each parameter set it is a good practice to have an additional input which will govern the timeframe at which the set will operate. For example if we have a filter and 2 other indicators which are the main strategy, then for each inputs set (filter and the 2 indicators) an input called “timeframe” is created. If all EA`s parameters operate on a same timeframe then we create only one “timeframe” input.

    This tool helps us to avoid any errors of attaching the EA on a wrong time framed chart. So if we have a strategy which operates on H1 and we have an input “timeframe”=60, then it doesn’t matter if we attach the Metatrader Expert Advisor to M5 or D1 and the code will work accordingly.

    When an Input is OFF

    Instead of adding an ON/OFF input which will govern if a certain calculation is switched on or off for every indicator, it is much more convenient just to make the code so that if the particular inputs are 0, then it means that this option is not included in the trading. For example if we have set 0 for a trading stop inputs then it is obvious that we are not using this feature.

    This option makes setting up the EA much more intuitive and reduces the number of inputs we have to deal with.

    Max Bars Look Back

    If the EA uses an indicator for its signals and this indicator is very heavy in a sense that it requires a lot of calculations then I use the tool called “Max Bars“ . It sets up how many bars backwards the indicators will display its dots, lines or whatever it does on the chart. It saves your Metatrader 4 form freezing and stop running properly.

    Personally I don`t use more than 500 for Max Bars and usually it is my default setting which means if you trade on H1, then you will be able to see 20+ days back which is pretty much enough.

    Symmetrical Buy and Sell

    It is tempting to create inputs for both side – long and short which will govern how the EA enters its buys and sells in a separate manner. However besides the curvefitting factor which is another big topic it creates a lot of trouble when you are optimizing the system. It triples your variations and this you will need 3 times more to do all the optimizations needed. This is the reason I use only one inputs for both sides.

    Summary

    I have presented to you 5 practical tips for a creation of a Metatrader Expert Advisor. They are based on personal experience and have helped me making my automated trading more convenient and easier. I hope they will be at your help as well.

    Source - http://professionaltradingsystems.com/blog/tips-when-programming-metatrader-expert-advisor