Import real (historical) trades

Modified on 2020/05/29 09:16 by Eugene — Categorized as: Community Components

Syntax


public enum TradeFileImportMode { Format1, Format2, NinjaTrader7, AccountsFile };

public static void ImportHistoricalTrades(this WealthScript ws, string TradeFileName, string DateTimeFormat, TradeFileImportMode FileFormat, string AccountName="")

Format1 is "one roundtrip per line" (equivalent to SimTradeFile), Format2 is "one trade per line" (equivalent to SimTradeFile_2), AccountsFile activates import of history of real trades from Accounts tool, NinjaTrader7 is self-descriptive.

Legacy syntax:


public static void SimTradeFile(this WealthScript ws, string TradeFileName, string DateTimeFormat) //one roundtrip per line
public static void SimTradeFile2(this WealthScript ws, string TradeFileName, string DateTimeFormat) //one trade per line
public static void SimTradeFile_NT(this WealthScript ws, string TradeFileName, string DateTimeFormat) //NinjaTrader7 files

public void SimTradeFile(string TradeFileName, string DateTimeFormat) public void SimTradeFile2(string TradeFileName, string DateTimeFormat) public void SimTradeFile_NT(string TradeFileName, string DateTimeFormat)

Parameter Description

TradeFileNameThe full path to the text file containing historical trades
DateTimeFormatOne of standard .NET strings that specifies the DateTime format used in the trade file (see below)

Description

This solution helps you pick up a series of real (historical) trades from a text file on your disk to visualize and analyze them as if they were natively created in a Wealth-Lab Strategy. It supports different data formats: one trade per line, one roundtip per line, importing trade history from Wealth-Lab's Accounts tool, and more.

It reads a file that contains trade executions and makes Wealth-Lab's engine take the same trades - with the help of EnterAtPrice and ExitAtPrice functions. Once you run the Strategy, you will have access to all of the familiar analysis tools in Wealth-Lab.

Features and conventions:

  1. Data in the file can be separated by tabs, commas or dots with commas
  2. You can specify the Date format by passing a specific string e.e. "ddMMyy". It actually can be any date format supported by .NET. For more details, see these documents:
  3. Trade size can be specified in the file (optional parameter). For Wealth-Lab to use the trade size, be sure to select the WealthScript Override (SetShareSize) option in the Position Size control
  4. Here, the separator char is ';' but it also can be any one of these: tab, comma, dot with comma.
  5. Here, the date format is dd-MM-yyyy but as stated above, it can be any supported format. Just make sure to pass a correct DateTime description string the method.
  6. The first line of the file cannot contain a heading.
  7. Empty lines are ignored
  8. The Strategy should be executed in single symbol mode backtest (SSB), not MSB.
  9. Dividend adjustments must be disabled for providers like Yahoo! Finance.

Setup and troubleshooting

It's important that both the trade data file and symbol price data is error free, or else Wealth-Lab will generate an error and may not take all of the trades. There is some basic error checking: entry and exit errors will be output to the debug window. If some of your trades isn't imported, here's a check list:

  1. Have you loaded enough historical data to include each and every trade being imported?
  2. Is your trade trying to enter above the high or below the low of bar?
  3. If your position isn't exited properly, check if you tried to exit below the low or above the high.
  4. Does the bar exist in your datasource? Refresh it if required.
  5. Is the bar range correct? Edit the bar if required.
  6. Is the symbol present in the DataSet?
  7. 'Index out of range' most likely indicates that the symbol requires updating its data.
  8. Are trades being duplicated? Ensure it's not running in a multi-symbol backest (MSB).
  9. Does chosen TradeFileImportMode mode match your trade data?

You can also run the script by opening it and pointing to a symbol. No matter which symbol you point to you will get the same list of trades that is specified in the trade file.

See supported trade history formats and a code example below:

TradeFileImportMode.SimTradeFile

Choose TradeFileImportMode.SimTradeFile when your data file contains trade executions in the "one trade (roundtrip) per line" format like shown below:

Short;QQQQ;25-06-2009;36;25-06-2009;35.5;2000
Long;AAPL;12-05-2009;124;26-05-2009;130.4

TradeFileImportMode.SimTradeFile2

TradeFileImportMode.SimTradeFile2 (a.k.a. Robert Sucher version) supports a different format of trade history data, "one trade per line":

