New style of combining strategies using Historical Trades
Author: KGo
Creation Date: 4/20/2020 8:37 PM
profile picture

KGo

#1
A powerful new style of combination strategy can now be created from the trades of individual strategies. Trade sizing is available based on portfolio equity at each entry bar. This is a fundamental design difference from the native WL Combination Strategy and the main reason for creating this capability.

Mechanics of Historical Trades Combination Strategy
1. All trades from each contributing (child) strategy are combined. A "child" here is one csv file containing trade data from one strategy. Many child strategies may be used to create a combination strategy.
2. All child strategy trades are combined to share one Portfolio Simulation backtest. All the trades are sized based on bar by bar portfolio equity as is the normal WL process. Most sizing methods are available. Child strategies are not constrained by individual starting equities but only by cash/margin available to each trade.
3. Trade priority is used to accept and reject trades on each bar when cash is limited. The original Child strategy priority may be user adjusted to give order of preference to child strategies.

Here is the how to detail:
1. Save Trades - Save trades from each strategy to a csv file by executing Community Components SaveHistoricalTrades once at the very end of each strategy Execute block:
CODE:
Please log in to see this code.

Change percent equity per trade value and use Open, Close or Price in above to match your strategy. Save HistoricalTrades.csv to your Documents folder after editing above path. Trades should be saved from each strategy to a different csv file name. Both Single Symbol Backtest (SSB) and Multi Symbol Backtest (MSB) modes are supported. Time scales are supported but mixing scales like daily and intraday combinations must be executed on the daily scale.
2. Check Config - Verify all csv files for the combination are active in HistoricalTradesConfig.txt located in the Documents folder.
3. Execute - Open Historical Trades: No Coding with By Symbol Optimization and select position sizing method. Results of the combination strategy will display. There is no coding required.

Position sizing:
Historical Trades is compatible with most position sizing methods. However the preferred method for combination strategies is PosSizer Position Options using Tag and percent equity. Save trades from 3 strategies with ~%Equity:100 on each to set the Tag value and Historical trades will assign 33% to each when run as a 3 system combination. If only 2 are combined they will automatically be assigned 50%. And executing any 1 system will automatically assign 100%. This is very helpful when for example a 4 position rotation strategy with ~%Equity:25 is combined with a single position strategy using ~%Equity:100. Historical Trades will automatically assign 12.5% and 50% of portfolio equity to the respective strategies. In effect position sizing becomes automatic as the number of combinations changes. The adjusted Position Options percent equity is output in the trade entry name to verify final sizing.


By Symbol analysis is available for the combination portfolio when all symbols are in the same dataset. See:
https://www.wealth-lab.com/Forum/Posts/New-By-Symbol-Analysis-Available-40088/
profile picture

abegy

#2
Hi KGo,

Firstly, I would like to thank you to have shared your nice tool.

I try to use it and I have a question about its usage.

I have created the HistoricalTrades.csv file for my strategy based on a Run in a MultiSymbol Backtest (MSB) mode.
When I open "Historical Trades: No Coding with By Symbol Optimization", do I need to run it also in a MultiSymbol Backtest (MSB) mode or select only the same symbol that I see in the HistoricalTrades.csv ?

