Engine API

The Engine module provides the core execution framework for Planar strategies. It handles data management, order execution, and coordinates between different execution modes (simulation, paper trading, and live trading).

Overview

The Engine serves as the central coordinator that:

  • Manages strategy execution across different modes
  • Handles data loading and OHLCV management
  • Coordinates with exchanges and executors
  • Provides unified interfaces for backtesting and live trading

Core Functions

Data Management

OHLCV Data Functions

Usage Examples

Advanced Data Operations

Strategy Execution

Execution Control

Example: Strategy Lifecycle

Exchange Integration

Exchange Access

Example: Exchange Operations

Data Structures and Types

Core Types

The Engine module works with several key types:

Data Handlers

Integration Patterns

Backtesting Pattern

Paper Trading Pattern

Live Trading Pattern

Performance Optimization

Efficient Data Access

Batch Operations

Error Handling

Robust Data Loading

Exchange Connection Handling

Complete API Reference

Strategies.StrategyMethod

Initializes a Strategy object in the Strategies module.

Strategy(
    self::Module,
    assets::Union{AbstractSet{String}, Tuple{Vararg{String}}, Dict, AbstractVector{String}};
    load_data,
    config,
    params,
    account,
    mode,
    margin,
    sandbox,
    timeframe
)

The Strategy function takes the following parameters:

  • self: a Module object representing the current module.
  • assets: a Union of a dictionary or iterable of strings representing the assets to be included in the strategy.
  • load_data (optional, default is true): a boolean indicating whether to load data for the assets.
  • config: a Config object representing the configuration settings for the strategy.
  • mode (optional, default is config.mode): a mode setting for the strategy.
  • margin (optional, default is config.margin): a margin setting for the strategy.
  • sandbox (optional, default is true): a boolean indicating whether to run the strategy in a sandbox environment.
  • timeframe (optional, default is config.min_timeframe): a timeframe setting for the strategy.

The function initializes a Strategy object with the specified settings and assets.

source
Base.fill!Method

fill! all the instances with given timeframes data...

fill!(
    ac::Collections.AssetCollection,
    tfs...;
    kwargs...
) -> DataFrames.DataFrame
source
Base.fill!Method

Pulls data from storage, or resamples from the shortest timeframe available.

fill!(ai::Instances.AssetInstance, tfs...; exc, force, from)

This fill! function takes the following parameters:

  • ai: an AssetInstance object which represents an instance of an asset.
  • tfs...: one or more TimeFrame objects that represent the desired timeframes to fill the data for.
  • exc (optional, default is ai.exchange): an Exchange object that represents the exchange to pull data from.
  • force (optional, default is false): a boolean that indicates whether to force the data filling, even if the data is already present.
  • from (optional, default is nothing): a DateTime object that represents the starting date from which to fill the data.

Fills the data for the specified timeframes. If the data is already present and force is false, the function does nothing.

source
Data.stub!Method

Replaces the data of the asset instances with src which should be a mapping. Used for backtesting.

stub!(ac::Collections.AssetCollection, src; fromfiat)

The stub! function takes the following parameters:

  • ac: an AssetCollection object which encapsulates a collection of assets.
  • src: The mapping, should be a pair TimeFrame => Dict{String, PairData}.
  • fromfiat (optional, default is true): a boolean that indicates whether the assets are priced in fiat currency. If true, the assets are priced in fiat currency.

The function replaces the OHLCV data of the assets in the ac collection with the data from the src mapping. This is useful for backtesting trading strategies.

Example:

using Scrapers.BinanceData as bn
using Strategies
using Exchanges
setexchange!(:binanceusdm)
cfg = Config(Symbol(exc.id))
strat = strategy!(:Example, cfg)
data = bn.binanceload()
stub!(strat.universe, data)
source

See Also