Mariner Backtesting -  md.bar.minute()

Minute Bar Market Data

bar. Overview:
  • minute() is a method of bar. It is used to retrieve the desired minute bar data. It returns a minute bars slice structure that goes back one or more minute(s).
Interface:
 minute(start=-1,
       end=None,
       include_empty=False,
       include_extended=False,
       bar_size=1,
       today_only=True)

Sample - Calling method

 md.bar.minute(start=-10)
Parameters:
Name Type Default Information
start integer or muts -1 Offset. May be a negative integer as a reference to now (e.g. -10 ... 10 bars ago), or a specific timestamp.
  • Negative integer. This represents 'bars back from now'.
  • Timestamp. Usually computed by service.time() or service.time_interval().
end integer or muts None Offset. May be a negative integer as a reference to now (e.g. -10 ... 10 bars ago), a specific timestamp, or None. A value of None indicates to include the most-recently-formed bar.
  • Negative integer. This represents 'bars back from now'. The default is None, which represents the most-recently formed minute-bar.
  • Timestamp. Usually computed by service.time() or service.time_interval().
include_empty boolean False If True, return bars for empty minutes instead of skipping over them, and check the "valid" property on the resulting bar slice to verify whether a bar is empty or not.
  • False - don't return empty bar data
  • True - return NaN if no trades happened in bar.
include_extended boolean False Include pre/post minute bars
  • False - don't include pre/post market bars
  • True - include pre/post market bars
bar_size integer 1 Minute bar size. Based on 9:30 start time.
today_only boolean True Bars for today only.
  • True - include bars for today only
  • False - include today and other days' minute bars
Returns:

Minute BarView - tuple of mostly arrays

Minute BarView Attributes:

Name Type Information
askvol Array[integer] items in the array - the volume of trades that occurred at the ask during timeframe
avgdelta Array[float] Average change in price between trades during time period. Live and Forward only, not Backtesting. Seems to return 0.00.
bidvol Array[integer] the volume of trades that occurred at the bid during timeframe
bvwap Array[float] the vwap for the bar
close Array[float] final, or closing price for the bar
count Array[integer] number of items contributing to the bar
high Array[float] highest price for the bar
length integer time length for the bar
low Array[float] lowest price for the bar
open Array[float] first qualifying trade of the bar
spread Array[float] average difference between the ask and the bid for the bar
symbol string symbol for the bar
timestamp Array[integer] timestamps for the beginning of each bar
valid Array[bool] whether or not bar is valid
volume Array[integer] volume for the bar
vwap Array[float] the vwap for the day so far at the time this bar ended (not the vwap for this bar - see bvwap for that)
Related Links:
  • There are a lot of bar examples under Knowledge.
Remarks:
  • Bars are formed - not forming. Forming data is part of L1.
  • Both start and end must be timestamps or negative integers (e.g. service.time(9, 45) - 9:45) .
  • When using this method, it may help to think of the minute bars as snapshots of trading data from previous minutes relative to the current simulation time. When using the bar_size argument, however, this analogy breaks down since minute-offsets are computed relative to the market-open time.
  • By default, if there is no market data on a given minute between start and end, the returned array will contain fewer items than expected. To retrieve the exact number of minute bars requested, set include_empty=True. When include_empty=True, the valid property (see below) provides a boolean which indicates if the bar was empty (valid == True means "not empty").
  • When using include_empty, be aware the max() and min() and the first item in the array is NaN can cause the return value to be NaN even if there are other values besides NaN. See http://stackoverflow.com/questions/4237914/python-max-min-builtin-functions-depend-on-parameter-order
  • Set today_only=False to retrieve bars from previous market days.
  • set include_extended=True to retrieve bars outside of core market hours.
  • The returned slice can be accessed via .property, where property may be one of the following: symbol, length, timestamp, open, high, low, close, volume, vwap, bvwap, spread, bidvol, askvol, count, avgdelta, valid.
  • At the moment one any given testing/trading day, 3 days of minute bars is available (starting 1/1/11).
Working Example:
 from cloudquant.interfaces import Strategy


class MinuteBarExample(Strategy):

    @classmethod
    def is_symbol_qualified(cls, symbol, md, service, account):
        return symbol == "AAL"

    def on_start(self, md, order, service, account):
        #symbol and timestamp
        print(self.symbol + "\n" + service.time_to_string(service.system_time) + "\tin on_start()\n\n")

        # count number of bars. Used to terminate early after 3 bars
        self.bar_count = 0

    def on_minute_bar(self, event, md, order, service, account, bar):
        print("on_minute_bar\t" + service.time_to_string(event.timestamp)[12:21])

        one_minute = bar.minute(include_extended = True)
        print("The low prices: " + str(one_minute.low))
        print("The high prices: " + str(one_minute.high) + "\n\n")

        # increment variable
        self.bar_count += 1
        # terminate after 3 bars
        if self.bar_count >= 3:
            service.terminate()

Console

AAL
2016-11-01 09:30:00.000000  in on_start()

on_minute_bar   9:30:00.0
The low prices: [ 40.65000153]
The high prices: [ 40.65000153]


on_minute_bar   9:31:00.0
The low prices: [ 40.50999832]
The high prices: [ 40.56999969]


on_minute_bar   9:32:00.0
The low prices: [ 40.5]
The high prices: [ 40.63999939]

 

 

Because the Start and End can be MUTS you can put in any time to get bars back between any periods we have available.

So on any trading day you could get the bars for the 6 hours after the previous day close as follows...

 def on_start(self, md, order, service, account): 
    mystart = service.get_market_hours(date_offset=-1)[1] # end of prev trading day
    myend = mystart + service.time_interval(6)  # plus 6 hours 
    print "backtest date",service.time_to_string(service.system_time)
    print "previous trading day end",service.time_to_string(mystart)
    print "previous trading day end plus 6 hours ",service.time_to_string(myend) 
    mybars = md.bar.minute(start=mystart, end=myend, include_extended=True, today_only=False)