Show / Hide Table of Contents

Class Algorithm

Base class for v2 trading algorithms.

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

Constructors

| Improve this Doc View Source

Algorithm()

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 Source

DataCache

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
| Improve this Doc View Source

EquityCurve

Equity curve generated by this algorithm.

Declaration
public List<BarType<OHLCV>> EquityCurve
Field Value
Type Description
System.Collections.Generic.List<T><BarType<OHLCV>>
| Improve this Doc View Source

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
| Improve this Doc View Source

Plotter

Plotter for default report.

Declaration
public Plotter Plotter
Field Value
Type Description
Plotter

Properties

| Improve this Doc View Source

Account

Account model.

Declaration
public IAccount Account { get; set; }
Property Value
Type Description
IAccount
| Improve this Doc View Source

Cash

Algorithm's current cash holdings.

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

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
| Improve this Doc View Source

EndDate

Simulation end date.

Declaration
public DateTime? EndDate { get; set; }
Property Value
Type Description
System.DateTime?
| Improve this Doc View Source

FitnessReturn

Return algorithm's fitness value (return component).

Declaration
public virtual double FitnessReturn { get; set; }
Property Value
Type Description
double
| Improve this Doc View Source

FitnessRisk

Return algorithm's fitness value (risk component).

Declaration
public virtual double FitnessRisk { get; set; }
Property Value
Type Description
double
| Improve this Doc View Source

FitnessValue

Return algorithm's fitness value (composite value).

Declaration
public virtual double FitnessValue { get; set; }
Property Value
Type Description
double
| Improve this Doc View Source

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
| Improve this Doc View Source

IsFirstBar

Determine if this is the first bar.

Declaration
public bool IsFirstBar { get; }
Property Value
Type Description
bool
| Improve this Doc View Source

IsLastBar

Determine if this is the last bar.

Declaration
public bool IsLastBar { get; }
Property Value
Type Description
bool
| Improve this Doc View Source

IsOptimizerParamsValid

Determine if optimizer parameter set is valid.

Declaration
public virtual bool IsOptimizerParamsValid { get; }
Property Value
Type Description
bool
| Improve this Doc View Source

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
| Improve this Doc View Source

Name

Return algorithm's friendly name.

Declaration
public virtual string Name { get; }
Property Value
Type Description
string
| Improve this Doc View Source

NetAssetValue

Algorithm's current net asset value.

Declaration
public double NetAssetValue { get; }
Property Value
Type Description
double
| Improve this Doc View Source

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
| Improve this Doc View Source

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>
| Improve this Doc View Source

OptimizerParamsAsString

String representation of the current settings of all optimizable parameters.

Declaration
public string OptimizerParamsAsString { get; }
Property Value
Type Description
string
| Improve this Doc View Source

Positions

Positions currently held by algorithm.

Declaration
public Dictionary<string, double> Positions { get; }
Property Value
Type Description
System.Collections.Generic.Dictionary<TKey, TValue><string, double>
| Improve this Doc View Source

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
| Improve this Doc View Source

SimDate

Current simulation timestamp.

Declaration
public DateTime SimDate { get; }
Property Value
Type Description
System.DateTime
| Improve this Doc View Source

StartDate

Simulation start date.

Declaration
public DateTime? StartDate { get; set; }
Property Value
Type Description
System.DateTime?
| Improve this Doc View Source

TradingCalendar

Trading calendar, converting simulation date range to enumerable of valid trading days.

Declaration
public ITradingCalendar TradingCalendar { get; set; }
Property Value
Type Description
ITradingCalendar
| Improve this Doc View Source

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 Source

Asset(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

| Improve this Doc View Source

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
| Improve this Doc View Source

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

| Improve this Doc View Source

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

| Improve this Doc View Source

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

| Improve this Doc View Source

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

| Improve this Doc View Source

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

| Improve this Doc View Source

Report()

Render default report.

Declaration
public virtual void Report()
| Improve this Doc View Source

Run()

Run backtest.

Declaration
public virtual void Run()
| Improve this Doc View Source

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
| Improve this Doc View Source

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
| Improve this Doc View Source

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>

Implements

IAlgorithm

Extension Methods

GlobalSettings.GetRegistryValue(Algorithm, string, object)
GlobalSettings.SetRegistryValue(Algorithm, string, object)
  • Improve this Doc
  • View Source
In This Article
Back to top Copyright © 2011-2023, Bertram Enterprises LLC dba TuringTrader.com