Cannot reach Optimization results in strategy
Author: wernerhh
Creation Date: 8/10/2020 11:12 AM
profile picture

wernerhh

#1
Hi,

when using the Optimization function, I am able to acquire high Annualized Gains.

I check out the parameters, which were used for the highest gain and then after

CODE:
Please log in to see this code.


I set it manual to the "optimal" value:

CODE:
Please log in to see this code.


This is just an example. But like this I do it with all the parameters. Still the Backtesting result is not equal to the high Annualized Gain from the Optimization function. Often it is significally lower, sometimes even higher, and almost never the same.

Provided these facts, must there be a serious problem in my code? Or is this effect common?

Best regards
Werner

profile picture

Eugene

#2
Hi Werner,

1. What position sizing are you using and how many symbols are in your DataSet?

2. Is this only the Annualized Gain affected or other parameters, too?
profile picture

LenMoz

#3
Not setting LastPosition.Priority is another possibility.
profile picture

wernerhh

#4
Hello, thank you for the replies.

Position Sizing:
Portfolio Simulation Mode / Percent of Equity = 22,50 / Margin Factor 1,11 to 1. (Preferences: Interest on margin loan amount = 7%)

(I use this margin settings to ensure that in case there are a few dollars missing for a purchase, a margin will be used. But besides that, no margin should be used.)

Dataset:
S&P 100 from Wealth-Data

Data Range:
01.07.2000 to 30.06.2020

Now I had a look at the other results and I can say that not only the Annualized Gain in % changes, also the others like, Net Profit, Number of trades, Average Profit per trade or Maximum Drawdown.

With this settings active, the Optimization function should use them as well, right? So, I wonder why the behaviour seems to be different.
profile picture

Eugene

#5
Looking at your position sizing and a big DataSet (and a probable lack of Position.Priority assignment), it's expected.

Your optimization is affected by the randomness factor in position sizing. Please see this FAQ: I'm using 100% Equity position sizing and strategy doesn't seem to use all capital and/or there are trades not included due to insufficient capital.

On the other hand, assigning a fixed Position.Priority value as @LenMoz suggested is a good approach. I would suggest that you use it to solve your issue. A turnkey alternative to it is to enable "Use Worst Trades in Portfolio Simulation" in Preferences > Backtest Settings but it may be less desirable for a "pessimistic" treatment of your backtest.
profile picture

wernerhh

#6
Hi Eugene,

the problem is arising despite having a position priority. Because of position priority I receive the same results whenever I do the backtesting. But I cannot reproduce the results achieved with optimization. Here is my priority setting:

CODE:
Please log in to see this code.


Best regards
Werner
profile picture

Eugene

#7
Hi Werner,

1. Although it looks good on the surface, this short code snippet is not enough to tell because e.g. LastPosition can be mistakenly applied in a multi-Position context etc.

2. To reproduce results achieved in an optimization you should double click on its row in the Optimization Results. This opens a backtest with exactly the settings applied. Are you doing this?

3. What kind of optimization are you running (type, parameters, settings) etc.?
profile picture

wernerhh

#8
HI Eugene,

thank you for your message. I think the priority settings works well, as I always get the same results when backtesting.

Yes, when I double click on a row in the Optimization Results, I get exactly this result shown in the results. However, when I put these values into the code, so that they are always used, I get different results.

I put this values in the code either by setting it as the standard value for a parameter:

CODE:
Please log in to see this code.


or I set the variable directly:

CODE:
Please log in to see this code.


I am running "Monte Carlo" with "APR%" to be optimized with very different settings for number of runs and number of Tests in reach run.

Werner
profile picture

Eugene

#9
Hi Werner,
QUOTE:
Yes, when I double click on a row in the Optimization Results, I get exactly this result shown in the results.

Great. At least everything is fine about this (crucial) aspect then.

QUOTE:
I am running "Monte Carlo" with "APR%" to be optimized with very different settings for number of runs and number of Tests in reach run.

What Scorecard are you optimizing and against and what APR% / Annualized Gain numbers you are comparing to in what Performance visualizer? Screenshots might help. If you decide to attach, make sure the left-most pane with strategy parameters and settings (position sizing, data loading and DataSets) are visible.
profile picture

wernerhh

#10
Hi Eugene,

thank you very much for your help and your efforts. I found out what the problem was. It was my mistake. You were right, it had to do with priority setting. My code was like this:

In the constructor I declared the parameter for the priority optimization:

CODE:
Please log in to see this code.


Then, at the top of the Execute funciton I had this:

CODE:
Please log in to see this code.


And, after that I set the variables for a strategy (the ones I found out through optimization):

CODE:
Please log in to see this code.


But at that time the "RSI.Series" was already set up. So it did not use the value - in this case 167. I could not figure it out as it was correct for the other parameters. So, in the end all I had to do is to put the statement "RSI rsi = RSI.Series(Close, pri_per);" below the setting of the variables.

Thank you very much for your help!
profile picture

Eugene

#11
Glad to see you've figured out the source of error!
profile picture

wernerhh

#12
Thank you for your help, Eugene!
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).