Is there an easy way to use Bollinger Bands in Quantopian? I saw the thread "Playing around with Bollinger Bands" but I can't get it to compile (uses super, deprecated code etc) and it uses temporals which I am unfamiliar with.
Is there an easy way to use Bollinger Bands in Quantopian? I saw the thread "Playing around with Bollinger Bands" but I can't get it to compile (uses super, deprecated code etc) and it uses temporals which I am unfamiliar with.
Hello Sheridan,
There's some code here:
Also, I see that Bollinger Bands are simply:
Ref.: http://en.wikipedia.org/wiki/Bollinger_Bands.
I don't have time now to tinker around with the code, but it should be straightforward. Note that there are some statistical tools in scipy. For example:
prices_z = stats.zscore(prices, axis=0, ddof=1)
Grant
Hi Sheridan,
I did some quick cleanup on the original bollinger band post and also added some record calls to plot the price and the bands. You can see the results in the attached backtest. You should be able to clone this algo and run it without any problems.
thanks,
fawce
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.
Hello Sheridan & Fawce,
Here's some z-score code...seems like trading on z-score could be formulated to be equivalent to trading using Bollinger Bands, right? And if multiple securities are all normalized with a z-score, they can be visualized together on the same plot (see below).
Grant
Thanks for the help! I cloned the BB algo because I am more familiar with them than z-scores. I tried modifying the BB algo to accept multiple sids by modifying it slightly. I'd paste my Backtest but can't get it to compile so I figured I'd reach out for help. I replaced
context.sid = sid(4283)
with
context.sids = [
sid(4922), sid(2), sid(679), sid(6653), sid(700), sid(698), sid(1267), sid(23112), sid(1900), sid(2119),
sid(8347), sid(3149), sid(3735), sid(3496), sid(3951), sid(3766), sid(4151), sid(23146), sid(4707), sid(5029),
sid(5061), sid(5923), sid(5938), sid(4283), sid(7560), sid(7883), sid(7792), sid(39702), sid(8229), sid(2190)
]
That obviously broke the next line:
context.add_daily = TemporalGrouper(context.sid)
so I tried commenting that out which subsequently broke
context.add_daily(data)
in my handle_data block. Any ideas on how to get this to compile with multiple SIDs? It doesn't seem very "Quantopian" to just monitor 1 security... I could do that by hand!
Hello Sheridan,
A z-score is given by (see http://en.wikipedia.org/wiki/Standard_score):
z = (x-mu)/sigma
or re-writing,
x = mu + z*sigma
According to http://en.wikipedia.org/wiki/Bollinger_Bands, we have the upper and lower Bollinger Band limits, respectively:
UB = mu + k*sigma
LB = mu - k*sigma
So, UB is the z = k limit and LB is the z = - k limit.
The %b is:
%b = (x − LB) / (UB − LB) = (mu + z*sigma - mu + k*sigma)/(mu + k*sigma - mu + k*sigma) = (z + k) / 2k
The bandwidth (BW) is:
BW = (UB - LB) / mu = 2k / mu
Regarding the code Fawce posted above...not sure what's going on with multiple securities. In any case, it should be straightforward to implement Bollinger Bands in Quantopian for multiple securities...I may tinker with it when I get the chance, since it appears to be effectively z-score based trading, which I'd explored previously on Quantopian.
By the way, it seems that for Bollinger Bands to work really well, one needs to identify securities with prices that are effectively stationary in time (see http://en.wikipedia.org/wiki/Stationary_process) and normally distributed (perhaps not a strict requirement). Any ideas how to do efficient screening over a large number of securities?
Grant
Grant,
Great insight! Let's fork your code to a new thread and I'll chip in to calculate the bands with your approach. Should be way faster.
Sheridan,
Could you post your question to the original thread? Maybe the original author will be interested in making it multi. If not, I can take a look tonight.
thanks,
fawce
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.
Hello Fawce,
I posted a new thread here:
https://www.quantopian.com/posts/z-score-route-to-bollinger-bands
In a day or two, I may have some time to tinker around with it. Feel free to do whatever you want with the code--maybe I'll learn a few tricks from you.
Grant