Back

Get price of multiple symbols in OnData and compare each symbol's price to each symbol's indicator value

I need some help handling multiple symbols for my strategy.  My goal is to buy multiple securities (UNH and DIS for simplicity sake) if the price for each symbol is <= to Lower Bollinger Band Indicator.  Assuming my Initialize block is ok, I need help getting the price for each symbol for comparison to each symbol's bolinger band and then buy each security if the condition is met.  Please see attached backtest.  I was able to do this successfully with one symbol but cannot pull it off with multiple.  Any help would be greatly appreciated!

from QuantConnect.Data.Market import TradeBar
from datetime import timedelta
from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
import decimal as d

class BollingerBreakoutAlgorithm(QCAlgorithm):

def Initialize(self):
self.SetStartDate(2017, 1, 1)
self.SetEndDate(2019, 11, 25)
self.SetCash(100000)
Symbols = ["UNH", "DIS"]
for symbol in Symbols:
securities = self.Securities[Symbols].Price
equity = self.AddEquity(Symbols, Resolution.Daily).Symbol
self.bband = self.BB(Symbols, 20, 2, MovingAverageType.Simple, Resolution.Daily)
self.SetWarmUp(20, Resolution.Daily)
self.SetBenchmark("SPY")

def OnData(self, data):
if not (self.bband.IsReady): return
#get price for each symbol
prices = self.Securities[Symbols].Price
#compare price for each symbol to the indicator
if Symbols <= self.bband.LowerBand.Current.Value:
#buy stock for each symbol that meets the criteria
self.MarketOrder(Symbols, 1)

 

Update Backtest







0

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 QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect 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. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Hi Rory Forrest ,

Your Initialize block was not OK and the code execution throws an exception.

When we are working with more than one security, one possible pattern to follow is using dictionaries keyed by Symbol. Please check out the example below: 

1

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 QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect 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. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Hi Alexandre,

Thank you for taking the time to look into this.  Exactly what I needed.  I can now carry this strategy forward.  Much appreciated

0

Update Backtest





0

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 QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect 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. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed