Python
Strategy
Overridable Methods

Strategy

This covers the compulsory class to use to interact with the Cybotrade runtime the Strategy class.

The strategy class exposes a few overridable event handlers (functions) that you are expected to define and override to 'handle the event' in your own personalized way.

Overridable Methods

on_candle_closed

async def on_candle_closed(
  self,
  strategy: StrategyTrader,
  topic: str,
  symbol: Symbol,
)

This method will be invoked by the runtime when requested candle_topics candle has closed. In [RuntimeMode.Live] this is invoked when the most latest candle has closed.

Function parameters

on_datasource_interval

async def on_datasource_interval(
  self,
  strategy: StrategyTrader,
  topic: str,
  data_list: List[Dict[str, str]],
)

This method will be invoked by the runtime when the provided datasource_topics's topic interval has elapsed.

Example:
"coinglass|1m|futures/fundingRate/ohlc-history?exchange=Binance&symbol=BTCUSDT&interval=1m": This topic will invoke the on_datasource_interval() method every 1 minute.
On the otherhand, "coinglass|1m|futures/fundingRate/ohlc-history?exchange=Binance&symbol=BTCUSDT&interval=5m": This topic will invoke the on_datasource_interval() method every 5 minutes.
If the current time when the Strategy is ran is 12:53, then the method will be invoked at 12:55, likewise for 1m interval, if the current time when the Strategy is ran is 12:53:57, then the method will be invoked at 12:54:00.

Function parameters

  • strategy - an instance of StrategyTrader.
  • topic - the DataSource Topic that has been updated.
  • data_list - represents the most latest set of data received. When requesting block data from CryptoQuant, this data_list may potentially be updated with more than 1 item. When requesting for other types of data, the length of the data_list will be 1.

set_param

async def set_param(
  self,
  identifier: str,
  value: str
)

This method should be defined when running in Backtest mode. This allows the use for Permutations.

Function parameters

  • identifier - a str identifier/name of the mutatable hyper-parameter.
  • value - a str value of the mutable hyper-parameter.

on_active_order_interval

async def on_active_order_interval(
  self,
  strategy: StrategyTrader,
  active_orders: List[ActiveOrder]
)

This method will be invoked by the runtime when the provided active_order_interval time has elapsed.
The active_orders parameter is used to retrieve the order parameters for a Trade that has either been 'PartiallyFilled' or is still in 'Created'.
For more details refer to this guide.

Do note that this method is only useful in RuntimeMode.Live and RuntimeMode.LiveTestnet.

Function parameters

on_order_update

async def on_order_update(self, strategy: StrategyTrader, update: OrderUpdate)

This method will be invoked by the runtime when there is an update for the orders placed on exchange.

Function parameters

on_backtest_complete

async def on_backtest_complete(self, strategy: StrategyTrader, performance: Performance)

This method will be invoked by the runtime when a single backtest permutation has been completed. Note that this method will only be called during backtests.

Function parameters

  • strategy - an instance of StrategyTrader.
  • performance - an instance of Performance.

on_shutdown

def on_shutdown(self)

This method will be invoked by the runtime when the runtime shuts down. This can be used for information gathering or user updating.