How to ensamble multiple algos with advise calls?

Posted By: Dalla

How to ensamble multiple algos with advise calls? - 01/01/18 20:39

I have in my script defined four different algos, three that are trained using different timeframes but using the same input data and neural network classifier.

What I want to do in my script is to use the COMBO algo as an ensemble for the other three algos. The script runs just fine, but I would expect that the performance of the individual components remains the same regardsless of including the COMBO algo or not. But this is not the case. If I exclude the COMBO algo, the indivudual performance of the other algos change, leading me to believe that I´ve misunderstood how the ensemble multiple algos.

Please advise!

Code:
#include <r.h>

#define HourLong AssetVar[0]
#define HourShort AssetVar[1]
#define FourHourLong AssetVar[2]
#define FourHourShort AssetVar[3]
#define EightHourLong AssetVar[4]
#define EightHourShort AssetVar[5]


var range(int n)
{
	return (HH(n) - LL(n))/priceClose(0);
}

// Trend trading function from Workshop 4
function advise()
{
  	vars Close = series(priceClose());
	
	// signals
	int ScalePeriod = 200;
	var change1 = scale(ROCP(Close, 1), ScalePeriod);
	var change2 = scale(ROCP(Close, 2), ScalePeriod);
	var change3 = scale(ROCP(Close, 3), ScalePeriod);
	var change4 = scale(ROCP(Close, 4), ScalePeriod);
	var change5 = scale(ROCP(Close, 5), ScalePeriod);
	var change6 = scale(ROCP(Close, 6), ScalePeriod);
	var change7 = scale(ROCP(Close, 7), ScalePeriod);
	var change8 = scale(ROCP(Close, 8), ScalePeriod);
	var change9 = scale(ROCP(Close, 9), ScalePeriod);
	var change10 = scale(ROCP(Close, 10), ScalePeriod);
	var range1 = scale(range(1), ScalePeriod);
	var range2 = scale(range(2), ScalePeriod);
	var range3 = scale(range(3), ScalePeriod);
	var range4 = scale(range(4), ScalePeriod);
	var range5 = scale(range(5), ScalePeriod);
	var range6 = scale(range(6), ScalePeriod);
	var range7 = scale(range(7), ScalePeriod);
	var range8 = scale(range(8), ScalePeriod);
	var range9 = scale(range(9), ScalePeriod);
	var range10 = scale(range(10), ScalePeriod);
	
	if(Train) 
	{
		Hedge = 2;
		//NumSampleCycles = 5;
		//LifeTime = 8;
		if(adviseLong(NEURAL+BALANCED, 0, 
			change1, change2, change3, change4, change5, change6, change7, change8, change9, change10,
			range1, range2, range3, range4, range5, range6, range7, range8, range9,range10) > 0.5)
		{
			enterLong();
		}
		if(adviseShort() > 0.5)
		{
			enterShort();
		}
	}
	
	if(!Train)
	{
		var LongPrediction = adviseLong(NEURAL, 0, 
			change1, change2, change3, change4, change5, change6, change7, change8, change9, change10,
			range1, range2, range3, range4, range5, range6, range7, range8, range9,range10);
		var ShortPrediction = adviseShort();

		if(Algo == "HOUR") {
			HourLong = LongPrediction;
			HourShort = ShortPrediction;
		}
		else if(Algo == "4HOUR") {
			FourHourLong = LongPrediction;
			FourHourShort = ShortPrediction;
		}
		else if(Algo == "8HOUR") {
			EightHourLong = LongPrediction;
			EightHourShort = ShortPrediction;
		}
		
		var EntryThrehsold = 0.5;
		var ExitThreshold = 0.5;
		
		if(LongPrediction > ExitThreshold)
		{
			exitShort();
			if(ShortPrediction < EntryThrehsold)
			{				
				reverseLong(1);
			}
		}
		
		if(ShortPrediction > ExitThreshold)
		{
			exitLong();
			if(LongPrediction < EntryThrehsold)
			{
				reverseShort(1);
			}
		}
		plot("LongPred", LongPrediction, NEW|BARS, BLUE+TRANSP);
		plot("ShortPred", ShortPrediction, 0|BARS, RED+TRANSP);	
	}
}

function adviseCombo()
{
  	if(!Train)
	{
		var LongPrediction = (HourLong + FourHourLong + EightHourLong)/3;
		var ShortPrediction = (HourShort + FourHourShort + EightHourShort)/3;
		
		var EntryThrehsold = 0.5;
		var ExitThreshold = 0.5;
		
		if(LongPrediction > ExitThreshold)
		{
			exitShort();
			if(ShortPrediction < EntryThrehsold)
			{				
				reverseLong(1);
			}
		}
		
		if(ShortPrediction > ExitThreshold)
		{
			exitLong();
			if(LongPrediction < EntryThrehsold)
			{
				reverseShort(1);
			}
		}
		plot("LongPred", LongPrediction, NEW|BARS, BLUE+TRANSP);
		plot("ShortPred", ShortPrediction, 0|BARS, RED+TRANSP);	
	}
}


