Skip to content

Backtesting

Summary

Provides methods and events to manage and monitor backtesting processes.

Signature

1
public abstract interface Backtesting

Namespace

cAlgo.API

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
 using cAlgo.API;
 using cAlgo.API.Internals;
 using System;
 namespace cAlgo.Plugins
 {
     [Plugin(AccessRights = AccessRights.None)]
     public class Test : Plugin
     {
         [Parameter(DefaultValue = "Sample Breakout")]
         public string RobotName {get; set;}
         [Parameter(DefaultValue = "EURUSD")]
         public Symbol Symbol {get; set;}
         [Parameter(DefaultValue = "Daily")]
         public TimeFrame TimeFrame {get; set;}
         [Parameter(DefaultValue = "2023-09-01T06:30:00")]
         public DateTime StartTime {get; set;}
         [Parameter(DefaultValue = "2025-09-01T06:30:00")]
         public DateTime EndTime {get; set;}
         [Parameter(DefaultValue = 10000)]
         public double AccountBalance {get; set;}
         [Parameter(DefaultValue = BacktestingDataMode.M1)]
         public BacktestingDataMode DataMode {get; set;}
         [Parameter()]
         public string DataFilePath {get; set;}
         [Parameter(DefaultValue = 50)]
         public double CommissionUsdPerMillionUsd {get; set;}
         [Parameter(DefaultValue = 1)]
         public double SpreadPips {get; set;}
         private BacktestingProcess _backtestingProcess;
         protected override void OnStart()
         {
             if (AlgoRegistry.Get(RobotName, AlgoKind.Robot) is not RobotType robotType)
             {
                 Print($"Robot type {RobotName} not found");
                 return;
             }
             var backtestingSettings = new BacktestingSettings 
             {
                 StartTimeUtc = StartTime,
                 EndTimeUtc = EndTime,
                 Balance = AccountBalance,
                 DataMode = DataMode,
                 DataFile = DataFilePath,
                 CommissionUsdPerMillionUsd = CommissionUsdPerMillionUsd,
                 SpreadPips = SpreadPips
             };
             _backtestingProcess = Backtesting.Start(robotType, Symbol.Name, TimeFrame, backtestingSettings);
             _backtestingProcess.ProgressChanged += args => Print($"Bakctesting progress changed: {args.Progress}");
             _backtestingProcess.Completed += args => Print($"Bakctesting completed: {args.JsonReport}");
             if (_backtestingProcess.IsCompleted)
                 Print($"Backtesting completed, error: {_backtestingProcess.BacktestingError}");
         }
         protected override void OnStop()
         {
             _backtestingProcess?.Terminate();
         }
     }        
 }
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
 import clr
 clr.AddReference("cAlgo.API")
 from cAlgo.API import *
 """
 You have to add these parameters to Plugin C# file:
     [Parameter(DefaultValue = "Sample Breakout cBot")]
     public string RobotName {get; set;}
     [Parameter(DefaultValue = "EURUSD")]
     public Symbol Symbol {get; set;}
     [Parameter(DefaultValue = "Daily")]
     public TimeFrame TimeFrame {get; set;}
     [Parameter(DefaultValue = "2023-09-01T06:30:00")]
     public DateTime StartTime {get; set;}
     [Parameter(DefaultValue = "2025-09-01T06:30:00")]
     public DateTime EndTime {get; set;}
     [Parameter(DefaultValue = 10000)]
     public double AccountBalance {get; set;}
     [Parameter(DefaultValue = BacktestingDataMode.M1)]
     public BacktestingDataMode DataMode {get; set;}
     [Parameter()]
     public string DataFilePath {get; set;}
     [Parameter(DefaultValue = 50)]
     public double CommissionUsdPerMillionUsd {get; set;}
     [Parameter(DefaultValue = 1)]
     public double SpreadPips {get; set;}
 """
 class Test():
     def on_start(self):
         algoType = api.AlgoRegistry.Get(api.RobotName, AlgoKind.Robot)
         if algoType is None:
             print(f"Robot type {api.RobotName} not found")
             return
         robotType = RobotType(algoType)
         backtestingSettings = BacktestingSettings()
         backtestingSettings.StartTimeUtc = api.StartTime
         backtestingSettings.EndTimeUtc = api.EndTime
         backtestingSettings.Balance = api.AccountBalance
         backtestingSettings.DataMode = api.DataMode
         backtestingSettings.DataFile = api.DataFilePath
         backtestingSettings.CommissionUsdPerMillionUsd = api.CommissionUsdPerMillionUsd
         backtestingSettings.SpreadPips = api.SpreadPips
         self.backtestingProcess = api.Backtesting.Start(robotType, api.Symbol.Name, api.TimeFrame, backtestingSettings)
         self.backtestingProcess.ProgressChanged += lambda args: print(f"Bakctesting progress changed: {args.Progress}")
         self.backtestingProcess.Completed += lambda args: print(f"Bakctesting completed: {args.JsonReport}")
         if self.backtestingProcess.IsCompleted:
             print(f"Backtesting completed, error: {self.backtestingProcess.BacktestingError}")
     def on_stop(self):
         if hasattr(self, "backtestingProcess") and self.backtestingProcess is not None:
             self.backtestingProcess.Terminate()

Methods

Start

Summary

Starts the backtesting process.

Signature

1
public abstract BacktestingProcess Start(RobotType robotType, string symbolName, TimeFrame timeFrame, BacktestingSettings settings, object[] parameterValues)

Parameters

Name Type Description
robotType RobotType The type of a robot to use for backtesting
symbolName string The symbol name for which backtesting is performed
timeFrame TimeFrame The time frame for the backtesting
settings BacktestingSettings The settings to configure the backtesting process
parameterValues object[] Additional parameters for the robot

Return Value

BacktestingProcess

Properties

DataSources

Summary

Provides access to backtesting data sources.

Signature

1
public abstract BacktestingDataSources DataSources {get;}

Return Value

BacktestingDataSources

Events

ProgressChanged

Summary

Occurs when the backtesting progress changes.

Signature

1
public abstract event Action<BacktestingProgressChangedEventArgs> ProgressChanged;

Completed

Summary

Occurs when the backtesting process is completed.

Signature

1
public abstract event Action<BacktestingCompletedEventArgs> Completed;