Gamestudio Links
Zorro Links
Newest Posts
Help with plotting multiple ZigZag
by degenerate_762. 04/30/24 23:23
M1 Oversampling
by 11honza11. 04/30/24 08:16
Trading Journey
by howardR. 04/28/24 09:55
Zorro Trader GPT
by TipmyPip. 04/27/24 13:50
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
3 registered members (Quad, Ayumi, AndrewAMD), 1,092 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
firatv, wandaluciaia, Mega_Rod, EternallyCurious, howardR
19050 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
adviseLong(NEURAL) in an asset loop #457103
12/26/15 04:32
12/26/15 04:32
Joined: Apr 2014
Posts: 482
Sydney, Australia
B
boatman Offline OP
Senior Member
boatman  Offline OP
Senior Member
B

Joined: Apr 2014
Posts: 482
Sydney, Australia
I'd like to save a separate machine learning model file for each asset in an asset loop using the NEURAL framework in the r.h file. I have some questions that I think would help my understanding of how to achieve this.

If I call adviseLong(NEURAL...) in an asset loop, the framework in the r.h file over writes the .ml file with each iteration of the loop. I'd like to save a separate .ml file for each asset.

Here is the NEURAL_SAVE and NEURAL_LOAD functions from r.h:
Code:
if(mode == NEURAL_SAVE)
		return Rx(strf("save(Models,file='%s')",slash(Data)),2);
	if(mode == NEURAL_LOAD)
 		return Rx(strf("load('%s')",slash(Data)),2);
	return 1;



The line
Code:
Rx(strf("save(Models,file='%s')",slash(Data)),2);


saves a .ml file with the name of the script. But how is the name of the script being accessed here since it is not explicitly specified?

If I try
Code:
Rx(strf("save(Models,file='%s_%s')", strx(Asset, "/", "_"), slash(Data)),2);

the script tries to save a file with the path Asset_Data/SCRIPT_NAME_1.ml'. Obviously that makes no sense. Likewise if I try
Code:
file='%s_%s')", slash(Data)),strx(Asset, "/", "_"),2);

the file path to be saved is Data/SCRIPT_NAME_1.mlASSET'. Again, that makes not much sense.

What am I missing? How can I save a model to a file of the format
Data/SCRIPT_NAME_ASSET_1.ml using the r.h framework?

Re: adviseLong(NEURAL) in an asset loop [Re: boatman] #457292
01/12/16 03:27
01/12/16 03:27
Joined: Apr 2014
Posts: 482
Sydney, Australia
B
boatman Offline OP
Senior Member
boatman  Offline OP
Senior Member
B

Joined: Apr 2014
Posts: 482
Sydney, Australia
OK, so I guess I must be the only one using the NEURAL functions. I've solved the naming problem above with a little help from Zorro support (thanks!). However, I've struck another obstacle to using NEURAL within an asset loop.

The manual states that NEURAL_TRAIN, NEURAL_SAVE and NERUAL_LOAD are each called only at the end of a WFO cycle. This is fine, but the result is that when called in an asset loop, these functions are executed for the final asset in the loop only.

Is there a workaround, or something that I'm missing, that would enable me to use these functions in an asset loop?

Thanks

Re: adviseLong(NEURAL) in an asset loop [Re: boatman] #457308
01/12/16 18:11
01/12/16 18:11
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
NEURAL_TRAIN is called at the end of any loop() cycle. This is necessary for training different assets and algos.

NEURAL_SAVE and NEURAL_LOAD are of course called at the end of a WFO cycle, since they are supposed to store and load all the trained models. Saving them under a name like SCRIPT_NAME_ASSET_1.ml makes no sense, since there is no particular asset. I think the support got it wrong here. Use the file name given by the Data string and save not only one, but all previously trained models in that file.

Re: adviseLong(NEURAL) in an asset loop [Re: jcl] #457315
01/12/16 23:15
01/12/16 23:15
Joined: Apr 2014
Posts: 482
Sydney, Australia
B
boatman Offline OP
Senior Member
boatman  Offline OP
Senior Member
B

Joined: Apr 2014
Posts: 482
Sydney, Australia
Thank you, that's clear now. In order to train different models for each asset, I've done this:

