Class Algorithm
Base class for v2 trading algorithms.
Inheritance
Implements
Inherited Members
Namespace: TuringTrader.SimulatorV2
Assembly: TuringTrader.Simulator.dll
Syntax
public abstract class Algorithm : IAlgorithm
Constructors
| Improve this Doc View SourceAlgorithm()
Initialize trading algorithm. Most trading algorithms will only do very little here; the majority of the initialization should be performed in Run(), to allow multiple runs of the same instance.
Declaration
protected Algorithm()
Fields
| Improve this Doc View SourceDataCache
Data cache, used to store algorithm data. The default behavior for this cache is a dummy, bypassing all requests directly to the miss function. Because the object cache is still active, this only prevents algorithms from sharing quotes and indicator results. However, the optimizer will activate this cache, reducing the memory footprint and increasing execution speed.
Declaration
public ICache DataCache
Field Value
Type | Description |
---|---|
ICache |
EquityCurve
Equity curve generated by this algorithm.
Declaration
public List<BarType<OHLCV>> EquityCurve
Field Value
Type | Description |
---|---|
System.Collections.Generic.List<T><BarType<OHLCV>> |
ObjectCache
Object cache, used to store algorithm-specific objects. Most importantly, algorithms store all time series in the cache, including those for quotes and indicators. It is worth noting that there is a separate cache for the actual raw data.
Declaration
public ICache ObjectCache
Field Value
Type | Description |
---|---|
ICache |
Plotter
Plotter for default report.
Declaration
public Plotter Plotter
Field Value
Type | Description |
---|---|
Plotter |
Properties
| Improve this Doc View SourceAccount
Account model.
Declaration
public IAccount Account { get; set; }
Property Value
Type | Description |
---|---|
IAccount |
Cash
Algorithm's current cash holdings. Expressed as a fraction of the account's NAV.
Declaration
public double Cash { get; }
Property Value
Type | Description |
---|---|
double |
CooldownPeriod
Cooldown period. This period follows EndDate. It is important to add a few days to the end of the backtest to make sure the simulator can calculate NextSimDate accordingly.
Declaration
public TimeSpan CooldownPeriod { get; set; }
Property Value
Type | Description |
---|---|
System.TimeSpan |
EndDate
Simulation end date.
Declaration
public DateTime? EndDate { get; set; }
Property Value
Type | Description |
---|---|
System.DateTime? |
FitnessReturn
Return algorithm's fitness value (return component).
Declaration
public virtual double FitnessReturn { get; set; }
Property Value
Type | Description |
---|---|
double |
FitnessRisk
Return algorithm's fitness value (risk component).
Declaration
public virtual double FitnessRisk { get; set; }
Property Value
Type | Description |
---|---|
double |
FitnessValue
Return algorithm's fitness value (composite value).
Declaration
public virtual double FitnessValue { get; set; }
Property Value
Type | Description |
---|---|
double |
IsDataSource
Return true, if algorithm is used as a data source. Use this feature to disable optional operations that are time or memory consuming.
Declaration
public bool IsDataSource { get; set; }
Property Value
Type | Description |
---|---|
bool |
IsFirstBar
Determine if this is the first bar.
Declaration
public bool IsFirstBar { get; set; }
Property Value
Type | Description |
---|---|
bool |
IsLastBar
Determine if this is the last bar.
Declaration
public bool IsLastBar { get; set; }
Property Value
Type | Description |
---|---|
bool |
IsOptimizerParamsValid
Determine if optimizer parameter set is valid.
Declaration
public virtual bool IsOptimizerParamsValid { get; }
Property Value
Type | Description |
---|---|
bool |
IsOptimizing
Return true, if algorithm is currently being optimized. Use this feature to disable optional operations that are time or memory consuming.
Declaration
public bool IsOptimizing { get; set; }
Property Value
Type | Description |
---|---|
bool |
Name
Return algorithm's friendly name.
Declaration
public virtual string Name { get; }
Property Value
Type | Description |
---|---|
string |
NetAssetValue
Algorithm's current net asset value. Expressed in currency.
Declaration
public double NetAssetValue { get; }
Property Value
Type | Description |
---|---|
double |
NextSimDate
Next simulation timestamp. This is useful for determining the end of the week/ month/ year.
Declaration
public DateTime NextSimDate { get; }
Property Value
Type | Description |
---|---|
System.DateTime |
OptimizerParams
Return full set of optimizer parameters.
Declaration
public Dictionary<string, OptimizerParam> OptimizerParams { get; }
Property Value
Type | Description |
---|---|
System.Collections.Generic.Dictionary<TKey, TValue><string, OptimizerParam> |
OptimizerParamsAsString
String representation of the current settings of all optimizable parameters.
Declaration
public string OptimizerParamsAsString { get; }
Property Value
Type | Description |
---|---|
string |
Positions
Positions currently held by algorithm. Returns a dictionary keyed with the nickname of the assets, and a value representing the fraction of the accounts NAV held.
Declaration
public Dictionary<string, double> Positions { get; }
Property Value
Type | Description |
---|---|
System.Collections.Generic.Dictionary<TKey, TValue><string, double> |
Progress
Return simulation progress as a number between 0 and 100. This is used to display a progress bar during simulation. Note that this method of calculation shows the percentage of simulation range completed, which is not identical to the percentage of simulation time completed.
Declaration
public virtual double Progress { get; }
Property Value
Type | Description |
---|---|
double |
SimDate
Current simulation timestamp.
Declaration
public DateTime SimDate { get; }
Property Value
Type | Description |
---|---|
System.DateTime |
StartDate
Simulation start date.
Declaration
public DateTime? StartDate { get; set; }
Property Value
Type | Description |
---|---|
System.DateTime? |
TradingCalendar
Trading calendar, converting simulation date range to enumerable of valid trading days.
Declaration
public ITradingCalendar TradingCalendar { get; set; }
Property Value
Type | Description |
---|---|
ITradingCalendar |
WarmupPeriod
Warmup period.This period comes before StartDate. It is crucial to have enough warmup before beginning to trade, so that indicators can settle on their correct values.
Declaration
public TimeSpan WarmupPeriod { get; set; }
Property Value
Type | Description |
---|---|
System.TimeSpan |
Methods
| Improve this Doc View SourceAsset(object)
Load quotations or run algorithm, dependent on the type of the object passed in.
Declaration
public virtual TimeSeriesAsset Asset(object obj)
Parameters
Type | Name | Description |
---|---|---|
object | obj | string or algorithm |
Returns
Type | Description |
---|---|
TimeSeriesAsset | asset time series |
Asset(string, Func<List<BarType<OHLCV>>>)
Load asset data through custom code. Subsequent calls to this method with the same name will be served from the cache.
Declaration
public virtual TimeSeriesAsset Asset(string name, Func<List<BarType<OHLCV>>> retrieve)
Parameters
Type | Name | Description |
---|---|---|
string | name | name of asset |
System.Func<TResult><System.Collections.Generic.List<T><BarType<OHLCV>>> | retrieve | retrieval function for custom data |
Returns
Type | Description |
---|---|
TimeSeriesAsset |
Asset(string)
Load quotations for tradeable asset. Subsequent calls to this method with the same name will be served from a cache.
Declaration
public virtual TimeSeriesAsset Asset(string name)
Parameters
Type | Name | Description |
---|---|---|
string | name | name of asset |
Returns
Type | Description |
---|---|
TimeSeriesAsset | asset time series |
Asset(IAlgorithm)
Run v2 algorithm and bring its results in as an asset. Subsequent calls to this method with the same generator will be served from a cache.
Declaration
public virtual TimeSeriesAsset Asset(IAlgorithm generator)
Parameters
Type | Name | Description |
---|---|---|
IAlgorithm | generator | algorithm used as asset |
Returns
Type | Description |
---|---|
TimeSeriesAsset | asset time series |
Clone()
Clone algorithm, including all optimizer parameters. The application uses this method to clone the 'master' instance, and create new algorithm instances before running them.
Declaration
public IAlgorithm Clone()
Returns
Type | Description |
---|---|
IAlgorithm | new algorithm instance |
Lambda(string, Func<double, double>, double)
Calculate indicator from lambda function.
Declaration
public TimeSeriesFloat Lambda(string cacheId, Func<double, double> barFun, double init)
Parameters
Type | Name | Description |
---|---|---|
string | cacheId | unique cache id |
System.Func<T, TResult><double, double> | barFun | lambda function |
double | init | initial value |
Returns
Type | Description |
---|---|
TimeSeriesFloat | output time series |
Lambda(string, Func<double>)
Calculate indicator from lambda function.
Declaration
public TimeSeriesFloat Lambda(string cacheId, Func<double> barFun)
Parameters
Type | Name | Description |
---|---|---|
string | cacheId | unique cache id> |
System.Func<TResult><double> | barFun | lambda function |
Returns
Type | Description |
---|---|
TimeSeriesFloat | output time series |
Report()
Render default report.
Declaration
public virtual void Report()
Run()
Run backtest.
Declaration
public virtual void Run()
SimLoop(Action)
Simulation loop. This override's bar function returns void. Therefore, the algorithm's output series is generated from the trading activity in the algorithm's Account object.
Declaration
public void SimLoop(Action barFun)
Parameters
Type | Name | Description |
---|---|---|
System.Action | barFun |
SimLoop(Func<OHLCV>)
Simulation loop. This override's bar function returns a bar object. This object is used to create teh algorithm's output series.
Declaration
public void SimLoop(Func<OHLCV> barFun)
Parameters
Type | Name | Description |
---|---|---|
System.Func<TResult><OHLCV> | barFun |
Universe(string)
Return constituents of universe at current simulator timestamp. Please note that not all data feeds support this feature. For those feeds, the list of symbols returned might be inaccurate or incomplete.
Declaration
public virtual HashSet<string> Universe(string name)
Parameters
Type | Name | Description |
---|---|---|
string | name |
Returns
Type | Description |
---|---|
System.Collections.Generic.HashSet<T><string> |