MLThon Strategy package

mlthon.strats.demo_strat module

ML Tech, Inc. Demo strat docstring.

This strategy serve as an example to help researchers understand the order entry, order management/tracking, market data collections, etc. Most of the functionalities provided by our trading environment are used in this strat.

This is a simple one instrument (BTC-USDT linear perpetual in Bybit) market-making strategy. Trading logic is: It rests at the top bid/offer behind a fixed threshold(_entry_qty_threshold_) size. If the size drops below the threshold, the order is canceled. If a resting buy(sell) order is filled, a closing sell(buy) order is placed if the size on the top bid(offer) is at least as much as a fixed threshold(_exit_qty_threshold_) size. If the size of the bid(offer) drops below the threshold, an aggressive order closing order is sent to close the position.

See more details on the comments below.

class DemoStrat(cfg)[source]

Bases: mlthon.api.istrategy.IStrategy

cancel_all_open_orders_(reason='cancel_all')[source]
cancel_all_open_orders_on_side_(side, reason='cancel_all')[source]
cancel_order_(order, instrument_id, reason='cancel_order')[source]

Function used by strat to send a cancel order to our trading system. Order manager is used to determine if order can be cancelled and to keep track of order through it’s lifecycle

format_price_and_qty_(price, qty, side)[source]

This function would round the price and qty to the nearest tick up/down (depending on the side) so that new/modify/cancel order could be sent with valid price and qty.

get_orders_command(params)[source]
get_pnl_command(params)[source]
get_position_command(params)[source]
hooked_command_test(params)[source]
is_market_valid_()[source]
modify_order_(client_id, side, price, qty, reason, exch)[source]

Function used by strat to send a modify order to our trading system. Order manager is used to determine if order can be modified and to keep track of order through it’s lifecycle

modify_resting_orders_(side, price, qty)[source]
on_account_info(exchange, user_id)[source]

In this callback method, researchers should define the behavior of the strategy when receiving account info. For now, this message (and thus, this method) is not very useful.

Parameters
  • exchange (Exchange) – exchange the account info comes from.

  • user_id (str) – account information - user ID of this account.

Return type

None

on_add_level(exchange, instrument_id, side, level_id, price, qty, recv_ts)[source]

In this callback method, researchers should define the behavior of the strategy when receiving a add-level market data. This method would be invoked by the trading environment, with information of this market data record.

Parameters
  • exchange (Exchange) – exchange with market data update (add level).

  • instrument_id (int) – instrument ID with market data update.

  • side (Side) – side of the add-level update.

  • level_id (int) – level ID of the update.

  • price (Price) – price for this level.

  • qty (Qty) – quantity for this level.

  • recv_ts (int) – timestamp when receiving this record. Trading environment will pass current timestamp to it if unspecified.

Return type

None

Example

For example, when the feed handler receive an add-level buy order update from exchange Bybit, for instrument ID 30000000000 (BTC-USD inverse perpetual in Bybit), with level ID 92265000, price 9226.5 and qty 13952, the trading environment will invoke this callback method with exchange Exchange.Bybit, instrument ID 30000000000, side Side.Buy, level_id 92265000, price Price(‘9226.50000000’) and qty Qty(‘13952.00000000’).

on_add_price_level(exchange, instrument_id, side, price, qty, recv_ts)[source]

In this callback method, researchers should define the behavior of the strategy when receiving a add-price-level market data. This method would be invoked by the trading environment, with information of this market data record.

Parameters
  • exchange (Exchange) – exchange with market data update (add price level).

  • instrument_id (int) – instrument ID with market data update.

  • side (Side) – side of the add-price-level update, Side.Sell or Side.Buy.

  • price (Price) – price for this level.

  • qty (Qty) – quantity for this level.

  • recv_ts (int) – timestamp when receiving this record. Trading environment will pass current timestamp to it if unspecified.

Return type

None

Example

For example, when the feed handler receive an add-price-level buy order update from exchange Bybit, for instrument ID 30000000000 (BTC-USD Inverse Perpetual in Bybit) BTC-USD, with price 9297 and qty 13952, the trading environment will invoke this callback method with exchange Exchange.Bybit, instrument ID 30000000000, side Side.Buy, price Price(‘9226.50000000’) and qty Qty( ‘13952.00000000’).

