#define FROM_CRYPTOCOMPARE 9999
#include <profile.c>

#define NN		10000	 // exact number of assets
string Names[NN];

DATE convertTime(long t32)
{
	return (double)t32 / (24.*60.*60.) + 25569.; // 25569. = DATE(1.1.1970 00:00)}
}

void removeChar(char* input, char ch)
{
    char* output = input;
    while (*input)
    {
        if (*input != ch)
        {
            *(output++) = *input;
        }
        ++input;
    }
    *output = 0;
}

bool historyFileDoUpdate(string code)
{

	//int xxx=dataLoad(1,strf("%s.t6", code),7);
	//DATE mydate=dataVar(1,0,0);
	//printf("\ndataLoad  %d, %s %d",xxx,strdate("%Y-%m-%d",mydate),mydate==wdate() );
	//printf("\nNOW %s ",strdate("%Y-%m-%d %H:%M:%S",NOW) );
	DATE fileDate = convertTime(file_date(strf("history\\%s.t6", code)));
	//printf("\n fileDate %s %d", strdate("%Y-%m-%d",fileDate), NOW >fileDate + 86400);
	//printf("\n NOW %s %d ", strdate("%Y-%m-%d",NOW), date(mydate + 86400));

	int cmpres = strcmp(strdate("%Y-%m-%d", fileDate), strdate("%Y-%m-%d", NOW));
	//printf("\n %d ", cmpres);

	if (cmpres == 0)
		return 0;
	else
		return 1;
}

var dataFromCryptoCompare(string Code, int mode, int timeframe)
{
	removeChar(Code,'/');
	//printf("\nCODE: %s ", Code);
	if (historyFileDoUpdate(Code))
	{
		string Format;
		Format = "%Y-%m-%d %H:%M,f3,f1,f2,,f4,f6";//UnadjAdj Close
		
		char cmdParam[256];
		sprintf(cmdParam, "%s %d", Code,timeframe);
		exec("CryptoCompareHistoryAPI.exe", cmdParam, 1);

		dataNew(1, 0, 7);
		int numRecord = dataParse(1, Format, "History\\history.csv");
		printf("\n%s %d records read from CRYPTOCOMPARE", Code, numRecord);
		dataSave(1, strf("%s.t6", Code));
		return numRecord;
	}
}

void assetHistory( string code, int mode, int timeframe)
{

	dataFromCryptoCompare(code, mode,timeframe);
	return;
	
	
}


function run()
{
	
	BarPeriod = 60;	// 1 hour bars
	StartDate = 2014;
	EndDate = 2017; 	// fixed simulation period
    //set(PRELOAD); // allow extremely long lookback period

	int N = 0;
	int x=0;

	assetList("AssetsCoinsBitTrexStrategy.csv");
	//assetList("AssetsZ8.csv");
	for(x = 0; Assets[x]; x++)	
	//while (asset(loop(Assets)))
	{
		if (is(INITRUN))
		{
			Names[x] =Assets[x];
			printf("\n %s", Names[x]);
			N++;
		}
		
	}
	
	
	int i=0;
	for(i = 0; i<N; i++)	
	{
		if (is(INITRUN))
		{
			asset(Names[i]);
			assetHistory(Names[i], FROM_CRYPTOCOMPARE,60);

		}
	}



}


