Log in to see Cloud of Tags

Wealth-Lab Wiki

Page History: Import real (historical) trades

Compare Page Revisions



« Older Revision - Back to Page History - Current Revision


Page Revision: 2018/06/19 18:10


Syntax


public static void SimTradeFile(this WealthScript ws, string TradeFileName, string DateTimeFormat)
public static void SimTradeFile2(this WealthScript ws, string TradeFileName, string DateTimeFormat)
public static void SimTradeFile_NT(this WealthScript ws, string TradeFileName, string DateTimeFormat)

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

Additional syntax starting from v2013.12:


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

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

Format1 is equivalent to SimTradeFile, Format2 is equivalent to SimTradeFile_2, AccountsFile is equivalent to SimTradeFile_2 reading history of real trades from Accounts tool, NinjaTrader7 is equivalent to SimTradeFile_NT.

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

Original SimTradeFile

SimTradeFile is a helper method that 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 reads a file that contains trade executions (one trade per line) and then forces Wealth-Lab's engine to 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.

Compared to the original V4 version, there are certain enhancements intended for a more flexible import:

  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:

Standard DateTime Format Strings
Custom DateTime Format Strings

You will need a data file that contains your trade executions in a similar format:

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

  • Trade size can be specified in the file, and it is supported using Wealth-Lab version 5.6 and up (optional parameter).
  • Here, the separator char is ';' but it also can be any one of these: tab, comma, dot with comma.
  • 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.
  • The first line of the file can't contain a heading.
  • Empty lines are ignored
  • The Strategy should be executed in single symbol mode backtest (SSB), not MSB.
  • Dividend adjustments must be disabled for providers like Yahoo! Finance.

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. Is your trade trying to enter above the high or below the low of bar?
  2. If your position isn't exited properly, check if you tried to exit below the low or above the high.
  3. Does the bar exist in your datasource? Refresh it if required.
  4. Is the bar range correct? Edit the bar if required.
  5. Is the symbol present in the DataSet?
  6. 'Index out of range' most likely indicates that the symbol requires updating its data.
  7. Are trades being duplicated? Ensure it's not running in a multi-symbol backest (MSB).
  8. Does the chosen iTradeFileImportMode/i mode choose 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.

Robert Sucher version

The library also contains a similar version (a.k.a. SimTradeFile2) created independently by Robert Sucher. Every bit of the description above applies to it, and the only difference is in the file format:

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 the history of real trades from Wealth-Lab's Accounts tool

SimTradeFile2 can also import the history of real (or paper) trades directly from Wealth-Lab's own Accounts tool 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

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.

Example using C# extension methods and modern syntax (v2013.12+):


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() { // Original SimTradeFile:

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

// Robert Sucher version: //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" this.ImportHistoricalTrades(string.Empty, @"dd-MM-yyyy HH:mm:ss", TradeFileImportMode.AccountsFile);

// 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"); } } }

Legacy syntax example:


using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using WealthLab;
using Community.Components; // real trades
/*** Requires installation of Community.Components Extension from www.wealth-lab.com > Extensions ***/

namespace WealthLab.Strategies { public class RealTradesDemo : WealthScript { protected override void Execute() { // Create an instance of the class that has the methods, // passing WealthScript as "this" PositionHelper ph = new PositionHelper( this ); // Original SimTradeFile: ph.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

// Robert Sucher version: //ph.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

// NinjaTrader files //ph.SimTradeFile_NT(@"C:\Data\Trades_NT7.txt", @"M/d/yyyy H:mm:ss tt"); } } }

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.