on_best_ask_level_update(exchange, instrument_id, price, qty, recv_ts)[source]

In this callback method, researchers should define the behavior of the strategy when best ask level is updated. This method would be invoked by the trading environment, with information of the new best ask level.

Parameters
  • exchange (Exchange) – the exchange where one of the product has best ask level update.

  • instrument_id (int) – the product with best ask level update.

  • price (Price) – price level for the new best ask.

  • qty (Qty) – quantity for the new best ask.

  • recv_ts (int) – timestamp when receiving this update. Trading environment will pass current timestamp to it if unspecified.

Return type

None

on_best_bid_level_update(exchange, instrument_id, price, qty, recv_ts)[source]

In this callback method, researchers should define the behavior of the strategy when best bid level is updated. This method would be invoked by the trading environment, with information of the new best bid level.

Parameters
  • exchange (Exchange) – the exchange where one of the product has best bid level update.

  • instrument_id (int) – the product with best bid level update.

  • price (Price) – price level for the new best bid.

  • qty (Qty) – quantity for the new best bid.

  • recv_ts (int) – timestamp when receiving this update. Trading environment will pass current timestamp to it if unspecified.

Return type

None

on_cancel_all_ack(exchange, instrument_id)[source]

When sending a cancel-all request and get acknowledgement from the exchange, exchange will send a cancel all ack message back to the trading environment. Then the environment will invoke this method with all the necessary information of a cancel order ack message. So in this callback method, researchers should define the behavior of the strategy (the api) when the cancel-all requested is acknowledged.

Parameters
  • exchange (Exchange) – exchange of the cancel-all request.

  • instrument_id (int) – instrument (product) of this cancel-all request, represented by ID. This DOES NOT correspond the the actual cancellation of the corresponding order(s), which is signaled by the on_order_cancelled callback.

Return type

None

on_cancel_all_reject(exchange, instrument_id, reject_code, reject_reason)[source]

In this callback method, researchers should define the behavior of the strategy (the api) when the cancel-all requested is rejected. Trading environment would invoke it with all the necessary information when the request is rejected by the exchange.

Parameters
  • exchange (Exchange) – exchange of this request.

  • instrument_id (int) – instrument/product of this request, represented as ID

  • reject_code (GtwRejectCode) – reject code from Gateway. Code represents the reason of rejection, e.g. 1000 = GtwNotReady (gateway is not ready for requests).

  • reject_reason (str) – attached reason for the rejection.

  • Note that ‘reject_reason’ may be empty. Strategy developers should familiarize themselves with

  • the meaning of GtwRejectCode, as it may help them react differently to different types of rejection.

Return type

None

on_cancel_order_ack(exchange, client_id)[source]

When sending a cancel order and get acknowledgement from the exchange, exchange will send a cancel order ack message back to the trading environment. Then the environment will invoke this method with all the necessary information of a cancel order ack message. So in this callback method, researchers should define the behavior of the strategy (the api) when the cancel order requested is acknowledged.

Parameters
  • exchange (Exchange) – exchange of this cancel order.

  • client_id (str) – client ID (generated and kept in the internal system) for this cancel order. This DOES NOT correspond the the actual cancellation of the corresponding order, which is signaled by the on_order_cancelled callback.

Return type

None

on_cancel_order_reject(exchange, client_id, reject_code, reject_reason)[source]

In this callback method, researchers should define the behavior of the strategy (the api) when the cancel order requested is rejected. Trading environment would invoke it with all the necessary information when the request is rejected by the exchange.

Parameters
  • exchange (Exchange) – exchange of this order.

  • nullable_client_id (str) – internal client ID. client_id is optional here. If it’s None, the corresponding request would be cancel-all.

  • reject_code (GtwRejectCode) – reject code from Gateway. Code represents the reason of rejection, e.g. 1000 = GtwNotReady (gateway is not ready for requests).

  • reject_reason (str) – attached reason for the rejection.

  • Note that ‘reject_reason’ may be empty. Strategy developers should familiarize themselves with

  • the meaning of GtwRejectCode, as it may help them react differently to different types of rejection.

