Building Trading Strategies Orders

Orders

A Botmain strategy can send orders through the OrderProcessor service. To use it, simply @Inject() it into your strategy:

import { OrderProcessor, InstrumentProcessorBase } from "@botmain/common";

export class InstrumentProcessor extends InstrumentProcessorBase {
  @Inject() orderProcessor: OrderProcessor;
}

OrderProcessor

The OrderProcessor service is the core of order management in Botmain, acting as the essential bridge between trading strategies and their execution in the market. It serves as the intermediary that ensures orders generated by strategies are executed efficiently, whether in testing or live conditions. Botmain distinguishes between two modes of order execution:

  • Backtesting: Here, the execution system is fully integrated within the Botmain platform itself. This setup allows for thorough testing of strategies against historical data and live data, providing insights without engaging in live markets.

  • Real-Time Trading: In this mode, order execution extends beyond Botmain to include external trading platforms or exchanges. Botmain's role transitions to managing the connectivity with these external entities, ensuring seamless order execution in live market conditions.

This architecture ensures that whether in simulation or live trading, Botmain effectively bridges strategy development with market execution.

Below is the OrderProcessor interface

export interface IOrderProcessor {
    getNextValidOrderId(): string;

    sendOrder(order: Order);
    modifyOrder(order: Order);

    cancelOrder(orderId: string, reason?: string);
    cancelPendingOrder(instrumentOrPortfolio?: string);
    cancelReplaceOrder(originalOrder: Order, replacementOrder: Order);

    getOrder(orderId: string): Order:
    
    getOrders(): Order[];
    getOrders(instrumentOrPortfolio: string): Order[];
    getOrders(instrument?: string, portfolio?: string): Order[];

    getPendingOrders(): Order[];
    getPendingOrders(instrumentOrPortfolio: string): Order[];
    getPendingOrders(instrument?: string, portfolio?: string): Order[];

    getPositionSymbols(portfolio?: string): SymbolKey[];
    getLongPositionSymbols(portfolio?: string): SymbolKey[];
    getShortPositionSymbols(portfolio?: string): SymbolKey[];

    getPosition(instrumentOrPortfolio: string, portfolio?: string): Position;
}

Simple Orders

Botmain has three simple order types MarketOrder, LimitOrder and StopOrder. Since all three order types are supported on all exchanges, these orders map directly to real orders placed on the target execution system or exchange.

Algo Orders

Botmain has a set of built-in execution strategies called algo orders. Each algo order is a strategy with some predefined logic that your strategy cannot change, except for through predefined options. Algo orders are not translated to external trading platforms directly, since most platforms do not support these order types. Instead, algo orders have internal logic that analyzes factors like market price, positions, and order events and creates or modifies one or more simple orders on the external platforms. Using algorithmic orders unlocks a vast world of trading possibilities. Learn more in Algo Orders.

Used-defined Portfolios

Botmain supports the concept of user defined portfolios. A portfolio in Botmain is essentially a collection of orders that have been grouped together by the user's trading strategy, with each portfolio maintaining its unique position and tracking its Profit and Loss (PnL) performance. By default, any order executed by the strategy is not assigned to a portfolio.

Portfolios within Botmain are easily identifiable and managed through their names. To initiate a portfolio, simply set the Order.portfolio property to your chosen name when sending an order. Botmain handles the rest:

  • Existing Portfolios: If the name matches an existing portfolio, Botmain will automatically add the order to this portfolio, consolidating its position and PnL calculations accordingly.

  • New Portfolios: In cases where the portfolio name does not match any existing ones, Botmain will create a new portfolio under that name, starting a fresh set of calculations for positions and PnL.

This feature empowers traders to meticulously organize their trades and strategies, providing a clear view of performance and aiding in the effective management of multiple trading approaches simultaneously.