Gamestudio Links
Zorro Links
Newest Posts
M1 Oversampling
by Petra. 04/24/24 10:34
Zorro FIX plugin - Experimental
by flink. 04/21/24 07:12
Data from CSV not parsed correctly
by EternallyCurious. 04/20/24 21:39
Scripts not found
by juergen_wue. 04/20/24 18:51
zorro 64bit command line support
by 7th_zorro. 04/20/24 10:06
StartWeek not working as it should
by jcl. 04/20/24 08:38
folder management functions
by VoroneTZ. 04/17/24 06:52
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
4 registered members (AndrewAMD, Ayumi, Quad, PeWi), 488 guests, and 6 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Mega_Rod, EternallyCurious, howardR, 11honza11, ccorrea
19048 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
optimize and PARAMETERS|RULES - in asset() & algo #476099
01/26/19 14:08
01/26/19 14:08
Joined: Jan 2019
Posts: 73
berlin
L
laz Offline OP
Junior Member
laz  Offline OP
Junior Member
L

Joined: Jan 2019
Posts: 73
berlin
Hi, guys...

I want to do the following:

Code:
while(asset(loop(ASSETS))) 
while(algo(loop(ALGOS))) {

   TakeProfit = optimize(1,1,4,1) * ATR(100);
   Stop       = TakeProfit / 2.0;

   advise()...

}


I want to see which TP/SL the model can train better - with a custom Objective depending on the TP/SL.

Combining rules and parameters - https://zorro-project.com/manual/en/training.htm

case 1 - "Parameters depend on rules" <- wrong here
case 2 - "Rules depend on Parameters" <- true here
case 3 - "Rules and parameters affect each other." <- true here

For every parameter step a new set of rules needs to be generated.

BUT 2 PROBLEMS:

1.) problem:
Code:
case 2 - "Rules depend on Parameters" 

if(is(TRAINMODE)) set(RULES|PARAMETERS); else set(RULES);


Quote:
[...]In [Test] and [Trade] mode set the RULES flag only, not PARAMETERS, as the parameter only affects rule generation and has no further relevance for the script.

I need the optimized value here. In this case the parameter affects rule generation
and has no further relevance for the script. So i can't use case 1 & 2.

2.) problem:

Code:
solution 3 - Rules and parameters affect each other

set(RULES|PARAMETERS);


the manual still says:

Quote:
Training rules and parameters at the same time only works with single assets, not with a portfolio system that contains loops with asset or algo calls.

Here i found an old post (2014), telling us that Zorro will support that in the future...
https://opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=441154#Post441154
Quote:
But a future Zorro version will support this with portfolio systems also.

So i can't use any of this methods confused ?

Or is it possible meanwhile?

What is the best way?

Last edited by laz; 01/28/19 01:24.
Re: optimize and PARAMETERS|RULES - in asset() & algo [Re: laz] #476103
01/27/19 01:46
01/27/19 01:46
Joined: Sep 2003
Posts: 929
Spirit Offline

Moderator
Spirit  Offline

Moderator

Joined: Sep 2003
Posts: 929
Seems to me a clear case 1, entry rules should be not affected by stop and takeprofit. Entry rules and exit rules are normally completely separate.

Re: optimize and PARAMETERS|RULES - in asset() & algo [Re: Spirit] #476112
01/28/19 00:34
01/28/19 00:34
Joined: Jan 2019
Posts: 73
berlin
L
laz Offline OP
Junior Member
laz  Offline OP
Junior Member
L

Joined: Jan 2019
Posts: 73
berlin
Hi Spirit and thanks for your answer, can we discuss it?

If you have Workshop7 in mind I would agree with you. Fixed patterns are searched first (Step 1) and in Step 2 the best matching parameters to catch the learned patterns. A subsequent change of the parameters does NOT change the patterns found here.

In NEURAL training the "next Trade Return / Result" or custom objectives that looks different (to me). Maybe I'm wrong but I think it does not make sense to train an ML model first on certain rules - if you then change these rules after training again. I'll post some code parts, let's see what you (or others) think about this approach.