Return type

None

on_delete_level(exchange, instrument_id, side, level_id, recv_ts)[source]

In this callback method, researchers should define the behavior of the strategy when receiving a delete-level market data. This method would be invoked by the trading environment, with information of this market data record.

Parameters
  • exchange (Exchange) – exchange with market data update (delete level).

  • instrument_id (int) – instrument ID with market data update.

  • side (Side) – side of the delete-level update.

  • level_id (int) – level ID of the update.

  • recv_ts (int) – timestamp when receiving this record. Trading environment will pass current timestamp to it if unspecified.

Return type

None

Example

For example, when the feed handler receive a delete-level buy order update from exchange Bybit, for instrument ID 30000000000 (BTC-USD inverse perpetual in Bybit), with level ID 92375000, the trading environment will invoke this callback method with exchange Exchange.Bybit, instrument ID 30000000000, side Side.Buy and level_id 92375000.

on_delete_price_level(exchange, instrument_id, side, price, recv_ts)[source]

In this callback method, researchers should define the behavior of the strategy when receiving a delete-price-level market data update. This method would be invoked by the trading environment, with information of this market data record.

Parameters
  • exchange (Exchange) – exchange with market data update (delete price level).

  • instrument_id (int) – instrument ID with market data update.

  • side (Side) – side of the delete-price-level update.

  • price (Price) – price level of this update

  • recv_ts (int) – timestamp when receiving this record. Trading environment will pass current timestamp to it if unspecified.

Return type

None

Example

For example, when the feed handler receive a delete-price-level buy order update from exchange Bybit, for instrument ID 30000000000 (BTC-USD inverse perpetual in Bybit), with price 9226.5, the trading environment will invoke this callback method with exchange Exchange.Bybit, instrument ID 30000000000, side Side.Buy and price Price(‘9226.50000000’).

on_feed_handler_status(exchange, status, detail)[source]

This callback method should define the behaviors of the strategy (the api) when receiving Feed Handler status transition. Possible status values are: ‘SnapshotStart’, ‘SnapshotEnd’, ‘Disconnected’:

SnapshotStart - the feed is connected and the following events are a snapshot of the feed
SnapshotEnd - all the events between SnaphotStart and SnapshotEnd constitute the initial snapshot of the feed. subsequent events are incremental updates (AKA delta updates)
Disconnected - The feed got disconnected, and is attempting to reconnect, once connected a new snapshot will be observed

The api (this callback method) should clear the orders books when receiving a Connecting status. Between SnapshotStart and End, the api should build the order book. After receiving SnapshotEnd, the api should continue building its order book, knowing that the updates it receives are now live incremental updates.

Parameters
  • exchange (Exchange) – exchange with Feed Handler status transition.

  • status (FeedHandlerStatus) – new Feed Handler status, ‘SnapshotStart’, ‘SnapshotEnd’ or ‘Disconnected’.

  • detail (str) – attached details for the status transition.

Return type

None

on_framework_connected(env)[source]

In this callback method, researchers should define the behavior of the strategy (the api) when it’s connected to the trading framework. When the trading framework (gateway, feed handler, etc.) is connected, the environment will invoke this method. Note that login via trading environment is REQUIRED in this method so that the strategy could be registered into the system and start sending msg back and forward. An example is provided below.

Parameters

env (IStratEnv) – this callback may provide trading env as a parameter

Return type

None

Example

# A simple implementation for this method (need to initiate the internal states in the constructor, in order to keep them) :

class ExampleStrat(IStrategy):
    def __init__(self, cfg):  # or no cfg
        ...
        self._env_ = None  # keep the internal state for trading env object
    ......
    def  on_framework_connected(env: IStratEnv):
        self._env_ = env
        ...
        self._env_.login("example strat")
        ...
    ......
on_framework_disconnected()[source]

In this callback method, researchers should define the behavior of the strategy (the api) when losing connection to the framework itself. When losing connections, the environment will invoke this method.

Return type

None

on_funding_info(exchange, instrument_id, funding_rate, next_funding_ts)[source]

