jcl says they will incorporate a way to save state information between Zorro restarts. I'm really looking forward to that feature because I can definitely see how you could be hurt if your Zorro stops at any point during live trading. The stop could be intentional or unintentional.

I've gone ahead and written this proof-of-concept which I will test a little further, then plan to implement (I'm using Zorro 1.20.1; don't know when the official features would be available).

I'm definitely a n00b when it comes to C programming so it took me quite a while to get this just right. I would greatly appreciate any feedback or improvements.

I'm working on incorporating this into my infrastructure that I use for building slopbots. I have tried to keep it as modular as possible. (I'll share the full latest infrastructure once I can verify it's all working correctly.)

Here is the theory why I built this:
When Zorro starts for the VERY FIRST time, it does not know anything about a given asset's current behavior with regard to equity-curve trading (ie, it doesn't know if the asset should currently be on phantom-only restriction due to a string of losses, for example). The way I deal with that situation can be seen in the logic of the CalculateMargin() function I use. This logic has been tested on my live account and I do believe it works.

When Zorro starts a SUBSEQUENT TIME after live trading has already begun, it only knows about phantom or real trades that are currently open (my understanding as of v1.20.1). Therefore, it is somewhat of a tragedy to stop/restart Zorro because that would almost definitely hurt the performance if equity-curve trading is used.

While this state info I'm saving to disk is not a perfect solution, I do believe it is "better" than nothing. One other thing I plan to look into is serializing a series array, so that potentially the equity curve itself could be saved to disk. That might be ideal. But my coding skills are weak, and the official Zorro feature is due out soon...

This is the margin calculation routine I use:
Code:
function calculateMargin(int direction)
{
	//calculate risk Margin based on OptimalF and trade direction
	Capital = 1000; //simulated account balance
	var riskCapital = 300; //basis to trade with

	if (direction && OptimalFLong>.001) //long trade, historically profitable
		{
		if (getOpt("emode") //uses equity-curve trading
			&& (NumWinLong<1 && NumWinShort<1 && NumLossLong<1 && NumLossShort<1) //no completed trades yet
			&& (EquityLong<=0 && EquityShort<=0)) //initial active trades are losing or non-existent
			{
				Lots = -1; //phantom only on first trade each asset
				if(is(TRADEMODE)) printf("\n%s initial trade restricted to Phantom only",Asset);
			}

		if(is(TRADEMODE)) printf("\nAsset=%s; direction=%i; OptimalF=%f;\nMargin=%f; Lots=%i",Asset,direction,OptimalF,(OptimalFLong*riskCapital),Lots);
		return OptimalFLong * riskCapital;
		}
	else if (!direction && OptimalFShort>.001) //short trade, historically profitable
		{
		if (getOpt("emode") //uses equity-curve trading
			&& (NumWinLong<1 && NumWinShort<1 && NumLossLong<1 && NumLossShort<1) //no completed trades yet
			&& (EquityLong<=0 && EquityShort<=0)) //initial active trades are losing or non-existent
			{
				Lots = -1; //phantom only on first trade each asset
				if(is(TRADEMODE)) printf("\n%s initial trade restricted to Phantom only",Asset);
			}

		if(is(TRADEMODE)) printf("\nAsset=%s; direction=%i; OptimalF=%f;\nMargin=%f; Lots=%i",Asset,direction,OptimalF,(OptimalFShort*riskCapital),Lots);
		return OptimalFShort * riskCapital;
		}

	return 0; //no Margin allocated for non-historically profitable
}



And here is the proof-of-concept for saving trade stats to disk:
Code:
#include <default.c>
#include <stdio.h>

