Hi All,
I've been reviewing everything I can find regarding the AddData/PythonData class. I have made a number of unsuccessful attempts to pull in some FRED data regarding daily federal funds rates. My desire is for either or both SPY and DFF data to appear in the OnData method call. I think that sometimes there might be SPY only or DFF only day but mostly they would appear on the same day.
If so, I have a number of questions:
- First of all, is that realistic? Will QuantConnect synthesize one stream of data where there might be gaps?
- I don't see the DFF data in the dozens and dozens of FRED symbols. That's not a big problem as I am able to call their API directly.
- Perhaps I've overlooked the DFF data source?
- In the attached backtest, I've defined a PythonData class and there are a number of issues with it.
- GetSource doesn't seem to have a way to accept the API key and observation start date.
- Where is the documentation for FileFormat.UnfoldingCollection? I am guessing that it does what I want but perhaps that's one issue?
- How can I pass in API key and start date?
- The Reader method is never called. Why?
- Most of the AddData examples appear to expect to read a line of data, like a line from a CSV file. The FRED DFF is a json file and looks like so:
{
"count" : 1,
"file_type" : "json",
"limit" : 100000,
"observation_end" : "9999-12-31",
"observation_start" : "2022-09-01",
"observations" : [
{
"date" : "2022-09-01",
"realtime_end" : "2022-09-06",
"realtime_start" : "2022-09-06",
"value" : "2.33"
}
],
"offset" : 0,
"order_by" : "observation_date",
"output_type" : 1,
"realtime_end" : "2022-09-06",
"realtime_start" : "2022-09-06",
"sort_order" : "asc",
"units" : "lin"
}
Here's the beginning part of my PythonData class:
# Doesn't work.
# - Never calls the Reader method, even when supplied with a valid key. Why?
# - need to find a way to pass in the API key and not hard code it in the GetSource call.
# - need to find a way to pass in the start date and not hard code it in the GetSource call.
#
class DailyFedFundRates2(PythonData):
def GetSource(self, config, date, isLiveMode):
source = f"https://api.stlouisfed.org/fred/series/observations?series_id=DFF&api_key=REDACTED&file_type=json&observation_start=2022-01-01"
return SubscriptionDataSource(source, SubscriptionTransportMedium.RemoteFile, FileFormat.UnfoldingCollection)
def Reader(self, config, line, date, isLiveMode):
if not (self.line.strip()):
raise Exception(f'Cannot read FRED DFF data. Aborting.' )
I've attached the code. I'm guessing there's some obvious thing I've missed? Advice welcome.
Thank you.
Regards,
Rich
Derek Melchin
Hi Rich,
If the SPY and DFF data have the same EndTime, LEAN will pass them to your algorithm in a single Slice.
There is currently no DFF data in the FRED integration.
You can hardcode your API key into the source URL. If you need to make the observation date dynamic, you can use the date argument. If you just want to store the API and observation date outside of the method, define some class variables in the PythonData definition or some module/global variables.
If the API returns a json object with multiple observations, use FileFormat.UnfoldingCollection. For more information about it, see Define Custom Types.
Either hardcode them into the source URL or save them as class/global variables.
Check if the API endpoint is actually returning any data.
For a full JSON example, see JSON Format Example.
Best,
Derek Melchin
Want to invest in QuantConnect as we build the Linux of quant finance? Checkout our Wefunder campaign to join the revolution.
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.
Rich Duzenbury
Hi Derek,
Thanks so much for getting back with me. I really appreciate it.
You mentioned several items, including a link to the same documentation that I've been looking at. The issue I have with it is this:
I don't understand:
– Why am I allowed to put a breakpoint in the Reader method but it is never hit. Is that a bug?
– If the Reader method is erroring out, I'm not getting any notification. I even tried throwing an error purposefully to no avail.
– I did manage to finally get it working by passing in my QCAlgorithm so that I could at least repeatedly call .Log.
(Credit to a discord comment by ‘Max’ on 12/9/21)
Here's the working version:
Thanks so much for the assist.
Regards,
Rich
Derek Melchin
Hi Rich,
Correct.
Yes, thanks for reporting this bug. To be notified when we fix this issue, subscribe to GitHub Issue 6625
We weren't able to reproduce this issue. We added
to line 172 of the BubbleAlgorithm and we got the following error:
Best,
Derek Melchin
Want to invest in QuantConnect as we build the Linux of quant finance? Checkout our Wefunder campaign to join the revolution.
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.
Rafael Trevisan
Hey Rich Duzenbury, I share your pain. I don't know how many times I tried to achieve reading a custom universe from a json rest and I gave up.
I literally just tried it again with this example
but guess what, copying/pasting the code didn't work and the demonstration algorithm at the end of the page is far from the examples, like the page says about reading json and the github demo is about csv with other file structure and so.
I will try to go over your code to see if I can adapt to my needs here.
Alexandre Catarino
Hi Rafael Trevisan ,
Please find the example from the documentation in the attached algorithm. It works as expected. We can verify it by adding a breakpoint in the FilterFunction method and inspecting the list variable.
I removed the typing from the following line
to avoid adding the StockDataSource class definition before the QCAlgorithm-inherited class or defining it in another file.
If the Community have any suggestion on how to improve the Importing Data section of the documentation, please let us know.
Best regards,
Alex
Want to invest in QuantConnect as we build the Linux of quant finance? Checkout our Wefunder campaign to join the revolution.
Rafael Trevisan
Thanks for replying Alexandre Catarino. I partially got it working after a lot of tries. For me, the trick was to change the SubscriptionTransportMedium from Rest to RemoteFile, which I think IMHO is misleading a bit as my data comes from a rest api and not from a remote file.
However it took much more time than I expected and the reason was the debug not working when I had the breakpoint anywhere in the code. I also couldn't log inside the GetSource or the Reader method which helped to make things a bit more challenging. I even found some post in the forum to define a static method in the PythonData class to refer to the algo so I could log, but it didn't work.
I didn't mean to be harsh. The platform is great but sometimes it's more challenging to connect pieces together than others, especially when coding in Python isn't your expertise, like for i.e., defining a Factory to be able to share variables between the algo and the other class.
Rafael Trevisan
Just adding another comment, I have a case here where adding a breakpoint in the very first line of the FilterSelector function won't work at all. It seems that if the GetSource or Reader gets any errors the algo isn't throwing them out and you can't debug.
Derek Melchin
Hi Rafael,
This issue will be addressed by GitHub Issue #6625.
Best,
Derek Melchin
Want to invest in QuantConnect as we build the Linux of quant finance? Checkout our Wefunder campaign to join the revolution.
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.
Derek Melchin
Hi everyone,
GitHub Issue #6625 has been fixed. We can now use the built-in debugger to debug PythonData classes.
Best,
Derek Melchin
Want to invest in QuantConnect as we build the Linux of quant finance? Checkout our Wefunder campaign to join the revolution.
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.
Rich Duzenbury
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!