Docs - Algo Trading
The Algo Trading tool is an advanced platform that enables users to write, test, and simulate custom trading algorithms using JavaScript. It provides a flexible, event-driven environment where users can implement complex trading strategies and backtest them against historical market data.
Fields:
- Tickers - An array of asset tickers that the algorithm will trade. These can include any of the ticker types described in the Tickers section.
- Balance - The initial cash balance available for trading, denominated in the base currency. This represents the starting capital for the trading algorithm.
- Date - A tuple of start and end dates defining the historical period over which the algorithm will be simulated.
- Leverage - When set to true, enables leveraged trading, allowing the algorithm to take positions larger than the available cash balance. This should be used cautiously as it increases both potential returns and risks.
- Init. Margin - The initial margin requirement, represented as a decimal between 0 and 1. This is only applicable when Leverage is enabled. It defines the proportion of the position value that must be covered by available cash.
- Min. Margin - The minimum margin requirement, also represented as a decimal between 0 and 1. This is the threshold below which a margin call would be triggered. Only applicable when Leverage is enabled.
- Code - The JavaScript code that defines the trading algorithm. This is where users implement their trading logic, using the provided AlgoWorkspace environment.
Code (AlgoWorkspace):
The AlgoWorkspace provides an event-driven environment for algorithm execution. It offers a set of events, properties, and methods that allow users to interact with the simulation environment and implement their trading logic.
Available events:
this.on('start', async (next, timeseries) => { ... })- Triggered at the start of the simulation. The timeseries parameter provides access to the entire historical dataset for all assets.this.on('data', async (next) => { ... })- Triggered for each data point (typically each trading day) in the simulation. This is where the main trading logic is typically implemented.this.on('end', async (next, result) => { ... })- Triggered at the end of the simulation. The result parameter provides access to the final simulation results.this.on('error', async (next, errors) => { ... })- Triggered when an error occurs during the simulation. The errors parameter contains details about the encountered error(s).
Properties and methods:
this.date- The current date in the simulation.this.assets- An array containing the current price and other relevant data for each asset being traded.this.portfolio- Represents the current state of the portfolio, including cash balance and asset positions.this.index- The current iteration index in the simulation.this.length- The total number of iterations in the simulation.this.inputs- Contains the initial input parameters for the simulation.this.state- A user-defined object for storing custom state data throughout the simulation.this.buy(assetCode: string, quantity: number): void- Executes a buy order for the specified asset and quantity.this.sell(assetCode: string, quantity: number): void- Executes a sell order for the specified asset and quantity.this.print(...msgs: string[]): void- Logs messages to the simulation output.this.depositCash(amount: number): void- Adds the specified amount of cash to the portfolio.this.loadScript(path: string): Promise- Loads an external JavaScript file, allowing the use of additional libraries or modules.this.download(obj: any, name?: string): void- Generates a downloadable file containing the specified object data.
These tools and methods provide a comprehensive environment for implementing and testing complex trading strategies. Users can access market data, manage a portfolio, execute trades, and analyze results all within the AlgoWorkspace.