Skip to content

MonteCarloSettings

record cAlgo.API Since 5.10

DesktopCLICloud

Represents the optional Monte Carlo simulation that is attached to a backtest. A backtest produces a single equity curve, the one historical ordering of its trades. A Monte Carlo simulation re-runs that result many times, each time resampling or perturbing it, and reports the distribution of the outcomes instead of the single historical path.

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

public class MonteCarloSettings

Namespace: cAlgo.API · Group: Backtesting

Examples

using cAlgo.API;
using cAlgo.API.Internals;
using System;

namespace cAlgo.Plugins
{
    public partial class MonteCarloTest : Plugin
    {
        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 = BacktestingDataMode.M1,
                SpreadPips = 1,
                MonteCarlo = new MonteCarloSettings(MonteCarloMethod.BlockBootstrap, MonteCarloOutputPath)
                {
                    Iterations = 5000,
                    Seed = 42,
                    StartingEquity = AccountBalance,
                    RuinFloor = 0,
                    BlockSize = 10
                }
            };

            _backtestingProcess = Backtesting.Start(robotType, Symbol.Name, TimeFrame, backtestingSettings);

            _backtestingProcess.Completed += args => Print($"Monte Carlo result: {_backtestingProcess.MonteCarloJsonReport}");
        }

        protected override void OnStop()
        {
            _backtestingProcess?.Terminate();
        }
    }
}
import clr
clr.AddReference("cAlgo.API")

from cAlgo.API import *

class MonteCarloTest():
    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 = BacktestingDataMode.M1
        backtestingSettings.SpreadPips = 1

        monteCarlo = MonteCarloSettings(MonteCarloMethod.BlockBootstrap, api.MonteCarloOutputPath)
        monteCarlo.Iterations = 5000
        monteCarlo.Seed = 42
        monteCarlo.StartingEquity = api.AccountBalance
        monteCarlo.RuinFloor = 0
        monteCarlo.BlockSize = 10
        backtestingSettings.MonteCarlo = monteCarlo

        self.backtestingProcess = api.Backtesting.Start(robotType, api.Symbol.Name, api.TimeFrame, backtestingSettings)
        self.backtestingProcess.Completed += lambda args: print(f"Monte Carlo result: {self.backtestingProcess.MonteCarloJsonReport}")

    def on_stop(self):
        if hasattr(self, "backtestingProcess") and self.backtestingProcess is not None:
            self.backtestingProcess.Terminate()
{
  "AccessRights": "FullAccess",
  "Parameters": [
    {
      "Name": "RobotName",
      "Type": "String",
      "DefaultValue": "Sample Breakout"
    },
    {
      "Name": "Symbol",
      "Type": "Symbol",
      "DefaultValue": "EURUSD"
    },
    {
      "Name": "TimeFrame",
      "Type": "TimeFrame",
      "DefaultValue": "Daily"
    },
    {
      "Name": "StartTime",
      "Type": "DateTime",
      "DefaultValue": "2023-09-01T06:30:00"
    },
    {
      "Name": "EndTime",
      "Type": "DateTime",
      "DefaultValue": "2025-09-01T06:30:00"
    },
    {
      "Name": "AccountBalance",
      "Type": "Double",
      "DefaultValue": 10000
    },
    {
      "Name": "MonteCarloOutputPath",
      "Type": "String",
      "DefaultValue": "C:\\Temp\\montecarlo-result.json"
    }
  ]
}

Constructors

Name Description
MonteCarloSettings 2 overloadsDesktopCLICloud Initializes a new instance of the MonteCarloSettings record. The method and the output file path are required, every other setting is optional.

Properties

Name Description
BlockSizeDesktopCLICloudint Gets the number of consecutive trades that are kept together when resampling. It is used only by the BlockBootstrap and StationaryBootstrap methods and is ignored by the others. It must be greater than or equal to 1 and the default value is 5.
IterationsDesktopCLICloudint Gets the number of simulated iterations. When it is 0, the default, the method default is used, which is 1000 for the resampling methods and 100 for SyntheticPricePath. Any other value outside the range of the method, which is 100 to 100000 for the resampling methods and 100 to 1000 for SyntheticPricePath, is reported as an invalid settings error instead of being clamped.
MethodDesktopCLICloudMonteCarloMethod Gets the Monte Carlo simulation method.
OutputFilePathDesktopCLICloudstring Gets the destination file path for the Monte Carlo result JSON. The file uses the same snapshot format the desktop Monte Carlo tab saves, so it can be loaded back by the desktop application.
RuinFloorDesktopCLIClouddouble Gets the equity level that is treated as account ruin. The risk of ruin is the share of the simulations whose equity drops to or below this level. It must be greater than or equal to 0 and the default value is 0.
SeedDesktopCLICloudint Gets the random seed that makes the simulation reproducible. The same seed with the same inputs produces the same result, even across application restarts. The default value is 0.
StartingEquityDesktopCLIClouddouble Gets the starting equity used as the additive base for the simulated equity curves. When it is 0, the default, the initial balance of the backtest is used.

See also