PEEK is used and DataHorizon = 30...

Code:
while(asset(loop(ASSETS))) 
while(algo(loop(ALGOS))) {

   TakeProfit = optimize(1,1,4,1) * ATR(100);
   Stop       = TakeProfit / 2.0;

   vars dat = series(price());
   vars rsi = series(RSI(dat,5));

   int pos = 0;            // future bar pos
   var fhv = priceHigh(0); // future highest high value
   var flv = priceLow(0);  // future lowest low value
   var obL = -1.0;         // custom objective long, never 0
   var obS = -1.0;         // custom objective short, never 0
		
   if(Train && !is(LOOKBACK)) {

      for(pos=1; pos <= DataHorizon; pos++) {

         fhv = max(fhv,priceHigh(-pos));
         flv = min(flv,priceLow(-pos));		
					
      }

      if(valley(rsi) && flv > (priceClose(0) - Stop) && fhv > (priceClose(0) + TakeProfit)) obL=1.0;
      if(peak(rsi)   && fhv < (priceClose(0) + Stop) && flv < (priceClose(0) - TakeProfit)) obS=1.0;

   }

   var avL = adviseLong(NEURAL+BALANCED,obL,INP,12);
   var avS = adviseShort(NEURAL+BALANCED,obS,INP,12);

}


Starting from the current position (Bar) I'm scanning the next 30 bars for it's lowest and highest values. After that i create a custom objective, if price starts from valley/peak and hits the Takeprofit but not the Stoploss, take this trade / objective = 1 else -1.

Question 1: Changing the TP/SL after i have trained the model with a custom objective makes no sense here?

Question 2: If you use Objective = 0, you will train your model for every positive trade. These often win only by chance, not because a rule is behind these entry's. I have never achieved meaningful results with this method (open a trade on every bar + learning all positive trade returns). What should an ML model generalize here?

Last edited by laz; 01/28/19 02:09.
Re: optimize and PARAMETERS|RULES - in asset() & algo [Re: laz] #476115
01/28/19 03:45
01/28/19 03:45
Joined: Jul 2000
Posts: 27,982
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,982
Frankfurt
In your script the rules are indeed affected by stop and takeprofit. So you have a case 3 here. The question is only if this generates a good model.

In our experience, a machine learning model usually deteriorates with the complexity of the objective. The best models use 0, the price difference, or the price sign for the objective. The "bad" system of your question 2 is in fact one of the best.

Re: optimize and PARAMETERS|RULES - in asset() & algo [Re: jcl] #476116
01/28/19 05:04
01/28/19 05:04
Joined: Jan 2019
Posts: 73
berlin
L
laz Offline OP
Junior Member
laz  Offline OP
Junior Member
L

Joined: Jan 2019
Posts: 73
berlin
Hi jcl, i was also thinking that - but in the manual is still written:

Quote:
Training rules and parameters at the same time only works with single assets, not with a portfolio system that contains loops with asset or algo calls.

I'm not sure what happens if i do it with set(RULES|PARAMETERS); in Asset/Algo loops ? Will it work correctly?

Quote:
In our experience, a machine learning model usually deteriorates with the complexity of the objective.

I agree... But using every possible next trade return (enter on every bar/input signal) is not complex?

Quote:
The best models use 0, the price difference, or the price sign for the objective. The "bad" system of your question 2 is in fact one of the best.

How is that possible? Imagine a simple Perceptron, if we change the Input, we also change the Output. In Training (by using Trade returns) we move the weights to separate our classes. We want the perceptron to fire if the input/s is(are) "in range X to Y".

But if we use Trade Returns from all possible entry's - no matter how small or big the profits are, f.i. 0.01 to 200 - trades starting from random/different Input values - aren't we confusing the TRAIN process here? What is our target? And if we use all the "next Trade returns" we train so many entry points which have no meaning? (bad or no local minima)

A Perceptron or NeuralNet is not a Support Vector Machine.