function loadStats(string filename)
{
	if(!is(TRADEMODE)) return; //only read state info when live trading
	
	char path[100]; //full path to parameter .ini file
	char param[30]; //asset-labeled parameter in file
	sprintf(path,"Strategy\\%s",filename);
	string fcontent = file_content(path);
	
	//load from disk current state info for each asset
	while(asset(loop("EURUSD","USDJPY")))
	{
		sprintf(param,"%s_NumWinLong",Asset);
		NumWinLong = strvar(fcontent,param);
		
		sprintf(param,"%s_NumWinShort",Asset);
		NumWinShort = strvar(fcontent,param);	
	
		sprintf(param,"%s_NumLossLong",Asset);
		NumLossLong = strvar(fcontent,param);
	
		sprintf(param,"%s_NumLossShort",Asset);
		NumLossShort = strvar(fcontent,param);
		
		sprintf(param,"%s_WinLong",Asset);
		WinLong = strvar(fcontent,param);

		sprintf(param,"%s_WinShort",Asset);
		WinShort = strvar(fcontent,param);
		
		sprintf(param,"%s_LossLong",Asset);
		LossLong = strvar(fcontent,param);
		
		sprintf(param,"%s_LossShort",Asset);
		LossShort = strvar(fcontent,param);
	}
}

function saveStats(string filename)
{
	if(!is(TRADEMODE)) return; //only save state info when live trading

	FILE *out;
	char path[100]; //full path to parameter .ini file
	char line[2500], tmp[2500]; //line of data for .ini file
	sprintf(path,"Strategy\\%s",filename);

	if (!(out = fopen(path, "w")))
	{
		printf("\nERROR trying to open file for write:\n%s\n",path);
		return;
	}

	//store to disk current state info for each asset
	while(asset(loop("EURUSD","USDJPY")))
	{
		sprintf(line,"%s_NumWinLong = %i\n",Asset,NumWinLong);
		sprintf(tmp,"%s_NumWinShort = %i\n",Asset,NumWinShort);
		strcat(line,tmp);
		sprintf(tmp,"%s_NumLossLong = %i\n",Asset,NumLossLong);
		strcat(line,tmp);
		sprintf(tmp,"%s_NumLossShort = %i\n",Asset,NumLossShort);
		strcat(line,tmp);
		sprintf(tmp,"%s_WinLong = %f\n",Asset,WinLong);
		strcat(line,tmp);
		sprintf(tmp,"%s_WinShort = %f\n",Asset,WinShort);
		strcat(line,tmp);
		sprintf(tmp,"%s_LossLong = %f\n",Asset,LossLong);
		strcat(line,tmp);
		sprintf(tmp,"%s_LossShort = %f\n",Asset,LossShort);
		strcat(line,tmp);
		
		if (!fwrite(line, sizeof(char), strlen(line), out))
		{
			printf("\nWRITE ERROR:\n%s\n",path);
			fclose(out);
			return;
		}
		else printf("\nSaved trade stats to disk");
	}

	fclose(out);
}

function run()
{
	StartDate = 20130520;
	EndDate = 20130920;
	BarPeriod = 15;
	LookBack = 200;
	static int numOpenLastCheck; //track changes in trade open/closes

	//load most recent stats that were saved to disk
	if(is(INITRUN)) loadStats("dt-file-readwrite.ini");

	//save stats to disk periodically, or upon trade count changes
	if(NumOpenTotal != numOpenLastCheck || minute()==30 ) saveStats("dt-file-readwrite.ini");
	numOpenLastCheck = NumOpenTotal;

	while(asset(loop("EURUSD","USDJPY")))
	{
		if(is(INITRUN)) printf("\nINIT %s\nEquityLong=%f; EquityShort=%f\n",Asset,EquityLong,EquityShort);
		if(is(EXITRUN)) printf("\nEXIT %s\nEquityLong=%f; EquityShort=%f\n",Asset,EquityLong,EquityShort);

		vars Price = series(price());
		vars Trend = series(LowPass(Price,1000));
	
		Stop = 4*ATR(100);
	
		if(valley(Trend))
			enterLong();
		else if(peak(Trend))
			enterShort();
	}
}



Thanks for any feedback.