var neural(int mode, int model, int numSignals, void* Data)
{
	if(!wait(0)) return 0;
// open an R script with the same name as the strategy script	
	if(mode == NEURAL_INIT) {
		if(!Rstart(strf("%s.r",Script),2)) return 0;
		Rx("neural.init()");
		return 1;
	}
// export batch training samples to a file to be read by R	
	if(mode == NEURAL_TRAIN) {
		string name = strf("Data\signals%i.csv",Core);
		file_write(name,Data,0);
		Rx(strf("XY <- read.csv('%s%s',header = F)",slash(ZorroFolder),slash(name)));
		if(!Rx(strf("neural.train(%i,XY)",model+1),2)) 
			return 0;
		return 1;
	}
// predict the target	
	if(mode == NEURAL_PREDICT) {
		Rset("X",(double*)Data,numSignals);
		Rx(strf("Y <- neural.predict(%i,X)",model+1));
		var pred = Rd("Y[1]");
		return pred;
	}
// save all trained models	
	if(mode == NEURAL_SAVE) {
		print(TO_ANY,"nStore %s",strrchr(Data,'\')+1);
		return Rx(strf("neural.save('%s')",slash(Data)),3);
	}
// load all trained models	
	if(mode == NEURAL_LOAD) {
		printf("nLoad %s",strrchr(Data,'\')+1);
		return Rx(strf("neural.load('%s')",slash(Data)),3); 		
 	}
	return 1;
}

function run()
{
	set(RULES|TESTNOW|PLOTNOW);
	StartDate = 20160101;
	EndDate = 20171201;
	BarPeriod = 60;
	//LookBack = 1600;
	LifeTime = 8;
	NumCores = -1;
	PlotWidth = 900;
	
	WFOPeriod = WFOPeriod = 125*1440/BarPeriod; 
	
	Spread = Commission = Slippage = RollLong = RollShort = 0;
	
	while(algo(loop("HOUR","4HOUR","8HOUR","COMBO"))) {
		
		if(Algo == "HOUR") {
			TimeFrame = 1;
			LookBack = 200;
			advise();
		}
		else if(Algo == "4HOUR") {
			TimeFrame = 4;
			LookBack = 800;
			advise();
		}
		else if(Algo == "8HOUR") {
			TimeFrame = 8;
			LookBack = 1600;
			advise();
		}
		
		else if(Algo == "COMBO") {
			TimeFrame = 1;
			LookBack = 200;
			adviseCombo();
		}
		
	}
}

Posted By: jcl

Re: How to ensamble multiple algos with advise calls? - 01/02/18 16:18

Something must be different then - first compare the logs with and without the additional algo.
Posted By: Dalla

Re: How to ensamble multiple algos with advise calls? - 01/03/18 07:10

OK, but just to let me know, there is nothing wrong with the approach of using an additional algo for ensambling?
Posted By: jcl

Re: How to ensamble multiple algos with advise calls? - 01/03/18 08:30

No, theoretically it should work. At least I see no reason why not. Of course there could be some detail issues that are not visible at first glance.
Posted By: laz

Re: How to ensamble multiple algos with advise calls? - 01/23/19 17:53

Did you find the problem Dalla ?

I have to say that I have not worked properly in this topic, but we are probably dealing with similar topics;). The deeplearn script and the advise() function are the reasons why I'm here wink.

I am new to Zorro, but I spent a lot of time with the topic "machine learning" in R. I will deal with the topic in more detail in the near future, I will then take a closer look at the way the data takes from Zorro to R and back.

Currently I'm still building the framework for various assets and Algos for that, as you know from my other thread wink...

Is it ensured that we can use the advise function with different algos? Also with different assets?
Posted By: laz

Re: How to ensamble multiple algos with advise calls? - 01/28/19 18:01

Just to know what the problem was here...

Is it possible that he confuses Zorro's counter by adding the combo only to test? You train 3 models, in Test you call 4 models if you include combo.

Zorro saves the models by it's number, if you add something only in TEST you change the counter - Zorro maybe loads the wrong models? But i have not tested it, just a guess...
Posted By: Dalla

Re: How to ensamble multiple algos with advise calls? - 01/28/19 18:43

Honestly can't recall if I got this working or not
© 2024 lite-C Forums