Skip to content

BacktestingDataSources.Add

method · BacktestingDataSources · cAlgo.API Since 5.3

DesktopCLICloud

Adds a new backtesting data source.

Environment behaviour

CLI — not supported. Available to plugins only, and plugins cannot run in the CLI.

Cloud — not supported. Available to plugins only, and plugins cannot run in the cloud.

Definition

BacktestingDataSource Add(string name, BacktestingDataSourceOptions options)
backtestingDataSources.Add(name: str, options: BacktestingDataSourceOptions) -> BacktestingDataSource

Parameters

Name C# type Python type Description
name string str Data source unique name.
options BacktestingDataSourceOptions BacktestingDataSourceOptions Data source options.

Returns BacktestingDataSource — BacktestingDataSource

Examples

protected override void OnStart()
{
    var options = new BacktestingDataSourceOptions(BacktestingDataSourceDataType.OpenPrices, OnMinMaxTimeRequested, OnDataRequested);
    var dataSource = Backtesting.DataSources.Add("My Data", options);
    Print("Added backtesting data source {0}", dataSource.Name);
}

private BacktestingMinMaxTime OnMinMaxTimeRequested(BacktestingDataSourceMinMaxTimeArgs args)
{
    return new BacktestingMinMaxTime(Server.Time.AddYears(-1), Server.Time);
}

private void OnDataRequested(BacktestingDataRequest request)
{
    var bars = MarketData.GetBars(request.PriceDataSourceId.TimeFrame, request.PriceDataSourceId.Symbol.Name);
    var data = bars.Where(bar => bar.OpenTime >= request.StartTime && bar.OpenTime <= request.EndTime)
        .Select(bar => new BacktestingBar(bar.OpenTime, bar.Open, bar.High, bar.Low, bar.Close, (long)bar.TickVolume))
        .ToArray();
    request.Complete(new BacktestingBarsData(data));
}
from System import Action, Func, Array

def on_start(self):
    options = BacktestingDataSourceOptions(BacktestingDataSourceDataType.OpenPrices, Func[BacktestingDataSourceMinMaxTimeArgs, BacktestingMinMaxTime](self.on_min_max_time_requested), Action[BacktestingDataRequest](self.on_data_requested))
    dataSource = api.Backtesting.DataSources.Add("My Data", options)
    api.Print(f"Added backtesting data source {dataSource.Name}")

def on_min_max_time_requested(self, args):
    return BacktestingMinMaxTime(api.Server.Time.AddYears(-1), api.Server.Time)

def on_data_requested(self, request):
    bars = api.MarketData.GetBars(request.PriceDataSourceId.TimeFrame, request.PriceDataSourceId.Symbol.Name)
    data = [BacktestingBar(bar.OpenTime, bar.Open, bar.High, bar.Low, bar.Close, int(bar.TickVolume)) for bar in bars if bar.OpenTime >= request.StartTime and bar.OpenTime <= request.EndTime]
    request.Complete(BacktestingBarsData(Array[BacktestingBar](data)))