Indicators oddities and maybe some bugs

Posted By: johnnyp

Indicators oddities and maybe some bugs - 09/15/17 08:51

I have been porting various indicators to MetaTrader 4.

Question: indicators.c has the following line in many places SETSERIES(Data,Period); what does it do?

Here are some curiosities that I have found during my testing on EURUSD 1 minute bars. For reference, I include my test script below.

Most of this is moot because I should respect a lookback period before using the indicators for actual trading.

ATR(7) returns some garbage value on Bar 0.
ATR(7) returns 0.00014 for the next 40 or so bars, even though TrueRange() varies from bar to bar. After that ATR works as expected. This only happens when I set StartDate to a year. If I set StartDate to 20170101 then Zorro will start a few bars before the end of 2016 and the ATR values vary from the first bar on, whereas if I set StartDate to 2017 then Zorro will start at the first bar of 2017 and the ATR values stay fixed for ~48 bars. Weird.

I also tried ATR(150) which stays stuck on 0.00016 for ~200 bars.

If the test starts at first bar of a history file, then the first two bars in the history file seem to be ignored. If you do vars Close = series(priceClose()); then the first non-zero value that appears in Close is the close of the third bar in the history file.

On Bar 0, Close[0] is 0, but SMA(Close, 10), ATR(7) and TrueRange() all return the same garbage value. This value changes if you close and reopen Zorro, then rerun the script. It doesn't seem to affect the subsequent values at all.

After running BBands(Close, 10, 2, 2, MAType_SMA); rRealUpperBand and rRealLowerBand are often set to the same value as rRealMiddleBand. This happens throughout the dataset. I suspect the implementation of BBands is buggy, especially for short periods. However this implementation seems to have good predictive power, so I'd like to find out how to imitate the bugs properly.

Code:
// Click [Test] for exporting price data to a .csv file in the Data folder

char filename[1000];
int period = 50; //lookback for calculating normalized/standardized values
char line[5000];
char header[5000];

function add(string column_header, var value)
{
	if(is(INITRUN)) strcat(header, strf(",%s",column_header));
	strcat(line, strf(",%.5f", 
		value
		));
}

function add(string column_header, vars data)
{
	add(column_header, data[0]);
}

function record()
{	
	if(is(INITRUN)) sprintf(header, "YYYY.MM.DD HH:MM");
	sprintf(line,"%02i.%02i.%02i %02i:%02i", year(),month(),day(),hour(),minute());
	
	vars Close = series(priceClose());
	
	add("bar", Bar);
	add("close", Close[0]);
	add("close20", Close[20]);
	add("sma10", SMA(Close, 10));
	add("tr", TrueRange());
	add("atr7", ATR(7));
	add("atr150", ATR(150));
	
	
	BBands(Close, 10, 2, 2, MAType_SMA);
	add("BBandsL", rRealLowerBand);
	add("BBandsM", rRealMiddleBand);
	add("BBandsU", rRealUpperBand);
	add("BBandsDist", rRealUpperBand-rRealLowerBand);
	add("4xStdDev", 4.*sqrt(Moment(Close, 10, 2)));
	
	
	if(is(INITRUN)) {
	   file_delete(filename);
		strcat(header,"n");
		file_append(filename, header);
		printf("nWriting header: %s", header);
	}
	strcat(line,"n");
	file_append(filename,line);
}

function run(){
	BarPeriod = 1;
	StartDate = 2017;
	EndDate = 20170104;
	LookBack = 1000;
	
	if(is(INITRUN))
	{
		sprintf(filename, "Data\Test.csv");
		printf("nWriting to %s", filename);
	}
	
	record();
}

Posted By: jcl

Re: Indicators oddities and maybe some bugs - 09/18/17 11:11

SETSERIES is a macro for testing purposes and can be ignored. I don't know how TA-LIB indicators behave when the prices are zero - that probably depends on the indicator. But I do not think that they return anything useful in that case.
© 2024 lite-C Forums