MACD + Flat Market Detector

Posted By: SFF

MACD + Flat Market Detector - 02/20/13 22:34

I tried to convert this free indicator and my current code is below.
Zorro says that "Error in 'line 22:
'FextMapBuffer6' undeclared identifier".

Why this happen?
Thanks in advance.

The original indicator.
http://www.pointzero-trading.com/FreeMetatraderDownloads/view/5

Code:
function run(){
	
 
int    FastEMA             = 9;
int    SlowEMA             = 26;
int    AvPeriod            = 400;
var    Gamma               = 0.75;


vars med = series(MedPrice());

vars median = series(MACD(med,FastEMA,SlowEMA,4));
vars FextMapBuffer6 = series(median[0] - median[1]);
vars FextMapBuffer5 = series(abs(FextMapBuffer6[0]));

var flat = EMA(FextMapBuffer5,AvPeriod);
vars FextMapBuffer3 = series((flat * Gamma));
vars FextMapBuffer4 = series(0 - (flat * Gamma));
}

if(FextMapBuffer6 > FextMapBuffer3)
exitShort();
enterLong();

if(FextMapBuffer6 < FextMapBuffer4)
exitLong();
enterShort();
}

Posted By: TankWolf

Re: MACD + Flat Market Detector - 02/21/13 06:55

Actually all it was from what I can tell is some wrong brackets in your code.

Originally Posted By: "Code"

function run(){
LookBack = 500;


int FastEMA = 9;
int SlowEMA = 26;
int AvPeriod = 400;
var Gamma = 0.75;


vars med = series(MedPrice());

vars median = series(MACD(med,FastEMA,SlowEMA,4));
vars FextMapBuffer6 = series(median[0] - median[1]);
vars FextMapBuffer5 = series(abs(FextMapBuffer6[0]));

var flat = EMA(FextMapBuffer5,AvPeriod);
vars FextMapBuffer3 = series((flat * Gamma));
vars FextMapBuffer4 = series(0 - (flat * Gamma));

if(FextMapBuffer6 > FextMapBuffer3) {
exitShort();
enterLong();
}
if(FextMapBuffer6 < FextMapBuffer4) {
exitLong();
enterShort();
}
}


Updated code above. Though I do have one question for jcl, when we compare series for our entry codes do we use the pointers (FextMapBuffer) to the series or the last variable in our series in this case (FextMapBuffer6[0])?
Posted By: SFF

Re: MACD + Flat Market Detector - 02/21/13 07:35

Great, Thank you.

I viewed a plot and I think something is wrong.
Could you check if this conversion is identical to the original code?
Posted By: TankWolf

Re: MACD + Flat Market Detector - 02/21/13 08:16

Well in the interests of the community I had a go at creating the indicator & it looks right but you should all test it probably before relying on it & by all means please make changes where you see necessary.

Quote:

function run(){
set(PLOTPRICE+PLOTNOW);
BarPeriod = 15;
LookBack = 500;
StartDate = 20120201;
NumDays = 1;

int FastEMA = 9;
int SlowEMA = 26;
int AvPeriod = 400;
var Gamma = 0.75;

vars med = series(MedPrice());

vars median = series(MACD(med,FastEMA,SlowEMA,4));
vars FextMapBuffer6 = series(median[0] - median[1]);
vars FextMapBuffer5 = series(abs(FextMapBuffer6[0]));
vars FextMapBuffer1 = series(0);
vars FextMapBuffer2 = series(0);

var flat = EMA(FextMapBuffer5,AvPeriod);
vars FextMapBuffer3 = series((flat * Gamma));
vars FextMapBuffer4 = series(0 - (flat * Gamma));

int NewWindow = 0;
plot("NewWindow",NewWindow,NEW,BLACK);
if(FextMapBuffer6[0] < FextMapBuffer6[1]) {
FextMapBuffer2[0] = FextMapBuffer6[0];
FextMapBuffer1[0] = 0;
plot("FextMapBuffer2",FextMapBuffer2[0],BARS,RED);
}
else if(FextMapBuffer6[0] > FextMapBuffer6[1]) {
FextMapBuffer1[0] = FextMapBuffer6[0];
FextMapBuffer2[0] = 0;
plot("FextMapBuffer1",FextMapBuffer1[0],BARS,BLUE);
}
plot("FextMapBuffer3",FextMapBuffer3[0],0,BLACK);
plot("FextMapBuffer4",FextMapBuffer4[0],0,BLACK);
}


Enjoy.
Posted By: SFF

Re: MACD + Flat Market Detector - 02/21/13 08:39

Good script.Thanks.

I check a plot again with your script and it looks that by comparing in Zorro and MT4 chart they are not same.

Please compare MT4 daily chart for EUR/USD with Zorro plot on 2012/09.
You know what I mean.
Posted By: TankWolf

Re: MACD + Flat Market Detector - 02/21/13 08:45

Lol I just read the description again...

Quote:
Description

This is the standard MACD indicator drawing two flat lines instead of a signal line. It allows you to detect and trade flat/range-bound markets. It is nothing fancy and was only created to run some test, but it might be useful to you.


By the sounds of this its just MACD with these flat lines drawn Ive reattached code to show the difference please note Ive just got all the bars red on normal MACD not sure how you change them with the built in indicator to show blue for accelerating.

