Mariner Backtesting - Beginner 3 Third Script Daily Bar

Daily Bars Lesson

Overview:

In this lesson, you will learn how to use daily bars.

What does md.bar.daily() do?
 md.bar.daily(start=-20)
  • Daily bars collect trading data for a complete day
  • Bars from previous days can be requested using a negative integer (x days ago) or a timestamp

Basic use case:

Daily bar data can be used to calculate weekly or monthly averages.

The list of daily bar attributes

Daily BarView

Working Example:

Daily Bars

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


class dailyBar(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" + service.time_to_string(service.system_time) + "\tin on_start()")

        # initialize variable
        self.bar_data_20 = None

        # access the bar data for the past 20 trading days
        self.bar_data_20 = md.bar.daily(start=-20)

        # variable to contain the sum of the volume
        self.volume_sum = 0

        # for each day add the total volume to the variable
        for day in self.bar_data_20.volume:
            self.volume_sum += day

        # calculate average volume over the 20 days
        # normally use doubles for division, but here we don't want decimals
        self.average_volume = self.volume_sum / 20

        # print the calculated data
        print("\nTotal Volume over 20 days: " + str(self.volume_sum))
        print("Average Volume over 20 days: " + str(self.average_volume))

        service.terminate()

Console

AAL
2016-08-08 09:25:00.000000  in on_start()

Total Volume over 20 days: 231570322
Average Volume over 20 days: 11578516