Mariner Backtesting - bar.start

Start Overview:

In this lesson, you will learn to use the bar parameter to control the amount of data you receive when you call a bar method.

What does start do?
 bar.minute(start = -5)
  • When passed a timestamp, all bars that are completely formed after the indicated start time are returned.
  • When passed a negative index, such as -5, it returns the past 5 bars.
  • Depending on bar_size, start=-5 could return data from 5 minutes ago (1 minute bars) or over an hour ago(20+ minute bars).
  • Start controls how much data is returned by the method call.
  • Start does not affect how much data is in each bar, but how many bars you receive.

Using Timestamps:

The start time (timestamp) in combination with bar_size can make predicting data tricky.

At 9:37 I request all 4 minute bars after 9:30 and print the timestamp attribute of the bar.

bar_4m = bar.minute(start=service.time(9,30,0,20), bar_size=4)
print("\nTimes for bar: ")
for time in bar_4m.timestamp:
    print(service.time_to_string(time)[11:19])

Only a single 4 minute bar bar is completely formed from 9:00-9:37.

Times for bar:
09:30:00

 

However, the same code executed at 9:38 will return 2 bars (therefore 2 timestamps), because a second 4 minute bar is completely formed(at 9:38).

Times for bar:
09:30:00
09:34:00

 

Working Example:

Negative Integer Start

 # Copyright Cloudquant, LLC. All right reserved.
from cloudquant.interfaces import Strategy


class timestampStart(Strategy):

    @classmethod
    def is_symbol_qualified(cls, symbol, md, service, account):

        # only run the script on AAL
        return symbol == "AAL"

    def on_start(self, md, order, service, account):

        # symbol and timestamp
        print(self.symbol + "\n---------- in on_start " + service.time_to_string(service.system_time))


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

        # if the time is after 2:00
        if event.timestamp > service.time(14):
            print ("\n---------- in on_minute_bar " + service.time_to_string(event.timestamp)[11:19])

            # initialize ea variable to count the number of bars
            bar_count = 1

            # request the 5 most recent 45 minute bars
            self.bar_45m = bar.minute(start=-5, bar_size=45)

            # for each timestamp in self.bar_45, print the starting time using service.time_to_string
            print("\nStarting times for bars in bar data:\n")
            for time in self.bar_45m.timestamp:

                # print the bar # and timestamp
                print("Bar " + str(bar_count) + " - " + service.time_to_string(time)[11:19])

                # increment counting variable
                bar_count +=1

            service.terminate()

Console

AAL
---------- in on_start 2016-09-07 09:30:00.000000

---------- in on_minute_bar 14:00:00

Starting times for bars in bar data:

Bar 1 - 10:00:00
Bar 2 - 10:45:00
Bar 3 - 11:30:00
Bar 4 - 12:15:00
Bar 5 - 13:00:00

Notice that even though start is set after 9:30, a bar starting at 9:30 is printed. This happens because start only cares about when the bar finishes forming.

Working Example 2:

Timestamp Start

 from cloudquant.interfaces import Strategy


class integerStart(Strategy):

    @classmethod
    def is_symbol_qualified(cls, symbol, md, service, account):

        # only run the script on AAL
        return symbol == "AAL"

    def on_start(self, md, order, service, account):

        # symbol and timestamp
        print(self.symbol + "\n---------- in on_start " + service.time_to_string(service.system_time))

        # create a timer at 9:37 that repeats every minute
        service.add_time_trigger(service.time(9, 37, 0, 20), repeat_interval=service.time_interval(minutes=1))

        # variable used to track bars printed
        self.bar_count = 0

    def on_minute_bar(self, event, md, order, service, account, bar):
        if event.timestamp > service.time(9,37):
            print ("\n---------- in on_minute_bar " + service.time_to_string(event.timestamp)[11:19])

            # request all 4 minute bars that finish forming after 9:30
            bar_4m = bar.minute(start=service.time(9, 30, 0, 20), bar_size=4)

            # print the number of bars
            print '\n', len(bar_4m.timestamp), 'bars'

            # print the times
            print("bar start times:")
            for time in bar_4m.timestamp:
                # printing [11:19] on a timestamp returns only the time
                print(service.time_to_string(time)[11:19])

            # increment variable
            self.bar_count += 1

            # if we have printed information for 2 bars, terminate
            if self.bar_count == 2:
                service.terminate()

Console

AAL
---------- in on_start 2016-09-07 09:30:00.000000

---------- in on_minute_bar 09:37:00

1 bars
bar start times:
09:30:00

---------- in on_minute_bar 09:38:00

2 bars
bar start times:
09:30:00
09:34:00