Show / Hide Table of Contents

Class SimulatorCore

Simulator engine core, managing data sources and instruments, processing a sequence of bars, simulating trades, keeping track of positions, and maintaining log information.

Inheritance
object
SimulatorCore
Algorithm
Inherited Members
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
object.ReferenceEquals(object, object)
object.ToString()
Namespace: TuringTrader.Simulator
Assembly: TuringTrader.Simulator.dll
Syntax
public abstract class SimulatorCore

Constructors

| Edit this page View Source

SimulatorCore()

Initialize simulator engine. Only very little is happening here, most of the engine initialization is performed in SimTimes, to allow multiple runs of the same algorithm instance.

Declaration
protected SimulatorCore()

Fields

| Edit this page View Source

CommissionPerShare

Commision to be paid per share. The default value is zero, equivalent to no commissions. Algorithms should set this to match the commissions paid on high account values/ large numbers of shares traded.

Declaration
protected double CommissionPerShare
Field Value
Type Description
double
| Edit this page View Source

EndTime

Time stamp, representing the last bar processed by the simulator. For simulations reaching into live trading, this should be set to a future time.

Declaration
protected DateTime EndTime
Field Value
Type Description
DateTime
| Edit this page View Source

IsLastBar

Flag, indicating the last bar processed by the simulator. Algorithms may use this to implement special handling of this last bar, e.g. setting up live trades.

Declaration
public bool IsLastBar
Field Value
Type Description
bool
| Edit this page View Source

Log

Simulator's order log.

Declaration
public List<LogEntry> Log
Field Value
Type Description
List<LogEntry>
| Edit this page View Source

NetAssetValue

Total net liquidation value of all positions plus cash.

Declaration
public TimeSeries<double> NetAssetValue
Field Value
Type Description
TimeSeries<double>
| Edit this page View Source

NetAssetValueHighestHigh

Highest high of net asset value.

Declaration
public double NetAssetValueHighestHigh
Field Value
Type Description
double
| Edit this page View Source

NetAssetValueMaxDrawdown

Maximum drawdown of net asset value, expressed as a fractional value between 0 and 1.

Declaration
public double NetAssetValueMaxDrawdown
Field Value
Type Description
double
| Edit this page View Source

Positions

Collection of all instrument objects with currently open positions. Typically, algorithms will use the Positions property of an instrument, instead of checking this collection for a match.

Declaration
public Dictionary<Instrument, int> Positions
Field Value
Type Description
Dictionary<Instrument, int>
| Edit this page View Source

SimTime

Time series of simulation time stamps with the most recent/ current time stamp at index 0.

Declaration
public TimeSeries<DateTime> SimTime
Field Value
Type Description
TimeSeries<DateTime>
| Edit this page View Source

StartTime

Time stamp representing the first bar, on which the simulator will perform trades. Most often, this is also the earliest bar being processed by the simulator, unless WarmupStartTime is set to an earlier time.

Declaration
protected DateTime StartTime
Field Value
Type Description
DateTime
| Edit this page View Source

TradingDays

Number of trading days processed. The first trading day is considered the bar, on which the very first trade is executed. This may or may not be the first trade submitted.

Declaration
public int TradingDays
Field Value
Type Description
int
| Edit this page View Source

WarmupStartTime

Optional value, specifying a time stamp earlier than StartTime, representing the first bar processed by the simulator. Setting this value allows to warm up indicators and internal calculations prior to starting trading activity.

Declaration
protected DateTime? WarmupStartTime
Field Value
Type Description
DateTime?

Properties

| Edit this page View Source

Cash

Currently available cash position. Algorithms will typically initialize this value at the beginning of the simulation.

Declaration
public double Cash { get; }
Property Value
Type Description
double
| Edit this page View Source

Instruments

Enumeration of instruments available to the simulator. It is important to understand that instruments are created dynamically during simulation such, that in many cases the number of instruments held in this collection increases over the course of the simulation.

Declaration
public IEnumerable<Instrument> Instruments { get; }
Property Value
Type Description
IEnumerable<Instrument>
| Edit this page View Source

Name

