Mariner Backtesting - L1 Market Data

Level 1 Market Data

md. (object) Overview:

A snapshot of the current market state (Level 1 Data).

Data Attributes:

Sample - Access Data Attribute

 md.L1.abs_net_change_percent_atr
Name Type Special Information
abs_net_change_percent_atr float NaN calculated:
if stat.atr > 0:
    return abs(net_change) / stat.atr * 100
else:
    return float('NaN')
acc_volume integer 0 Accumulated volume for current trading day (including extended hours). Uses trades that meet criteria for 'Last Trade Price'.
agr_ask_size integer 0 Sum of all ask participants in the SIP data.
agr_bid_size integer 0 Sum of all bid participants in the SIP data.
ask float NaN Current best ask price.
ask_change float NaN Difference between previous and current ask. Positive for increase and negative for decrease. Replaced ask_direction.
ask_exchange string "" Best ask exchange ID. Exchange with largest size. ask_exchange details
ask_size integer 0 Number of lots (from md.stat.lot_size) at current best ask price.
bid float NaN Current best bid price.
bid_change float NaN Difference between previous and current bid. Positive for increase and negative for decrease. Replaced bid_direction.
bid_exchange string "" Best bid exchange ID. Exchange with largest size. bid_exchange details
bid_size integer 0 Number of lots (from md.stat.lot_size) at current best bid price.
change_from_open float 0.0 calculated:
last - open
close float NaN Closing price for the current day. ??? should this be daily_close since part of bar ???.
closing_trade float NaN The actual closing print for the primary exchange. (An example for when this may be used is when looking for a symbol that has not printed a close even though the market has closed.)
core_acc_volume integer 0 Accumulated volume during core hours only (excludes extended hours). Uses trades that meet criteria for 'Last Trade Price'.
daily_askvol integer 0 Volume traded at or above the ask during the day. Includes extended hours.
daily_bidvol integer 0 Volume traded at or below the bid during the day. Includes extended hours.
daily_count integer 0 Number of trades today that impact 'Last Trade Price'.
daily_high float NaN Highest trade price seen since the strategy started.
daily_high_timestamp muts 0 Timestamp of highest trade price.
daily_low float NaN Lowest price seen since the strategy started.
daily_low_timestamp muts 0 Timestamp of lowest trade price.
daily_spread float NaN Average of the difference between bid-ask for the day.
daily_vwap float NaN Volume-weighted average price.
exchange string "" exchange ID where stock was last traded. exchange details
gap float 0.0 calculated:
open - stat.prev_close
halt_reason string "" Text description for halt from primary exchange.
halt_time muts 0 Defaults to 0.
is_halted boolean False Boolean (True/False) for whether or not a symbol is halted.
last float NaN Last trade price.
last_in_range float NaN calculated:
if daily_high - daily_low > 0.0001:
    return (last - daily_low) / (daily_high - daily_low) * 100
else:
    return float('NaN')
last_size integer 0 Number of shares in last trade.
lower_limit_price_band float NaN The lower limit price band of LU/LD.
minute_askvol integer 0 Volume traded at or above the ask during the current minute (forming bar).
minute_bidvol integer 0 Volume traded at or below the bid during the current minute (forming bar).
minute_close float NaN Last trade price after closing.
minute_count integer 0 Number of trades during the current minute (forming bar) that impact 'Last Trade Price'.
minute_high float NaN Highest price traded during the current minute (forming bar).
minute_low float NaN Lowest price traded during the current minute (forming bar).
minute_open float NaN Opening price of the current minute (forming bar).
minute_spread float NaN Average of the difference between bid-ask during the current minute (forming bar).
minute_start_timestamp muts 0 The timestamp of the most recent minute bar start time. Use minute_start_timestamp to know when to call md.bar.minute(). Example
minute_volume integer 0 Quantity of shares traded during the current minute (forming bar).
minute_vwap float NaN Volume-weighted average price during the current minute (forming bar).
net_change float 0.0 calculated:
last - stat.prev_close
net_percent_change float 0.0 calculated:
if stat.prev_close > 0:
    return (last / stat.prev_close - 1) * 100
else:
    return float('NaN')
open float NaN Opening price.
opening_trade float NaN The actual opening print for the primary exchange. (An example for when this may be used is when looking for a symbol that has not opened yet even though the market has opened.)
percent_change_from_open float NaN calculated:
if open > 0:
    return (last / open - 1) * 100
else:
    return float('NaN')
primary_acc_volume integer 0 Accumulated volume on the primary exchange.
rvol float NaN Relative volume. Multiplier of the average 21 day volume based on time of day with an adjustment for simple cosine wave (higher volumes at beginning and end).
sliding_gap float NaN calculated:
if math.isnan(open):
    return last - prev_close
else:
    return open - prev_close
sliding_gap_percent float NaN calculated:
if stat.prev_close > 0:
    return sliding_gap / stat.prev_close * 100
