please help my code is failing here:
Runtime Error: 'TradeBar' object has no attribute 'BidVolume' at if pair.Key != self._basketSymbol and pair.Value.BidVolume > 2 * pair.Value.AskVolume] in main.py: line 41 at GetBestStock stocks = [SecurityData(pair.Key in main.py: line 40 at OnData best_stock = self.GetBestStock(data) at Python.Runtime.PythonException.ThrowLastAsClrException() at Python.Runtime.PyObject.Invoke(PyTuple args in main.py: line 22
from AlgorithmImports import *
class MyAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2020, 1, 1)
self.SetCash(100000)
self._basketSymbol = Symbol.Create('Basket', SecurityType.Equity, Market.USA)
self._lastTradeDate = datetime.min
self._purchasePrice = 0
self._targetProfit = 0.05 # 5% profit
self._stopLoss = 0.02 # 2% stop loss
self.UniverseSettings.Resolution = Resolution.Minute
self.AddUniverse(self.CoarseSelectionFunction)
def CoarseSelectionFunction(self, coarse):
selected = [x for x in coarse if 0.01 < x.Price < 1 and x.HasFundamentalData and x.Volume > 800000]
return [x.Symbol for x in selected]
def OnData(self, data):
if not self.Portfolio.Invested:
best_stock = self.GetBestStock(data)
if best_stock is not None:
self._currentStockSymbol = best_stock.Symbol
self._purchasePrice = best_stock.Price
self.SetHoldings(self._currentStockSymbol, 1.0)
elif self._currentStockSymbol is not None and self._currentStockSymbol in data:
current_price = data[self._currentStockSymbol].Price
if current_price >= self._purchasePrice * (1 + self._targetProfit):
self.Liquidate()
elif current_price <= self._purchasePrice * (1 - self._stopLoss):
self.Liquidate()
def GetBestStock(self, data):
stocks = [SecurityData(pair.Key, pair.Value.Close, pair.Value.Volume, pair.Value.BidVolume, pair.Value.AskVolume) for pair in data
if pair.Key != self._basketSymbol and pair.Value.BidVolume > 2 * pair.Value.AskVolume]
if not stocks:
return None
stocks.sort(key=lambda x: x.DailyVolume, reverse=True)
return stocks[0]
class SecurityData:
def __init__(self, symbol, price, daily_volume, bid_volume, ask_volume):
self.Symbol = symbol
self.Price = price
self.DailyVolume = daily_volume
self.BidVolume = bid_volume
self.AskVolume = ask_volume
Mia Alissi
The error message you're seeing is because the
TradeBar
object does not have aBidVolume
attribute. TheTradeBar
object has the following attributes:Open
,High
,Low
,Close
,Volume
,Time
,EndTime
, andSymbol
as shown in the documentation.If you want to access bid and ask data, you should use
QuoteBar
instead ofTradeBar
. TheQuoteBar
object hasBid
andAsk
attributes, each of which hasOpen
,High
,Low
,Close
, andLastSize
attributes. However, note that theBid
andAsk
attributes can potentially beNone
if no bid/ask data exists at a given point-in-time.Here is how you can access
QuoteBar
data:Please revise your code accordingly.
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.
Ferjo Ombwayo
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!