- Class: Backtest
- Class: Strategy
- strategy.init()
- strategy.next(context)
- strategy.buy(options)
- strategy.sell(options)
- strategy.addIndicator(name, values)
- strategy.getIndicator(name)
- strategy.getSignal(name, values)
- strategy.getSignal(name)
- strategy.data
- strategy.equity
- strategy.position
- strategy.orders
- strategy.trades
- strategy.closedTrades
- Class: Position
- Class: Order
- Class: Trade
This class represents a backtesting task that backtest a custom strategy on input data.
data{Object | Array} The historical candles data.Strategy{Strategy} A custom trading strategy class, which inherits fromStrategy.options{Object}cash{number} The initial cash. Default:10000.commission{number} The commission ratio. Default:0.margin{number} The margin ratio required for a leveraged account. Default:1.tradeOnClose{boolean}trueif market orders will be filled based on the current bar's closing price instead of the next bar's open. Default:false.hedging{boolean} Whether or not to allow trading in both long and short positions concurrently.falseif the opposite-facing orders first close existing trades in a FIFO manner. Default:false.exclusiveOrders{boolean}trueif each new order automatically closes the previous trade or position, making at most a single trade (long or short) in effect at each time. Default:false.
Create a new Backtest instance.
options{Object}params{Record<string, number>} The parameters for the trading strategy.
- Returns: {Promise} Fulfills with
Statsresults upon success.
Run the backtest for the strategy.
options{Object}params{Record<string, number[]>} The combination of parameters for the trading strategy.
- Returns: {Promise} Fulfills with
Statsresults upon success.
Optimize strategy parameters.
- Returns: {this}
Print the results of the backtest run.
- Returns: {this}
Plot the equity curve of the backtest run.
Abstract class for implementing a trading strategy.
Initialize the strategy to declare indicators and signals.
context{Object} The context of the current bar.index{number} The index of the current bar.data{Object} The OHLCV data of the current bar.date{string} The ISO 8601 date string.open{number} The open price of the bar period.high{number} The highest price of the bar period.low{number} The lowest price of the bar period.close{number} The close price of the bar period.volume{number} The volume of the bar period.
indicators{Map} To access the values of the custom indicators for the current bar.signals{Map} To access the custom trading signals for the current bar.prev{Object} The context of the previous bar.
options{Object}size{number} The size of the order.limitPrice{number} The limit price of the order.stopPrice{number} The stop price of the order.slPrice{number} The stop-loss price of the order.tpPrice{number} The take-profit price of the order.price{number} The price at which the trade is executed.
Place a new long order.
options{Object}size{number} The size of the order.limitPrice{number} The limit price of the order.stopPrice{number} The stop price of the order.slPrice{number} The stop-loss price of the order.tpPrice{number} The take-profit price of the order.price{number} The price at which the trade is executed.
Place a new short order.
name{string} The indicator name.values{number[] | Record<string, number>[]} The values of the indicator.
Add an indicator.
name{string} The indicator name.
Get indicator by name.
name{string} The signal name.values{boolean[]} The values of the signal.
Add a signal.
name{string} The signal name.
Get signal by name.
- {danfo.DataFrame}
Get the DataFrame of the historical candles data.
- {number}
Get current account equity.
- {Position}
Get current position.
- {Order[]}
Get a list of orders waiting to be executed.
- {Trade[]}
Get a list of active trades.
- {Trade[]}
Get a list of settled trades.
Currently held asset position.
- {number}
Position size in units of asset. Negative if position is short.
- {number}
Profit (positive) or loss (negative) of the current position in cash units.
- {number}
Profit (positive) or loss (negative) of the current position in percent.
- {boolean}
true if the position is long (position size is positive).
- {boolean}
true if the position is short (position size is negative).
portion{number} The portion of the position.
Close portion of position by closing portion of each active trade.
- {number}
Order size (negative for short orders).
- {number}
Order limit price for limit orders, or undefined for market orders, which are filled at next available price.
- {number}
Order stop price for stop-limit/stop-market order, otherwise undefined if no stop was set, or the stop price has already been hit.
- {number}
A stop-loss price at which, if set, a new contingent stop-market order will be placed upon the Trade following this order's execution.
- {number}
A take-profit price at which, if set, a new contingent limit order will be placed upon the Trade following this order's execution.
- {boolean}
true if the order is long (order size is positive).
- {boolean}
true if the order is short (order size is negative).
- {boolean}
true for contingent orders, i.e. OCO stop-loss and take-profit bracket orders placed upon an active trade. Remaining contingent orders are canceled when their parent Trade is closed.
Cancel the order.
- {number}
Trade size (volume; negative for short trades).
- {number}
Trade entry price.
- {number}
Trade exit price (or undefined if the trade is still active).
- {number}
Candlestick bar index of when the trade was entered.
- {number}
Candlestick bar index of when the trade was exited (or undefined if the trade is still active).
- {Order}
Get stop-loss order.
- {Order}
Get take-profit order.
- {string}
Datetime of when the trade was entered.
- {string}
Datetime of when the trade was exited.
- {boolean}
true if the trade is long (trade size is positive).
- {boolean}
true if the trade is short (trade size is negative).
- {number}
Trade profit (positive) or loss (negative) in cash units.
- {number}
Trade profit (positive) or loss (negative) in percent.
- {number}
Trade total value in cash (volume * price).
- {number}
Stop-loss price at which to close the trade.
- {number}
Take-profit price at which to close the trade.
Place new Order to close portion of the trade at next market price.