Log in to see Cloud of Tags

Wealth-Lab Wiki

Backtesting deposits and withdrawals

RSS

What it is

Wealth-Lab doesn't have a built-in feature for backtesting the effect that deposits/withdrawals might have on an account. This workaround makes it possible. It consists of several easy steps:

  1. Install (or update) MS123 Extra Fundamental/News providers
  2. Create history of account transfer records in a text file
  3. Create dummy "dividend generator" symbol with synthetic historical data
  4. Configure Wealth-Lab
  5. Adjust your WealthScript Strategy

Let's get started:

Create history of account transfers (deposits, withdrawals)

The fundamental provider recognizes them through a plain text file under WL's user data folder. Navigate there with hidden file visibility enabled and create the file named AccountTransfers.txt in Notepad with the history of account transfers:

c:\Users\Windows account name\AppData\Roaming\Fidelity Investments\WealthLabDev\1.0.0.0\Data\AccountTransfers.txt

NOTE! Make sure the date matches your local format.

Account1,05/01/2010,1000
Account1,13/06/2011,10000
Account1,14/09/2012,6000
Account1,16/10/2013,-2000
Account1,05/11/2014,2000
Account1,22/06/2015,-3000
Where Account1 is identical to the synthetic symbol that generates dividends (you'll create its fake price history in ASCII file in Step 2). Multiple accounts should be supported: just put there Account2, Account10 etc. Positive numbers will deposit funds to the equity curve and negative withdraw from it. The dates of dividends (deposits) must be real and not fall on weekends or holidays.

The file can be edited on the fly to experiment with its effect on backtests.

Create synthetic symbol that generates "dividends"

Next you need to create the Account1 symbol with dummy historical data and connect it to Wealth-Lab using ASCII provider (in fact it's the easiest option, but other providers will do e.g. Excel). To avoid synchronization hiccups you might want to use the backtested symbol's actual data, export it, and modify as instructed:

  • Step on your backtested symbol and run this code: Export2ASCII. Your resulting file should be saved as Account1.csv (or txt).
  • Open the resulting CSV file in Excel and leave only two columns: Date and Close. Leave Dates as is.
  • The crucial step is to replace all Close prices with some little value like "1". The less the dummy price the less is its impact on the actual backtest performance.

There you have it looking something like this:

29.01.1993,1
01.02.1993,1
...
11.01.2016,1
  • Now create a new ASCII DataSet, make sure the symbol is named exactly Account1 or whatever name you used in AccountTransfers.txt.
  • In Wealth-Lab's Preferences, change the "Dividend Fundamental Item" to Transfer. This is the special item created by the Transfer fundamental provider (part of MS123 Extra Fundamental/News providers).

Adjust position sizing and Strategy

The idea is to "trade" the dividend-generating symbol Account1 as an external symbol using SetContext() with 1 share. This tells Wealth-Lab to apply the history of transfers to (or from) your account as "big dividends". Failure to buy exactly 1 share would totally distort and inflate backtest results by duplicating (triplicating etc) the numbers. Failure to buy any shares will effectively turn the synthetic "dividends" off. As trading the other, real instruments is usually not performed with 1 share - rather with percent equity - the only position sizing method applicable out of the box is the Position Options PosSizer. It has basic means to assign a percentage of equity to each trade via WealthScript code. You will assign an insignificant percent enough to buy only 1 share of Account1 and use your normal figure for the other trades. At some later time we may be able to come up with a more flexible solution.

  1. Firstly, configure the PosSizer to Use Tag: "% Equity".
  2. Now, let's modify your Strategy code

How to set up the Position Options PosSizer to make the solution work

These "LastPosition.Tag..." lines connect the strategy with the PosSizer, telling it to purchase exactly 1 stock of your Account1 symbols and whatever percentage of equity you like for the other symbols. Play with the .Tag value for Account1 until it buys exactly 1 share.

Every participating Strategy's code would have to be manually tweaked. Run this example code on your real symbol or DataSet (not Account1):

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using WealthLab;
using WealthLab.Indicators;

namespace WealthLab.Strategies { public class MyStrategy : WealthScript { protected override void Execute() { // Buy dummy stock "Account1" for dividends try { SetContext("Account1",true); // Buy the smallest amount possible. Adjust to your equity. // 0.01 means 0.01% equity. You need to make it buy exactly 1 share. if( BuyAtMarket(1) != null ) LastPosition.Tag = 0.01; RestoreContext(); } catch (Exception) { throw; } // Now buy your real B&H symbol e.g. SPY // Sure you can do this many times in the loop, here I just made one sample trade if( BuyAtMarket(1) != null ) LastPosition.Tag = 10; // 10 means you buy for 10% of account equity } } }

Here's the effect on strategy's Equity. You'll also notice a system-wide effect in every other performance visualizer:

Image

Limitations

  • Caution: since Wealth-Lab includes dividends in the trading P&L, it distorts the results.
  • Naturally, systems which make use of real dividends are incompatible with this approach
  • Can not be used in Rule-based strategies

Important Disclaimer: The information provided by Wealth-Lab is strictly for informational purposes and is not to be construed as advice or solicitation to buy or sell any security.  The owner of Wealth-Lab.com assumes no liability resulting from the use of the material contained herein for investment purposes. By using this web site, you agree to the terms of this disclaimer and our Terms of Use.


ScrewTurn Wiki. Some of the icons created by FamFamFam.