Historical Trades: splits, cash flow, multiple files, shared equity by KGo

Historical Trades:
splits, cash flow, multiple files, shared equity
By: KGo (Kevin)
Forum Link Go to the Wealth Lab Forum for discussion, code examples and to download example files.
Document Outline Section 1 - Overview
Section 2 - Quick Start
Section 3 - File Layouts
Section 4 - File Setup Instructions
Section 5 - Background Details
Section 1 - Overview Historical Trades reads multiple trade list and cash flow files of varying format including actual brokerage history into a single Wealth Lab model.
For broker history with actual shares and prices it will automatically split entries to match multiple exits and split exits to match multiple entries. It will create LIFO and/or FIFO trades at actual prices even when prices have split after the sale. If desired, it will use actual commission, fees and all cash flow transactions including actual dividends, interest, deposits, withdrawals and any other category cash flow to create a complete, accurate account model. Multiple accounts can be processed simultaneously to create a consolidated view with all the Wealth-Lab metrics.
Historical Trades creates a model from trade list files based on shares or %Equity. It accepts trades from Wealth Lab strategies, Active Trader Pro history, Fidelity.com history or third party datasets. It is effortless to combine multiple files into a single backtest. Some symbol substitution, trade selection, percent equity adjustment and other changes can be made on the fly without changing the file(s). Trade timing is flexible with same day or next day executions at open, close or price as needed for each trade in each file. Every trade has all data visible and can be reconciled to its input so there is never a question of accuracy. Historical Trades can save a file documenting all transactions and their source setup to precisely reproduce any execution.
Top 10 Highlights 1. Combine multiple intraday and daily strategies into one backtest.
2. Automatically match entries with exits while splitting positions as needed to create final WL positions without manual effort.
3. Apply LIFO or FIFO rules to each trade as needed to match closed lots and open positions.
4. Use your broker history directly to automatically create trades at actual prices without manually revising shares and prices for splits.
5. For multisystem combinations apply percent equity position sizing based on the changing multisystem equity curve. Automatically proportion percent equity based on the number of systems in a multisystem backtest and/or scale sizing as desired.
6. Make changes in trade selection, direction, symbol, timing, priority and % equity on the fly with simple text changes.
7. Create a WL model that matches actual broker account results including account balance, exact equity curve, trades, deposits, withdrawals, dividends etc. with all the Wealth-Lab reporting.
8. Easily apply trade signals, cash flow deposits and withdrawals from various sources into a unified backtest. Transactions from any bank, brokerage or trade signal source may be input.
9. Save the setup, trades and cash transfers from any model in a single file for reference, rerun or inclusion in new multiple system models.
10. Apply priority as needed to control trades taken when capital is limited.
Preparing Wealth-Lab Wealth Lab itself requires limited setup to use all functionality.
1. To use the Cash Flow Dividend transfer capability install MS123 Extra Fundamental/News Providers found at https://www.wealth-lab.com/Extensions/Details/34 . Transfers require Position Options>>Tag>>%Equity PosSizer or WealthScript Override (SetShareSize) and dividends must be set to Transfer. No other provider setup is required.
2. Review settings for dividends, commission, price rounding, slippage, round lots, and turn off limit/stop order rounding entirely. Call CheckHistoricalTradesPreferences() anytime to print cautions if preferences are not optimal for matching Fidelity account balances. Preference settings are optional but suggested for best historical trade accuracy.
3. Check Microsoft.VisualBasic in .Net References (click References on Editor tab toolbar)
4.Set Data Panel: In Data Panel (Ctrl+D) select WealthScript Override (SetShareSize) or PosSizer Position Options>>Tag>>%Equity.
General Process To run the system:
1. Store Trade Data under Column Headers: Supply trade and cash flow data in a delimited values format (comma, tab and semicolon delimiter) using system recognized column headers to specify which data is is which column. Unrecognized headers are ignored. Wealth Lab, Active Trader Pro, Fidelity.com and some other headers are recognized. See full list below if needed.
2. File Sort: Sort the file data by date with oldest date first. The entry must be before or on the same line as the corresponding exit. Cash Flow transfers/dividends do not require sorting.
3. Setup Instructions: Supply setup instructions to specify how to process the file data. The text setup instructions for each file are coded in the MultiFile() statement and/or stored near the top of the file. Create one MultiFile() statement for each file and make one call to HistoricalTrades to execute trades from all files.
4. Flexible Recomendation: Keep the MultiFile() setup instruction for many files available. Then simply comment and uncomment the lines for the desired combination to run as a single system. This is especially powerful with automatic %Equity resizing based on the number of active files. So when adding a fourth system there is typically no need to adjust %Equity for four systems as that is done automatically.
5. Verify the trades. Trades can be verified by reviewing the Debug, WL output trade list and/or saved trades list. See the end of this guide for trade details coded into the output trade list entry and exit names. All trade data can be seen in the file created by HistoricalTrades() save file name option.
Note: All symbols are syncronized to the clicked symbol. Click any high volume symbol like SPY that is verified to have data on every bar. Run this system on one symbol not over the entire data set.
Section 2 - Quick Start Each execution requires one call to HistoricalTrades() and each file requires one call to MultiFile(). The FileSetupString within the MultiFile() statement contains ~Keyword:Modifier instructions to process the file. Keywords are preceded by ~ and followed by Modifiers. Modifiers are separated by : (colon). Some startup examples are given here.
Coding There are 4 statements available
MultiFile() MultiFile( FileDirectoryString, FileNameString, FileSetupString, ref mfn, ref mfs ) ;
FileDirectoryString Full path to the directory for FileNameString.
FileNameString The name of the file in the given file directory. A full path given will be used. A relative path given will be appended to the FileDirectoryString.
FileSetupString See examples and Section 4 - Setup Instructions below.
mfn Initialize as empty string before the first MultiFile() statement.
mfs Initialize as empty string before the first MultiFile() statement.
HistoricalTrades() HistoricalTrades( mfn, mfs, saveFileName, skipThem, addExistingTransfers );
mfn Initialize as empty string before the first MultiFile() statement.
mfs Initialize as empty string before the first MultiFile() statement.
saveFileName (string) This optional file name string may include optional keyword suffixes. The ~Signal keyword produces signal dates instead of trade dates and the ~CloseOut keyword exits all open positions at the open after the last bar. The format is "FileName.csv ~Signal ~CloseOut " or just "FileName.csv" to produce trade dates without close out. Keywords are not case sensitive. They must be after the file name in any order. They are trimmed of blank spaces and mis-spellings are ignored. EnterAt, ExitAt, Entry Date and Exit Date are adjusted for signal dates.
If the file name is missing there is no saved file. The file trades and cash flow will be saved in the first file directory used. A full path given will be used. A relative path given will be appended to the first file directory used.
MultiFile() statements, run date, date range and position sizing method notes are stored in the saved file to reproduce the analysis from source files. To copy the MultiFile() statements open the file in a text editor like Notepad. This facilitates reproducing the analysis at a later date when more data is available.
In addition to storing trades from any set of files, this method converts a Single Position Layout and/or Single Action Layout to a Dual Action Layout. Dual Action Layout is most secure since it is less dependent on accurate split data history.
Shares will always be equal to 1 if trading by %Equity but the %Equity value will be given. Commission from the file setup is always output. WL Preferences commission is never output.
For all symbols including _SB and _SBx suffix, the x is saved for reference in the Reverse Adjustment Factor (RAF) column.
skipThem (string) An optional comma separated list of symbols to skip in all input files Typically this is not needed.
addExistingTransfers (bool) This optional boolean is set to false if not given and the existing AccountTransfers.txt file is discarded as not used. Set to true to preserve the transfers from the prior execution of HistoricalTrades() and add them to the current execution. True is used for the second or more calls to Historical Trades in the same execution so generally it is not needed.
CheckHistoricalTradesPreferences(); This optional statement checks preferences and prints debug messages if they are not optimal to model actual price paid trade data. It has no direct effect on HistoricalTrades execution. Many models (ie. using WL commissions) do not require optimal preferences.
PrintSinglePositioMode() PrintSinglePositionMode( bSignalDates, sPrint ) ;
bSignalDates Optional value set to false if not given. False prints trade dates and the trade alert appears on the first future bar. True prints trade signal dates and the trade alert appears on the last bar.
bCloseOutCash Optional value set to false if not given. False does not print close out to cash. True prints the date after the alert date with a cash signal to prevent an open position from persisting into an untested future. The close out symbol is printed as "CashOut" to be clear this is close out not an alert. Historical Trades treats CashOut as Cash.
sPrintStyle Optional string will print all bar dates unless "min" is given which will print just the signal change dates.
Notes: This optional statement is used to convert positions to SinglePositionMode Layout. The first bar, last bar and alert are always printed. Invalid Signal warnings are given if positions overlap or if positions are bought and sold on the same bar.
Other Use: This method is standalone in that it can be used with any WL single position strategy.
Example File type FileSetupString
WL Trades List (Trading shares) The WL trades list is a Dual Action Layout meaning the entry and exit are on the same line. Use " ~EnterAt:Price ~ExitAt:Price " ( Note: Price is Same Day Price(SDP) - use Open Close or Price as needed). Copy and paste the entire WL trade list to the file. No editing is required.
WL Trades List (Trading %Equity) " ~EnterAt:Price ~ExitAt:Price ~%Equity:100 " Use any number. %Equity could vary by trade if added in a new column in the file but it must be given somewhere to trade %Equity with the Position Options PosSizer. Simple systems can use other data panel sizing options.
Active Trader Pro History (Shares) and Fidelity.com history (Shares) Fidelity.com and ATP history are a Single Action Layout, meaning an entry is on one line and the exit is on another. Each cash transfer/dividend is on one line. Sort it oldest to newest with entries before exits then save. Be sure to save full accuracy prices. Use " ~EnterAt:SDP ~ExitAt:SDP ~Print:All " (The minimum needed without cash transfers)
Or as recommended for cash flows and more visibility use " ~EnterAt:SDP ~ExitAt:SDP ~Print:All ~ShowPositionLine:On ~LIFO:On ~Transfers:on ~TransPhrases:distr:tax paid:royalty:in leiu:fee charged:cap gain:return of capital:check:debit:deposit:charged " ( Note: All those added are optional but recommended to inspect position matching and do dividends and cash transfers. Change LIFO to FIFO if that's how the account is set up. )
Other Files The system accepts both signal (NDO, NDC, NDP) and trade (SDO, SDC, SDP) dates so always tell it which to use with the ~EnterAt: and ~ExitAt: keywords. Use WL trade list or Historical Trades saved file column title names to title any trade list.
More Column Headers The full set of available column headers is in Section 2.
More Setup ~Keywords:Modifiers The full set of recognized keywords and modifiers is in Section 3.
Reserved Characters The characters , ; and tab ('\t') are reserved for delimiters between fields of the trade list input file.
The characters :~ are reserved for keyword and modifier separators
The character | is reserved for system use and may not appear in the setup instructions.
End Quick Start
Section 3 - File Layouts
Valid Column Headers Requirement: The column header row must be the first row with data in column B ( 2nd delimited token) and it must contain the words date and symbol. Prior rows may contain comments, be empty or contain default setup instructions.
Use the column names given below to define data in the column
If multiple column titles exist for a data element (ie. transfer and dividend the right most column will be the data source.
Column Headers [Value format] and "Alternates" available - Any alternate here may be used. They are not case insensitive.
%Equity [Any number] "%equity" "% equity " "percent equity" "pct equity" "pctequity" "pcteq" "pct eq" "percent" "%"
Action [Long, Short, Buy, Sell, Cover, Transfer, Dividend, You Bought, You Sold, Skipit, a short, b cover, you sold short sale, you bought short cover, and user defined transfer actions] "action" "position" "description" "transaction description" Note: replace short and cover actions with "a short" and "b cover" to facilitate alpha sorting of intraday shorts to get entries listed before exits.
Commission [Any number to max 2 decimal places or empty] "commission" "commission ($)"
Date [Date in m/d/yyyy or m/d/yyy hh:mm or m/d/yyy hh:mm AM/PM in US or use your local computer format] "date" "entrydate" "entry date" "trans. date" "enterdate" "enter date" "signal date" "action date" "actiondate" "run date" "trade date" "tradedate" A valid form of this date title is required to process each file.
Entry At [SDO, SDC, SDP, NDO, NDC, NDP, OPEN, CLOSE, PRICE, or empty] "entryat" "entry at" "enterat" "enter at"
Entry Name [Any text except , : ;~ | and tab reserved characters] "entryname" "entry name" "entername" "enter name" "entrysignal"
Price [Any number] "price" "entryprice" "entry price" "enterprice" "enter price" "action price" "actionprice" "price ($)"
EntryCF [Any number to max 2 decimal places or empty] "entrycommission" "entrycf"
Exit At [SDO, SDC, SDP, NDO, NDC, NDP, OPEN, CLOSE, PRICE, or empty] "exittime" "exit time" "exitat" "exit at"
Exit Date [Date in m/d/yyyy or m/d/yyy hh:mm format] "exitdate" "exit date"
Exit Name [Any text except , : ;~ | and tab reserved characters] "exitname" "exit name" "exitsignal"
Exit Price [Any number] "exitprice" "exit price"
ExitCF [Any number to max 2 decimal places or empty] "exitcommission" "exitcf"
Fee [Any number to max 2 decimal places or empty] "fees" "fee" "fee ($)"
fifo/lifo [LIFO, FIFO, L, F] "fifo" "f/lifo" "fifolifo" "fifo lifo" "lifo fifo" "fifo/lifo" "lifo/fifo"
Priority [Any number] "priority"
Shares [Any number- Sign +/- is ignored] "quantity" "shares"
Symbol [Any text except , : ;~ | and tab reserved characters] "symbol" "[Any Title]" Note: When a symbol column number is not given the system will use the rightmost column titled simply 'symbol'. If that is not found it will use the rightmost column with 'symbol' in the column header. The word symbol must be in at least one column header.
Transfer [Any number to max 2 decimal places or empty] "transfer" "amount" "cashflow" "dividend" "cash" "profit $" "amount ($)"
File Data layouts Four data layouts are processed and 3 of the 4 may be combined in a single file
1. Single Action Layout - A single entry or exit or cash flow transfer is on the data line. Requires a minimum of 3 columns, date, action and symbol. Amount column is needed for transfers. Each row contains entry or exit or transfer instruction. Price, commission and other columns are optional. Use a single date column titled "Date" and a single price column titled "Price" for both entry and exit. Use this layout to automatically match entries and exits for historical trades when shares are given. This is the csv export layout from Fidelity.com and Fidelity Active Trader Pro. Both csv files may be processed without changes. The trade data must be sorted by date oldest to newest trade top to bottom which can be done on the site or in ATP before saving. The sort order is used to fulfill LIFO/FIFO exit instructions. Transfer data may be unsorted and may occur anywhere below the column header line.
2. Dual Action Layout - Both entry and exit for one position are on the line. Requires a minimum of 4 columns, action, symbol, entry date and exit date. Each row contains both entry and exit instruction for one position. This is the trade list layout from Wealth-Lab trades tab. WL trade list may be input without changes. Sorting is optional.
3. Single Position Layout - Only one position is held at a time. Although Single Action and Dual Action layouts can do a single position system, this Single Position Layout gives an exit by changing symbols. CASH symbol is used to exit to cash otherwise any new symbol will cause an exit and a new entry. It requires a minimum of 2 columns, date and symbol. All positions are assumed long unless otherwise specified. Only changes in position are required but the same signal may be repeated each day while a position is open. To trade single position both long and short give date, symbol and action where cash symbol has Long, Short or Cash action. ~SinglePositionMode:On must be specified to enable single position layouts. SinglePositionMode is fully compatible with keyword based ~Action and ~Symbol substitution. A Single Position Layout file may contain many independent signals in different symbol columns which all reference the same date column. See ~SinglePositionMode keyword below.
4. Cash Flow Layout - Cash Flow is accepted four ways. 1. Cash flows may be input as date, transfer action, amount in the Single Action Layout. 2. Individual Cash Flow, dividends, deposits and withdrawals may be added to the trades file using the AccountTransfer.txt data format. These comma separated values must be all in column one or separated into the first 3 columns. They do not need to line up with other column titles but must begin with CASH-FLOW in column one. 4. Multiple comma delimited files using the AccountTransfer.txt data format may be input via the ~Transfers:on:FileName.txt:FileName2.csv keyword setup. These transfer files should be in the same directory as the trades file. The entries may be noted with any symbol including CASH-FLOW, and all entries are accumulated to cash flow.
5. Combining Layouts: A) Single Action, Dual Action and Cash Flow layouts may be used as needed in the same file. Use the Single Action column "Date" to hold the Dual Action EntryDate and the Single Action column "Price" to hold the Dual Action EntryPrice. Leaving Dual Action exit information empty creates a Single Action layout line. B) Cash Flow Layout may be combined with Single Position Layout. Any layout with Cash Flow must have an Amount (or equivalent) column header.
Trade File Comments Comment lines begin with // characters and are ignored.
Comments may be added anywhere above the column header beginning in the 3rd value without using // to start the line.
Section 4 - File Setup Instructions The setup instructions supplement file data so changes can be made without changing the file. They are given before the trades header and/or in the MultiFile() statement code. Keywords in the code will override keywords in the file default statement.
Coded Setup Instruction Each file requires one call to MultiFile() in the following format where mfn and mfs are strings.
MultiFile( FileDirectoryString, FileNameString, FileSetupString, ref mfn, ref mfs ) ;
The coded MultiFile() 'FileSetupString' contains Keywords and Modifiers as detailed below. These are adjustable settings for the file. It may be an empty string if all the trades setup instruction was completed in the file. A coded Keyword will override or supplement the instruction in the file.
In File Setup Instruction An optional 'FileSetupString' is placed anywhere above the column headers in the file. Typically this will execute the system with recommended settings which may be modified by the coded setup.
FileSetupString Keywords are preceded by the ~ character. Keyword modifiers follow a keyword and are preceded by a : character. Keywords and Modifiers are not case sensitive. Keywords without modifiers are ignored. Keywords and modifiers are summarized below. Most keywords are the same as column headers but there are some general instruction keywords like "~Print".
Keywords - with Header Name [Modifiers] and "Alternates" - Any alternate may be used. They are case insensitive. Use in coded setup instruction or in file setup Instruction.
~%Equity [Any number] "%equity" "pctequity"
~Action [LS, LL, SS, SL, XL, XS, LX, SX, XX] "action" "position"
~Commission [Any $ Amount] "commission"
~EntryAt [Open, Close, Price, SDO, SDC, SDP, NDO, NDC, NDP] "entryat" "entry at" "enterat" "enter at"
~EntryCF [Any $ Amount] "entrycommission" "entrycf"
~EntryName [Any Text] "entryname" "entry name" "entername" "enter name"
~ExitAt [Open, Close, Price, SDO, SDC, SDP, NDO, NDC, NDP] "exitat" "exit at" "exittime" "exit time"
~ExitCF [Any $ Amount] "exitcommission" "exitcf"
~ExitName [Any Text] "exitname" "exit name"
~Fees [Any $ Amount] "fees" "fee"
~Fifo [FIFO, LIFO, empty] "fifo" "lifo"
~Priority [AnyNumber, AnyNumberF, AnyNumberX, Empty] "priority"
~Symbol [Any Text] "symbol"
~Shares [] "Shares"
~Transfers [On, off, empty, TransferFileNames] "transfers" "dividends"
Keywords - For General Instruction
~File [Quotes, NoQuotes] "file"
~Default [] "Default"
~Exclude [Any Text] "exclude"
Ignored Keywords [Any] "default" "noinclude" "noexclude" "nosymbol" "shares" "noshares"
~Include [Any Text] "include"
~Print [On, Off] "print"
~ShowPositionLine [On, Off] "showpositionline" "showpline" "showpl"
~SinglePositionMode [On, Off, On:ColumnNumber] "singlepositionmode"
~SystemCount [Any Number] "syscnt" "systemcount"
~TransPhrases [Any Text] "transphrases"
Keyword Modifier Values Format Modifier values like column values listed above. For example, any text may not include reserved characters of , : ;~ | and tab. Commission is max 2 decimal places.
More KeyWord / Modifier Detail Description -- Modifier options for each Keyword are given below.
Header Reference Keyword Keywords in yellow : Modifiers in green
80x The x means multiply the %Equity in the file by this percentage.
100 Just a number means make this the %Equity for all trades in this file. And adjust this value by percent system count if there are multiple files.
25F The F means make this the final %Equity for all trades in this file. Do not adjust this percentage for the number of systems in the file or number total systems combined.
~Action Give one 2 letter instruction. First letter applies to longs in the trades list and second letter applies to shorts. Letter X means skip the trade.
LS Trade longs in the file long and shorts in the file short. If ~Action is not specified LS is assumed for Single Action and Dual Action Layouts.
LL Make all trades long
SS Make all trades short
LS Trade longs in the file long and shorts in the file short
XL Skip all long and trade shorts in trades list long
XS Skip all longs in and trade shorts short
LX Trade longs in trades list long and skip shorts. If ~Action is not specified LX is assumed for Single Position Layouts.
SX Trade longs in trades list short and skip shorts.
XX Skip all trades
Any $ amount Keyword ~Commission is applied to both entry and exits. Commission given in column is applied to both entry and exit on that line. Both Keyword and in Column commission are overridden by EntryCF and/or ExitCF.
~EntryAt: and ~ExitAt: Specify when to trade the date. These default to SDO when unspecified.
Open Same as SDO. Make all entry/exit at the open.
Close Same as SDC. Make all entry/exit at the close.
Price Same as SDP. Make all entry/exit at price when price is given. If price is zero or not given trade will be at SDO
SDO Same Day Open
SDC Same Day Close
SDP Same Day Price. Make all entry/exit at price when price is given. If price is zero or not given trade will be at SDO
NDO Next Day Open
NDC Next Day Close
NDP Next Day Price. Make all entry/exit at price when price is given. If price is zero or not given trade will be at NDO
  Note: An entry at Open or Close may not be combined with an exit at price on a split series.
