MQL4 - automated forex trading   /  

Code Base

Read Financial Magazines in MetaTrader 4 and MetaTrader 5!

Code Base  Expert Advisors  Moving Average To post a new code, please log in or register

Download MetaTrader 5 and visit MQL5.community Code Base
and Trade on the Go!
and Trade on the Go!
Couldn't find the right code? Order it in the Jobs section

This Expert Advisor is for
MetaTrader 4
Download MT 4 - 455 Kb

and Trade on the Go!

and Trade on the Go!

22.10.2011 13:48 barauskasjustin

Name:
Moving Average [ ru | cn ]
Author: MetaQuotes (2005.11.29 13:04)
Rating: 9
Downloaded: 44530
Download:
 Moving Average.mq4 (4.6 Kb) View
 Moving Average TesterGraph2.gif (3.7 Kb)


    The Moving Average expert for forming trade signals uses one moving average. Opening and closing of positions are performed when the moving average meets the price at the recently formed bar (bar index equals to 1). The lot size will be optimized according to a special algorithm.

    The expert advisor analyzes concurrence of the moving average and the market price chart. The checking is performed by the CheckForOpen() function. If the moving average meets the bar in such a way that the former is higher than Open price but lower than Close price, the BUY position will be opened. If the moving average meets the bar in such a way that the former is lower than Open price but higher than Close price, the SELL position will be opened.

    Money Management used in the expert is very simple, but effective: the control over each position volume is performed depending on the previous transactions results. This algorithm is implemented by the LotsOptimized() function. The basic lot size is calculated on basis of the maximum allowable risk:

    lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);

    The MaximumRisk parameter displays the basic risk percentage for each transaction. It usually possesses a value between 0.01 (1%) and 1 (100%). For example, if free margin (AccountFreeMargin) equals to $20,500 and rules of capital management prescribe to use risk of 2%, the basic lot size will make 20500 * 0.02 / 1000 = 0.41. It is very important to control over the lot size accuracy and to normalize the result with the allowable values. Normally, fractional lots with step of 0.1 are allowed. A transaction having volume of 0.41 will not be performed. To normalize, the NormalizeDouble() function is used with accuracy up to 1 character after the point. This results in the basic lot of 0.4. The basic lot calculation on basis of free margin allows to increase in volumes of operation depending on trading successfulness, i.e., to trade with reinvesting. This is the basic mechanism with obligatory capital management for increasing of trading effeсtiveness.

    DecreaseFactor is the extent to which the lot size will be reduced after unprofitable trading. Normal values are 2,3,4,5. If the preceding transactions were unprofitable, the subsequent volumes will decrease by a factor of DecreaseFactor in order to wait through the unprofitable period. This is the main factor in the capital management algorithm. The idea is very simple: if trading is successfully increasing, the expert works with the basic lot making maximum profit. After the very first unprofitable transaction, the expert will "reduce the speed" until a new positive transaction is made. The algorithm allows to disable "speed reducing", for doing it, one has to specify DecreaseFactor = 0. The amount of the last successive unprofitable transactions is calculated in the trade history. The basic lot will be recalculated on this basis:

    if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);

    Thus, the algorithm allows to effectively reduce the risk occurring as a result of a series of unprofitable transactions.The lot size is obligatorily checked for the minimum allowable lot size at the end of the function because the previously made calculations can result in lot = 0:

    if(lot<0.1) lot=0.1;

    The expert is mainly intended for working with daily period, and in the testing mode - for doing at close prices. It will trade only at opening of a new bar, that is why the modes of every-tick modeling are not needed.

    Testing results are represented in the report.

Strategy Tester Report

Moving Average


SymbolEURUSD (Euro vs US Dollar)
Period1 Hour (H1) 2003.01.08 00:00 - 2003.11.25 00:00
ModelEvery tick (based on all available least timeframes with fractal interpolation of every tick)
ParametersLots=0.1; MaximumRisk=0.01; DecreaseFactor=1; MovingPeriod=16; MovingShift=11;

Bars in test19371Ticks modelled656918Modelling quality25.00%

Initial deposit10000.00



Total net profit1695.20Gross profit4293.20Gross loss-2598.00
Profit factor1.65Expected payoff10.80

Absolute drawdown40.35Maximal drawdown (%)318.50 (3.0%)


Total trades157Short positions (won %)73 (26.03%)Long positions (won %)84 (32.14%)

Profit trades (% of total)46 (29.30%)Loss trades (% of total)111 (70.70%)
Largestprofit trade262.55loss trade-91.00
Averageprofit trade93.33loss trade-23.41
Maximumconsecutive wins (profit in money)2 (387.15)consecutive losses (loss in money)7 (-287.25)
Maximalconsecutive profit (count of wins)387.15 (2)consecutive loss (count of losses)-287.25 (7)
Averageconsecutive wins1consecutive losses3
12 comments: 1 2   To post a new comment, please log in or register

No comment.

26.02.2014 11:31 broketrader


DIFFERENT SETTINGS - LIKE THE ONES METAQUOTES USED