In this callback method, researchers should define the behavior of the strategy when receiving funding info. Some exchanges reward users for being long or short certain derivative products. This method will be invoked by the trading environment, with information about the rate of such rewards.

Parameters
  • exchange (Exchange) – exchange this info comes from.

  • instrument_id (int) – instrument ID of this funding info.

  • funding_rate (float) – funding rate of this funding info.

  • next_funding_ts (int) – timestamp of next funding event.

Return type

None

on_gateway_status(exchange, status, detail)[source]

This callback method should define the behaviors of the strategy (the api) when receiving Gateway status transition. Possible status values are ‘ReadyToTrade’, ‘Connecting’ and ‘Maintenance’:

ReadyToTrade - connected to trading system and ready to trade (get/send orders etc..) Connecting - The gateway is connecting and trying to get ready Maintenance - The gateway of some exchanges would report this status when it’s in maintenance

The api should only try and send order on an gateway if it is in ReadyToTrade status. Not doing so will result in a reject.

Parameters
  • exchange (Exchange) – exchange with gateway status transition. In the example above, it’s Exchange.Blade.

  • status (GtwStatus) – new gateway status. In the example above, it’s GtwStatus.ReadyToTrade.

  • detail (str) – attached details for this record.

Return type

None

Example

For example, when the gateway for Bybit is connected and ready to trade, the trading environment would invoke this callback method, with exchange Exchange.Bybit, status ReadyToTrade, and detail ‘null’. Then in this callback method, researchers can set up some internal states to tell the strategy (the api) that it’s able to send orders now.

on_instruments(instruments)[source]

Define the behavior of this strat when receiving all the information of instruments.

A common practice is to keep the instrument inside this strat class. This strat will log the instrument info here at the same time, to help researchers check all the information.

on_modify_level(exchange, instrument_id, side, level_id, new_qty, recv_ts)[source]

In this callback method, researchers should define the behavior of the strategy when receiving a modify-level market data. This method would be invoked by the trading environment, with information of this market data record.

Parameters
  • exchange (Exchange) – exchange with market data update (modify level).

  • instrument_id (int) – instrument ID with market data update.

  • side (Side) – side of the modify-level update.

  • level_id (int) – level ID of the update.

  • new_qty (Qty) – new quantity for this level.

  • recv_ts (int) – timestamp when receiving this record. Trading environment will pass current timestamp to it if unspecified.

Return type

None

Example

For example, when the feed handler receive a modify-level buy order update from exchange Bybit, for instrument ID 30000000000 (BTC-USD inverse perpetual in Bybit), with level ID 92360000 and qty 64628, the trading environment will invoke this callback method with exchange Exchange.Bybit, instrument ID 30000000000, side Side.Buy, level_id 92360000 and new_qty Qty(‘64628.00000000’).

on_modify_order_ack(exchange, client_id, new_price, new_qty, leaves_qty)[source]

When sending a modify order and get acknowledgement from the exchange, exchange will send a modify order ack message back to the trading environment. Then the environment will invoke this method with all the necessary information of a modify order ack message. So in this callback method, researchers should define the behavior of the strategy (the api) when the modify order requested is acknowledged.

Parameters
  • exchange (Exchange) – exchange of this order, e.g. Exchange.Bybit.

  • client_id (str) – client ID is an identification in the internal system.

  • new_price (Price) – new price of this modified order.

  • new_qty (Qty) – new quantity of this modified order.

  • leaves_qty (Qty) – remaining quantity of this order.

  • Note that new_price and new_qty are optional, but at least one of them must be present.

Return type

None

on_modify_order_reject(exchange, client_id, reject_code, reject_reason)[source]

In this callback method, researchers should define the behavior of the strategy (the api) when the modify order requested is rejected. Trading environment would invoke it with all the necessary information when the request is rejected by the exchange.

Parameters
  • exchange (Exchange) – exchange of this order.

  • client_id (str) – internal client ID.

  • reject_code (GtwRejectCode) – reject code from Gateway. Code represents the reason of rejection, e.g. 1000 = GtwNotReady (gateway is not ready for requests).

  • reject_reason (str) – attached reason for the rejection.

  • Note that ‘reject_reason’ may be empty. Strategy developers should familiarize themselves with

  • the meaning of GtwRejectCode, as it may help them react differently to different types of rejection.

