Hello, i found this site a couple of days ago. Very cool! Ive just been starting with investing and now with python and im finding a problem which I want to blame on the stddev function. This will start to run, then it will say "run time error". So my idea was to filter stocks with high standard deviations (which i hope will indicate volatility) and then trade them based on their averages.
def initialize(context):
set_universe(universe.DollarVolumeUniverse(98, 99))
def handle_data(context, data):
#the idea is to find stocks with high standard deviation,
#and trade them above and below their moving averages.
volatileStocks = list()
for stock in data.keys():
if data[stock].stddev(3) > 2:
volatileStocks .append(stock)
for stock in volatileStocks:
price = data[stock].price
avg = data[stock].vwap(3)
if price < avg * .98:
order(stock, +context.portfolio.cash/price)
elif price > avg * 1.02:
order(stock, -context.portfolio.cash/price)
Thanks for reading