else:
    return float('NaN')
ssr integer 0 Short stock restriction code (not available in backtesting):

 

  • 0 == Not in effect
  • 1 == Activated
  • 2 == Continued

To short a stock with a restriction of 1 or 2, the stock must be on a plus bid. There are order types to help in these situations. In live trading, orders are subject to the firm's position when marking sell orders (long or short). Important to be aware of when exiting long positions with on open and on close orders.

timestamp muts 0 Current market time (integer in milliseconds unix timestamp [muts]). Use service.time_to_string() for human readable.
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).
trade_direction integer 0 >??? Difference from last trade. What is the value for the first trade of the day???.
trade_position integer 0 Integer between 1-7 inclusive indicating the position relative to bid and ask prices. Relative Trade Position number code describing the last trades relative position to the NBBO (1-5), plus two special codes for locked and cross markets (6 & 7).

 

  • 1 - Trade Below Bid
  • 2 - Trade At Or Near Bid (Inside the spread)
  • 3 - Trade Between Bid Ask
  • 4 - Trade At Or Near Ask (Inside the spread)
  • 5 - Trade Above Ask
  • 6 - Trade Locked
  • 7 - Trade Cross

See cloudquant.interfaces.NBBOPositions enum for more info.

upper_limit_price_band float NaN The upper limit price band of LU/LD.
Remarks:
  • Level 1 attributes are updated when a symbol has a trade event (time and sales event).
  • Note: The following attributes are not available in backtesting, and in forward and live environments, on_trade() is not called when they change:
    • halt_reason
    • halt_time
    • is_halted
Working Example:

Print attributes of md.L1 when on_trade() is called.

 from cloudquant.interfaces import Strategy


class L1Data(Strategy):
    @classmethod
    def on_strategy_start(cls, md, service, account):
        print service.time_to_string(service.system_time, '%Y-%m-%d')

    @classmethod
    def is_symbol_qualified(cls, symbol, md, service, account):
        return symbol == 'SPY'

    def on_start(self, md, order, service, account):
        print self.symbol

    def on_trade(self, event, md, order, service, account):
        print '\nin on_trade\n\t%s\n' % service.time_to_string(service.system_time, '%H:%M:%S.%f')

        print '\n\nmd.L1\n'

        print ' md.L1.abs_net_change_percent_atr    -   ', md.L1.abs_net_change_percent_atr
        print ' md.L1.acc_volume    -   ', md.L1.acc_volume
        print ' md.L1.agr_ask_size  -   ', md.L1.agr_ask_size
        print ' md.L1.agr_bid_size  -   ', md.L1.agr_bid_size
        print ' md.L1.ask   -   ', md.L1.ask
        print ' md.L1.ask_change    -   ', md.L1.ask_change
        print ' md.L1.ask_exchange  -   ', md.L1.ask_exchange
        print ' md.L1.ask_size  -   ', md.L1.ask_size
        print ' md.L1.bid   -   ', md.L1.bid
        print ' md.L1.bid_change    -   ', md.L1.bid_change
        print ' md.L1.bid_exchange  -   ', md.L1.bid_exchange
        print ' md.L1.bid_size  -   ', md.L1.bid_size
        print ' md.L1.change_from_open  -   ', md.L1.change_from_open
        print ' md.L1.close -   ', md.L1.close
        print ' md.L1.closing_trade -   ', md.L1.closing_trade
        print ' md.L1.core_acc_volume   -   ', md.L1.core_acc_volume
        print ' md.L1.daily_askvol  -   ', md.L1.daily_askvol
        print ' md.L1.daily_bidvol  -   ', md.L1.daily_bidvol
        print ' md.L1.daily_count   -   ', md.L1.daily_count
        print ' md.L1.daily_high    -   ', md.L1.daily_high
        print ' md.L1.daily_high_timestamp  -   ', md.L1.daily_high_timestamp
        print ' md.L1.daily_low -   ', md.L1.daily_low
        print ' md.L1.daily_low_timestamp   -   ', md.L1.daily_low_timestamp
        print ' md.L1.daily_spread  -   ', md.L1.daily_spread
        print ' md.L1.daily_vwap    -   ', md.L1.daily_vwap
        print ' md.L1.exchange  -   ', md.L1.exchange
        print ' md.L1.gap   -   ', md.L1.gap
        print ' md.L1.halt_reason   -   ', md.L1.halt_reason
        print ' md.L1.halt_time -   ', md.L1.halt_time
        print ' md.L1.is_halted -   ', md.L1.is_halted
        print ' md.L1.last  -   ', md.L1.last
        print ' md.L1.last_in_range -   ', md.L1.last_in_range
        print ' md.L1.last_size -   ', md.L1.last_size
        print ' md.L1.lower_limit_price_band    -   ', md.L1.lower_limit_price_band
        print ' md.L1.minute_askvol -   ', md.L1.minute_askvol
        print ' md.L1.minute_bidvol -   ', md.L1.minute_bidvol
        print ' md.L1.minute_close  -   ', md.L1.minute_close
        print ' md.L1.minute_count  -   ', md.L1.minute_count
        print ' md.L1.minute_high   -   ', md.L1.minute_high
        print ' md.L1.minute_low    -   ', md.L1.minute_low
        print ' md.L1.minute_open   -   ', md.L1.minute_open
        print ' md.L1.minute_spread -   ', md.L1.minute_spread
        print ' md.L1.minute_start_timestamp    -   ', md.L1.minute_start_timestamp
        print ' md.L1.minute_volume -   ', md.L1.minute_volume
        print ' md.L1.minute_vwap   -   ', md.L1.minute_vwap
        print ' md.L1.net_change    -   ', md.L1.net_change
        print ' md.L1.net_percent_change    -   ', md.L1.net_percent_change
        print ' md.L1.open  -   ', md.L1.open
        print ' md.L1.opening_trade -   ', md.L1.opening_trade
        print ' md.L1.percent_change_from_open  -   ', md.L1.percent_change_from_open
        print ' md.L1.primary_acc_volume    -   ', md.L1.primary_acc_volume
        print ' md.L1.rvol  -   ', md.L1.rvol
        print ' md.L1.sliding_gap   -   ', md.L1.sliding_gap
        print ' md.L1.sliding_gap_percent   -   ', md.L1.sliding_gap_percent
        print ' md.L1.ssr   -   ', md.L1.ssr
        print ' md.L1.timestamp -   ', md.L1.timestamp
        print ' md.L1.trade_condition   -   ', md.L1.trade_condition
        print ' md.L1.trade_direction   -   ', md.L1.trade_direction
        print ' md.L1.trade_position    -   ', md.L1.trade_position
        print ' md.L1.upper_limit_price_band    -   ', md.L1.upper_limit_price_band

        service.terminate()

