MQL4 - automated forex trading   /  

Code Base

A Method of Drawing the Support/Resistance LevelsA Method of Drawing the Support/Resistance Levels Selective Kendall rank correlationSelective Kendall rank correlation Try product
Selective Kendall rank correlation
Author: tonjo
Screenshot
EURUSD, H1
Demo
Trendlines DayIndicator
Trendlines Day
Author: Scriptor
Subscribe to signal
VIPEA OANDA
36 707 124.14%, 1 311.04 USD

Code Base  Indicators  Optimize cross 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!
Prepare for the Championship
Couldn't find the right code? Order it in the Jobs section

This library is for
MetaTrader 4
Download MT 4 - 455 Kb

and Trade on the Go!

and Trade on the Go!

Name:
Optimize cross Moving average
Author: tflores (2010.03.03 13:15)
Rating: 8.8
Downloaded: 10599
Download:
 MAProfit.mq4 (24.0 Kb) View
 MAProfit2.mq4 (2.5 Kb) View

Updated Version.

See description below.



Description:

Still believing in Cross Moving Average and -- as a programmer -- always seeking the simplest solution. I came acros the sentance "there is no magic setting for cross MA". This indicator tries out a lot of settings each time the time frame or the symbol changes or even one each new candle. It works by dull "trading" the last 100 or so candles and choosing the settings with the best success. It is simply measuring the distance between a short and a long signal, as if anybody have traded this without stop loss. It takes the spread into account.

The lower window shows the distance between the short and the long Moving Average positive values are for long trades, negative values are for short trades in pips. Using the "profit oszilator" you can end a trade in profit by examining if short/long trades have a maximum difference and ending right before the maxium.

The upper line says "Profit today with MA 5/19 is 60 pips". The indicator or the user has choosen 5 for the fast MA and 19 for the slow MA. The next text field displays the results from yesterday followed by the signal Long or Short. Traders may like to drop two moving averages to the chart and set them up to the given value.

I am searching for more different MA recomondations in literatur.

Parameters

  • PeriodShort=6;
    Period for the fast MA. Ignore if optimize is true
  • PeriodLong=40;
    Period for the slow MA. Ignore if optimize is true
  • Method=0;
    Method for iMA
  • Optimize=true;
    The indicator automatically chooses values for fast and slow MA
  • DrawTringles=true;
    Draw triangles into the chart
  • MinShortMA=2;
    MaxShortMA=20;
    MaxLongMA=100;
    Min and Max values for the optimization, it will try out values between 2 and 20 for the fast MA and 7 to 100 for the slow MA
  • StepLongMA=5;
    StepShortMA=5;
    For speeding up the search, it is trying out every third value
  • CountOptimize=200;
    It is analyzing 200 candles from the past. The more candles you analyze the slower it will be, a big number may also result in less good results
  • OptimizeOnNewCandle=false;
    Start optimization on each new candle. Note: Optimization can take some time and slow down your terminal
  • Alarm=true;
    Ring the bell if a new signal arises

Next step, I want to create an expert Advisor of it, however I am still wondering how to detect a sidwards trend which should not be traded with cross MA. Until now my EA based on optimized cross MA sometimes makes excellent gains and burns it the next day.

Updated Version

New features:



- The indicator draws now the moving averages inside the chart, the "profit oszilator" is inside a different indicator (MAProfit2), both communicate with global variables


- Supports MA Channels (see ebook at www.vnchanger.org), the slow moving average is split into two lines, one for low and one for high values, this should avoid losses in sidewards market


- Instead of testing all combinations, it can test certain MA ranges found in the literature. To do this set OptimizeAll to false and OptimizeSystems to true.


You can add or modify the systems table. Be sure to terminate it with 0,0,0,0,0,0



extern bool OptimizeAll=false;
extern bool OptimizeSystems=true;

int Systems[] = {PRICE_MEDIAN,MODE_SMA,50, PRICE_MEDIAN,MODE_SMA,200,
PRICE_MEDIAN,MODE_SMA,50, PRICE_MEDIAN,MODE_SMA,100, // Death Cross
PRICE_MEDIAN,MODE_SMA,10, PRICE_MEDIAN,MODE_SMA,40,
PRICE_MEDIAN,MODE_SMA,13, PRICE_MEDIAN,MODE_SMA,26,
PRICE_MEDIAN,MODE_SMA,5, PRICE_MEDIAN,MODE_SMA,10,
PRICE_CLOSE, MODE_EMA,5, PRICE_OPEN, MODE_EMA,6,
PRICE_MEDIAN,MODE_SMA,3, PRICE_MEDIAN,MODE_SMA,8,

0,0,0,0,0,0};


