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.
Inherited Members
Namespace: TuringTrader.Simulator
Assembly: TuringTrader.Simulator.dll
Syntax
public abstract class SimulatorCore
Constructors
| Improve this Doc View SourceSimulatorCore()
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
| Improve this Doc View SourceCommissionPerShare
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 |
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 |
---|---|
System.DateTime |
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 |
Log
Simulator's order log.
Declaration
public List<LogEntry> Log
Field Value
Type | Description |
---|---|
System.Collections.Generic.List<T><LogEntry> |
NetAssetValue
Total net liquidation value of all positions plus cash.
Declaration
public TimeSeries<double> NetAssetValue
Field Value
Type | Description |
---|---|
TimeSeries<double> |
NetAssetValueHighestHigh
Highest high of net asset value.
Declaration
public double NetAssetValueHighestHigh
Field Value
Type | Description |
---|---|
double |
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 |
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 |
---|---|
System.Collections.Generic.Dictionary<TKey, TValue><Instrument, int> |
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<System.DateTime> |
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 |
---|---|
System.DateTime |
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 |
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 |
---|---|
System.DateTime? |
Properties
| Improve this Doc View SourceCash
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 |
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 |
---|---|
System.Collections.Generic.IEnumerable<T><Instrument> |
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 |
NextSimTime
Next simulator time stamp
Declaration
public DateTime NextSimTime { get; }
Property Value
Type | Description |
---|---|
System.DateTime |
PendingOrders
List of pending orders.
Declaration
public List<Order> PendingOrders { get; }
Property Value
Type | Description |
---|---|
System.Collections.Generic.List<T><Order> |
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 |
---|---|
System.Collections.Generic.IEnumerable<T><System.DateTime> |
Methods
| Improve this Doc View Source_calcNetAssetValue()
calculate algorithm's net asset value.
Declaration
protected virtual double _calcNetAssetValue()
Returns
Type | Description |
---|---|
double | nav |
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 |
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 |
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 |
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 |
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 |
---|---|---|
System.Collections.Generic.IEnumerable<T><string> | nicknames | enumerable of nicknames |
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<T><DataSource> | enumerable of newly created data sources |
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 |
---|---|---|
System.DateTime | timestamp |
Returns
Type | Description |
---|---|
System.DateTime | next simulator timestamp |
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 |
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 |
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 |
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 |
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 |
HasInstruments(IEnumerable<string>)
Check, if we have instruments for all given nicknames
Declaration
protected bool HasInstruments(IEnumerable<string> nicknames)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<T><string> | nicknames | enumerable with nick names |
Returns
Type | Description |
---|---|
bool | true, if all instruments exist |
HasInstruments(IEnumerable<DataSource>)
Check, if we have instruments for all given data sources
Declaration
protected bool HasInstruments(IEnumerable<DataSource> sources)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<T><DataSource> | sources | enumerable of data sources |
Returns
Type | Description |
---|---|
bool | true, if all instruments exist |
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 |
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 |
---|---|---|
System.DateTime | timestamp | simulator timestamp |
Returns
Type | Description |
---|---|
bool | true, if valid |
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 |
---|---|
System.Collections.Generic.List<T><Instrument> | list of option instruments |
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 |
---|---|
System.Collections.Generic.List<T><Instrument> |
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 |
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 |