Skip to content

Strategy API

The following methods are available inside your strategy by default:

  • open
  • close
  • cancel_order
  • cancel_all_orders
  • get_balances
  • get_position
  • get
  • data

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 requirements
  • position_type: Either PositionType.LONG or PositionType.SHORT
  • quantity: The quantity to trade
  • order_type: OrderType.LIMIT or OrderType.MARKET
  • price: Optional, only for LIMIT orders

Both methods return an Order object.

python
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:

python
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:

python
success = await self.cancel_all_orders("BTC/USDT")

3. Getting Balances

get_balances returns the balances on the exchange in the following form:

python
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:

  • pair
  • position_type
  • balance
  • average_open
  • total_pnl
python
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.

python
btc_ohlcv_list = self.data("BTC/USDT", "5m")

Returns the last ohlcv value for the given source and timeframe, including any partial bar aggregates.

python
btc_ohlcv = self.get("BTC/USDT", "5m")

Understanding Bar Aggregates

Key points about data processing in the Strategy:

  1. DataSource sends raw bar data to the Strategy.
  2. Strategy processes raw bars and builds aggregations for all timeframes based on DataSource settings.
  3. DataStore updates:
    • For completed bars: Strategy dispatches on(timeframe, bar) event. get and data results include the completed bar.
    • For incomplete bars: Strategy updates the current bar.
  4. 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.