Beispiel #1
0
def clear_queue(context, data):

    """
    Every minute, attempts to take any stocks that were not able to trade and
    tries again. The first time it will show details about stock, and every 5
    minutes thereafter it will log what is left in the trade queue.
    """

    if context.rebalance_complete:
        if bool(context.trade_queue):
            LOG.info('Attempting to clear trading queue')
            remove_queue_list = []
            for security, amount in context.trade_queue.items():
                if data.can_trade(security):
                    order_target_pct(security.symbol, amount, 'market')
                    remove_queue_list.append(security)
                else:
                    if context.clear_queue_run == 0:
                        LOG.warning('{} is not able to trade'.format(security.symbol))
                        LOG.info('Asset info: {}'.format(API.get_asset(security.symbol)))
            for security in remove_queue_list:
                del context.trade_queue[security]
            context.clear_queue_run += 1
            if bool(context.trade_queue):
                if context.clear_queue_run % 5 == 0:
                    LOG.info('Items remaining in trade queue: {}'.format(context.trade_queue))
            else:
                LOG.info('Trade queue is now empty')
Beispiel #2
0
def clear_queue(context, data):
    if context.rebalance_complete:
        if bool(context.trade_queue):
            log.info('Attempting to clear trading queue')
            remove_queue_list = []
            for security, amount in context.trade_queue.items():
                if data.can_trade(security):
                    order_target_pct(security.symbol, amount, 'market')
                    remove_queue_list.append(security)
                else:
                    if context.clear_queue_run == 0:
                        log.warning('{} is not able to trade'.format(
                            security.symbol))
                        asset_info = api.get_asset(security.symbol)
                        log.info('Asset info: {}'.format(asset_info))
            for security in remove_queue_list:
                del context.trade_queue[security]
            context.clear_queue_run += 1
            if bool(context.trade_queue):
                if context.clear_queue_run % 5 == 0:
                    log.info('Items remaining in trade queue: {}'.format(
                        context.trade_queue))
            else:
                log.info('Trade queue is now empty')
Beispiel #3
0
def rebalance(context, data):

    """
    The execution module that checks the status of volatility to determine what
    to trade and then places orders. Includes a feature to determine whether a
    circuit breaker has been hit.
    """

    if context.ndays % context.idays == 0 or context.vol_threshold_hit:

        if context.str_weight == 1:

            LOG.info('Rebalancing existing positions')
            not_tradeable_count = 0

            for security in context.portfolio.positions:
                if security not in context.longs and security not in context.shorts:
                    if data.can_trade(security):
                        order_target_pct(security.symbol, 0.0, 'market')
                    else:
                        not_tradeable_count += 1
                        LOG.warning('{} is not able to trade'.format(security.symbol))
                        context.trade_queue[security] = 0.0
            LOG.info('Rebalancing existing positions complete!')

            # Check circuit breaker
            if not_tradeable_count/30 > 0.75:
                LOG.info('Circuit breaker may have been hit')

            LOG.info('Buying new long positions')
            for security in context.longs:
                if data.can_trade(security):
                    order_target_pct(security.symbol, context.long_weight, 'market')
                else:
                    LOG.warning('{} is not able to trade'.format(security.symbol))
                    context.trade_queue[security] = context.long_weight
            LOG.info('Buying new long positions complete!')

            LOG.info('Selling new short positions')
            for security in context.shorts:
                if data.can_trade(security):
                    order_target_pct(security.symbol, context.short_weight, 'market')
                else:
                    LOG.warning('{} is not able to trade'.format(security.symbol))
                    context.trade_queue[security] = context.short_weight
            LOG.info('Selling new short positions complete!')
            LOG.info('Trading complete!')

            context.rebalance_complete = True

        else:
            for security in context.portfolio.positions:
                if security.symbol not in context.SPY.symbol:
                    if data.can_trade(security):
                        order_target_pct(security.symbol, 0.0, 'market')
                    else:
                        LOG.warning('{} is not able to trade'.format(security.symbol))
                        context.trade_queue[symbol] = 0.0

            order_target_pct(context.SPY, context.spyleverage, 'market')

    context.ndays += 1
    LOG.info('Ndays = {}'.format(context.ndays))
