Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions BacktestingKit/Analysis/BKBacktestCoreModels.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,21 @@ public enum PositionStatus: String, Codable {

/// Represents `BKBar` in the BacktestingKit public API.
public struct BKBar: Codable, Equatable {
/// Timestamp associated with this value.
public var time: Date
/// Open price for the bar.
public var open: Double
/// High price for the bar.
public var high: Double
/// Low price for the bar.
public var low: Double
/// Close price for the bar.
public var close: Double
/// Adjusted close price for the bar when available.
public var adjustedClose: Double?
/// Trading volume for the bar.
public var volume: Double
/// Indicators associated with this value.
public var indicators: [String: Double]

/// Creates a new instance.
Expand Down Expand Up @@ -62,7 +70,9 @@ public struct BKBar: Codable, Equatable {

/// Represents `BKBacktestOptions` in the BacktestingKit public API.
public struct BKBacktestOptions {
/// Whether to record stop price.
public var recordStopPrice: Bool
/// Whether to record risk.
public var recordRisk: Bool

/// Creates a new instance.
Expand All @@ -74,21 +84,37 @@ public struct BKBacktestOptions {

/// Represents `BKPosition` in the BacktestingKit public API.
public struct BKPosition {
/// Direction associated with this value.
public var direction: TradeDirection
/// Entry time associated with this value.
public var entryTime: Date
/// Entry price associated with this value.
public var entryPrice: Double
/// Profit associated with this value.
public var profit: Double
/// Profit percentage associated with this value.
public var profitPct: Double
/// Growth associated with this value.
public var growth: Double
/// Initial unit risk associated with this value.
public var initialUnitRisk: Double?
/// Initial risk percentage associated with this value.
public var initialRiskPct: Double?
/// Current risk percentage associated with this value.
public var curRiskPct: Double?
/// Current r multiple associated with this value.
public var curRMultiple: Double?
/// Risk series associated with this value.
public var riskSeries: [BKTimestampedValue]?
/// Holding period associated with this value.
public var holdingPeriod: Int
/// Initial stop price associated with this value.
public var initialStopPrice: Double?
/// Current stop price associated with this value.
public var curStopPrice: Double?
/// Stop price series associated with this value.
public var stopPriceSeries: [BKTimestampedValue]?
/// Profit target associated with this value.
public var profitTarget: Double?
}

Expand All @@ -99,7 +125,9 @@ public typealias ExitPositionFn = () -> Void

/// Represents `BKEnterPositionOptions` in the BacktestingKit public API.
public struct BKEnterPositionOptions {
/// Direction associated with this value.
public var direction: TradeDirection?
/// Entry price associated with this value.
public var entryPrice: Double?

/// Creates a new instance.
Expand All @@ -111,8 +139,11 @@ public struct BKEnterPositionOptions {

/// Represents `BKRuleParams` in the BacktestingKit public API.
public struct BKRuleParams {
/// Bar associated with this value.
public var bar: BKBar
/// Lookback associated with this value.
public var lookback: [BKBar]
/// Parameters associated with this value.
public var parameters: [String: Double]

/// Creates a new instance.
Expand All @@ -125,10 +156,15 @@ public struct BKRuleParams {

/// Represents `BKOpenPositionRuleArgs` in the BacktestingKit public API.
public struct BKOpenPositionRuleArgs {
/// Entry price associated with this value.
public var entryPrice: Double
/// Position associated with this value.
public var position: BKPosition
/// Bar associated with this value.
public var bar: BKBar
/// Lookback associated with this value.
public var lookback: [BKBar]
/// Parameters associated with this value.
public var parameters: [String: Double]
}

Expand All @@ -143,13 +179,21 @@ public typealias BKProfitTargetFn = (_ args: BKOpenPositionRuleArgs) -> Double

/// Represents `BKStrategy` in the BacktestingKit public API.
public struct BKStrategy {
/// Parameters associated with this value.
public var parameters: [String: Double]
/// Lookback period associated with this value.
public var lookbackPeriod: Int
/// Prep indicators associated with this value.
public var prepIndicators: ((_ input: [BKBar], _ parameters: [String: Double]) -> [BKBar])?
/// Entry rule associated with this value.
public var entryRule: BKEntryRuleFn
/// Exit rule associated with this value.
public var exitRule: BKExitRuleFn?
/// Stop loss associated with this value.
public var stopLoss: BKStopLossFn?
/// Trailing stop loss associated with this value.
public var trailingStopLoss: BKStopLossFn?
/// Profit target associated with this value.
public var profitTarget: BKProfitTargetFn?

/// Creates a new instance.
Expand Down
19 changes: 19 additions & 0 deletions BacktestingKit/Analysis/BKOptimizationModels.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ public enum OptimizeSearchDirection: String, Codable {

/// Represents `ParameterDef` in the BacktestingKit public API.
public struct ParameterDef: Codable, Equatable {
/// Name associated with this value.
public var name: String
/// Starting value associated with this value.
public var startingValue: Double
/// Ending value associated with this value.
public var endingValue: Double
/// Step size associated with this value.
public var stepSize: Double
}

Expand All @@ -25,11 +29,17 @@ public enum OptimizationType: String, Codable {

/// Represents `OptimizationOptions` in the BacktestingKit public API.
public struct OptimizationOptions: Codable, Equatable {
/// Search direction associated with this value.
public var searchDirection: OptimizeSearchDirection?
/// Optimization type associated with this value.
public var optimizationType: OptimizationType?
/// Whether to record all results.
public var recordAllResults: Bool?
/// Random seed associated with this value.
public var randomSeed: Double?
/// Number starting points represented by this value.
public var numStartingPoints: Int?
/// Whether to record duration.
public var recordDuration: Bool?

/// Creates a new instance.
Expand All @@ -52,26 +62,35 @@ public struct OptimizationOptions: Codable, Equatable {

/// Represents `OptimizationIterationResult` in the BacktestingKit public API.
public struct OptimizationIterationResult<ParameterT: Codable & Equatable>: Codable, Equatable {
/// Params associated with this value.
public var params: ParameterT
/// Result associated with this value.
public var result: Double
/// Number trades represented by this value.
public var numTrades: Int
}

/// Represents `OptimizationResult` in the BacktestingKit public API.
public struct OptimizationResult<ParameterT: Codable & Equatable>: Codable, Equatable {
/// Best result associated with this value.
public var bestResult: Double
/// Best parameter values associated with this value.
public var bestParameterValues: ParameterT
/// All results associated with this value.
public var allResults: [OptimizationIterationResult<ParameterT>]?
/// Duration ms associated with this value.
public var durationMS: Double?
}

/// Represents `WalkForwardOptimizationResult` in the BacktestingKit public API.
public struct WalkForwardOptimizationResult: Codable, Equatable {
/// Trades associated with this value.
public var trades: [BKTrade]
}

/// Represents `MonteCarloOptions` in the BacktestingKit public API.
public struct MonteCarloOptions: Codable, Equatable {
/// Random seed associated with this value.
public var randomSeed: Double?
/// Creates a new instance.
public init(randomSeed: Double? = nil) {
Expand Down
1 change: 1 addition & 0 deletions BacktestingKit/Analysis/BKRandom.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation

/// Deterministic pseudo-random number generator used by optimization helpers.
public final class Random {
private var state: UInt64

Expand Down
Loading
Loading