It is simple to use set_universe to get all the tradable securities
but when I add set_do_not_order_list(security_lists.leveraged_etf_list)
I am busy error handling.
Can set_universe by default bring the list does not contain these "blacklisted" securities.
import talib
def initialize(context):
# we want to try this on a range of highly liquid stocks
set_do_not_order_list(security_lists.leveraged_etf_list)
set_universe(universe.DollarVolumeUniverse(98, 99))
def handle_data(context, data):
days = 7
prices = history(bar_count=days, frequency='1d', field='price').dropna(axis=1)
volumes = history(bar_count=days, frequency='1d', field='volume').dropna(axis=1)
# Store each stock's OBV value in a dict
# obv = {'sid1': val1, 'sid2': val2, ...}
obv = {}
try:
for stock in volumes:
obv[stock] = talib.OBV(prices[stock], volumes[stock])[-1]
# Rank obv values in ascending order (in a list of tuples)
# obv = [(sid1, val1), (sid2, val2), ...]
except:
log.error("key error")
obv = sorted(obv.items(), key=lambda x: x[1])
obv_length = len(obv)
obv_long = obv[:10]
obv_short = obv[obv_length-10:]
if context.account.total_positions_value == 0:
for stock in obv_long:
log.info(stock[0:][-2])
#Giving order of 5% of portfolio
order_target_percent(stock[0:][-2], 0.05)
for stock in obv_short:
log.info(stock[0:][-2])
order_target_percent(stock[0:][-2], -0.05)
record(leverage= context.account.leverage)