Gamestudio Links
Zorro Links
Newest Posts
Blobsculptor tools and objects download here
by NeoDumont. 03/28/24 03:01
Issue with Multi-Core WFO Training
by aliswee. 03/24/24 20:20
Why Zorro supports up to 72 cores?
by Edgar_Herrera. 03/23/24 21:41
Zorro Trader GPT
by TipmyPip. 03/06/24 09:27
VSCode instead of SED
by 3run. 03/01/24 19:06
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
4 registered members (Nymphodora, AndrewAMD, Quad, TipmyPip), 889 guests, and 6 spiders.
Key: Admin, Global Mod, Mod
Newest Members
sakolin, rajesh7827, juergen_wue, NITRO_FOREVER, jack0roses
19043 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
How to ensamble multiple algos with advise calls? #470186
01/01/18 20:39
01/01/18 20:39
Joined: Feb 2017
Posts: 369
D
Dalla Offline OP
Senior Member
Dalla  Offline OP
Senior Member
D

Joined: Feb 2017
Posts: 369
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();
		}
		
	}
}


Re: How to ensamble multiple algos with advise calls? [Re: Dalla] #470202
01/02/18 16:18
01/02/18 16:18
Joined: Jul 2000
Posts: 27,977
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,977
Frankfurt
Something must be different then - first compare the logs with and without the additional algo.

Re: How to ensamble multiple algos with advise calls? [Re: jcl] #470213
01/03/18 07:10
01/03/18 07:10
Joined: Feb 2017
Posts: 369
D
Dalla Offline OP
Senior Member
Dalla  Offline OP
Senior Member
D

Joined: Feb 2017
Posts: 369
OK, but just to let me know, there is nothing wrong with the approach of using an additional algo for ensambling?

Re: How to ensamble multiple algos with advise calls? [Re: Dalla] #470214
01/03/18 08:30
01/03/18 08:30
Joined: Jul 2000
Posts: 27,977
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,977
Frankfurt
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.

Re: How to ensamble multiple algos with advise calls? [Re: jcl] #476053
01/23/19 17:53
01/23/19 17:53
Joined: Jan 2019
Posts: 73
berlin
L
laz Offline
Junior Member
laz  Offline
Junior Member
L

Joined: Jan 2019
Posts: 73
berlin
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?

Last edited by laz; 01/23/19 17:58.
Re: How to ensamble multiple algos with advise calls? [Re: laz] #476130
01/28/19 18:01
01/28/19 18:01
Joined: Jan 2019
Posts: 73
berlin
L
laz Offline
Junior Member
laz  Offline
Junior Member
L

Joined: Jan 2019
Posts: 73
berlin
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...

Re: How to ensamble multiple algos with advise calls? [Re: laz] #476131
01/28/19 18:43
01/28/19 18:43
Joined: Feb 2017
Posts: 369
D
Dalla Offline OP
Senior Member
Dalla  Offline OP
Senior Member
D

Joined: Feb 2017
Posts: 369
Honestly can't recall if I got this working or not


Moderated by  Petra 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1