Mariner Backtesting - service.write_file()

service.write_file()

service. (method) Overview:

Method to write data to file. Creates a new file if the file does not exist.

Interface:
 write_file(filename,
           data,
           end='\n',
           mode='append',
           group='',
           path=None)

Sample - Calling Method

 service.write_file( 'my_filename', 'hello file' )
Parameters
Name Type Default Information
filename string required Filename to write to
data string required Data to be written
end string '\n' End of input delimiter
mode string 'append' Whether to append or override an existing file
  • 'append'
  • 'overwrite'
group string '' File sharing group. The default is an empty string.
path dictionary None Tells what directory to write to based on the environment (backtest, live, forward). In CQ backtesting, this does nothing. Example path={'BACKTESTING':None, 'FORWARD':'//EQKTG02/xxx/', 'PRODUCTION':''}
More Writing Examples

Write CSV Advanced Write

Working Example:
from cloudquant.interfaces import Strategy


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


    def on_start(self, md, order, service, account):
        # create file name
        file_name = 'get_trading_days_{}.csv'.format(service.time_to_string(service.system_time, '%Y-%m-%d') )

        # headline line of the file
        service.write_file(file_name, 'offset,date,month,day_of_week' )

        for i in range(0, -100, -1):

            #  service.get_market_hours( date_offset=i ) returns a tuple like (1455633000000000, 1455656400000000)
            previous_trading_date = service.get_market_hours( date_offset=i )[0] 

            #string formatting http://strftime.org/
            date = service.time_to_string(previous_trading_date, '%Y-%m-%d')
            month = service.time_to_string(previous_trading_date, '%B')
            day_of_week = service.time_to_string(previous_trading_date, '%A')

            # data line in file
            service.write_file(file_name, '{},{},{},{}'.format(i, date, month, day_of_week) )

        service.terminate()

Console

Check User Data for File