Quote:
An SVM model is a representation of the examples as points in space, mapped so that the examples of the separate categories are divided by a clear gap that is as wide as possible.

Am I thinking wrong?

Thank you, I do not know anybody else I could ask such questions wink

PS: Maybe your models didn't train well (with a custom objective) because +BALANCED does not work with extreme unbalanced classes. I have seen it with that example and did the up-sampling in R.

Last edited by laz; 01/28/19 05:36.
Re: optimize and PARAMETERS|RULES - in asset() & algo [Re: laz] #476118
01/28/19 06:54
01/28/19 06:54
Joined: Jul 2000
Posts: 27,982
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,982
Frankfurt
I think the single asset restriction is still in place, at least I have no otherwise notice. And yes, you're confusing the training process when you use stops and then train trade returns. Train the system only with plain returns or with the price curve or other fundamental data, but not with artificial values generated with stops or other algorithms.

Your conception of a SVM looks correct.

Re: optimize and PARAMETERS|RULES - in asset() & algo [Re: jcl] #476120
01/28/19 08:44
01/28/19 08:44
Joined: Jan 2019
Posts: 73
berlin
L
laz Offline OP
Junior Member
laz  Offline OP
Junior Member
L

Joined: Jan 2019
Posts: 73
berlin
Ok thanks for the information, maybe you can add this feature in the future wink...

So for me the only way to get this running is to do it in separate for loops?

Quote:
... train all portfolio components separately and combine the resulting .c files. The Combine.c script can be used as a template for automatically combining parameters and rules from different assets or algos. Machine learning models (.ml files) can be combined with an appropriate R function.

I'll later look into Combine.c and i can do the R part, no problem.

When i use separate for loops, i can use every option? For WFO nothing changes?

1. Parameters depend on rules.
2. Rules depend on parameters.
3. Rules and parameters affect each other.

Quote:
And yes, you're confusing the training process when you use stops and then train trade returns. Train the system only with plain returns or with the price curve or other fundamental data, but not with artificial values generated with stops or other algorithms.

I have to think about it wink thanks

with the price curve = regression?

But one question comes up again, in neural.train() you cast every positive return to 1 and everything else to 0.
How do you define the targets/close of your trades? What am I missing here? confused ?

Last edited by laz; 01/28/19 17:50.
Re: optimize and PARAMETERS|RULES - in asset() & algo [Re: laz] #476132
01/28/19 19:03
01/28/19 19:03
Joined: Jul 2000
Posts: 27,982
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,982
Frankfurt
The Combine script is for .c files, but when you use R you have .m files and must handle the combining on the R side.

WFO works. You'll then get several .m files.

Trades for training have normally no target, but a fixed lifetime. Instead of 0/1, you can also scale the result to an analogue 0..1 range - what is better depends on your ML algo.

Re: optimize and PARAMETERS|RULES - in asset() & algo [Re: jcl] #476134
01/28/19 23:50
01/28/19 23:50
Joined: Jan 2019
Posts: 73
berlin
L
laz Offline OP
Junior Member
laz  Offline OP
Junior Member
L

Joined: Jan 2019
Posts: 73
berlin
Thanks for the feedback, I have looked at the combine script. And I already have loaded the .ml files in R, I am familiar with R. In R I just have to load the ml files and create a new (per WFOCycle) list and save it. I will post the R code here, if someone needs it again.

Quote:
Trades for training have normally no target, but a fixed lifetime. Instead of 0/1, you can also scale the result to an analogue 0..1 range - what is better depends on your ML algo.

I do not quite understand this approach yet but I will think about it. So far, I have tried using forecasting/regression rather than classification, because of the question: How to train an ML-Algo on a target (class) that I can not clearly define. If I had a trainer that could produce accurate train-signals, I would not need to teach ML-Algos crazy ...

I do not understand with this principle, which of the many different training signals the net should learn. And what should it learn from it. Is there somewhere more detailed information on the subject?