Console

2016-09-07
SPY

in on_trade
    09:30:00.041000



md.L1

    md.L1.abs_net_change_percent_atr    -   12.7547559738
    md.L1.acc_volume    -   502126
    md.L1.agr_ask_size  -   1
    md.L1.agr_bid_size  -   158
    md.L1.ask   -   218.830001831
    md.L1.ask_change    -   0.00999450683594
    md.L1.ask_exchange  -
    md.L1.ask_size  -   1
    md.L1.bid   -   218.820007324
    md.L1.bid_change    -   0.010009765625
    md.L1.bid_exchange  -
    md.L1.bid_size  -   38
    md.L1.change_from_open  -   0.0
    md.L1.close -   nan
    md.L1.closing_trade -   nan
    md.L1.core_acc_volume   -   200
    md.L1.daily_askvol  -   200
    md.L1.daily_bidvol  -   0
    md.L1.daily_count   -   1142
    md.L1.daily_high    -   218.839996338
    md.L1.daily_high_timestamp  -   1473255000036000
    md.L1.daily_low -   218.839996338
    md.L1.daily_low_timestamp   -   1473255000036000
    md.L1.daily_spread  -   0.00999450683594
    md.L1.daily_vwap    -   218.839996338
    md.L1.exchange  -   Z
    md.L1.gap   -   -0.190811157227
    md.L1.halt_reason   -
    md.L1.halt_time -   0
    md.L1.is_halted -   False
    md.L1.last  -   218.839996338
    md.L1.last_in_range -   nan
    md.L1.last_size -   200
    md.L1.lower_limit_price_band    -   nan
    md.L1.minute_askvol -   200
    md.L1.minute_bidvol -   0
    md.L1.minute_close  -   218.839996338
    md.L1.minute_count  -   1
    md.L1.minute_high   -   218.839996338
    md.L1.minute_low    -   218.839996338
    md.L1.minute_open   -   218.839996338
    md.L1.minute_spread -   0.00999450683594
    md.L1.minute_start_timestamp    -   1473255000000000
    md.L1.minute_volume -   200
    md.L1.minute_vwap   -   218.839996338
    md.L1.net_change    -   -0.190811157227
    md.L1.net_percent_change    -   -0.0871181488037
    md.L1.open  -   218.839996338
    md.L1.opening_trade -   nan
    md.L1.percent_change_from_open  -   0.0
    md.L1.primary_acc_volume    -   0
    md.L1.rvol  -   nan
    md.L1.sliding_gap   -   -0.190811157227
    md.L1.sliding_gap_percent   -   -0.0871161296964
    md.L1.ssr   -   0
    md.L1.timestamp -   1473255000036000
    md.L1.trade_condition   -   F
    md.L1.trade_direction   -   1
    md.L1.trade_position    -   5
    md.L1.upper_limit_price_band    -   nan