Return type

None

on_modify_price_level(exchange, instrument_id, side, price, new_qty, recv_ts)[source]

In this callback method, researchers should define the behavior of the strategy when receiving a modify-price-level market data. This method would be invoked by the trading environment, with information of this market data record.

Parameters

exchange (Exchange) – exchange with market data update (modify price level).

instrument_id: int

instrumen id with market data update.

side: Side

side of the modify-price-level update.

price: Price

price level ID of the update.

new_qty: Qty

new quantity for this level.

recv_ts: int

timestamp when receiving this record. Trading environment will pass it as current timestamp if unspecified.

Return type

None

Example

For example, when the feed handler receive a modify-price-level buy order update from exchange Bybit, for instrument ID 30000000000 (BTC-USD inverse perpetual in Bybit), with price 9226.5 and new qty 64628, the trading environment will invoke this callback method with exchange Exchange.Bybit, instrument ID 30000000000, side Side.Buy, price Price(‘9226.50000000’).

on_new_order_ack(exchange, client_id, exchange_id, instrument_id, side, price, qty, leaves_qty, order_type, tif, exec_instr)[source]

When sending a new order and get acknowledgement from the exchange, exchange will send a new order ack message back to the trading environment. Then the environment will invoke this method with all the necessary information of a new order ack message. So in this callback method, researchers should define the behavior of the strategy (the api) when the new order requested is acknowledged.

Parameters
  • exchange (Exchange) – exchange of this order, e.g. Exchange.Bybit.

  • client_id (str) – client ID is an identification in the internal system.

  • exchange_id (str) – ID in external exchange.

  • instrument_id (int) – product of this order, represented via its ID, e.g. 30000000000 represents BTC-USD inverse perpetual in Bybit.

  • side (Side) – side of this order, Side.Buy or Side.Sell.

  • price (Price) – price of this order.

  • qty (Qty) – quantity of this order.

  • leaves_qty (Qty) – leave quantity (number of shares still open for execution) in this order.

  • order_type (OrderType) – type of this order, OrderType.Unset or OrderType.Limit (limit order)

  • tif (TIF) – time in force, TIF.Unset, TIF.GTC (Good to Cancel), TIF.IOC (Execute immediately, even partially, the rest is cancelled)

  • exec_instr (ExecInstructions) – execution instruction, ExecInstructions.Unset or ExecInstructions.PostOnly

Return type

None

on_new_order_reject(exchange, client_id, reject_code, reject_reason)[source]

In this callback method, researchers should define the behavior of the strategy (the api) when the new order requested is rejected. Trading environment would invoke it with all the necessary information when the request is rejected by the exchange.

Parameters
  • exchange (Exchange) – exchange of this order.

  • client_id (str) – internal client ID.

  • reject_code (GtwRejectCode) – reject code from Gateway. Code represents the reason of rejection, e.g. 1000 = GtwNotReady (gateway is not ready for requests).

  • reject_reason (str) – attached reason for the rejection.

  • Note that ‘reject_reason’ may be empty. Strategy developers should familiarize themselves with

  • the meaning of GtwRejectCode, as it may help them react differently to different types of rejection.

Return type

None

on_one_shot_five_secs()[source]
on_order_cancelled(exchange, client_id, unsolicited, engine_ts, recv_ts, cancel_code, cancel_reason)[source]

In this callback method, researchers should define the behavior of the strategy (the api) when the order is cancelled. Trading environment would invoke it, with all the necessary information, on the cancellation of an order, regardless of whether it was requested by the user or not.

Parameters
  • exchange (Exchange) – exchange of this order.

  • client_id (str) – internal client ID.

  • unsolicited (bool) – True means that the cancel was not requested by the user.

  • engine_ts (int) – timestamp at which the cancellation occurred. 0 if unknown.

  • recv_ts (int) – record reception timestamp. Trading environment will pass current timestamp to it if unspecified.

  • cancel_code (CancelCode) – cancel code, ‘Unset’, ‘CanceledByUser’ or ‘SelfTradePrevention’ (cancellation not requested by user)

  • cancel_reason (str) – reason for cancellation