Re: optimize and PARAMETERS|RULES - in asset() & algo [Re: laz] #476144
01/29/19 02:37
01/29/19 02:37
Joined: Jul 2000
Posts: 27,982
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,982
Frankfurt
There are many online informations or books about machine learning. In finance the goal is normally predicting the future price direction or trade result. I suppose that's also the intention of your script. In special cases you want to predict other parameters such as volatiliy or market efficiency.

Re: optimize and PARAMETERS|RULES - in asset() & algo [Re: jcl] #476151
01/29/19 11:03
01/29/19 11:03
Joined: Jan 2019
Posts: 73
berlin
L
laz Offline OP
Junior Member
laz  Offline OP
Junior Member
L

Joined: Jan 2019
Posts: 73
berlin
Quote:
In finance the goal is normally predicting the future price direction or trade result. I suppose that's also the intention of your script.

Exactly, that's what i try. Maybe we understand each other wrong, but I'm talking about the method Objective = 0, which causes every positive trade to count as a signal. I do not understand what an ML-Algo should / can learn if we open randomly a trade on each bar and then use the results as a learning guide -> because in neural.train() in R you're doing this: Y <- ifelse(Y>0,1,0)

So every Trade with profit is learned as GOOD. No matter where in the chart it starts, no matter how big or small it was, what's "the underlying rule / the strategy / the edge" of signals generated in that way? What is the trade based on? Sorry that I ask so much, that's because I would like to understand the approach.

But I am currently on a very different problem cry . This thread was actually about using RULES | PARAMETERS with asset() and algo() in loops(). We have already clarified that this is not possible, which is a pity. The solution was using "custom for loops" and not the loop() function from Zorro.

Code:
string myALGO[2];

int algoList() {

	myALGO[0] = "W4T0";
	myALGO[1] = "W4T1";

	return(2);

}

function W4T() {

	TimeFrame = 1;

	var  op1 = optimize(100,100,200,100);	
	var  op2 = 3;// optimize(1,1,2,1);

	vars dat = series(price());
	vars trd = series(LowPass(dat,op1));
	vars mmi = series(MMI(dat,300));
	vars smo = series(LowPass(mmi,300));
		
	Stop  = op2 * ATR(100);
	Trail = 0;

	if(strstr(Algo,"W4T0")) {
		
		if(valley(trd)) enterLong(); else			
		if(peak(trd))   enterShort();

	} else if(strstr(Algo,"W4T1")) {

		if(falling(smo) && valley(trd)) enterLong(); else			
		if(falling(smo) && peak(trd))   enterShort();

	}

}

function run() {

	set(COMMONSTART|PARAMETERS|OPENEND|LOGFILE);

	StartDate    = 2016;
	EndDate      = 2017;
	BarPeriod    = 60;
	LookBack     = 500;

	DataSplit    = 70;
	NumWFOCycles = 5;
	
        PlotWidth    = 3000;
	PlotHeight1  = 666;	
	Verbose      = 3; // default 1

	int asc = assetList("AssetsFix.csv"); // please edit	
	int alc = algoList();
	
	int i   = 0;
	int j   = 0;	

	for(i=0;i<2;i++) { // test only scan 0 & 1
	
	   asset(Assets[i]);

		for(j=0;j<alc;j++) {
	
			algo(myALGO[j]);			

			Script = strf("%s%s",Asset,Algo);
	
			if(is(INITRUN)) watch("#INITRUN @ Bar",Bar,Asset,Algo,Script);	
	
			if(strstr(Algo,"W4T")) W4T();
	
		}
	
	}

}


I expected Zorro to produce single .par files for each Asset / Algo / WFOCycle, at least after changing the script name. However, only 1 file per WFOCycle is created. It contains 4 parameters, that is wrong or what I have misunderstood here?

Zorro creates the file in EXITRUN? How can i force the creation of separate files inside the for loops?