Return class type name. This method will return the name of the derived class, typically a proprietary algorithm derived from Algorithm.

Declaration
public virtual string Name { get; set; }
Property Value
Type Description
string
| Edit this page View Source

NextSimTime

Next simulator time stamp

Declaration
public DateTime NextSimTime { get; }
Property Value
Type Description
DateTime
| Edit this page View Source

PendingOrders

List of pending orders.

Declaration
public List<Order> PendingOrders { get; }
Property Value
Type Description
List<Order>
| Edit this page View Source

SimTimes

Enumerable of available simulation time stamps. An algorithm processes bars by iterating through these time stamps using a foreach loop.

Declaration
protected IEnumerable<DateTime> SimTimes { get; }
Property Value
Type Description
IEnumerable<DateTime>

Methods

| Edit this page View Source

AddDataSource(object)

Create new data source and attach it to the simulator. This overload allows to flexibly create data sources from various types.

Declaration
protected DataSource AddDataSource(object obj)
Parameters
Type Name Description
object obj

nickname, algorithm object, or data source object

Returns
Type Description
DataSource

data source created and attached

| Edit this page View Source

AddDataSource(string)

Create new data source and attach it to the simulator. If the simulator already has a data source with the given nickname attached, the call is ignored.

Declaration
protected DataSource AddDataSource(string nickname)
Parameters
Type Name Description
string nickname

nickname to create data source for

Returns
Type Description
DataSource

data source created and attached

| Edit this page View Source

AddDataSource(Algorithm)

Create new data source and attach it to the simulator. If the simulator already has a data source representing the given algo attached, the call call is ignored.

Declaration
protected DataSource AddDataSource(Algorithm algo)
Parameters
Type Name Description
Algorithm algo

algorithm to create data source for

Returns
Type Description
DataSource

data source created and attached

| Edit this page View Source

AddDataSource(DataSource)

Attach existing data source to the simulator. If this data source has already been attached to the simulator, the call is ignored. This call is typically used to attach custom data sources, which have been created without using TuringTrader's object factory.

Declaration
protected DataSource AddDataSource(DataSource dataSource)
Parameters
Type Name Description
DataSource dataSource

new data source

Returns
Type Description
DataSource

data source attached

| Edit this page View Source

AddDataSources(IEnumerable<string>)

Add multiple data sources at once and return an enumeration of data sources. If the simulator already has data sources with any of the given nicknames, those data sources will be re-used.

Declaration
protected IEnumerable<DataSource> AddDataSources(IEnumerable<string> nicknames)
Parameters
Type Name Description
IEnumerable<string> nicknames

enumerable of nicknames

Returns
Type Description
IEnumerable<DataSource>

enumerable of newly created data sources

| Edit this page View Source

CalcNextSimTime(DateTime)

Determine next sim time. This hook is used by the simulator to determine the value for NextSimTime, after reaching the end of the available historical bars. The default implementation assumes the trading calendar for U.S. stock exchanges.

Declaration
protected virtual DateTime CalcNextSimTime(DateTime timestamp)
Parameters
Type Name Description
DateTime timestamp
Returns
Type Description
DateTime

next simulator timestamp

| Edit this page View Source

Deposit(double)

Deposit cash into account. Note that the deposit amount must be positive.

Declaration
protected void Deposit(double amount)
Parameters
Type Name Description
double amount

amount to deposit

| Edit this page View Source

FillModel(Order, Bar, double)

Order fill model. This method is only called for those orders which are executed, but not for those which expired. The default implementation fills orders at their theoretical price. Algorithms can override this method to implement more realistic fill models reflecting slippage.

Declaration
protected virtual double FillModel(Order orderTicket, Bar barOfExecution, double theoreticalPrice)
Parameters
Type Name Description
Order orderTicket

original order ticket

Bar barOfExecution

bar of order execution

double theoreticalPrice

theoretical fill price

Returns
Type Description
double

custom fill price. default: theoretical fill price

| Edit this page View Source

FindInstrument(string)

Find an instrument in the Instruments collection by its nickname. In case multiple instruments have the same nickname, the first match will be returned.

Declaration
protected Instrument FindInstrument(string nickname)
Parameters
Type Name Description
string nickname

