Hello,
I am trying to run trading scripts in Quantconnect using my data from the local REST API. My data is of type JSON (it looks like this: https://raw.githubusercontent.com/thisisneon/importdata/main/file.json)
I've looked for "Custom Data" examples, but there's no such thing as a tutorial using JSON data and the local REST API. Is there a way to add data using a JSON URL or from the local REST API?
I have been able to add my data from a CSV URL, but from the JSON URL and the local REST API, there is still an issue.
Looking forward to your help.
Cheers,
Quang Ha
(This is my project of adding data from the CSV URL )
Cole S
Hi Quang,
It does not appear that JSON is a supported file format for the SubscriptionDataSource. Below I have posted the code that shows the file formats supported. My suggestion would be to create an intermediary class that can convert the JSON to CSV on the fly.
public enum FileFormat
{
/// <summary>
/// Comma separated values
/// </summary>
Csv,
/// <summary>
/// Binary file data
/// </summary>
Binary,
/// <summary>
/// Only the zip entry names are read in as symbols
/// </summary>
ZipEntryName,
/// <summary>
/// Reader returns a BaseDataCollection object.
/// </summary>
/// <remarks>Lean will unfold the collection and consume it as individual data points</remarks>
UnfoldingCollection,
/// <summary>
/// Data stored using an intermediate index source
/// </summary>
Index,
/// <summary>
/// Data type inherits from BaseDataCollection.
/// Reader method can return a non BaseDataCollection type which will be folded, based on unique time,
/// into an instance of the data type.
/// </summary>
FoldingCollection
}
Fred Painchaud
Hi Quang,
I am not sure what you have in mind when you refer to the “local REST API”. Would you please elaborate on what you are referring to?
Converting JSON to CSV on the fly can do it but you could also make sure that the JSON for your file is formatted as one entry per line, which is not typical for JSON but the point is that Reader will give you each of those lines…. you could then parse those JSON lines (one by one as you get them) and return your object with proper attributes set.
Cheers,
Fred
P.S. The code posted makes me wonder what happens when LEAN sees the format as “Binary”. What are “string lines” then (in Reader())….. I will investigate. It appears that file formats detection is based on extensions and from what your object inherits. I believe binary would be when the extension is .dat for instance or when all other cases are not met. Anyway, I will look at the code to see how LEAN parses binary to cut it in strings. Binary does not look compatible with the Reader model. It may be that the Python model provided (through PythonData) is a subset of what can be done in C#, for simplicity.
Quang Ha
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!