Thanks Jamie.
I'm using "set_universe"and I'm getting this error: "ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()"
import talib
def initialize(context):
set_universe(universe.DollarVolumeUniverse(90, 91))
#context.size = float(context.portfolio.cash / 30.0)
#context.shorts = context.size * 0.5
context.shorting= True
context.max_positions = 10
#context.max_positions2 = 6
#schedule_function(logic,date_rules.every_day(),time_rules.market_close())
def handle_data(context, data):
context.l = float(( 0.5 * context.portfolio.portfolio_value) /7)
#context.s = float(( -0.5 * context.portfolio.portfolio_value) /7)
open_orders = get_open_orders()
position_count = get_position_count(context)
for s in data:
if s in open_orders:
continue
#price = data[s].price
current_position = context.portfolio.positions[s].amount
price = data[s].price
high1 = history(15, "1d", "high")
low1 = history(15, "1d", "low")
close= history(15, "1d", "price")
#highs=high1[s][-1]
#lows=low1[s][-1]
#closes=close[s][-1]
adx = talib.ADX(high1[s], low1[s], close[s] , timeperiod=14)
if position_count < context.max_positions:
if (current_position == 0) :
if (adx > 35) :
order_target_value(s, context.l )
log.info("LONG " + str(s.symbol))
position_count +=1
record(Leverage = context.account.leverage)
def get_position_count(context):
n = 0
for position in context.portfolio.positions.itervalues():
if position.amount != 0:
n += 1
return n