Originally Posted By: Geek
Interesting thread, I enjoy reading things that inspire lateral thinking.

Good luck with this acidburn and keep us updated on your journey for the holy grail laugh


Thanks, Geek!

I have some good news, and some bad news.

The good news is that I really made a nice progress with this, as you can tell by the attached code. The bad news is that I think this is not worth pursuing further. The problem is that Zorro is so centered on BarPeriod clock, that trying to break out of that concept is a world of pain! At moments it felt like I was rewriting the Zorro... in Zorro!

I mostly succeeded making the volatility based bars (not renko's, not CBR's, but conceptually close!). But the plots are still created at BarPeriod resolution, so they're just not useful to develop strategies on custom bars. Unless jcl chimes in with some breakthrough idea, of course, but even then I see lots of obstacles on the way. The slight problem is also that there's no realloc. And I also see how troublesome it would become to take care of the proper lookback for strategies with multiple indicators.

I heavily commented the code, so if anybody wants to continue with this, be my guest! I guess I just got tired of fighting the system. laugh But, I learned a lot about Zorro in the process, so it was actually a time good spent.

Code:
function run()
{
	set(PLOTNOW);
	// just some random week for playing
	StartDate = 20111202;
	EndDate = 20111209;
	BarPeriod = 1; /* mandatory */

	static int NumPriceBars, NumLRSBars;
	static var Base;
	static vars Price, LRS;

	// need to reset static variabes on every run because they somehow leak
	// between the runs. can't just initialize them in the declarations above
	// (learned that the hard way)
	if (is(INITRUN)) {
		NumPriceBars = 0;
		NumLRSBars = 0;
		Base = 0;
	}

	// we need to allocate memory for the arrays, no series land :(
	if (!Price) {
		// wait for Zorro to provide the total number of bars
		// to get some idea how much memory to allocate
		if (!NumBars)
			return;
		// don't know any better formula atm, and no realloc(), damn!
		Price = malloc(NumBars * sizeof(var));
		LRS = malloc(NumBars * sizeof(var));
		// let's be sure we really got our memory
		if (!Price || !LRS) {
			printf("\nOut of memory!");
			quit();
		}
	}

	// we're getting somewhere, this is our base price
	// I'm scared to use == with floats, but it seems to work
	// at least when comparing with zero
	if (Base == 0) {
		printf("\nInitializing Base...");
		Base = priceClose();
	}

	// finally! we're printing a new bar only when price moves this many
	// pips from the base price, so our new bars will be based on price
	// volatility and not on time period
	if (abs(priceClose() - Base) / PIP >= 10) {
		// if there are previous prices, we need to shift them to the right
		// this is what series data type would do for us behind the scene
		if (NumPriceBars)
			memcpy(Price + 1, Price, (NumPriceBars - 1) * sizeof(var));
		// insert new bar...
		Price[0] = priceClose();
		// increment the counter...
		NumPriceBars++;
		// and reset the base price
		Base = priceClose();
		// mostly the same drill for the indicator, except we also have to
		// take care of the needed lookback ourselves argh &^%$#
		if (NumPriceBars > 10) {
			if (NumLRSBars)
				memcpy(LRS + 1, LRS, (NumLRSBars - 1) * sizeof(var));
			LRS[0] = LinearRegSlope(Price, 10);
			NumLRSBars++;
		}
	}

	// finally we can trade our own bars, can you believe? :)
	if (crossOver(LRS, 0)) {
		if (!NumOpenLong)
			enterLong();
	} else if (crossUnder(LRS, 0)) {
		if (!NumOpenShort)
			enterShort();
	}

	// unfortunately graphs are at the original BarPeriod resolution
	// don't know how to fix that. yes, this is a showstopper :(
	plot("OrigPrice", priceClose(0), 0, BLACK);
	// be extra careful when printing our bars, they're not available
	// right from the start!
	if (NumPriceBars)
		plot("NewPrice", Price[0], 0, BLUE);
	if (NumLRSBars)
		plot("LRS", LRS[0], NEW, RED);

	// finally, we need to release the allocated memory, so we don't run
	// out of memory, traditionally exiting the program releases
	// all the allocated memory, but not in Zorro!
	if (is(EXITRUN)) {
		if (Price) {
			free(Price);
			Price = NULL;
		}
		if (LRS) {
			free(LRS);
			LRS = NULL;
		}
		// some statistics
		printf("\nStats: Bars %d, PriceBars %d LRSBars %d", NumBars, NumPriceBars, NumLRSBars);
	}	
}