Quote:

function run(){
set(PLOTPRICE+PLOTNOW);
BarPeriod = 15;
LookBack = 500;
StartDate = 20120201;
NumDays = 1;

int FastEMA = 9;
int SlowEMA = 26;
int AvPeriod = 400;
var Gamma = 0.75;

vars med = series(MedPrice());

vars median = series(MACD(med,FastEMA,SlowEMA,4));
vars MACDHistory = series(rMACDHist);
vars FextMapBuffer6 = series(median[0] - median[1]);
vars FextMapBuffer5 = series(abs(FextMapBuffer6[0]));
vars FextMapBuffer1 = series(0);
vars FextMapBuffer2 = series(0);

var flat = EMA(FextMapBuffer5,AvPeriod);
vars FextMapBuffer3 = series((flat * Gamma));
vars FextMapBuffer4 = series(0 - (flat * Gamma));

int NewWindow2 = 0;
plot("NewWindow2",NewWindow2,NEW,BLACK);
plot("MACD",MACDHistory[0],BARS,RED);
plot("FextMapBuffer3",FextMapBuffer3[0],0,BLACK);
plot("FextMapBuffer4",FextMapBuffer4[0],0,BLACK);

int NewWindow = 0;
plot("NewWindow",NewWindow,NEW,BLACK);
if(FextMapBuffer6[0] < FextMapBuffer6[1]) {
FextMapBuffer2[0] = FextMapBuffer6[0];
FextMapBuffer1[0] = 0;
plot("FextMapBuffer2",FextMapBuffer2[0],BARS,RED);
}
else if(FextMapBuffer6[0] > FextMapBuffer6[1]) {
FextMapBuffer1[0] = FextMapBuffer6[0];
FextMapBuffer2[0] = 0;
plot("FextMapBuffer1",FextMapBuffer1[0],BARS,BLUE);
}
plot("FextMapBuffer3",FextMapBuffer3[0],0,BLACK);
plot("FextMapBuffer4",FextMapBuffer4[0],0,BLACK);
}


Posted By: SFF

Re: MACD + Flat Market Detector - 02/21/13 09:40

Why there is difference when the code is correctly converted?
Posted By: TankWolf

Re: MACD + Flat Market Detector - 02/21/13 10:23

Been playing some more there was still some errors Ive got it close but it still doesnt look exact though I think that may be because of the way the daily bars are grouped for FXCM and Zorro differently.

Quote:

function run(){
set(PLOTPRICE+PLOTNOW);
BarPeriod = 1440;
LookBack = 500;
StartDate = 20120901;
NumDays = 100;

int FastEMA = 9;
int SlowEMA = 26;
int AvPeriod = 400;
var Gamma = 0.75;

vars FextMapBuffer1 = series(0);
vars FextMapBuffer2 = series(0);

//Median
vars Price = series(price());
vars MedianPrice = series(SMA(Price,500));

vars median = series(MACD(MedianPrice,FastEMA,SlowEMA,4));
vars median1 = series(MACD(MedianPrice+1,FastEMA,SlowEMA,4));

//Difference
vars FextMapBuffer6 = series(median[0] - median1[0]);

//Save current absoulte value
vars FextMapBuffer5 = series(abs(FextMapBuffer6[0]));

//Flat lines
vars flat = series(EMA(FextMapBuffer5,AvPeriod));
vars FextMapBuffer3 = series((flat[0] * Gamma));
vars FextMapBuffer4 = series(0 - (flat[0] * Gamma));

int NewWindow = 0;
plot("NewWindow",NewWindow,NEW,BLACK);

//Deceleration
if(FextMapBuffer6[0] < FextMapBuffer6[1]) {
FextMapBuffer2[0] = FextMapBuffer6[0];
FextMapBuffer1[0] = 0;
plot("FextMapBuffer2",FextMapBuffer2[0],BARS,RED);
}
//Acceleration
else if(FextMapBuffer6[0] > FextMapBuffer6[1]) {
FextMapBuffer1[0] = FextMapBuffer6[0];
FextMapBuffer2[0] = 0;
plot("FextMapBuffer1",FextMapBuffer1[0],BARS,BLUE);
}
plot("FextMapBuffer3",FextMapBuffer3[0],0,BLACK);
plot("FextMapBuffer4",FextMapBuffer4[0],0,BLACK);
}


Posted By: jcl

Re: MACD + Flat Market Detector - 02/21/13 15:34

How MT4 daily charts look depends on when the MT4 was started and to which broker it was connected. So you can not compare an MT4 chart to a chart of another platform.

There are several reasons for this. MT4 bars depend on the broker's time zone, while Zorro bars are time zone independent. Zorro's EMA and MACD are window length independent, while the MT4 EMA and MACD return different values dependent on the previous length of the chart.

So you will most likely not get the same signals from Zorro and from MT4 even when the algorithm is completely identical. You can also not load an MT4 EA from a forum and expect to get exactly the same trade signals as the guy who posted it.
Posted By: SFF

Re: MACD + Flat Market Detector - 02/22/13 00:16

Thank you for explaining JCL.

I will test both platforms and take a (more)profitable one.
© 2024 lite-C Forums