Free Forex MQL Training

Free Forex, Candlestick Charts, and MetaTrader Training

A Sample Expert Advisor Uses Moving Averages E-mail
Written by Al Parsai   
Sunday, 08 March 2009

Sponsor Articles

If you are familiar with MQL programming you know how easily you could automate many of trading systems. MQL or rather MetaQuotes Language is equipped with several tools that enable the programmer/trader to turn his/her ideas into an automated system. Of course you may also use MQL to develop scripts in order to manage complex tasks or develop custom indicators to analyze the market. Nonetheless, many programmers/traders use MQL to develop fully automated trading systems or rather Expert Advisors. Click here for more information about MQL/MetaTrader programs.

 

The purpose of this article is to share a sample Expert Advisor with you. This is an educational EA that was first suggested by one of my students. Thanks to him I spent about two hours to develop and test the EA. It is now available to all our visitors.

 

Caution: This is an educational EA. Do not trade it live. The intention of this EA is to show you how you can develop an Expert Advisor not to offer you a proftable trading system.

 

 

How "Sample Moving Average EA" Enters Trades

This EA uses two exponential moving averages (EMAs) to enter trades. If the fast moving average (default period 5) crosses up the slow moving average (default period 25) then the EA enters a long trade. If the fast EMA crosses down the slow EMA it enters a short trade.

The EA ignores the current bar. It uses the past two bars to spot the cross.

 

The EA enters one trade at a time. So if a trade with this EA on the current currency pair is already open it does not enter a new trade. You may attach this EA to any Forex chart or any time frame. If you attach it to more than one chart at a time then it could open concurrent trades on different currency pairs but not on the same currency pair.

 

 

How "Sample Moving Average EA" Exits Trades

The EA places a Stop Loss and a Take Profit for each trade. The default values of the SL and TP are 200. You may change these values. The EA closes the trade if the trade hits SL or TP. An alternative situation is when the market direction changes. For example if a long trade is open and fast moving average crosses down the slow moving average the EA closes the long trade and enters a short trade.

 

How "Sample Moving Average EA" Avoids Multiple Trades

The "sample_ma_trader" uses a Global Variable or GV to store the time it entered the latest trade. It then compares the open time of the current bar with the registered time. It the registered time falls into the current bar or candle then it does not enter a new trade. This is a safe approach as MetaTrader saves the value of GVs even if you shut down the platform for up to four weeks.

 

The EAs Input Variables

This Expert Advisor comes with some input variables that could help in enhancing its behaviour (see image).

Moving Average Expert Advisor Input Variables

(Click imgage to enlarge)

 

  • MA_Fast: The fast EMA period
  • MA_Slow: The slow EMA period
  • stop_loss: The value of Stop Loss in pips
  • take_profit: The value of Take Profit in pips
  • LotsPerTrade: The number of lots you wish to trade

 

Other Features

This is EA is compatible with fractional pips also known as fifth digit.

 

Download the EA [download]

Click here to download the EA. I have documented the code so you may understand how it analyzes the market and enters or exits trades. The code does not include error management so if something goes wrong it may not be able to address it correctly. Please do not use it in live environment.

 

If you would like to learn MQL programming, click here for your options.

 

Important Note: This EA is not designed for live trading. I have designed it as a sample Expert Advisor to show you how you may develop one. Do not consider it as a trading system. Do not trade live with this EA. Also I do not take responsibility of the results you may get by using this Expert Advisor or any other tools published on ForexBrace.com. Neither this website nor I offer financial advice. I am also not responsible for any malfunctions of the programs offered on this website. Risk Warning!

 

 

 

Discuss this article!

 

Add Comments!

 

 

 

 

 

Last Updated ( Monday, 16 March 2009 )
 
< Prev   Next >

Learn MQL (MetaTrader Programming) one on one

Our Favourites

MetaTrader Window

Metatrader Colours

Candlestick Charts Training

Pip in Forex Trading

MetaTrader Brokers

Subscribe to Our Mailing List
Sister Websites

Investatech.com

PipBoxer.com

GridBoxer.com

PBHelp.com

AlParsai.com

AParsai.ca

Settler.ca

IdealQualityEvents.ca

RSS Feed

 

SiteMap RSS

ForexBraceToolbar

Download Free ForexBrace.com Toolbar