nickname of instrument to find

Returns
Type Description
Instrument

instrument matching nickname

| Edit this page View Source

HasInstrument(string)

Check, if the we have an instrument with the given nickname. Use this to check if an instrument is available for a given data source.

Declaration
protected bool HasInstrument(string nickname)
Parameters
Type Name Description
string nickname

nickname to check

Returns
Type Description
bool

true, if instrument exists

| Edit this page View Source

HasInstrument(DataSource)

Check if we have an instrument for the given datasource.

Declaration
protected bool HasInstrument(DataSource ds)
Parameters
Type Name Description
DataSource ds

data source to check

Returns
Type Description
bool

true, if instrument exists

| Edit this page View Source

HasInstruments(IEnumerable<string>)

Check, if we have instruments for all given nicknames

Declaration
protected bool HasInstruments(IEnumerable<string> nicknames)
Parameters
Type Name Description
IEnumerable<string> nicknames

enumerable with nick names

Returns
Type Description
bool

true, if all instruments exist

| Edit this page View Source

HasInstruments(IEnumerable<DataSource>)

Check, if we have instruments for all given data sources

Declaration
protected bool HasInstruments(IEnumerable<DataSource> sources)
Parameters
Type Name Description
IEnumerable<DataSource> sources

enumerable of data sources

Returns
Type Description
bool

true, if all instruments exist

| Edit this page View Source

IsValidBar(Bar)

Validate bar. This hook is used by the simulator to validate bars. Invalid bars are relevant in the following situations: (1) option chain: Simulator.OptionChain will not return contracts with invalid bars at the current sim time. (2) nav calculation: The simulator will ignore invalid bars when calculating Simulator.NetAssetValue The default implementation marks bars invalid under the following conditions: (1) bid volume or ask volume is zero. (2) bid price is less than 20% of ask price.

Declaration
protected virtual bool IsValidBar(Bar bar)
Parameters
Type Name Description
Bar bar
Returns
Type Description
bool
| Edit this page View Source

IsValidSimTime(DateTime)

Validate simulator timestamp. Timestamps deemed invalid will be skipped and not passed on to the user algorithm. The default implementation is geared at the U.S. stock market and skips all bars not within regular trading hours of the NYSE.

Declaration
protected virtual bool IsValidSimTime(DateTime timestamp)
Parameters
Type Name Description
DateTime timestamp

simulator timestamp

Returns
Type Description
bool

true, if valid

| Edit this page View Source

OptionChain(string)

Retrieve option chain by its nickname. This will return a list of all instruments with the given nickname, marked as options, and with bars available at the current simulation time.

Declaration
protected List<Instrument> OptionChain(string nickname)
Parameters
Type Name Description
string nickname

option nickname

Returns
Type Description
List<Instrument>

list of option instruments

| Edit this page View Source

OptionChain(DataSource)

Retrieve option chain by its data source. This will return a list of all instruments with the given data source, marked as options, and with bars available at the current simulation time.

Declaration
protected List<Instrument> OptionChain(DataSource ds)
Parameters
Type Name Description
DataSource ds
Returns
Type Description
List<Instrument>
| Edit this page View Source

QueueOrder(Order)

Queue order ticket for execution. Typically, algorithms won't use this function directly, but use Instrument.Trade instead.

Declaration
public void QueueOrder(Order order)
Parameters
Type Name Description
Order order
| Edit this page View Source

Withdraw(double)

Withdraw cash from account. Note that the withdrawal amount must be positive.

Declaration
protected void Withdraw(double amount)
Parameters
Type Name Description
double amount

amount to withdraw

| Edit this page View Source

_calcNetAssetValue()

calculate algorithm's net asset value.

Declaration
protected virtual double _calcNetAssetValue()
Returns
Type Description
double

nav

Extension Methods

GlobalSettings.GetRegistryValue(SimulatorCore, string, object)
GlobalSettings.SetRegistryValue(SimulatorCore, string, object)
  • Edit this page
  • View Source
In this article
Back to top Copyright © 2011-2023, Bertram Enterprises LLC dba TuringTrader.com