Mariner Backtesting - service.read_file()

service.read_file()

Return the data read from the specified file.

Interface:
 read_file(filename,
          format='raw',
          group='',
          path=None)

Sample - Calling Method

 service.read_file( 'my_file' )
Parameters
Name Type Default Information
filename string required Filename to read from local machine.
format string 'raw' Either 'raw', 'csv', or 'csv_noheader'. format='raw' returns the raw data from 'filename' as a string. format='csv' returns CSV data from 'filename' as a cloudquant.interfaces.CSV object.'csv_noheader' is the same as csv, but for files without a header.
group string '' File sharing group. Default is an empty string.
path dictionary None Tells what directory to read from based on the environment (backtest, live, forward). In CQ backtesting, this does nothing. Example path={'BACKTESTING':None, 'FORWARD':'//EQKTG02/xxx/', 'PRODUCTION':''}
More Reading Examples

Read CSV

Advanced Read

Remarks:
  • The CSV object returned by csv and csv_noheader is an unprintable generator.
  • To access the data in a csv file, use a for loop. Each row is organized in a dictionary.
Working Example:

secret_symbol_list.txt (symbol list)

TGT
AAPL
SBUX
FB

 

entry_stats.csv

symbol, side, order_quantity
TGT,long,88
AAPL,long,100
SBUX,short,120
WMT,short,150

 

from cloudquant.interfaces import Strategy


class ReadingFiles(Strategy):
    # note that this doesn't start with "self" because it's a @staticmethod
    @classmethod
    def on_strategy_start(cls, md, service, account):

        # open the csv file as a list of dictionaries
        temp_file = service.read_file('entry_stats.csv', format='csv')

        # populate class variable (dictionary)
        cls.stats = {}

        # for each dictionary created for each row:
        for row in temp_file:
            # save the data from the file to a class variable based on symbol
            # access the symbol from the 'symbol' key in row, row['symbol']
            cls.stats[row['symbol']] = row

        # read the user data file as a string
        symbols = service.read_file('secret_symbol_list.txt', format='raw')

        # create a symbol list by splitting the lines into a list of symbols
        cls.symbol_list = symbols.splitlines()

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

        # qualify only symbol in both the symbol list and in the entry stats file
        if symbol in cls.symbol_list and symbol in cls.stats.keys():
            return True

    # called at the beginning of each instance
    def on_start(self, md, order, service, account):

        print 'in on_start ' + self.symbol

        # set the quantity equal to the value from the csv for this symbol
        quantity = self.__class__.stats[self.symbol]['order_quantity']

        # check if the side is 'long'
        if self.__class__.stats[self.symbol]['side'] == 'long':

            # buy order
            # set the order_quantity = to the quantity from 'entry_stats'
            order.algo_buy(self.symbol, 'market', 'init', order_quantity=quantity)

        # check if the side is 'short'
        elif self.__class__.stats[self.symbol]['side'] == 'short':

            # sell order
            # set the order_quantity = to the quantity from 'entry_stats'
            order.algo_sell(self.symbol, 'market', 'init', order_quantity=quantity)

        # print order information:
        # print the time, the side, the quantity, the shares, and the symbol
        print service.time_to_string(service.system_time), '\t', \
                self.__class__.stats[self.symbol]['side'], quantity, 'shares of', self.symbol, '\n'

Console

in on_start AAPL
2016-12-12 09:30:00.000000  long 100 shares of AAPL

in on_start SBUX
2016-12-12 09:30:00.000000  short 120 shares of SBUX 

in on_start TGT
2016-12-12 09:30:00.000000  long 88 shares of TGT