- New alerts can be given as voice, in order to support this, you need to download gspeak, for example from http://codebase.mql4.com/5036



If you do not want voice, you need to modify the code. Remove the lines from #import "speak.dll" until #import and uncomment the gSpeak function. Thanks the autor for this wonderful DLL.



#import "speak.dll"
void gRate(int rate);
void gVolume(int rate);
void gPitch(int rate);
void gSpeak(string text);
#import

// if you do not have (or want) the speach.dll uncomment this
/*
void gSpeak(string x)
{
}
*/

If you don't remove the voice, after some profit you may begin to love "Oncle Sams" voice speaking.



- At the firsts start or on parameter change, it remembers the candle on the first trade, this should avoid re-painting old trades with different ones.



- The treeangles now have three colors: Green for long trades, Red for short trades and Violett for trades with loss (long or short). The colors can be modified in the source code:
int ColorLongTrade = MediumSpringGreen;
int ColorShortTrade = Red;
int ColorBadTrade = Violet;

- The steps in MA Optimization have been set to 5

- The internal name of this indicator has been changed to SMA (Smart Ass ... it displays the how you should have traded aferwards).

Image:


MA Optimizer


34 comments: 1 2 3 4   To post a new comment, please log in or register
Is it possible to have a version that draws not a channel for the slow ma, but just the two MAs (slow and fast one) ? Thanks.
05.01.2013 03:23 airquest
tflores:

It is an indicator, it only has access to the bars, open, close, high and low. The trades are made by a certain point on the candle, which of course is differnt than running it as an EA on tick level. Until there is no way to work with old tick data, it can not be more precise.

However I hope that the optimized curves are better than expecting a certain combination of periods for all time.

Can you make a custom function that we can optimized whatever indicator?

I tried to understand your code, but still unclear to me.



03.07.2012 21:01 blap

It is an indicator, it only has access to the bars, open, close, high and low. The trades are made by a certain point on the candle, which of course is differnt than running it as an EA on tick level. Until there is no way to work with old tick data, it can not be more precise.

However I hope that the optimized curves are better than expecting a certain combination of periods for all time.

10.01.2012 04:26 tflores
Is there a working EA for this? Thanks.
11.06.2011 19:02 hedgehogging

Hello tflores, it is a very usefull indicator.

When I compile it I get these errors. What should I do?

13.12.2010 16:51 tntpeter

I've not looked at the code yet, but judging from the triangles the indicator points on the chart, it looks like you enter a new trade magically at the open price of the bar which will generate the MA cross.

Then - after the cross has really been generated, you exit the old trade. In other word - your optimizations seems to posess some magical vision of the future regarding the entries...


m.

19.11.2010 18:02 mfeldt

I did not understand, how it changes the MA with time frames.Please explain.

Did u created EA ?

07.08.2010 06:16 shaival
It looks great, works best on 30 mins and Daily charts.
07.08.2010 06:15 shaival

Hello Thomas,

Your MAProfit indicator runs well, it looks like gaining profits under M15 and M30 timeframe. But there are some points can be improved.

It repaints sometimes, leading to opening and closing orders improperly. Then how to modify or fix?

To test the indicator and strategy, we have to develop and EA according to the signals. It should have stoploss and takeprofit. You mentioned you have developed an EA, can you send to me by email or post on mql4.com?

If the gains orders are more than the losing orders, that will be OK.

I hope to receive your great EA soon, appreciate.

email: shaqichen@qq.com; shaqichen@163.com;

Peter

30.03.2010 11:21 shaqichen
You could keep this out of Sideways market using RSI thresholds. I.e. do not trade unless RSI is above 60 or below 40. If you use MA and RSI, and pair it with stop-loss and take profit then you have a high winning rate. I use take profit because getting greedy just exposes you to more risk. I also use stop losses because if a signal is bad, the trade goes against you right away, so you may as well get out rather than start position trading. I used your indicator to help optimize my MA/RSI EA and it seems to be much faster at optimizing over the built in strategy tester. This was definitely a useful tool, so thanks.
26.03.2010 06:27 antisyzygy