On Trade Event
This method will be called whenever a trade occurs on the SIP feed for trades that would be included in the minute bar (Sales Conditions).
Interface: def on_trade(self,
event,
md,
order,
service,
account):
pass
Event attributes returned in backtesting differ from those returned in live environments (see bottom of page)
If you plan to take your code live we recommend using md.L1. variables rather than event. variables in your code (to ensure smooth transfer to live environment).
ie use md.L1.ask rather than event.ask
Name | Type | Information |
---|---|---|
agr_ask_size | integer | Sum of all ask participants in the SIP data. |
agr_bid_size | integer | Sum of all bid participants in the SIP data. |
ask | float | Current best ask price. |
ask_exch | string | Best ask exchange ID. Exchange with largest size. ask_exch details *LIVE AND FORWARD ONLY* |
ask_size | integer | Number of lots (from md.stat.lot_size) at current best ask price. |
bid | float | Current best bid price. |
bid_exch | string | Best bid exchange ID. Exchange with largest size. bid_exch details *LIVE AND FORWARD ONLY* |
bid_size | integer | Number of lots (from md.stat.lot_size) at current best bid price. |
exchange | string | Exchange of the last trade. exchange details |
flags | integer | |
is_halted | boolean | Text description for halt from primary exchange. *LIVE AND FORWARD ONLY*. |
kind | integer | Event enum (12) representing the kind of event. *LIVE AND FORWARD ONLY*, represents the type of event (trade, quote, news, imbalance etc) |
last | float | Price of the last trade. |
last_size | integer | Number of shares of last trade. |
symbol | string | Event Symbol. In most cases symbol is obvious (self.symbol, md.symbol). If the script has subscribed to multiple symbols events, then event.symbol should be used to identify the symbol. |
timestamp | muts | Timestamp of the event |
trade_condition | string | Up to 4 conditions associated with the last trade. Treat this as a Python list (e.g. '6' in [ '6', 'X' ] == True is True. 'sales' information (from the trade data). |
- Event Symbol. In most cases symbol is obvious (self.symbol, md.symbol). If the script has subscribed to multiple symbols events, then event.symbol should be used to identify the symbol.
Print the attributes of event when on_trade() is called.
from cloudquant.interfaces import Strategy
class OnTradeExample(Strategy):
do_once = True
@classmethod
def is_symbol_qualified(cls, symbol, md, service, account):
return symbol in ['SPY', 'AAPL', 'FB', 'GOOGL', 'AMZN']
# create a variable to track the trade count
def on_start(self, md, order, service, account):
self.trade_count = 0
def on_trade( self, event, md, order, service, account):
self.trade_count += 1
def on_finish(self, md, order, service, account):
print(self.symbol + ' tradecount:\t ' + str(self.trade_count) + '\n')
Console
AAPL tradecount: 234393 AMZN tradecount: 67387 FB tradecount: 132999 GOOGL tradecount: 37270 SPY tradecount: 412893
Data Attribute returns for Event on Backtesting vs Live systems :
Backtesting = (timestamp, symbol, last, last_size, exchange, ask, ask_size, bid, bid_size, trade_condition, agr_bid_size, agr_ask_size, flags)
CQLive = (timestamp, symbol, last, last_size, exchange, trade_condition, trade_id, trade_direction, trade_position, minute_bar_index, ext_minute_bar_index, daily_bar_index, is_halted, is_excluded_from_minute_volume, is_excluded_from_minute_price, is_excluded_from_daily_volume, is_excluded_from_daily_price, minute_bar_shifted, num_trades)
Gr8py = (exchange, kind, last, last_size, trade_condition, trade_id, trade_positon, trade_direction, opening_trade, opening_trade_volume, opening_trade_timestamp, opening_trade, closing_trade, closing_trade_volume, closing_trade_timestamp, close, day_high, day_low, acc_volume, core_acc_volume, primary_acc_volume, rvol, vwap, ssr, bid_exch, bid, bid_size, agr_bid_size, ask_exch, ask, ask_size, is_halted, lower_limit_price_band, upper_limit_price_band, symbol, micros_in_queue)