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);
}
}