AAPL;12-05-2009;Buy;124
AAPL;26-05-2009;Sell;130.4
QQQQ;25-06-2009;Short;36
QQQQ;25-06-2009;Cover;35.5

As you see, each trade component - long, short, sell or cover - must be placed on its own line and is treated individually. Optionally, trade size is supported (5th column):

QQQQ;25-06-2009;Cover;35.5;100

Import history of real trades from Wealth-Lab's Accounts tool

A history of real (or paper) trades directly from Wealth-Lab's own Accounts tool can also be imported for performance visualization. The history is parsed and saved into a temporary file in Wealth-Lab's Data folder called ExportedTradeHistoryFromAccounts.csv with the following format (sample):

QQQQ,12-01-2010 00:00:00,Buy,45.87
AA,25-11-2014 12:34:56,Buy,17.6

Starting from v2018.07, trade size is exported and optional parameter supports filtering trades by account name (see code below for an example).

Import NinjaTrader trade log files

The SimTradeFile_NT method, added in version 2011.02, allows to import historical trade log files (CSV format) produced by NinjaTrader 7. If the format was not specified when calling the method, it defaults to M/d/yyyy H:mm:ss tt. Here is a fragment illustrating the file format:

Trade-#,Instrument,Account,Strategy,Market pos.,Quantity,Entry price,Exit price,Entry time,Exit time,Entry name,Exit name,Profit,Cum. profit,Commission,MAE,MFE,ETD,Bars,
1,$AUDUSD,Backtest,ACE,Long,30000,0.7611,0.7646,8/1/2005 4:25:00 AM,8/2/2005 2:44:00 AM,Buy,Sell,0.00459860727893831,0.00459860727893835,0,0.00197083169097352,0.00972276967546967,0.00512416239653136,936,
2,$AUDUSD,Backtest,ACE,Long,30000,0.7611,0.7683,8/1/2005 4:25:00 AM,8/3/2005 7:53:00 AM,Buy,Sell,0.00945999211667321,0.014102102184218,0,0.00197083169097352,0.00972276967546967,0.000262777558796451,2131,
3,$AUDUSD,Backtest,ACE,Short,30000,0.7711,0.7612,8/4/2005 7:25:00 PM,8/9/2005 5:50:00 AM,Sell short,Buy to cover,0.0128388017118403,0.0271219579897215,0,0.00557644922837501,0.018155881208663,0.00531707949682271,2546,

Example

Below is a demo Strategy that shows how to use the function using C# extension methods:


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

namespace WealthLab.Strategies { public class RealTradesDemo : WealthScript { protected override void Execute() { // One position (roundtrip) per line:

this.ImportHistoricalTrades(@"C:\Data\Trades.csv", @"dd-MM-yyyy", TradeFileImportMode.Format1); //this.SimTradeFile(@"C:\Data\Trades.csv", @"dd-MM-yyyy"); // Try it with file containing the following lines: //Short,QQQQ,25-06-2009,36,25-06-2009,35.5 //Long,AAPL,12-05-2009,124,26-05-2009,130.4

// One trade per line: //this.ImportHistoricalTrades(@"C:\Data\Trades2.txt", @"dd-MM-yyyy", TradeFileImportMode.Format2); //this.SimTradeFile2(@"C:\Data\Trades2.txt", @"dd-MM-yyyy"); // Try it with file containing the following lines: //AAPL;12-05-2009;Buy;124 //AAPL;26-05-2009;Sell;130.4 //QQQQ;25-06-2009;Short;36 //QQQQ;25-06-2009;Cover;35.5

// Import from Accounts tool // Notes: // 1. Path is not used. Pass a string.Empty or anything // 2. Date/Time format string MUST be "dd-MM-yyyy HH:mm:ss" // 3. Optionally, specify account name e.g. "PaperAccount1" this.ImportHistoricalTrades(string.Empty, @"dd-MM-yyyy HH:mm:ss", TradeFileImportMode.AccountsFile); //this.ImportHistoricalTrades(string.Empty, @"dd-MM-yyyy HH:mm:ss", TradeFileImportMode.AccountsFile, "PaperAccount1");

// NinjaTrader files //this.ImportHistoricalTrades(@"C:\Data\Trades2.txt", @"dd-MM-yyyy", TradeFileImportMode.NinjaTrader7); //this.SimTradeFile_NT(@"C:\Data\Trades_NT7.txt", @"M/d/yyyy H:mm:ss tt"); } } }