Please can you help me the code snippet for Bollinger Bands ie want to create an order when price is below lower bollinger band.
A sample code on Bollinger Band strategy is below
import numpy as np
import time
lookback = 100
def initialize(context):
context.stock = symbol('AAPL')
def handle_data(context, data):
prices = data.history(context.stock, 'close', lookback, '1m')
mean_price, std_dev = getMeanStd(prices)
z_score = (prices.iloc[0]-mean_price)/std_dev
# Bollinger bands, if price is 2 std deviation away from the mean then initiate trade
print z_score
if z_score > 2:
# long
order_target(context.stock, 1)
time.sleep(20)
if z_score < -2:
# short
order_target(context.stock, -1)
time.sleep(20)
def getMeanStd(prices):
mean_price = prices.mean()
std_dev = prices.std()
if mean_price is not None and std_dev is not None :
return (mean_price, std_dev)
else:
return None
Thank you very much…