Return type

None

on_order_execution(exchange, client_id, side, price, fill_qty, leaves_qty, exec_ts, recv_ts)[source]

In this callback method, researchers should define the behavior of the strategy (the api) when the order is executed (filled). Trading environment would invoke it on the execution (or fill) of an order, with all the necessary information.

Parameters
  • exchange (Exchange) – exchange of this order.

  • client_id (str) – internal client ID.

  • side (Side) – side of this order.

  • price (Price) – price level of this order.

  • fill_qty (Qty) – filled quantity of this order.

  • leaves_qty (Qty) – leave quantity (number of shares still open for execution) of this order.

  • exec_ts (int) – execution timestamp.

  • recv_ts (int) – record reception timestamp. Trading environment will pass current timestamp to it if unspecified.

Return type

None

on_order_status(exchange, instrument_id, client_ord_id, exch_ord_id, side, price, leaves_qty, status, order_type, tif, exec_instr)[source]

In this callback method, define behaviors of the strategy when receiving the order information for a single order as requested by a call to fetch_order_status().

Parameters
  • exchange (Exchange) – exchange of this order.

  • client_ord_id (str) – client ID is an identification in our internal system.

  • exch_ord_id (str) – ID in external exchange.

  • instrument_id (int) – instrument ID of this single order info.

  • side (Side) – side of this order, Side.Buy or Side.Sell.

  • price (Price) – price of this order.

  • leaves_qty (Qty) – leave quantity (number of shares still open for execution) in this order.

  • status (OrderStatus) – status of the order, e.g. filled

  • order_type (OrderType) – type of this order, OrderType.Unset or OrderType.Limit (limit order)

  • tif (TIF) – time in force, TIF.Unset, TIF.GTC (Good to Cancel), TIF.IOC (Execute immediately, even partially, the rest is cancelled)

  • exec_instr (ExecInstructions) – execution instruction, ExecInstructions.Unset or ExecInstructions.PostOnly

Return type

None

on_orders_info(exchange, orders)[source]

In this callback method, researchers should define the behavior of the strategy when receiving orders info. The trading environment would invoke this method with exchange and orders containing a list of open orders matching the filtering criteria passed when making the request.

Parameters
  • exchange (Exchange) – exchange of this orders info

  • orders (typing.List[Order]) – a list of open orders matching the filtering criteria passed when making the request

Return type

None

on_position_info(exchange, instrument_id, position)[source]

In this callback method, researchers should define the behavior of the strategy when receiving position info. For derivative products, this callback method would be invoked by the trading environment with the current position. For non derivative products, cf. on_wallet_info.

Parameters
  • exchange (Exchange) – exchange this info comes from.

  • instrument_id (int) – instrument ID of this position info.

  • position (Qty) – current position for this instrument.

Return type

None

on_public_trade(exchange, instrument_id, side, price, qty, exec_ts, recv_ts)[source]

This method invoked when a public trade is observed in the market data. So in this callback method, researchers should define the behavior of the strategy when receiving a public trade market data record.

Parameters
  • exchange (Exchange) – exchange with market data update (public trade).

  • instrument_id (int) – instrument ID with market data update.

  • side (Side) – taker side of this public trade.

  • price (Price) – price of this public trade.

  • qty (Qty) – quantity of this trade.

  • exec_ts (int) – trade execution timestamp.

  • recv_ts (int) – timestamp when receiving this record. Trading environment will pass current timestamp to it if unspecified.

Return type

None

Example

For example, when the feed handler receive a public trade from exchange Bybit, for instrument ID 30000000000 (BTC-USD inverse perpetual in Bybit), with sell taker side, price 9297 and qty 50, executed at 1594097430153 in exchange and received to us at 1594097430427, trading environment will invoke this method with exchange Exchange.Bybit, instrument ID 30000000000, side Side.Sell, price Price(‘9297.0’), qty Qty(‘50.0’), exec_ts 1594097430153 and recv_ts 1594097430427.