Last edited by laz; 01/29/19 11:34.
Re: optimize and PARAMETERS|RULES - in asset() & algo [Re: laz] #476154
01/30/19 04:25
01/30/19 04:25
Joined: Jul 2000
Posts: 27,982
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,982
Frankfurt
I do not really understand the randomly opened trade issue. You do not open them randomly, you open them always. Thats just how training works. The more trades, the better the training. The example uses classification, but the method is the same for regression.

Your multi asset optimizing does probably not work because loop() is missing. And Script renaming has no effect until the parameters are stored. They go into a single file. Look up loop() in the manual and check out workshop 6, which you can use as a template. If you want many files for some reason, either train all assets separately, or write a script to split the .par file into many.

Re: optimize and PARAMETERS|RULES - in asset() & algo [Re: jcl] #476218
02/05/19 15:32
02/05/19 15:32
Joined: Jan 2019
Posts: 73
berlin
L
laz Offline OP
Junior Member
laz  Offline OP
Junior Member
L

Joined: Jan 2019
Posts: 73
berlin
Hi jcl, I soon realized that I had probably misunderstood something.

Quote:
Training rules and parameters at the same time only works with single assets, not with a portfolio system that contains loops with asset or algo calls. If required, train all portfolio components separately (manually) and combine the resulting .c files. The Combine.c script can...

I had thought that you could use several assets and algos + parameter optimization if you run it in a self created loop grin. I'm already using the asset () and algo () loop from W6 again. The parameter optimization is done in R. Fortunately, I found my old "caret" framework and integrated it into Zorro.

Question 1 is solved laugh

Quote:
I do not really understand the randomly opened trade issue. You do not open them randomly, you open them always. Thats just how training works. The more trades, the better the training. The example uses classification, but the method is the same for regression.

And how do you know how you have to apply this later? How do you then determine the targets and the stops of these trades in Testing/Trading?

Quote:
is the same for regression

Is that the hidden hint? crazy ?

Last edited by laz; 02/05/19 15:39.
Re: optimize and PARAMETERS|RULES - in asset() & algo [Re: laz] #476221
02/05/19 17:30
02/05/19 17:30
Joined: Jul 2000
Posts: 27,982
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,982
Frankfurt
Stops and targets are normally not used in training.

Re: optimize and PARAMETERS|RULES - in asset() & algo [Re: jcl] #476224
02/05/19 22:39
02/05/19 22:39
Joined: Jan 2019
Posts: 73
berlin
L
laz Offline OP
Junior Member
laz  Offline OP
Junior Member
L

Joined: Jan 2019
Posts: 73
berlin
I understand that, but the question was:

How do you then determine the targets and the stops of these trades in Testing / Trading?

After you have fitted the model in training...

Last edited by laz; 02/05/19 22:39.
Re: optimize and PARAMETERS|RULES - in asset() & algo [Re: laz] #476225
02/06/19 07:41
02/06/19 07:41
Joined: Jul 2000
Posts: 27,982
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,982
Frankfurt
Normally not at all, since the trained trades have no stop and TP. But often distant stops are still used for preventing rogue trades. The distance is then determined by average volatility.

Of course, if you want, you can optimize stops and TPs with a case 1 combination, after training the ML model.

Re: optimize and PARAMETERS|RULES - in asset() & algo [Re: jcl] #476228
02/06/19 11:36
02/06/19 11:36
Joined: Jan 2019
Posts: 73
berlin
L
laz Offline OP
Junior Member
laz  Offline OP
Junior Member
L

Joined: Jan 2019
Posts: 73
berlin
Now it's getting really interesting, thank you very much for sharing your knowledge with us/me. smile

To summarize your explanations:

1.0) Training:
1.1) - you open a new trade on every bar (no TP / SL), Zorro saves the returns of every trade/bar
1.2) - in your examples you convert the trade return results to binary 0/1
1.3) - you train your ml-model with the binary(0/1) signal as target
1.4) - the lifetime of the trade is regulated only by the parameter LifeTime

2.0) Testing / Trading:
2.1) - a far away stop outside of the "normal working area" is used as an emergency anchor
2.2) - the distance (TP/SL/both?) is then determined by average volatility
2.3) - the LifeTime parameter is ...?
2.4) - you close the trade by ...?