Code:
if(mode == NEURAL_TRAIN) {
	file_write("Data\\signals.csv",Data,0); // export batch training data to R
    Rx(strf("Data <- read.csv('%sData/signals.csv',header=F)",slash(ZorroFolder)));
   
    if(!Rx(strf("Model_%s <- <<machine learning algorithm>>)", strx(Asset,"/","")))) return 0;



which works as I expected. I've verified in R that each model is actually trained on the correct data too. I'll load the models in the NEURAL_LOAD function via a string that is dynamically created to accommodate each asset in the loop.

One final comment: the manual states that NEURAL_TRAIN is called at the end of any WFO cycle, however I have verified that it is in fact called at the end of any loop() cycle, as you stated. That misleading statement in the manual was a source of some confusion for me. If that could be updated, I'd really appreciate it.

Thanks for looking into this for me, jcl.

Re: adviseLong(NEURAL) in an asset loop [Re: boatman] #457317
01/13/16 08:57
01/13/16 08:57
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
Yes, the manual was misleading. "Training run" was referring to a loop cycle. I'll make the description more clear. You can also use the "model" parameter as an index of a particular model, and store them in R in a list of lists, f.i. Models[[model+1]].

Re: adviseLong(NEURAL) in an asset loop [Re: jcl] #457331
01/13/16 23:10
01/13/16 23:10
Joined: Apr 2014
Posts: 482
Sydney, Australia
B
boatman Offline OP
Senior Member
boatman  Offline OP
Senior Member
B

Joined: Apr 2014
Posts: 482
Sydney, Australia
I don't understand how the "model" parameter is incremented within the function. Where should I specify the models that I want to use? And how is that related to the "model" parameter? Let me explain my confusion through my current approach:

At the moment, I'm doing the following to train a knn model and a random forest model on the same signals for each asset in a loop (although its not relevant, the 'train' function is from the caret package in R):

Code:
if(mode == NEURAL_TRAIN) {
file_write("Data\\signals.csv",Data,0); // export batch training data to R
Rx(strf("Data <- read.csv('%sData/signals.csv',header=F)",slash(ZorroFolder)));

if(!Rx(strf("knn_%s <- train(Data[, c(1,2)], Data[, 3], method = 'knn')",  strx(Asset,"/","")))) return 0; 
if(!Rx(strf("rf1_%s <- train(Data[, c(1,2)], Data[, 3], method = 'rf')",  strx(Asset,"/","")))) return 0;



which seems to work as expected. However, saving and loading the correct models quickly becomes difficult since using this method, I need to specify each model expclicitly:

Code:
if(mode == NEURAL_SAVE) {// save the trained model in the Zorro Data folder
return Rx(strf("save(knn_AUDUSD, knn_EURUSD, rf_AUDUSD, rf_AUDUSD, file='%s')", slash(Data)),2);



With many machine learning algorithm types and many assets, this approach would quickly lead to disarray.

So my question is, how exactly would I specify the above training and saving cycles using the list approach you suggested? Maybe if you could describe how I interact with the "model" parameter, I would understand better. At the moment, its not clear to me how that parameter is used or specified - I have an inkling that I need to specify it in the adviseLong() call?

Thanks for your help, jcl.

Last edited by boatman; 01/13/16 23:45.
Re: adviseLong(NEURAL) in an asset loop [Re: boatman] #457337
01/14/16 09:01
01/14/16 09:01
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
The model number is counted up by asset and algo calls. Currently you're identifying models by asset name, but the 'official' method is identifying them by the model number.

You can store them in a list, save and load the whole list, and access single models in R f.i. with modellist[[model+1]].

Re: adviseLong(NEURAL) in an asset loop [Re: jcl] #457338
01/14/16 09:33
01/14/16 09:33
Joined: Apr 2014
Posts: 482
Sydney, Australia
B
boatman Offline OP
Senior Member
boatman  Offline OP
Senior Member
B

Joined: Apr 2014
Posts: 482
Sydney, Australia
Thanks, I think I'm catching on. If I understood you correctly, something like this would be the 'official' way to use the NEURAL function (obviously the code is truncated to show only the relevant functions):

Code:
{
  if(mode == NEURAL_TRAIN) {
	file_write("Data\\signals.csv",Data,0); // export batch training data to R
    Rx(strf("Data <- read.csv('%sData/signals.csv',header=F)",slash(ZorroFolder)));

if(!Rx("modellist[[model+1]] <- train(Data[, c(1,2)], Data[, 20], method = 'knn')) return 0;  
if(!Rx("modellist[[model+1]] <- train(Data[, c(1,2)], Data[, 20], method = 'rf')) return 0;

...
}



while(asset(loop("AUD/USD", "EUR/USD")))
while(algo(loop("knn", "rf"))) {
	if (Algo == 'knn') {
		adviseLong(NEURAL....);
	}
	if (Algo == 'rf') {
		adviseLong(NEURAL....);
	}
}



Will the two calls to the adviseLong() function work as I expect them to in the case above? That is, will the "model" parameter be incremented in both function calls?

Last edited by boatman; 01/14/16 09:33.
Re: adviseLong(NEURAL) in an asset loop [Re: boatman] #457341
01/14/16 10:42
01/14/16 10:42
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
No, model is incremented by asset, algo, and long/short. In your example above it is not incremented.

For training two different methods 'knn' and 'rf', normally you would just use two different algos and then get two different model numbers.

if(mode == NEURAL_TRAIN) {
...
if(Algo == "KNN")
Rx("modellist[[model+1]] <- train(Data[, c(1,2)], Data[, 20], method = 'knn');
else if (Algo == "RF")
Rx("modellist[[model+1]] <- train(Data[, c(1,2)], Data[, 20], method = 'rf');
...

Re: adviseLong(NEURAL) in an asset loop [Re: jcl] #457363
01/14/16 23:22
01/14/16 23:22
Joined: Apr 2014
Posts: 482
Sydney, Australia
B
boatman Offline OP
Senior Member
boatman  Offline OP
Senior Member
B

Joined: Apr 2014
Posts: 482
Sydney, Australia
Thank you, jcl. The correct use of the NEURAL function is clear now. For completeness, I used string formatting to access the "model" parameter as follows:

Code:
if(!Rx(strf("modellist[[%d+1]] <- train(Data[, c(1,2)], Data[, 20], method = 'knn')", model)) return 0;



A simple hack for using the model training capacity of the caret package is to name the items in the algo loop so that they correspond with methods in caret and then access those methods via string formatting and the "Algo" variable. For example, the following trains a k-nearest neighbours model and a random forest model and stores them in the list "modellist":

Code:
if(mode == NEURAL_TRAIN) {
...
if(!Rx(strf("modellist[[%d+1]] <- train(Data[, c(1,2)], Data[, 20], method = '%s')",model, Algo)) return 0;
...
}
...
while(algo(loop("knn", "rf"))) {
...



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