I need to upload a list of tickers (in JSON-format: {"TSLA":10,"AAPL":20,"SPY":30} ) for adding those tickets to my portfolio in my trading strategy.
How can I upload the fail and How can I access the file from my strategy? (if the answer is in documentation, please give me a specific link)?
Thanks.
Fred Painchaud
Hi,
If you want to download a list of tickers in JSON format from your algo, you can use “string self.Download(string URI)” in QCAlgorithm. Note the contents of the file is returned as a string. You can then parse JSON. The easiest would most probably be to use the json Python module for that.
Never tested but it should work. I've seen json in the list of supported python librairies.
Fred
Bolt.Investments
Oops... There became more questions...
1) Do I have to publish my list on the internet firstly? It is inappropriate< because I publish a core of my strategy - a list of shares.
2) I expected It is possible to upload MyFile.json to the QuantConnect folder and after that, I will be read the file from python-code.
In fact, the main question: How can I upload my file (.json? .csv?... ) to project-folder?
(P.S. Thanks for the answer, but I expected other answers).
Gabi.C
Hi,
You don't need to publish you list.
Could use:
the output will be: 10
Gabi
Fred Painchaud
Hi Bolt,
The code I provided downloads a list that can change and your algo can periodically re-download it. The example provided by Gabi is more or less the same but integrates a static list into the algo. Of course, if you do not need the dynamic (changing) nature for the list, don't bother uploading, as you wanted, or downloading, as I answered, just copy and paste the list into your code, of course.
Fred
Bolt.Investments
I tryed:
contents = self.Download(address="sftp://my_login@my_domen.com:20022/home/my_login/Downloads/pricing/file.txt", userName="my_login", password="my_password")
I have got:
TypeError : No method matches given arguments for Download: ()
Fred Painchaud
Hi,
You are missing a param. Try:
but I do not know if LEAN handles SFTP… It will depend with what it is implemented on the C# side. I'd bet it is not implemented…
But I just noticed you include your login/password in the URL. So it's worth also trying this:
But again, it depends on if the library used on the C# side handles SFTP. I'd be surprised.
You can also just copy and paste the contents of your file directly inside your algo, as already mentioned, since it is JSON (so text).
Fred
Bolt.Investments
Hi. Thanks for a help
About sftp. At least I cannot specify a port in URI. CLI command is:
sftp -oPort=20022 my_login@my_domen.com:/home/my_login/Downloads/pricing/.
So in Download() method, there is no parameter for the port.
The link "sftp://my_login@my_domen.com:20022/home/my_login/Downloads/pricing/file.txt" cannot work. Because it means the string "20022/home/my_login/Downloads/pricing/" is a folder name.
Bolt.Investments
The problem is not in a port. See error message:
"
20220310 18:30:32.382 ERROR:: Engine.Run(): During the algorithm initialization, the following exception has occurred: WebException : Api.Download(): Failed to download
data from sftp://*****@******.com:/home/****/CryStone/ with username: **** and password *****. Please verify the source for
missing http:// or https://
"
*** are my changes.
The method does understand nothing except "http:// or https://".
Bolt.Investments
I found a solution!! It was very tricky! There is only one working syntaxis.---- csv: str = self.Download(address="ftp://my_ftp_login:my_ftp_password@files.000webhost.com/public_html/Portfolios.csv", headers={}, userName="my_ftp_login", password="my_ftp_password")-----You must specify my_ftp_login and my_ftp_password two times!! Because local backtesting requires parameters ----headers={}, userName="my_ftp_login", password="my_ftp_password")----But cloud backtesting understands only address= parameter!
Fred Painchaud
Hi,
Just so you know, as a Python developer, you are used to named parameters, of course. This is common in Python. However, in LEAN, your Python code interacts with C# code in .NET. So it is not “standard” Python development, or “pure” Python development. One difference is that named parameters are not supported, they are ignored. Thus, I would advise you not to use them in your Python code to avoid potential subtle problems with your code.
For instance, instead of calling:
try calling:
Standard Python code would not have any issue with that - you can pass named parameters in any order. But as named parameters are not supported by the technology used to bridge Python with .NET in LEAN, the second call is in fact interpreted as:
which of course, does not have the parameters in the right order, at the right places, so there is no method that corresponds to those parameters. Note: if there is no method, you'll get a runtime error which is nice because you know there is a problem. But if the call happens to match another version of the method, that other version will be called without errors but you may very likely call a method that does things a bit differently and you don't know your algo is actually doing that / calling that method instead of the one you believe it is calling…
This also means that you cannot mix positional parameters with named parameters to only pass a subset of all parameters to a method. For instance, you cannot do this:
or more interestingly this:
as these calls are really seen as:
which of course correspond to no known methods.
Fred
Fred Painchaud
Hi again,
Ok. Just to be super clear for posterity.
I should have written:" One difference is that named parameters are not supported, they are ignored when your Python code calls C#/.NET code inside LEAN. "
Of course, named parameters are NOT ignored when Python code calls other Python code since then, the entire execution happens in the Python runtime.
There, my terms stand clear. 😊
Fred
Bolt.Investments
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!