Questions:
I am really sorry but some parts of this approach are still unclear, if it is ok - I would like to ask more?

Q-1.2) you convert the trade returns to a binary outcome and train that 0/1 result.
But - in this way, you remove exactly the information as to how successful the predicted trade may be or am I wrong? Why?

Q-2.2) The distance (determined by average volatility) of the target / stop or both?

Q-2.3) The LifeTime parameter is now enabled/disabled in Testing / Trading?

Q-2.4) You close the trade by TP/SL or on opposite signal only?

The most confusing thing is the conversion into a 0/1 signal. If you were going to use different
classes (<= 10Pip || >10Pip/>20Pip...) as a target or make a regression on the trade return - I
would understand that. You already mentioned something similar.

Very interesting to talk about it, thank you!

Re: optimize and PARAMETERS|RULES - in asset() & algo [Re: laz] #476271
02/09/19 10:39
02/09/19 10:39
Joined: Jul 2000
Posts: 27,982
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,982
Frankfurt
Removing unnecessary information can be essential for effective machine learning. You often use 0/1 classification, not regression. Same reason why you normally use only a few signals as inputs, not the whole price curve. Just test it and you'll see what is better in your case.

A frequent trade setup is no TP, distant Stop, and Lifetime the same as in training.

Re: optimize and PARAMETERS|RULES - in asset() & algo [Re: jcl] #476338
02/19/19 17:12
02/19/19 17:12
Joined: Jan 2019
Posts: 73
berlin
L
laz Offline OP
Junior Member
laz  Offline OP
Junior Member
L

Joined: Jan 2019
Posts: 73
berlin
I'm still testing it, but I can not come to a reasonable conclusion. The hit ratio is quite impressive, but the many small trades with little profit do not compensate for the losses? That was also the reason why I wondered, why every trade return > 0 is to be considered as positive. The NN/ANN is trained to execute also the small trades.

If we cast TradeReturns into 1/0 we get the direction but we lose any information about the expected movement?

I have been searching for long time for the topic "trade returns and/or classification machine learning etc.", but I did not find anything that works profitable with this approach. Fitting a classification model on the binary (0/1) trade results makes sense, because you do not need a trainer and your classes are more balanced. But how do I get that profitable? That's why i switched to forecasting/predicting the next x values, before I discovered Zorro.

But this is not the way how Zorro or all of your examples work.

Maybe I was on the wrong track, what did I misunderstand?

Last edited by laz; 02/19/19 18:03.
Re: optimize and PARAMETERS|RULES - in asset() & algo [Re: laz] #476387
02/21/19 23:14
02/21/19 23:14
Joined: Jan 2019
Posts: 73
berlin
L
laz Offline OP
Junior Member
laz  Offline OP
Junior Member
L

Joined: Jan 2019
Posts: 73
berlin
After endless testing and thinking and testing again, I now think I have come to a conclusion. I now understand how it works & why you are not a fan of fixed TP / SL.

Code:
!-- mfe/mea check --------------------------- PIP 0.00010 
!hrz 3 | trade @ bar 6808 | TakeProfit 88.97132 pips | Stop 44.48566 pips 
!hrz 3 | trade @ bar 6808 | 13.40032 pips (mfe) profit | -36.59964 pips (mae) loss 
!-- mfe/mea check --------------------------- PIP 0.00010 
!hrz 3 | trade @ bar 6809 | TakeProfit 89.31737 pips | Stop 44.65868 pips 
!hrz 3 | trade @ bar 6809 | 32.19962 pips (mfe) profit | -6.69956 pips (mae) loss



If you look at those numbers and think about them, you'll notice why every type of fixed SL / TP in training is totally counterproductive.

You just can not impose your will on the market wink ...

MFE & MAE are for the next 3 bars ...

And now I know in which direction I will continue to search, thank you for notifying me JCL and sorry for my lack of clarity.

Page 1 of 2 1 2

Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1