I would like to test my algo on a basket of 5 symbols but do not know how to.
Must I create an array variable or something similar?
QUANTCONNECT COMMUNITY
I would like to test my algo on a basket of 5 symbols but do not know how to.
Must I create an array variable or something similar?
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.
Michael Handschuh
// with a list var symbols = new List{"SPY","AAPL","GOOG","MSFT","IBM"};
foreach (var symbol in symbols)
{
AddSecurity(SecurityType.Equity, symbol);
}
// with individual calls:
AddSecurity(SecurityType.Equity, "SPY");
AddSecurity(SecurityType.Equity, "AAPL");
AddSecurity(SecurityType.Equity, "GOOG");
AddSecurity(SecurityType.Equity, "MSFT");
AddSecurity(SecurityType.Equity, "ABM");
I tend to prefer the list style, it makes it easier to add/remove symbols.Nicholas Caffrey
Michael Handschuh
Nicholas Caffrey
Michael Handschuh
Nicholas Caffrey
Michael Handschuh
Nicholas Caffrey
Nicholas Caffrey
Michael Handschuh
Pradeep Lakshmanan
Hello,Â
I want to add multiple Equity Symbols in one Algo and want to get price of each symbol on OnData() to run our logic for long or short.
I referred this forum and other forums as follows.
What I found is there is no foreach loop supported in QC as per QC documentation.
I am attaching my code, in which I am trying to give multiple symbols and want to fetch prices for each symbols respectively. I am getting only last symbol price from the list i.e. UBER.
class Module45LongShort(QCAlgorithm):
 Â
  def Initialize(self):
   Â
    self.SetStartDate(2021, 12, 1)  # Set Start DateÂ
    self.SetEndDate(2021, 12, 22) # Set End Date
    self.SetCash(10000)  # Set Strategy Cash
   Â
    ################## User Inputs ########################
   Â
    #self.scriptName = "PEP"
    self.scriptNameList = ["PEP","AAPL","TWTR","UBER"]
    self.timeFrame = Resolution.Hour  # Should be Hour / Minute
   Â
    ############## Long Inputs
    self.ExecuteMod45Long = "Yes"    # should be Yes/No
    self.Mod4ActualTradesLong = "No"   # should be Yes/No
   Â
    self.Mod4Long_MAX = 1        # Order Qty = MAX
    self.Mod5ActualTradesLong = "Yes"  # should be Yes/No
    self.Mod4TradeCountLong = 1     #Â
   Â
    ############## Short Inputs
    self.ExecuteMod45Short = "No"    # should be Yes/No
    self.Mod4ActualTradesShort = "No"   # should be Yes/No
   Â
    self.Mod5ActualTradesShort = "Yes"  # should be Yes/No
    self.Mod4TradeCountShort = 1     #Â
   Â
    ################## User Inputs ########################
   Â
    self.entryPrice = 0
    self.Mod4BoughtCount = 0
    self.previousPrice = 0
   Â
    self.entryPriceShort = 0
    self.Mod4SoldCount = 0
    self.previousPriceShort = 0
   Â
    #symb = self.AddEquity(self.scriptName, self.timeFrame , Market.USA)
    #symb.SetDataNormalizationMode(DataNormalizationMode.Raw)
   Â
    for scriptName in self.scriptNameList:
      self.symb = self.AddEquity(scriptName, self.timeFrame , Market.USA).Symbol
     Â
    self.Log("stockSymbolxxx = @" + str(self.symb))
   Â
  def OnData(self, data):
    if not data.Bars.ContainsKey(self.symb):
      self.Log("No Data found for Symbol "+str(self.symb))
      return
   Â
    if not self.symb in data:
      return
   Â
    # price = data.Bars[self.spy].Close
    #price = data[self.symb].Close
    # price = self.Securities[self.spy].Close
   Â
    tBars = data.Bars
    symbTBars = tBars[self.symb]
    price = symbTBars.Close
   Â
    #prices = self.Securities[self.symb].Price
    self.Log("Symbol "+str(self.symb)+" Price= "+str(price))
Vladimir
Pradeep Lakshmanan,
Try this way.
Nicholas Caffrey
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!