Beispiel #4
0
def rebalance(context, data):

    if context.ndays % context.idays == 0:

        if context.str_weight == 1:

            # Place trades
            log.info('Rebalancing existing positions')
            not_tradeable_count = 0

            for security in context.portfolio.positions:
                if security not in context.longs and security not in context.shorts:
                    if data.can_trade(security):
                        order_target_pct(security.symbol, 0.0, 'market')
                    else:
                        not_tradeable_count += 1
                        log.warning('{} is not able to trade'.format(
                            security.symbol))
                        context.trade_queue[security] = 0.0
            log.info('Rebalancing existing positions complete!')

            # Check circuit breaker
            if not_tradeable_count / 30 > 0.75:
                log.info('Circuit breaker may have been hit')

            log.info('Buying new long positions')
            for security in context.longs:
                if data.can_trade(security):
                    order_target_pct(security.symbol, context.long_weight,
                                     'market')
                else:
                    log.warning('{} is not able to trade'.format(
                        security.symbol))
                    context.trade_queue[security] = context.long_weight
            log.info('Buying new long positions complete!')

            log.info('Selling new short positions')
            for security in context.shorts:
                if data.can_trade(security):
                    order_target_pct(security.symbol, context.short_weight,
                                     'market')
                else:
                    log.warning('{} is not able to trade'.format(
                        security.symbol))
                    context.trade_queue[security] = context.short_weight
            log.info('Selling new short positions complete!')
            log.info('Trading complete!')

            context.rebalance_complete = True

        else:

            for security in context.portfolio.positions:
                if security not in context.SPY:
                    if data.can_trade(security):
                        order_target_pct(security.symbol, 0.0, 'market')
                    else:
                        not_tradeable_count += 1
                        log.warning('{} is not able to trade'.format(
                            security.symbol))
                        context.trade_queue[security] = 0.0

            order_target_pct(context.SPY, context.spyleverage, 'market')

    context.ndays += 1
Beispiel #5
0
def rebalance(context, data):

    if context.ndays % context.idays == 0:

        if context.str_weight == 1:

            pdf = context.preds_df

            # Remove stocks with trade restrictions and non-shortable stocks for short selection
            assets = api.list_assets()
            asset_dict = {}
            for i in range(len(assets)):
                asset_dict.update({assets[i].symbol: assets[i].easy_to_borrow})
            pdf['etb'] = pdf['symbol'].map(asset_dict)
            pdf = pdf[~pdf['symbol'].isin(context.combined_restrictions)]
            pdf_short = pdf[pdf.etb == True]

            # Select securities for long and short
            longs = pdf['preds'].nlargest(context.num_longs)
            shorts = pdf_short['preds'].nsmallest(context.num_shorts)

            # Select and log stocks to trade
            context.longs = longs.index.tolist()
            context.shorts = shorts.index.tolist()

            log.info('longs: {}'.format(context.longs))
            log.info('Shorts: {}'.format(context.shorts))
            log.info('Portfolio positions: {}'.format(
                context.portfolio.positions))

            # Place trades
            log.info('Rebalancing existing positions')
            not_tradeable_count = 0

            for security in context.portfolio.positions:
                if security not in context.longs and security not in context.shorts:
                    if data.can_trade(security):
                        order_target_pct(security.symbol, 0.0, 'market')
                    else:
                        not_tradeable_count += 1
                        log.warning('{} is not able to trade'.format(
                            security.symbol))
                        context.trade_queue[security] = 0.0
            log.info('Rebalancing existing positions complete!')

            # Check circuit breaker
            if not_tradeable_count / 30 > 0.75:
                log.info('Circuit breaker may have been hit')

            log.info('Buying new long positions')
            for security in context.longs:
                if data.can_trade(security):
                    order_target_pct(security.symbol, context.long_weight,
                                     'market')
                else:
                    log.warning('{} is not able to trade'.format(
                        security.symbol))
                    context.trade_queue[security] = context.long_weight
            log.info('Buying new long positions complete!')

            log.info('Selling new short positions')
            for security in context.shorts:
                if data.can_trade(security):
                    order_target_pct(security.symbol, context.short_weight,
                                     'market')
                else:
                    log.warning('{} is not able to trade'.format(
                        security.symbol))
                    context.trade_queue[security] = context.short_weight
            log.info('Selling new short positions complete!')
            log.info('Trading complete!')

            context.rebalance_complete = True

        else:

            for security in context.portfolio.positions:
                if security not in context.SPY:
                    if data.can_trade(security):
                        order_target_pct(security.symbol, 0.0, 'market')
                    else:
                        not_tradeable_count += 1
                        log.warning('{} is not able to trade'.format(
                            security.symbol))
                        context.trade_queue[security] = 0.0

            order_target_pct(context.SPY, context.spyleverage, 'market')

    context.ndays += 1