Instruments
To accurately backtest an instrument, a little more information is needed than just price. BarSeries
expose an Instrument
property containing key contract specifications used to calculate P&L's. The code snippet below creates an instrument for the 30Yr Treasury Bond future which trades in 32nd's (i.e. a tick size of 0.03125) and has a point value or multiplier of $1000 (i.e. a one point move is worth $1000 per contract).
new Instrument("US", InstrumentType.Future, 1000, 1D / 32);
Required fields
The following fields are required to define a valid instrument for backtesting:
- Symbol
- Instrument type
- Point value
- Tick size
- Currency
Note
If not specified, currency is assumed to be the base currency found in the SettingsManager, which defaults to USD unless overridden.
Instrument file
An instrument file is a simple xml document containing instrument definitions. If a file named "instruments.xml" is found within a data directory, a BarServer
will use it to populate the instrument dictionary and set the BarSeries.Instrument
property automatically when a symbol is loaded. If a symbol is not found within an instrument file, or no instrument file exists, most BarServers will assume the default instrument is a generic equity with a tick size of 0.01 and a point value of 1.
Creating an instrument file
The easiest way to create an instrument file is to instantiate a new InstrumentCollection
, poplulate the instruments of interest, and then call Save()
.
var instruments = new InstrumentCollection();
instruments.Add(new Instrument("US", InstrumentType.Future, 1000, 1D / 32));
instruments.Add(new Instrument
{
Symbol = "RX",
Name = "Bund",
InstrumentType = InstrumentType.Future,
PointValue = 1000,
TickSize = 0.01,
Currency = "EUR",
});
instruments.Save(@"c:\temp\instruments.xml");
The resulting xml file will look like this:
<?xml version="1.0" encoding="utf-8"?>
<Instruments>
<Instrument>
<Symbol>RX</Symbol>
<Name>Bund</Name>
<PointValue>1000</PointValue>
<TickSize>0.01</TickSize>
<Currency>EUR</Currency>
<Exchange></Exchange>
<InstrumentType>Future</InstrumentType>
<InitialMargin>0</InitialMargin>
<MaintenanceMargin>0</MaintenanceMargin>
<LimitMove>0</LimitMove>
<Commission>0</Commission>
<Slippage>0</Slippage>
<DecimalsToDisplay>2</DecimalsToDisplay>
<Sector></Sector>
<Group></Group>
<Months></Months>
<ConversionFactor>1</ConversionFactor>
<AltIdentifier></AltIdentifier>
</Instrument>
<Instrument>
<Symbol>US</Symbol>
<Name></Name>
<PointValue>1000</PointValue>
<TickSize>0.03125</TickSize>
<Currency>USD</Currency>
<Exchange></Exchange>
<InstrumentType>Future</InstrumentType>
<InitialMargin>0</InitialMargin>
<MaintenanceMargin>0</MaintenanceMargin>
<LimitMove>0</LimitMove>
<Commission>0</Commission>
<Slippage>0</Slippage>
<DecimalsToDisplay>5</DecimalsToDisplay>
<Sector></Sector>
<Group></Group>
<Months></Months>
<ConversionFactor>1</ConversionFactor>
<AltIdentifier></AltIdentifier>
</Instrument>
</Instruments>
Optional properties
Although there are only a handful of properties required to specify a valid Instrument
for backtesting, you can see from the output above that quite a few additional fields are listed. If they are available, the backtester will use them to do things like calculate margin requirements, group performance results by sector, etc. Also note the ConversionFactor
property. Some data providers have quoting conventions that don't necessarily match the exchange or perhaps you would simply prefer to see an instrument quoted in a different format. The conversion factor can be used to adjust the price, typically by shifting the decimal place by applying a conversion factor like 100 or 0.01. Most built-in bar servers will automatically apply the conversion factor as a series is loaded.
Warning
For conversion factors other than the default of 1, you will need to adjust the point value and tick size as needed. This is not done automatically.
Global instrument file
If no instrument file is found in the data directory, a BarServer
will check the directory where the program is running to see if it can find a global instrument file. A global instrument file is useful when you use the same symbology or data provider across multiple directories. The backtester includes an instrument file with contract specifications for common CSI symbols that can be used for this purpose.
To add this to your Visual Studio project, right click on the project and select 'Add' and then 'Existing item'. Navigate to where the backtester dlls are stored and select the instruments.xml file.
After instruments.xml has been added to your project, remember to set the 'Copy to Output Directory' property to 'Copy always' or 'Copy if newer' so the backtester can find the instrument file when it runs.
Excel
Large and/or complex instrument files can also be created by saving an Excel spreadsheet as an xml file. You can open the instruments.xml file included with the backtester distribution as a starting point.