Backtesting deposits and withdrawals

Modified on 2017/08/30 10:45 by Eugene — Categorized as: Knowledge Base, Providers

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:


There you have it looking something like this:

29.01.1993,1
01.02.1993,1
...
11.01.2016,1

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