All strategy modules are child classes of TimeIterator, which is called via c_tick() every second.
What this means is, a running strategy module is called every second via its c_tick() method to check on the markets and wallets, and decide whether it should perform any trades or not. One way to think about it is that a strategy module acts like it's watching a movie frame-by-frame via c_tick(), and reacts to what it sees in real time via trading actions.
If you're reading or writing a strategy module, the c_tick() function should be treated as the entry point of a strategy module. If you're reading a strategy module's code, c_tick() should be where you start. If you're writing a new strategy module, c_tick() is also going to where you start writing the important bits of your strategy.
The StrategyBase class comes with a set of interface functions for handling market events from associated ConnectorBase objects, which may be overridded by child classes to receive and process market events.
StrategyBase includes pre-defined logic for creating and cancelling trading orders - which are the primary ways for a strategy to interact with associated markets.
It is highly encouraged to use these functions to create and remove orders, rather than calling functions like c_buy() and c_sell() in ConnectorBase objects directly - since the functions from StrategyBase provides order tracking functionalities as well.
Creates a buy or a sell order in the market specified by market_trading_pair_tupleand returns the order ID string.
Arguments
market_trading_pair_tuple: a MarketTradingPairTuple object specifying the ConnectorBase object and trading pair to create the order for.
amount: a Decimal object, specifying the order size in terms of the base asset.
order_type: an optional OrderType enum specifying the order type. Default value is OrderType.MARKET.
price: an optional Decimal object, specifying the price for a limit order. This parameter is ignored if order_type is not OrderType.LIMIT or OrderType.LIMIT_MAKER.
expiration_seconds: an optional number, which specifies how long a limit should automatically expire. This is only used by Ethereum-based decentralized exchanges like Radar Relay where active order cancellation costs gas. By default, passive cancellation via expiration is used on these exchanges.
Each StrategyBase object comes with an internal attribute _sb_order_tracker, which is an OrderTracker object. The OrderTracker object is responsible for tracking all active and in-flight orders created by the StrategyBase object, and also all in-flight order cancels.
When writing or modifying a strategy module, you can use the built-in OrderTracker object to query the active or in-flight orders / cancels you currently have. It's useful for preventing issuing duplicate orders or order cancels.
Below are some of the user functions or properties under OrderTracker that you can use:
active_maker_orders property
Returns a list of still active limit orders, with their market object.
Returns a list of in-flight or active market orders, with their market object. This is useful for decentralized exchanges where market orders may take a minute to settle due to block delay.
A BudgetChecker takes an OrderCandidate and fills in fees to be paid for this particular order (in pre-defined tokens). Then checks if, after accounting for the fees, the account balances allow for a placement of this order, and if not, adjusts the order amount accordingly.
Fees can be payable in the base tokens, quote tokens, or 3rd party tokens.
In general, if an order opens a position, fees will be charged as an additional cost of the order, and if an order closes a position, fees will be deducted from the returns.
A few exchanges however don't follow this principle. Strategies however don't have to handle this and should rely on the BudgetChecker obtained from the exchange to calculate fees the correct way.
Once the candidate order has been sized by the BudgetChecker, the strategy can examine the sized order to get more information such as OrderCandidate.collateral_dict to get a dictionary of the costs associated with the order, or OrderCandidate.potential_returns to get an idea of the token and amount of the returns associated with the order.
Is intended to be a single universal solution for fee accounting and checking feasibility of OrderCandidates.
Provides utilities for strategies to check the potential impact of OrderCandidates on user account balances.
Mainly used to determine if sufficient balances are available to place a set of strategy-proposed orders.
It can work with a single OrderCandidate or a list of OrderCandidates.
In case of multiple OrderCandidates the BudgetChecker verfies if the set of orders as a whole is feasible.
The BudgetChecker also locks in collateral required for orders and adjusts collateral available for future OrderCandidates.
budget_checker=market_info.market.budget_checkerorder_candidate=OrderCandidate(trading_pair=market_info.trading_pair,is_maker=False,order_type=OrderType.LIMIT,order_side=TradeType.BUY,amount=order_amount,price=order_price,)adjusted_candidate_order=budget_checker.adjust_candidate(order_candidate,all_or_none=True)ifadjusted_candidate_order.amount<order_amount:# Order cannot be placedelse:# Order can be placed