Collection of Useful General-Purpose functions

Posted By: Squalou

Collection of Useful General-Purpose functions - 11/06/12 18:25

Hi all,

I am starting this thread to build together some kind of "useful general-purpose functions" repository.

The idea is to post in this thread all kinds of functions that you found useful to yourself, and could be also helpful to others.

Having a centralized area collecting these functions would greatly help newcomers to Zorro in writing their own strategies with even less time than it is already ! cool

Hopefully this thread could even become a new SECTION in the forum, i'm sure it will attract a lot of programmers.

Perhaps we can think of some way to put some of these functions in a true Zorro source code library file, as it is the case with indicators.c for instance.

My first contribution comes right next...

Sq
Posted By: Squalou

Re: Collection of Useful General-Purpose functions - 11/06/12 18:31

So, here is my first contribution.

a function to plot the Equity and Balance curves below your Zorro result charts.

Code:
// plotting Equity & Balance curves on your Zorro result charts.

// add this forward declaration on top of your script:
function plot_balance_equity();

// next comes your script:

//---------------------------------------
function run()
//---------------------------------------
{
	// set this to automatically produce the result chart at the end of a [Test]
	set(PLOTNOW);

	// your strategy code goes here, for instance:

	while(asset(loop("EUR/USD"/*,"AUD/USD","GBP/USD","GER30","NAS100","SPX500","UK100","UKOil","US30","USD/CAD","USD/CHF","USD/JPY","USDOLLAR","USOil","XAG/USD","XAU/USD"*/)))
	{

		// ... your trading decisions here ...

		//---------------------------------------
		// now plot the Balance & Equity curves
		plot_balance_equity();
		//---------------------------------------
		
	} // asset(loop(...))
	
	
	// set the size of the resulting chart PNG file
	PlotWidth   = 2000;
	PlotHeight1 = 500;
}

// here is the actual function:

//---------------------------------------
function plot_balance_equity()
//---------------------------------------
{
	var *EquityCurve = series(WinTotal - LossTotal + WinValTotal - LossValTotal);
	plot("Balance",Balance,NEW,BLUE);
	plot("Equity",*EquityCurve,0,GREEN);
}

//end

Posted By: Squalou

Re: Collection of Useful General-Purpose functions - 11/06/12 18:44

Here is an example a very simple strategy script to illustrate the use of this function.

Code:
// plotting Equity & Balance curves on your Zorro result charts.

function plot_balance_equity(); // forward declaration of the new function

//---------------------------------------
function run()
//---------------------------------------
{
	set(PLOTNOW); // set this to automatically produce the result chart at the end of a [Test]

// your strategy code goes here, for instance:

	while(asset(loop("EUR/USD"/*,"AUD/USD","GBP/USD","GER30","NAS100","SPX500","UK100","UKOil","US30","USD/CAD","USD/CHF","USD/JPY","USDOLLAR","USOil","XAG/USD","XAU/USD"*/)))
	{

		// ... your trading decisions come here ...
		var *ClosePrice = series(priceClose());
		var *distfromMA = series(*ClosePrice-EMA(ClosePrice,100));
		if (distfromMA[0] > 0) enterLong();  // enter Long above MA (& close all Shorts)
		if (distfromMA[0] < 0) enterShort(); // enter Short below MA (& close all Longs)
		
		//---------------------------------------
		// now plot the Balance & Equity curves
		plot_balance_equity();
		//---------------------------------------
		
	} // asset(loop(...))
	
	
	// set the size of the resulting chart PNG file
	PlotWidth   = 2000;
	PlotHeight1 = 500;
}

//---------------------------------------
function plot_balance_equity()
//---------------------------------------
{
	var *EquityCurve = series(WinTotal - LossTotal + WinValTotal - LossValTotal);
	plot("Balance",Balance,NEW,BLUE);
	plot("Equity",*EquityCurve,0,GREEN);
}

//end




Sorry, i haven't found yet how to attach a file to a post...
(i cannot find the "File Manager" link on the "new post" page... HELP!)

I would have also loved posting a resulting chart to actually show how this looks like...
blush

Sq
Posted By: Guiom

Re: Collection of Useful General-Purpose functions - 11/06/12 19:52

Great function here, thanks.

In the forum FAQ is shows that to "In order to add an image to your message or your signature, you must have the image already available on a web server and reachable by a URL"

Guiom
Posted By: SFF

Re: Collection of Useful General-Purpose functions - 12/12/12 01:26

I am looking for a generic anti-martingale money management function.
It is a simple and basic money management.
Posted By: Pipinator

Re: Collection of Useful General-Purpose functions - 12/24/12 19:11

Thanks Squalu! Good idea for the thread.

Pip
© 2024 lite-C Forums