For your information, if I run it in a MultiSymbol Backtest (MSB) mode and apply an optimization with Exhaustive optimizer (say DS Symbol#), WLD stops to work automatically during the process if I have selected "MS123 Scorecard". Maybe a bug somewhere...

Take care of you
profile picture

KGo

#3
QUOTE:
if I run it in a MultiSymbol Backtest (MSB) mode and apply an optimization with Exhaustive optimizer (say DS Symbol#), WLD stops to work automatically during the process if I have selected "MS123 Scorecard".

Sorry my instruction was incomplete - see edit below. Start an optimization by symbol with one symbol selected in the data tree not the entire dataset. The optimizer should not be set for MSB since all results would be the same and it would take forever. Set optimizer to one symbol at a time by selecting one symbol in the datatree and setting the default of the first parameter to 1-1Sym. Only check the "DS Symbol #" parameter and adjust the max value for the number of symbols in your dataset. The settings are to run one symbol at a time to get results for each. It should take a few minutes for each 100 symbols. Very long times are likely setup incorrectly. Instruction from Strategy Description is edited below.

QUOTE:
do I need to run it also in a MultiSymbol Backtest (MSB) mode or select only the same symbol that I see in the HistoricalTrades.csv ?
Here is what I typically do. It will give a feel for how parameters work. Click around to get a feel for it.

Step one. First check the entire backtest. Select a symbol with all bars and leave all parameters set to zero. This will duplicate the original MSB run if all other WL settings are the same. Please start with this when you open the program.

Step two. Second check one symbol results. Set first parameter to one (1-1Sym) to tell the system to run only one symbol per execution. The next parameter "0-DSS# 1-Click." Zero tells the system to execute the symbol defined by the Dataset Symbol Number or 1 to execute the symbol clicked in the dataset tree. Try both ways.

Step three. Run an MSB after resetting all parameters to zero. This will take much longer since it is checking all trades for every symbol. It will give the same result as first step above but will add the By Symbol tab

Step four. Lastly use the optimizer to get all one symbol results. You MUST set default of first parameter to 1-1Sym. If it is left at zero all symbols will produce the same MSB result and it will take a very long time. See instructions in the Strategy Description which is edited below to select one symbol in data tree not entire dataset. Know that you can double click an optimized row to run just that symbol.

Below is instruction for step 2, 3, 4 copied from the Strategy Description
Strategy Parameter Descriptions:
"0 - All, 1 - 1Sym"    0-Trades all symbols in the file. 1-trades only the active symbol.
"0 - DSS#, 1-Click"    0-The active symbol is the given DataSet Symbol Number(DSS#). 1-The active symbol is clicked in the dataset tree.
"DS Symbol # "     The DataSet Symbol index (0 to DataSet.Symbol.Count-1)
"Enter/ExitAt"      See table below.
"PercentEquity"     Must use Position Options set Tag %Equity with values > 0 percent
"SPM Sys Num"     Single Position Mode column numbers to execute or optimize a spreadsheet of signals

To Run MultiSymbol Backtest (MSB) and create by symbol tab for trades taken:
Set 1-1Sym parameter
Click Dataset name in Data Panel
On Data Set tab click Backtest on all Symbols
Click By Symbol tab to view by symbol performance for trades actuallly taken.

To optimize by symbol and create performance metrics by symbol:
Set Data Panel as when running the strategy (EDIT - but select only one symbol not the dataset tree)
Uncheck all parameters in Otimizer
Set optimizer default to 1-1Sym to process one symbol at a time.
Set optimizer default to 0=DSS# to use the dataset symbol number needed by the optimizer
Check DS Symbol # and set stop to number of symbols in dataset-1 to process each symbol in dataset.
Set percent equity default value if using Position Options. Zero uses the value(s) set in the csv or config file.
Begin Optimization
View Results tab in optimizer. Double ckick any result for full analysis

To get results for 1 symbol:
Set 1-1Sym parameter
Set 1-Click parameter
Click any symbol in the DataSet tree to see results
profile picture

abegy

#4
Thank you very much. I understand my mistake.

Can I suggest you a new feature ? Is it possible to add the backtest for multicurrency strategies ?
Cone gave an idea how to do that here : https://www.wealth-lab.com/Forum/Posts/Normalize-on-USD-30665

I think you need to introduce global parameters to your tool :
- allow to specify your base currency
- allow to give you the possibility to specify currency and the associate symbol data series
- add a parameter to specify by FileNameString the currency to used
- implement the Cone's logic in your tool

In this case, conditions to run in a multicurrency mode are :
- have a dataset with currency symbols
- split your symbols list in a separate dataset by currency
- create a global dataset with all symbols in the previous datasets
- run your strategy by separet dataset
- modify HistoricalTradesConfig.txt to specify the currency for each FileNameString
- open Historical Trades: No Coding with By Symbol Optimization

What do you think about this ?
profile picture

KGo

#5
QUOTE:
Is it possible to add the backtest for multicurrency strategies ?

This solution for multi-currency strategies can be backtested now. It's quite simple and requires no modification to Historical Trades. Disclaimer: Untested but consistent with other applications of Historical Trades. Please verify and correct if needed.

1. Create a low priority dataset with all the different currency symbols in it with either their original price value or an exchange rate adjusted value. (See KGo Post #8)
2. Save trades from original dataset(s) strategies using SaveHistoricalTrades.
3. Run Historical Trades in the single currency dataset created in step 1.

Generally it's not the absolute price that matters but the correct percent difference between prices on different bars. Data saved with the correct percent differences will work like they are all the same currency. Much like stock splits change the price values but not the performance results.

Percent equity or fixed dollar sizing are best for this backtest.
profile picture

abegy

#6
Hi KGo,

Except if I'm wrong, your solution is about the possibility to backtest forex trading strategy on differents currency pair. Right ?
My need is different. I want to backtest a strategy which buys stocks in USD from AMEX and buys stocks in SEK from Stockholm stock exchange.

Look this example :

Currenctly, 1 USD = 10 SEK
Each trade will be aquivalent to 1 000 USD.
To trade 24Storage A (quotation = 40 SEK), I need to push an order with 250 shares
To trade Alcoa (quotation = 7 USD), I need to push an order with 142 shares

If I realize 10% profit, the result will be :
24Storage A = 1 000 SEK
Alcoa = 100 USD

In the backtest result, you will see : 1100 as the final result. This is wrong because the result must be 200 USD
To correct it, you need to normalize each order to the same base currency.
In this case, you will have to buy 25 shares for 24Storage A. For your information, I prefer adjust shares quantity and not stocks price to be able to see signal on the chart without special chart adjustment.

Do you see the subject now ?
profile picture

Eugene

#7
Alexandre, you can't squeeze blood from a turnip. Multi-currency backtests have already been made possible with the help of 3rd party utility created by Carros at Domintia:

Multi Currency Solution
Multi-currency (FX conversion for non$ denominated trades)
How to set up multi-currency futures trading?
etc.
profile picture

KGo

#8
QUOTE:
In this case, you will have to buy 25 shares for 24Storage A. For your information, I prefer adjust shares quantity and not stocks price to be able to see signal on the chart without special chart adjustment.

This is Example 2 below using SEK prices in the USD backtest. WL HT will buy 25 shares at $40 each for $1000 position. Historical Trades will show the signal on the chart for both examples. It's your choice of either example method.

It seems the proposed HT solution does exactly what you want. It is for backtesting a multiple currency stock portfolio all in one dataset. Your example with Alcoa in USD and 24Storage in SEK will yield $200 USD backtest profit as expected. Example 2 will buy 25 adjusted shares as you prefer or Example 1 will buy 250 shares when you adjust prices 10:1. Both examples will show the trade on the chart. Your choice.

Here are the 2 examples with further description:

Example 1 is based on the SEK price series converted to USD with the 10:1 exchange rate. This is Cones's method. It works in HT but you need to create the price data in 1 dataset. Excellent if trading at the open or close. If your original strategy buys and sells at open or close I prefer Example 1.

Example 2 leaves the prices in SEK values. Note resulting backtest shares are factored by the exchange rate (25 not 250). And the exchange rate is never entered. Best if trading at a price and saving trades (#2 in post #5 SaveHistoricalTrades) from the original SEK data. You did not specify if trading at price so this method was recommended. Remember the trade list with prices is created from an original SEK currency dataset. ( Note: If you trade at price in HT and do adjust the price series per Example 1 or any split factor, HT will automatically correctly adjust the series back to make the buy at the trade list price given. Trade will not show on the chart since it will be a created split bar "_SB" series - But that's another topic.)

In fact you may adjust the SEK prices in the HT dataset by any exchange rate/split factor, the profit will be the same. Shares will vary. It is your choice to adjust SEK prices or not. There is no downside to adjusting when trading at Open or Close since actual shares to buy are output and they will show on the chart. Shares will always be adjusted to fit the position size and profit will be the same. Share rounding not considered.
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).