Questions about dependent scripts

[Deleted User][Deleted User] Posts: 0
edited March 2017 in Under The Hood

Hi,

I have a question about the behavior of an account object in a pairs-trading like strategy:

I am wondering if the exit script sees the same account object (and the same positions and execution data) from the entry script, or those info. in the account object gets wiped out once the entry script is terminated and the control is passed to the exit script ?

Thanks!

Comments

  • [Deleted User][Deleted User] Posts: 0
    edited March 2017

    I would answer my own question here:

    The dependent strategy would inherit all the account information from its precedent. Here is an example:

    from cloudquant.interfaces import Strategy, Event
    
    ALGO_BUY = '{2b4fdc55-ff01-416e-a5ea-e1f1d4524c7d}'  # Buy Market ARCA
    ALGO_SELL = '{8fdee8fe-b772-46bd-b411-5544f7a0d917}'  # Sell Market ARCA
    
    class dependent_strategy_entry(Strategy):
        @classmethod
        def on_strategy_start(cls, md, service, account):
            print('denpendent_strategy_entry: on_strategy_start')
            print('Account ID: %s' % account.account_id)
            symbol_dict = {'side': 'long',
                           'base_shares': 0,
                           'entry_shares': 0,
                           'exit_shares': 0,
                           'entry_price': 0,
                           'order_id': None,
                           'retries': 0
                           }
            cls.symbols = {
                           'AAPL': symbol_dict,
                           'FB': symbol_dict,
                           'MSFT': symbol_dict,
                           'WMT': symbol_dict,
                           'SPY': symbol_dict
                           }
            for count, symbol in enumerate(cls.symbols):
                if count % 2 == 0:
                    cls.symbols[symbol]["side"] = 'short'
    
            for symb in cls.symbols:
                print symb, cls.symbols[symb]
    
        @classmethod
        def is_symbol_qualified(cls, symbol, md, service, account):
            # create 1 instance
            return symbol == min(cls.symbols)
    
        @classmethod
        def backtesting_extra_symbols(cls, symbol, md, service, account):
            # load other symbols data
            return symbol in cls.symbols and symbol != min(cls.symbols)
    
        def entry_orders(self, event, md, order, service, account):
            for symbol in self.__class__.symbols:
                # self.state = 1
                # trade_data = {}
                order_quantity = abs(self.__class__.symbols[symbol]['base_shares'])
                if self.__class__.symbols[symbol]['side'] == 'long':
                    # -------------------- none --------------------
                    self.__class__.symbols[symbol]['order_id'] = order.algo_buy(symbol, ALGO_BUY, 'init',
                                                                                order_quantity=order_quantity)
                    # --------------------------------------------------
                elif self.__class__.symbols[symbol]['side'] == 'short':
                    # -------------------- none --------------------
                    self.__class__.symbols[symbol]['order_id'] = order.algo_sell(symbol, ALGO_SELL, 'init',
                                                                                 order_quantity=order_quantity)
                    # --------------------------------------------------
    
        def on_start(self, md, order, service, account):
            ###################################################################################
            #
            # The pair strategy runs as a single instance that funnels all events for desired symbols
            #
            ###################################################################################
            service.clear_event_triggers()
            # note the list of symbols
            service.add_event_trigger([symbol for symbol in self.__class__.symbols], [Event.TRADE, Event.FILL, Event.REJECT])
    
            self.state = 0
            dollar_of_capital = 100000
            for symbol in self.__class__.symbols:
                self.__class__.symbols[symbol]['base_shares'] = int(dollar_of_capital / md[symbol].stat.prev_close)
    
            # Sent entry basket orders at 9:35
            self.basket_entry_time = md.market_open_time + service.time_interval(minutes=5)
    
            # Exit Script Timer
            service.add_time_trigger(service.time(15,45))
    
        def on_trade(self, event, md, order, service, account):
            if self.state == 0 and event.timestamp >= self.basket_entry_time:
                self.state = 1
                self.entry_orders(event, md, order, service, account)
    
        def on_timer(self, event, md, order, service, account):
    
            # params = {}
            # params['symbols_dict'] = self.__class__.symbols
            print('Account info before dependent exit strategy starts:')
            print('Account ID: %s' % account.account_id)
            print('Positions:')
            print(account)
            service.start_strategy("dependent_strategy_exit")
            print 'starting exit'#, params
    
        # called when an order is rejected (locally or other parts of the order processes e.g. the market)
        def on_reject(self, event, md, order, service, account):
            print event
            pass
    
  • [Deleted User][Deleted User] Posts: 0
    edited March 2017
    from cloudquant.interfaces import Strategy, Event
    
    ALGO_BUY = '{2b4fdc55-ff01-416e-a5ea-e1f1d4524c7d}'  # Buy Market ARCA
    ALGO_SELL = '{8fdee8fe-b772-46bd-b411-5544f7a0d917}'  # Sell Market ARCA
    
    class dependent_strategy_exit(Strategy):
    
        @classmethod
        def on_strategy_start(cls, md, service, account):
            pass
    
        @classmethod
        def is_symbol_qualified(cls, symbol, md, service, account):
            return symbol == min(cls.symb_positions)
    
        @classmethod
        def backtesting_extra_symbols(cls, symbol, md, service, account):
            # load other symbols data
            return symbol in cls.symb_positions and symbol != min(cls.symb_positions)
    
        def on_start(self, md, order, service, account):
            print('dependent_strategy_exit on_start:')
            print('Account ID: %s' % account.account_id)
            print('Positions inherited from entry script:')
            print(account.open_symbols)
            print('Positions:')
            print(account)
            ###################################################################################
            #
            # The pair strategy runs as a single instance that funnels all events for desired symbols
            #
            ###################################################################################
    
            for symbol in account.open_symbols:
                if account[symbol].position.shares > 0:
                    self.sell_order_id = order.algo_sell(symbol, ALGO_SELL, 'exit')
    
                elif account[symbol].position.shares < 0:
                    self.buy_order_id = order.algo_buy(symbol, ALGO_BUY, 'exit')
    
            service.terminate()
    
  • I output the account info right before and after the precedent: dependent_strategy_entry hand over the control to dependent_strategy_exit, and they are the same:

    Account info before dependent exit strategy starts:
    Account ID: 0
    Positions:
    account_id: 0
    entry_pl: 0.00
    mtm_pl: 0.00
    buying_power: None
    open_capital_long: 0.00
    open_capital_short: -229329.00
    pending_capital_long: 0.00
    pending_capital_short: -615360.24
    max_capital_used: 229329.00
    max_capital_used_timestamp: 1420209300038700
    open_symbols: ['SPY', 'AAPL', 'FB', 'MSFT', 'WMT']
    pending_symbols: []

    starting exit
    dependent_strategy_exit on_start:
    Account ID: 0
    Positions inherited from entry script:
    ['SPY', 'AAPL', 'FB', 'MSFT', 'WMT']
    Positions:
    account_id: 0
    entry_pl: 0.00
    mtm_pl: 0.00
    buying_power: None
    open_capital_long: 0.00
    open_capital_short: -229329.00
    pending_capital_long: 0.00
    pending_capital_short: -615360.24
    max_capital_used: 229329.00
    max_capital_used_timestamp: 1420209300038700
    open_symbols: ['SPY', 'AAPL', 'FB', 'MSFT', 'WMT']
    pending_symbols: []

Sign In or Register to comment.