Strategy API
The following methods are available inside your strategy by default:
openclosecancel_ordercancel_all_ordersget_balancesget_positiongetdata
1. Opening and Closing Orders
To open or close a position, you can use either of the two convenience methods: open and close. Both methods take the same parameters:
pair: The trading pair as per the targeted exchange requirementsposition_type: EitherPositionType.LONGorPositionType.SHORTquantity: The quantity to tradeorder_type:OrderType.LIMITorOrderType.MARKETprice: Optional, only for LIMIT orders
Both methods return an Order object.
order = await self.open("BTC/USDT", PositionType.LONG, 0.1, OrderType.LIMIT, price)
order = await self.close("BTC/USDT", PositionType.LONG, 0.1, OrderType.LIMIT, price)2. Canceling Orders
You can cancel orders using either cancel_order or cancel_all_orders. Both methods return a boolean indicating success.
cancel_order requires the original order object to be passed:
success = await self.cancel_order(order)cancel_all_orders doesn't require a parameter, but can be passed a pair parameter to only cancel orders of the same pair:
success = await self.cancel_all_orders("BTC/USDT")3. Getting Balances
get_balances returns the balances on the exchange in the following form:
balances = await self.get_balances()
# get the available USD balance
balances.free["USD"]
# same as
balances.currencies["USD"].free
# also available
balances.used["USD"]
balances.total["USD"]
balances.debt["USD"]Please see https://github.com/ccxt/ccxt/wiki/Manual#account-balance for more details.
4. Get Position
get_position retrieves the position tracked by the Super Algorithm module.
WARNING
Please note that this is not the position on the live exchange, but the position maintained by the strategy. Every trade executed by the strategy will add to or remove from this position. If you had two strategies running both trading the same pair each would have their own Position information.
The returned Position object has these properties:
pairposition_typebalanceaverage_opentotal_pnl
position = self.get_position("BTC/USDT", PositionType.LONG)This position represents the cumulative effect of all trades executed through the strategy, not the actual position on the exchange.
5. Get Data
Returns the list of ohlcv values for a given source and timeframe, including any partial bar aggregates.
btc_ohlcv_list = self.data("BTC/USDT", "5m")Returns the last ohlcv value for the given source and timeframe, including any partial bar aggregates.
btc_ohlcv = self.get("BTC/USDT", "5m")Understanding Bar Aggregates
Key points about data processing in the Strategy:
DataSourcesends raw bar data to the Strategy.- Strategy processes raw bars and builds aggregations for all timeframes based on
DataSourcesettings. DataStoreupdates:- For completed bars: Strategy dispatches
on(timeframe, bar)event.getanddataresults include the completed bar. - For incomplete bars: Strategy updates the current bar.
- For completed bars: Strategy dispatches
- Strategy calls
on_tick(bar)event with raw bar data from DataSource.
Use on(timeframe, bar) for strategies focused on closing prices of specific timeframes. Use on_tick(bar) for strategies requiring intra-bar updates.