on_rate_limit_info(exchange, rate_limit, rate_remaining, reset_ts)[source]

Exchanges restrict the rate at which users may send requests. This callback would be invoked by the trading environment to informs the api of its rate usage. The gtw arbitrarily invokes this callback when 50% has been used, 90% and 100% has been used. When in the danger zone (90%+) the api (this callback) should take actions to reduce its exposure so as to not end up being locked out. Repetitive violation of these limits may result in a ban.

Parameters
  • exchange (Exchange) – exchange reaching this rate limit.

  • rate_limit (int) – overall rate limit of this exchange. In the example above, it’s 100, i.e. 100% is the limit of Bybit.

  • rate_remaining (int) – rate limit remaining. In the example above, it’s 10, i.e. have 10% rate left.

  • reset_ts (int) – timestamp when the usage of the rate limit is expected to be reset to zero.

Return type

None

Example

For example, when reaching a 90% rate limit for requests in Bybit exchange, and the timestamp of rate limit reset is 1594159619, the trading environment would invoke this method, with exchange Exchange.Bybit, rate_limit 100, rate_remaining 10 and reset_ts 1594159619. Since that it has already been in the danger zone, researchers should take actions, e.g. reduce the rate of the requests, etc., in this callback method.

on_recurring_two_secs()[source]
on_request_reject(exchange, reject_code, detail, rejected_rqst_type)[source]

In this callback method, researchers should define the behavior of the strategy when the request is rejected. This method would be invoked by the trading environment when a request sent by the strategy got rejection in gateway.

Parameters
  • exchange (Exchange) – exchange rejecting the request.

  • reject_code (GtwRejectCode) – reject code from Gateway. Code represents the reason of rejection, e.g. 1000 = GtwNotReady (gateway is not ready for requests).

  • detail (str) – attached details for the rejection.

  • rejected_rqst_type (str) – type of the rejected request.

Return type

None

on_start(params)[source]

This method is called by the environment when running ‘start’ command hook to start the strategy manually. During and after this call, the strat may request market data and/or gateway connections (if no manual start, do these in on_framework_connected).

Parameters

params (str) – params for ‘start’ command book. If manually starting the strat with some parameter setups via command hook, the parameters entered for the command hook would be passed here, as one string

Return type

None

on_stop(params)[source]

This method is called by the environment when the strategy should be stopped (running a ‘stop’ command hook manually). the strategy (the api) may take usual actions as long after this call, but it is expected to exit its trading logic.

Return type

None

on_strat_params_update(strat_params)[source]

This strat doesn’t need any strat params, so just put a log and skip it

on_unsupported_op(exchange, unsupported_msg_type)[source]
Some operations may only be supported by certain exchanges. If an exchange does not support a request sent

by the api, this callback method is invoked by the trading environment.

Parameters
  • exchange (Exchange) – exchange not supporting this operation.

  • unsupported_msg_type (str) – a string indicating the message type of this unsupported operation.

Return type

None

Example

For example, when sending a Modify Order request but not supported by Blockchain.com exchange, the trading environment would invoke this method, with exchange Exchange.Blockchain and unsupported_msg_type ‘ModifyOrder’.

on_wallet_info(exchange, coin, total_balance, available_balance)[source]

In this callback method, researchers should define the behavior of the strategy when receiving wallet info. Trading environment would invoke this method with the balance of the wallet for the listed coin. Note that for non spot products, this is what the strategy (the api) may use to derive positional changes.

Parameters
  • exchange (Exchange) – exchange this info comes from.

  • coin (str) – coin of this wallet info.

  • total_balance (Qty) – total balance of the listed coin in this wallet.

  • available_balance (Qty) – available balance of the listed coin in this wallet.

Return type

None

send_order_(side, price, qty, reason, exch, instrument_id, exec_instr=ExecInstructions.Unset, ord_type=OrderType.Limit, tif=TIF.GTC, close_position=False)[source]

Function used by strat to send a new order to our trading system. Order manager is used to determine if order can be sent and to keep track of order through it’s lifecycle

start_trading_command(params)[source]
stop_trading_command(params)[source]
update_orders_()[source]

This function implements the trading logic and updates orders as necessary