Mariner Backtesting - md.bar.daily_by_index()

bar. Overview:
  • Return a daily-bars slice structure by requesting indexes into an array of market bars.
Interface:
 daily_by_index(start=-1,
               end=None)

Sample - Calling method

 md.bar.daily_by_index(start=-10)
Parameters:
Name Type Default Information
start integer -1 Offset. May be a negative integer as a reference to now (e.g. -10 ... 10 days ago)
end integer None Offset. May be a negative integer as a reference to now (e.g. -10 ... 10 days ago)
Returns:

Daily BarView - tuple of mostly arrays

Daily BarView Attributes:

Name Type Information
askvol Array[integer] items in the array - the volume of trades that occurred at the ask during timeframe
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
closing_trade Array[float] closing price for the symbol's primary exchange
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
opening_trade Array[float] opening price for the symbol's primary exchange
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] volume weighted average price(vwap) of the bar
Related Links:
  • There are a lot of bar examples under Knowledge.
Remarks:
  • Bars are formed - not forming. Forming data is part of L1.
  • Index doesn't use time to filter data.
  • The returned bar slice may contain fewer elements than expected due to non-trading days and empty bars (days where the symbol had no trades).
  • Daily bar data is static information (it doesn't change during the trading day). You should only need to request a symbol's daily bar data once in a script (example - in on_start() or in a function )
  • When using this method, it may help to think of the daily 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".
  • 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, 1 year of daily bars is available (starting 1/1/11).

How is this different from md.bar.daily(): - md.bar.daily_by_index(-10) vs md.bar.daily(-10) - md.bar.daily_by_index(-10) will have up to 10 items no matter how many days it needs to go back - md.bar.daily(-10) returns all available data for the previous 10 trading days. - You will see more difference with lower volume stocks

Working Example:

To see the difference between md.bar.daily_by_index(-10) and md.bar.daily(-10) Backtest for GALTW on 8/26/16 from 4:00-4:01.

 import itertools
from cloudquant.interfaces import Strategy


class DailyByIndexExample(Strategy):
    @classmethod
    def is_symbol_qualified(cls, symbol, md, service, account):
        return symbol =='AAL'


    def on_start(self, md, order, service, account):
        # get bars

        print self.symbol

        print '\n\n..........................\n\nmd.bar.daily_by_index(start=-20)'
        bars = md.bar.daily_by_index(start=-20)

        print '\nget the last high value'
        print 'bars.high[-1]\t-\t', bars.high[-1]

        print '\nget the first low value'
        print 'bars.low[0]\t-\t', bars.low[0]

        print
        for timestamp in bars.timestamp:
            print service.time_to_string(timestamp, '%Y-%m-%d')


        print '\n\n..........................\n\nmd.bar.daily(-20)'
        bars = md.bar.daily(start=-20)

        print '\nget the last high value'
        print 'bars.high[-1]\t-\t', bars.high[-1]

        print '\nget the first low value'
        print 'bars.low[0]\t-\t', bars.low[0]

        print
        for timestamp in bars.timestamp:
            print service.time_to_string(timestamp, '%Y-%m-%d')    



        service.terminate()

Console

AAL

..........................

md.bar.daily_by_index(start=-20)

get the last high value
bars.high[-1]   -   35.75

get the first low value
bars.low[0] -   28.1900005341

2016-07-05
2016-07-06
2016-07-07
2016-07-08
2016-07-11
2016-07-12
2016-07-13
2016-07-14
2016-07-15
2016-07-18
2016-07-19
2016-07-20
2016-07-21
2016-07-22
2016-07-25
2016-07-26
2016-07-27
2016-07-28
2016-07-29
2016-08-01


..........................

md.bar.daily(-20)

get the last high value
bars.high[-1]   -   35.75

get the first low value
bars.low[0] -   28.1900005341

2016-07-05
2016-07-06
2016-07-07
2016-07-08
2016-07-11
2016-07-12
2016-07-13
2016-07-14
2016-07-15
2016-07-18
2016-07-19
2016-07-20
2016-07-21
2016-07-22
2016-07-25
2016-07-26
2016-07-27
2016-07-28
2016-07-29
2016-08-01