#include <default.c>
#include <stdio.h>
function loadStats(string filename)
{
if(!is(TESTMODE)) return; //only read state info when live trading
char path[100]; //full path to parameter .ini file
char param[30]; //asset-labeled parameter .ini file
char fcontent[2500]; //file content from parameter .ini file
sprintf(path,"Strategy\\%s",filename);
if(!file_date(path)) msg("#>>WARNING<< no stats file found!\nThis is normal if this is the very first run of a new strategy");
string fcontent = file_content(path);
//load from disk current state info for each asset
while(asset(loop("EURUSD")))
{
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(TESTMODE)) 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")))
{
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()
{
set(PARAMETERS+FACTORS);
StartDate = 2010;
EndDate = 2011;
BarPeriod = 15;
LookBack = 200;
NumWFOCycles = 5;
static int LastWFOCycle;
if(WFOCycle != LastWFOCycle) printf("\nWFOCycle = %i",WFOCycle);
if(is(INITRUN)) LastWFOCycle=0; else LastWFOCycle = WFOCycle;
static int numOpenLastCheck, zorroIteration; //track changes in trade open/closes
if(is(INITRUN)) numOpenLastCheck=0;
if(is(INITRUN)) zorroIteration=1; else zorroIteration++;
if (zorroIteration==2) loadStats("dt-file-readwrite.ini"); //load most recent stats that were saved to disk
if(NumOpenTotal != numOpenLastCheck)
{
saveStats("dt-file-readwrite.ini"); //save stats to disk upon trade count changes
numOpenLastCheck = NumOpenTotal;
}
asset("EURUSD");
{
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 = ATR(100) * optimize(4,1,5,1);
if(valley(Trend))
enterLong();
else if(peak(Trend))
enterShort();
}
}