Mariner Backtesting - account.inventory_export()

writes account information about currently held positions

account.() (method) Overview:

inventory_export() writes account information about currently held positions to the specified file.

Use Case:

Export the current account information with inventory_export() to be later used by inventory_import() to test different exit strategies on the same account inventory.

Interface:
 inventory_export(filename)

Sample - Calling method

 account.inventory_export('my_inventory.json')
Parameters
Name Type Default Information
filename string required The path to the file that will contain the open inventory for the account. This file will either be created or overwritten.
Returns: Remarks:
  • Only information about currently held positions is exported.
  • Information is written in json/dictionary format.
  • Used with inventory_import() to share account information between different submissions.
Working Example:

Use inventory_export() and inventory_import() to test two different exit strategies on the same account data.

First script containing inventory_export(). Run this script first to generate 'account_data.json'.

 from cloudquant.interfaces import Strategy


class ExportData(Strategy):

    @classmethod
    def is_symbol_qualified(cls, symbol, md, service, account):
        # American Airlines, Delta Airlines, Walmart, and Target
        return symbol in ['AAPL', 'FB']


    def on_start(self, md, order, service, account):
        print '--------------------', service.time_to_string(service.system_time) +\
                                        ' in on_start for ' + self.symbol

        # buy shares
        order.algo_buy(self.symbol, algorithm='market', intent='init', order_quantity=100)
        print '\n\n100 shares of ' + self.symbol + ' purchased\n\n'

    def on_minute_bar(self, event, md, order, service, account, bar):
        print '--------------------', service.time_to_string(event.timestamp)[11:19] + \
                                        ' in on_minute_bar for ' + self.symbol

        # after the 9:31 minute bar ...
        if event.timestamp > service.time(9, 31) and event.timestamp < service.time(9, 32):
            # sell 50 shares
            order.algo_sell(self.symbol, algorithm='market', intent='decrease', order_quantity=50)
            print '\n\n50 shares of ' + self.symbol + ' sold\n\n'
        # after the 9:32 minute bar ...
        elif event.timestamp > service.time(9,32):
            print '\n\nExporting Inventory\n\n'
            # write the account data to a user data file
            account.inventory_export('account_data.json')
            service.terminate()

account_data.json file created by inventory_export()

{"DAL": [{"mtm_price": 41.75, "execution": {"symbol": "DAL", "side": 1, "shares": 100, "price": 41.75, "time": 1478093400001700, "collect": null, "order_id": "0000000000000002", "clordid": null, "instruction_id": "9c61115e-e517-5a54-a96e-2f11c67b5f97", "script_id": "37474ee2-ab4f-4eb6-bf06-c32d017cf9d2", "user_key": "9c61115e-e517-5a54-a96e-2f11c67b5f97"}, "remaining_shares": 100}], "AAL": [{"mtm_price": 40.029998779296875, "execution": {"symbol": "AAL", "side": 1, "shares": 100, "price": 40.029998779296875, "time": 1478093400001700, "collect": null, "order_id": "0000000000000001", "clordid": null, "instruction_id": "4ba78c95-ea09-52d5-9a48-7be8ba41cd73", "script_id": "37474ee2-ab4f-4eb6-bf06-c32d017cf9d2", "user_key": "4ba78c95-ea09-52d5-9a48-7be8ba41cd73"}, "remaining_shares": 100}], "TGT": [{"mtm_price": 67.77999877929688, "execution": {"symbol": "TGT", "side": 1, "shares": 100, "price": 67.77999877929688, "time": 1478093400001700, "collect": null, "order_id": "0000000000000003", "clordid": null, "instruction_id": "2f067a2b-8db9-54d6-ad70-bb17af9f128c", "script_id": "37474ee2-ab4f-4eb6-bf06-c32d017cf9d2", "user_key": "2f067a2b-8db9-54d6-ad70-bb17af9f128c"}, "remaining_shares": 100}], "WMT": [{"mtm_price": 69.52999877929688, "execution": {"symbol": "WMT", "side": 1, "shares": 100, "price": 69.52999877929688, "time": 1478093400001700, "collect": null, "order_id": "0000000000000004", "clordid": null, "instruction_id": "1319b278-689e-5802-9de7-08577fc1c465", "script_id": "37474ee2-ab4f-4eb6-bf06-c32d017cf9d2", "user_key": "1319b278-689e-5802-9de7-08577fc1c465"}, "remaining_shares": 100}]}
Second script containing inventory_import()
 from cloudquant.interfaces import Strategy


class ImportData(Strategy):
    @classmethod
    def is_symbol_qualified(cls, symbol, md, service, account):
        # American Airlines, Delta Airlines, Walmart, and Target
        return symbol in ['WMT', 'AAL', 'TGT', 'DAL']

    @classmethod
    def on_strategy_start(cls, md, service, account):
        print 'in on_strategy_start\n'

        # print the account before the import
        print 'Account before Import:\n\n\n', account, '\n\n\n----------------------------------------------\n\n\n'

        # import account data from the user data file
        account.inventory_import('account_data.json', apply_corporate_actions=True)

        # print the account after the import
        print 'Account after Import:\n\n\n', account

Console 2 - Account before and after inventory_import()

in on_strategy_start

Account before Import:


account_id:                                 0
entry_pl:                                0.00
mtm_pl:                                  0.00
buying_power:                            None
open_capital_long:                       0.00
open_capital_short:                      0.00
pending_capital_long:                    0.00
pending_capital_short:                   0.00
max_capital_used:                        0.00
max_capital_used_timestamp:                 0
open_symbols:                []
pending_symbols:             []



----------------------------------------------



Account after Import:


account_id:                                 0
entry_pl:                                0.00
mtm_pl:                                  0.00
buying_power:                            None
open_capital_long:                   11896.50
open_capital_short:                      0.00
pending_capital_long:                    0.00
pending_capital_short:                   0.00
max_capital_used:                    11896.50
max_capital_used_timestamp:  1473255000000000
open_symbols:                [u'AAPL', u'FB']
pending_symbols:             []
AAPL:
    Execution count     :              0
    realized_pl         :
        entry_pl        :           0.00
        mtm_pl          :           0.00
        matched_trades  :              0
    unrealized_pl       :
        entry_pl        :           0.00
        mtm_pl          :           0.00
    position            :
        shares          :             50
        entry_price     :         107.87
        mtm_price       :         107.68
        capital_long    :        5393.50
        capital_short   :           0.00
    pending             :
        count_long      :              0
        shares_long     :              0
        capital_long    :           0.00
        count_short     :              0
        shares_short    :              0
        capital_short   :           0.00
FB:
    Execution count     :              0
    realized_pl         :
        entry_pl        :           0.00
        mtm_pl          :           0.00
        matched_trades  :              0
    unrealized_pl       :
        entry_pl        :           0.00
        mtm_pl          :           0.00
    position            :
        shares          :             50
        entry_price     :         130.06
        mtm_price       :         129.73
        capital_long    :        6503.00
        capital_short   :           0.00
    pending             :
        count_long      :              0
        shares_long     :              0
        capital_long    :           0.00
        count_short     :              0
        shares_short    :              0
        capital_short   :           0.00