~EntryCF EntryCF overrides entry Commission and Fee.
Any $ amount Total commission and fee to enter the trade.
This cost is output in the signal name and can be re-traded when separated into this column
EntryCF greater than zero in file column will supersede Commission and Fees. Zero or empty EntryCF file field will not override Commission and Fees.
Any ~EntryCF value including zero in a setup statement will override all other input.
~ExitCF ExitCF overrides exit Commission and Fee.
Any $ amount Total commission and fee to exit the trade.
This cost is output in the signal name and can be re-traded when separated into this column
ExitCF greater than zero in file column will supersede Commission and Fees. Zero or empty ExitCF file field will not override Commission and Fees.
Any ~ExitCF value including zero in a setup statement will override all other input.
~EntryName: and ~ExitName
anyText Will be printed in the WLP trade list. EntryName is used to include and exclude trades. These are case sensitive and will be in the trade list as entered. Include and exclude search in the EntryName text and are case sensitive too.
Any $ amount Fees after ~Fee are only applied to sell and short as is market custom. Fees given in column are applied to both entry and exit on that line. Both Keyword and in Column fee are overridden by EntryCF and/or ExitCF.
~FIFO or ~LIFO LIFO is used if neither is given. Use one or the other not both. They are active when no Lifo/Fifo file column value is given. They do not override the file Lifo/Fifo column values.
On On will turn on the Keyword as the default.
Off Off will turn off both LIFO and FIFO keyword overrides. So ~FIFO:off also turns off LIFO. The file Lifo/Fifo column values are never turned off.
empty Keywords with an empty modifier are ignored.
~Priority Note: the priority is set to a random number between zero and one if no priority instruction is given and the file column is empty.
3x An "x" after a number will multiply the priority in the file by this number
3.0+ A "+" after a number will add this number to the priority in the file
2.5 Set the priority to 2.5 for all trades in this file
Sub Optional keyword to enable symbol substitutions.
NOSub Optional keyword to disable symbol substitutions. No need to erase the substitutions that may be there.
QQQ=QLD Make QQQ in the trades list be QLD. Use multiple pairs as needed. Separate pairs by a colon
NOTE: Adding _SKIPIT or simply _S suffix to a symbol in the file will skip line.
~Shares A reminder to set share size in the data panel. Shares usage is controlled by the data panel setting. Select WealthScript Override (SetShareSize).
~NOShares A reminder that shares are not to be used.
~Transfers Required keyword to enable cash transfers into and out of the simulation. Transfers appear as dividends. Transfers requires Position Options>>%Equity PosSizer or WealthScript Override (SetShareSize). Transfers execute on the date given. Enter At setting is not applied.
On Use on to enable Transfers. Transfer must be selected and enabled under Tools>Preferences>Dividend Item Selected
Off Use off to disable all Transfers
FileName(s) FileNames are colon (:) separated following the on/off parameter. Use comma separated values in a txt or csv file containing CASH-FLOW,date,value. This is the same file format used for the AccountTransfer.txt file for the MS123 Extra Fundamental/News Providers extension. See the Backtesting deposits and withdrawals item for that provider. No other setup is required.
Tip: Create csv or text files from the cash flow of any brokerage, bank, savings or checking account by running the transactions through HistoricalTrades. Then optionally add those files names to any coded setup instruction to get a consolidated equity curve.
General Instruction Keywords
Quotes Optional use. The file may have some fields enclosed in quotes. This works on both quoted and non-quoted and is the system default when not specified. History saved directly from Fidelity Active Trader Pro has quoted fields.
NoQuotes Optional use when there are no quoted fields in the file.
DEFAULT Required as the first keyword in a trades file setup instruction. It is optional in a Coded Setup Instruction.
The entire in file setup instruction must be in column A. That's the first delimited token of any line before the column header.
~NoExclude Ignore this keyword and modifiers. No need to delete the excludes that may be there.
~Exclude Exclude trades with the following text in the entry name.
anyText Exclude trades with this text in the entry name. Leading and trailing spaces are ignored. Exclude TEXT IS CASE SENSITIVE. Exclude Modifiers override Include Modifiers. So ~Exclude:AbC has priority over Include:All. In that case, all except AbC will be included.
None Exclude no trades
~NoInclude Ignore this keyword and modifiers. No need to delete the includes that may be there.
~Include: Only include trades with the following text in the entry name.
anyText Only include trades with this text in the entry name. Leading and trailing spaces are ignored. Include TEXT IS CASE SENSITIVE.
All Include all trades
~Print Optional keyword. The header, all trades, cautions and error messages may be printed to the debug window.
Min or Errors Minimize print. Prints only cautions and error messages. This is default behavior and limits print to debug window.
All Prints all trades and messages. All is required to override Min/Errors as the default file setup instruction. ~Print:All is required in the last file processed to print the contents of the AccountTransfers.txt file.
~ShowPositionLine: or ~ShowPLine or ShowPL Required keyword to put position number and input line reference in the entry signal. Line reference will be put in the exit signal.
On Use on to enable reference in Entry and Exit names in WL trade list
Off Turn off disable. Off is assumed if ShowPLine keyword is not used.
NOTE: Use this to see the source of any position entry and exit. It is especially helpful for system LIFO/FIFO created split positions or any position created using Single Action Layout.
~SinglePositionMode Required keyword to enable single position mode. Without it explicit exits must be input.
On Use on to enable single position mode which will enable "CASH" symbol to exit to cash. "On" enables the first date with new symbol to create an exit for the last open position.
Off Turn off Single Position System Mode.
Column number Use any integer number as in ~SinglePositionMode:On:15 to change the source symbol column to column 15. The first column is column zero. Omit the column number modifier to use the default column with title containing "Symbol". To assign the symbol column number SinglePositionMode must be on. The column number will print with the column title which may be any identifying text. Typically multiple columns are used to keep many long only systems in one file.
NOTE: The "CASH" symbol as an exit to cash instruction is only available in Single Position Mode.
~SystemCount: or ~SysCnt: Record the number of systems in the file. A Base %Equity is printed for each file equal to (File System count / Total System count) *100. This is the portion of total equity allocated to this file. The final equity per trade is then the file trade equity * Base %Equity. This means that a file %Equity is only adjusted for the number of systems in other files. On average a file with 2 systems in it would have a trade size of 50% equity assigned by column or keyword.
Float or integer > 0 Put the number of systems in this file. Multiple systems may be included in one file. Default is 1 if not given.
NOTE: If SystemCount >1 it MUST be in the coded setup statement. The DEFAULT statement atop the file will not apply a SystemCount > 1.
~TransPhrases and ~NoTransPhrases Required keyword to add phrases to select transfer actions. NoTransPhrases stops the attached phrases from being used but does not remove any phrase already set up. The words "dividend" and "transfer" are already set up.
any text Colon separated text which if found in the action description will designate the the input line to be a transfer amount. Text may not include reserved characters of , : ;~ | and tab and text is trimmed so leading and trailing spaces are deleted.
Tip: For Fidelity history try these phrases to see cash flow transactions ~TransPhrases:distr:tax paid:royalty:in leiu:fee charged:cap gain:return of capital:check:debit:deposit:charged: and more depending on the type of account and transaction.
Tip: For Fidelity do not use "reinvestment" or "purchase into" or "redemption from" as these core account activities are not cash additions or subtractions.
Section 5 - Background Details
Combining Market and Price Orders One price for entry or exit may paired with a market order. To combine market and price orders on one trade the system allows these situations:
a. Use Single or Dual Action Layout for a price on the stored series or an entry price on the _SB series.
b. Use only a Dual Action Layout if the exit price is on the _SB series.
Note: One price determines the price series to use. As with trades on the stored series there are no share or price adjustments made for price splits during the trade.
Price Series Splits WLP will use Fidelity split data and WLD will use Yahoo! split data. The system will model historical trades with actual shares and actual prices. It always fits trade prices onto a price series scaled from available data series. Price series splits are only used when trading at price. The system allows these situations.
a. Use any trade layout if both prices fall on the stored price series or on the unadjusted price series (suffix _SB) which is based on stored fundamental split data.
b. Use a Dual Action Layout if both prices can be made to fall on an estimated price series using a custom split reverse factor of x ( _SBx where x is a double as in _SB1.15 ). An _SBx series selection requires entry and exit dates to be on the chart.
d. Use a Dual Action Layout if there was a price split during the trade and the entry is on the _SB series. Use actual trade prices. A price entry on _SB series and a market (at open or close) exit are a valid trade. However a market entry with a price exit is on an _SB series is not valid. An _SBx suffix will be added and the exit price will be automatically adjusted accordingly. This models shares distributed from the split within the original trade so distributed shares can be ignored via _SKIPIT. If the entry is not on the _SB series the Dual Action Layout will fail and the exit price must be manually adjusted so a custom _SBx will succeed.
e. Trade Failures: If A Dual Action Layout cannot find an _SBx series to match prices an error is printed and a manual price investigation is needed. The stored prices may have been adjusted for a mid-trade split or other corporate action.
Tip: See sample data 'Combo Layout Test Data Finish 02.csv'. When shares of an owned position are split: The Fidelity file will contain a distribution of shares for the split and the system will print a caution to create the _SBx Dual Action Layout for the trade. When the Dual Action Layout successfully models the split add _SKIPIT or simply _S as the distribution symbol suffix in the file to skip the shares distribution line and suppress the distribution Caution message. Likewise add _SKIPIT to the exit symbol when date price and commission/fees are moved to create a Dual Action Layout Trade.
Tip: See sample data 'Combo Layout Test Data Finish 04.csv'. When creating a Dual Layout from Fidelity csv, create ExitDate, ExitPrice and ExitCF column headers with no data. Then, create 3 more columns with the same data via formula to populate them with date, price and total of Commission and fee. Simply copy the formula data for any exit to the 3 empty exit columns to create a dual action trade. Then mark the exit symbol as _S to skip it.
Position Splits Define: Position Splits occur when exit shares do not exactly match the LIFO/FIFO entry position shares. Exit and/or entry are split into multiple positions to fill the exit. Position splits are created to match shares. There are no position splits when using percent equity position sizing.
Position splits require shares in the file and WealthScript Override (SetShareSize) in Data Pane.
Position splits are automatically managed for the Single Action Layout only. If a user scales in and out of positions, entries and exits will be managed according to the LIFO FIFO rules given. A 2000 share buy order may have been filled at 2 or more different prices. An entry for each price is accepted. Likewise a exit order for 2000 shares may have been filled at 2 or more prices and an exit for each fill is accepted. Scale in and scale out strategies are fully supported. Position splits are managed according to the FIFO/LIFO rule selected. This process follows the Fidelity practice exactly and allows using the Fidelity csv export directly as input to Historical Trades. Cost and tax basis disposition methods are not supported. Overall account results are the same regardless of selected disposition method.
When using WealthScript Override (SetShareSize): Percent equity when given will be split and recorded in the entry signal. This allows translating shares to percent equity for subsequent percent equity based backtesting.
Commission, Fees, EntryCF, ExitCF and %Equity are split along with the shares.
Shares To use shares select WealthScript Override (SetShareSize) in Data Pane.
Percent Equity Sizing Fixed Percent of Equity sizing in the Data Pane may be used to override the file shares and %Equity. Transfers however will not work with DataPane % Equity.
Use PosSizer Position Options configured to Use Tag with % Equity to vary the %Equity as set in the file and/or %Equity Keyword. Transfers work with this PoSizer or SetShareSize..
Intraday and/or Daily Daily and intraday files may be combined into a single daily bar backtest with intraday systems trading at price or at the daily open or close.
Files with daily DateTime are supported for daily datasets.
Caution: Files with daily DateTime run on an intraday chart will only attempt the trade on the opening bar of the day. So, an SDC trade will execute at the close of the first intraday bar.
Files with intraday DateTime trading at price are supported for both intraday and daily datasets.
Cash transfers always use a daily date even for intraday backtests. Intraday scale transfers are always applied on the first bar of the day.
Symbol_SB and _SBx The system adds symbol suffix _SB and _SBx when needed. Any symbol with suffix added may used in a new file. The suffix will be ignored and recreated if needed. The suffix is absolutely required in a saved trades file only when there was an in trade price series split and the file now contains the adjusted not actual price. The save trades function always saves the _SBx symbol for this reason.
Output Tradeslist Entry Name The output entry name provides quick reference to trade sizing and costs. It can be separated into fields and used to reproduce the trades using %Equity, EntryAt, EntryCF columns. It enables translating Single Action Layouts to Dual Action Layouts. The Dual Action Layout is more secure because it is not dependent on fundamental split data which may go bad over time. In addition, Single Action Shares can be translated to a Dual Action %Equity.
Example Price:23.333:Eq:7.95:RT2_SigA:P360 L213
Price EntryAt - It will always be Open, Close or Price which mean SDO, SDC or SDP on the entry date in the trade list.
23.333 %Equity - This is the final %Equity after position splits. The system tracks this even when positions are sized from shares.
Eq Sizing Method(Shrs or %Equity) - Indicates if the position was sized from shares or %Equity
7.95 EntryCF - The total commission and fee (CF) applied to the entry. This is the final CF after position splits.
RT2_SigA This is 'Setup Instruction EntryName _ File Column Entry Name' - Typically the Setup instruction portion references the system name and the file portion references the entry signal name. The system separates them with an underscore. Any part of this can be used to include or exclude trades.
P360 L213 This is the WL position number followed by file line number. Both parts of a split position retain the original entry position and line number so it is easy to identify all positions created from the same entry. It ties the output trade list to the input file so any entry can be tracked to its source.
Output Tradeslist Exit Name The output exit name can be separated into fields to check or reproduce the trades using ExitAt, ExitCF columns.
Example Price:8.41:LIFO:RT2_ExitA:L213
Price ExitAt - It will always Open, Close or Price which mean SDO, SDC or SDP on the exit date in the trade list.
8.41 ExitCF - The total commission and fee (CF) applied to the exit. This is the final CF after position splits.
LIFO The method used to match exit to entry. This represents how the trade was created and is not needed to retrade the trade list as all Dual Action Trades are automatically set to LIFO.
RT2_ExitA This is 'Setup Instruction ExitName _ File Column Exit Name' - Typically the Setup instruction portion can reference the system name and the file portion can reference the exit signal name. The system separates them with an underscore.
L213 The file line number. It ties the output trade list to the input file so any exit can be tracked to its source.
Reserve %Equity of Cash Cash can be reserved by using less than 100% equity from the included csv files. Or create a system with no trades using ~Include:DoesNotExist and ~SystemCount:x.xx where x.xx reserves as many systems worth of cash as desired. The cash reserve will be printed as the Base %Equity for the file with no trades.
Disclaimer: Historical Trades is supplied as is with all its faults and may not be suitable for any particular purpose. Independently verify any results.
Author: KGo
Category: Utilities
Creation Date: 12/19/2017
Licence: Freeware
Availability: Globally
Instructions for Script Download
  1. In Wealth-Lab client software, open the Strategy Explorer (Ctrl+O)
  2. Click the "Download..." button
  3. Click "Begin Download"