SymbolEURUSDFXF (Euro vs US Dollar)
Period1 Hour (H1) 2007.03.30 17:01 - 2011.09.30 00:59 (2007.03.01 - 2011.06.20)
ModelEvery tick (the most precise method based on all available least timeframes)
ParametersLots=0.1; MaximumRisk=0.01; DecreaseFactor=1; MovingPeriod=16; MovingShift=11;
Bars in test28117Ticks modelled34632921Modelling quality99.00%
Mismatched charts errors0
Initial deposit1000000.00
Total net profit-424287.00Gross profit1015708.80Gross loss-1439995.80
Profit factor0.71Expected payoff-272.50
Absolute drawdown426566.80Maximal drawdown445606.40 (43.73%)Relative drawdown43.73% (445606.40)
Total trades1557Short positions (won %)778 (21.34%)Long positions (won %)779 (29.40%)
Profit trades (% of total)395 (25.37%)Loss trades (% of total)1162 (74.63%)
Largestprofit trade101270.40loss trade-36944.00
Averageprofit trade2571.41loss trade-1239.24
Maximumconsecutive wins (profit in money)4 (17427.00)consecutive losses (loss in money)23 (-2310.40)
Maximalconsecutive profit (count of wins)129294.80 (3)consecutive loss (count of losses)-44613.40 (4)
Averageconsecutive wins1consecutive losses4
SymbolEURUSDFXF (Euro vs US Dollar)
Period1 Hour (H1) 2007.03.30 17:01 - 2011.09.30 00:59 (2007.03.01 - 2011.06.20)
ModelEvery tick (the most precise method based on all available least timeframes)
ParametersLots=0.1; MaximumRisk=0.02; DecreaseFactor=3; MovingPeriod=12; MovingShift=6;
Bars in test28117Ticks modelled34632921Modelling quality99.00%
Mismatched charts errors0
Initial deposit10000.00
Total net profit2786.20Gross profit71494.00Gross loss-68707.80
Profit factor1.04Expected payoff1.26
Absolute drawdown600.60Maximal drawdown3375.60 (24.72%)Relative drawdown24.72% (3375.60)
Total trades2205Short positions (won %)1102 (25.50%)Long positions (won %)1103 (28.92%)
Profit trades (% of total)600 (27.21%)Loss trades (% of total)1605 (72.79%)
Largestprofit trade1155.60loss trade-1006.80
Averageprofit trade119.16loss trade-42.81
Maximumconsecutive wins (profit in money)6 (353.40)consecutive losses (loss in money)18 (-650.40)
Maximalconsecutive profit (count of wins)1170.00 (4)consecutive loss (count of losses)-1280.80 (9)
Averageconsecutive wins1consecutive losses4
22.10.2011 13:41 barauskasjustin

see this scalping EA

01.10.2011 16:17 FSS

hi there,

is it possible to remove the auto-close features?

04.04.2011 07:45 ghost1980

Simple but useful money management.

One comment: HistoryTotal() is the old name of that function and has been replaced by OrdersHistoryTotal.

So, in the Moving average.mq4 code you need to replace this sentence:

int ClosedOrders=HistoryTotal();

with this one

int ClosedOrders=OrdersHistoryTotal();

19.01.2010 13:29 blackhawk

My test results:

Strategy Tester Report
Moving Average
FXDD-MT4 Live Server (Build 220)

SimboloEURUSD (Euro vs. United States Dollar)
Periodo1 Ora (H1) 2007.07.02 01:00 - 2009.01.05 23:00 (2007.07.01 - 2009.01.06)
ModelloOgni tick (il metodo precissimo sulla base di tutti gli intervalli di tempo minimi)
ParametriLots=0.1; MaximumRisk=0.02; DecreaseFactor=3; MovingPeriod=12; MovingShift=6;

Barre sotto esame10284Ticks adoperati per il modello3949236Qualita' del modello90.00%
Errori di grafici16




Deposito iniziale5000.00



Profitto totale netto1306.60Profitto lordo6988.01Perdita lorda-5681.41
Fattore di profitto (profit factor)1.23Ricompensa attesa3.19

Drawdown assoluto259.11Drawdown massimo637.92 (9.26%)Drawdown relativo9.26% (637.92)

Operazioni totali409Posizioni al ribasso (vincite %)229 (25.33%)Posizioni al rialzo (vincite %)180 (27.78%)

Operazioni con profitto (% del totale)108 (26.41%)Operazioni in perdita (% del totale)301 (73.59%)
Il piu' grandeoperazione con profito462.31operazione in perdita-109.70
Mediaoperazione con profito64.70operazione in perdita-18.88
Massimovincite consecutive (profitto in denaro)3 (11.96)perdite consecutive (perdita in denaro)20 (-140.16)
Massimaleprofitto consecutivo (numero delle vincite)462.31 (1)perdita consecutiva (numero delle perdite)-463.86 (11)
Mediavincite consecutive1perdite consecutive3
06.01.2009 17:20 alberto
if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);

Function is incorrect i.m.o.

if(losses>1 && DecreaseFactor > losses) lot=NormalizeDouble(lot-lot*(losses/DecreaseFactor),1);

should work better.


23.11.2008 18:36 Russell
What are the stoploss/trailing stops? I can't find them anywhwere. Or does this close by % loss alone?

I tested it w/ 400.00, and got stopped on one with -$3.10...which is nowhere near the 2% loss the thing is set at???
01.12.2007 09:58 saint_berzerker
Umm, a modeling quality of 25% is a bit too low.
29.07.2007 20:13 dna