Hi there
I'm using the following Bollinger Band Algo (cloned from another user) to explore what I really want to achieve for my own indicator, which is to vary the date range used to calculate the current indicator value.
In the example below, the Bollinger Band is calculated from the last 20 day's closing prices (t-20, ... , t-1), controlled by this line:
prices = data.history(context.jj,'price', 20 , '1d')
Rather than this, what I would like is to calculate the Bollinger bands from the closing prices of just a 10 day period starting 20 days ago and running to 10 days ago ( t-20, ... , t-10)
I was wondering if an offset could be used, but cant seem to find any examples.
Any pointers appreciated!
# Setup our variables
def initialize(context):
context.jj = sid(4151)
schedule_function(check_bands,date_rules.every_day())
def check_bands(context, data):
cur_price = data.current(context.jj,'price')
# Load historical data for the stocks
prices = data.history(context.jj,'price', 20 , '1d')
avg = prices.mean()
std = prices.std()
lower_band = avg - 2*std
upper_band = avg + 2*std
if cur_price <= lower_band:
order_target_percent(context.jj, 1.0)
print('Buying')
print('Current price is: ' + str(cur_price))
print("Lower band is: "+str(lower_band))
elif cur_price >= upper_band:
order_target_percent(context.jj, -1.0)
print('Shorting')
print('Current price is: ' + str(cur_price))
print("Upper band is: "+str(upper_band))
else:
pass
record(upper=upper_band,
lower=lower_band,
mvag_20=avg,
price=cur_price)