Mariner Backtesting - md.bar.minute_by_index()

bar. Overview:
  • minute_by_index() is a method of bar. It is used to retrieve the desired minute bar data using indexes into an array of market bars.
Interface:
 minute_by_index(start=-1,
                end=None,
                include_extended=False,
                today_only=True)

Sample - Calling method

 md.bar.minute_by_index( 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 minutes ago), or a specific timestamp.
  • Negative integer. This represents 'minutes 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 minutes ago), a specific timestamp, or None. A value of None indicates to include the most-recently-formed bar.
  • Negative integer. This represents 'minutes 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_extended boolean False Include pre/post minute bars
  • False - don't include pre/post market bars
  • True - include pre/post market bars
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.
  • When using this method, it may help to think of the minute bars as a large array that can be indexed into via the start and end arguments. Positive integers are indexed from the "beginning of the array", and negative integers are from the "end of the array".
  • 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).

How is this different from md.bar.minute(): - md.bar.minute_by_index(-10) vs md.bar.minute(-10) - md.bar.minute_by_index(-10) will have up to 10 items no matter how many minutes it needs to go back (limited by the parameters of the method) - md.bar.minute(-10) returns all available data for the previous 10 minutes. - You will see more difference with lower volume stocks

Working Example:
 from cloudquant.interfaces import Strategy


class MinuteByIndexExample(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")

    def on_minute_bar(self, event, md, order, service, account, bar):

        # bar data for the past 2 minutes
        bar_2m = bar.minute_by_index(-2)

        # make sure the bar data goes back 2 minute to avoid an error
        if len(bar_2m.high) > 1:
            # if the lowest price in the past minute is greater than the highest price of the minute before
            # if the price is steadily increasing
            # no position is currently held
            if bar_2m.low[1] > bar_2m.high[0] and account[self.symbol].position.shares == 0:
                print(service.time_to_string(event.timestamp) + "\tin on_minute_bar\n\n")

                # buy order
                order.algo_buy(self.symbol, "market", intent="init", order_quantity=100)

                # bar_2m.high[0] gives the high price from 2 minutes ago ([0] accesses the earliest bar)
                # bar_2m.low[1] gives the low price for the most recent minute
                print("The highest price in the bar from 2 minutes ago was: " + str(bar_2m.high[0]))
                print("The lowest price in the bar from the last minute was: " + str(bar_2m.low[1]))
                print("An order has been sent")

            # sell the shares when $20 will be made, or $10 will be lost.
            elif account[self.symbol].unrealized_pl.entry_pl > 20 or account[self.symbol].unrealized_pl.entry_pl < -10:
                # sell the shares
                order.algo_sell(self.symbol, 'market', intent='exit')
                print '\nYour shares in ' + self.symbol + ' have been sold!'
                service.terminate()

Console

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

2016-11-08 11:35:00.329000  in on_minute_bar


The highest price in the bar from 2 minutes ago was: 41.4399986267
The lowest price in the bar from the last minute was: 41.4500007629
An order has been sent

Your shares in AAL have been sold!