class MyCustomDataType(PythonData):
def GetSource(self,
config: SubscriptionDataConfig,
date: datetime,
isLive: bool) -> SubscriptionDataSource:
source = "https://docs.google.com/spreadsheets/d/e/2PACX-1vRCAhj2JCJX-f6oAHAW7G5zbXCrynpNIQqWnMyFkJABCERCKjvGP-Sx8MX2Zb1lCFWrEIY466CdwGsL/pub?output=csv"
return SubscriptionDataSource(source, SubscriptionTransportMedium.RemoteFile)
def Reader(self,
config: SubscriptionDataConfig,
line: str,
date: datetime,
isLive: bool) -> BaseData:
if not (line.strip()):
return None
index = MyCustomDataType()
index.Symbol = config.Symbol
try:
data = line.split(',')
index.Time = datetime.strptime(data[0], '%Y.%m.%d %H:%M:%S')
index.EndTime = index.Time + timedelta(hours=1)
index.Value = data[4]
index["XAU1"] = float(data[1])
index["USD1"] = float(data[2])
except ValueError:
self.Log("No Data (Reader) ")
# Do nothing
return None
return index
class MyAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetWarmUp(timedelta(50))
self.SetStartDate(2023, 5,30)
self.SetEndDate(2023, 7, 24)
self.SetCash(10000)
self.custom_symbol = self.AddData(MyCustomDataType, "MyCustomDataType", Resolution.Hour).Symbol
self.eurusd = self.AddForex("EURUSD", Resolution.Hour)
def OnData(self, slice: Slice) -> None:
if slice.ContainsKey(self.custom_symbol):
custom_data = slice[self.custom_symbol]
else:
self.Log("No Data (Algorithm)")
Mia Alissi
From the code you've provided, it seems like you're trying to import custom data from a CSV file hosted on Google Sheets into your algorithm. You've defined a custom data type
MyCustomDataType
and overridden theGetSource
andReader
methods, which is the correct approach according to the QuantConnect documentation.In your
Reader
method, you're correctly handling lines that are empty or contain unusable data by returningNone
. You're also correctly parsing the data from each line and assigning it to properties of your custom data type.In your algorithm
MyAlgorithm
, you're adding a subscription to your custom data type in theInitialize
method and attempting to access the custom data in theOnData
method. This is also the correct approach according to the QuantConnect documentation.If you're encountering issues, it might be due to the format of your CSV file or the way you're parsing the data. Make sure that the data in your CSV file is correctly formatted and matches the way you're parsing it in the
Reader
method. For example, you're expecting the date in the format '%Y.%m.%d %H:%M:%S', so make sure your CSV file follows this format.Also, ensure that the URL you're using in the
GetSource
method is correct and accessible. If the URL is incorrect or the file is not accessible, your algorithm won't be able to read the data.If you're still having issues, please provide more details about the problem you're facing.
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.
Vlady
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!