Mia Alissi Can you please assist with the following query:
Assume there is a csv file in the project directory containing a list of 3 stocks. Write code to loop through the list and separately backtest investing in each stock. Write the Sharpe Ratio for each backtest to another csv file in the project directory.
Thank you.
Mia Alissi
Here is a code template for what 'Dat' requested:
Instructions:
tickers.csv
file with one ticker per line in your project directory.sharpe_results.csv
file with the Sharpe Ratios for each ticker.Let me know if you'd like true per-backtest isolation scripting as well!
Sources: docs writing algorithms statistics algorithm statistics, Requesting Data - QuantConnect.com, Pairs Trading With Country ETFs, Multiple Symbol Consolidation Algorithm, Probabilistic Sharpe Ratio
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.
Dat En
Thanks Mia Alissi . However the example would run a single backtest for all symbols. Can the code be updated to run one backtest for each symbol consecutively?
Mia Alissi
Here is an updated approach that will run one backtest per symbol consecutively, writing the Sharpe Ratios to a CSV. This script is suitable for a QuantConnect Research Notebook or as an orchestrator using the QuantConnect API:
Key Notes for Use:
ticker
parameter (likely viaself.GetParameter("ticker")
inInitialize
).USER_ID
,API_TOKEN
, andPROJECT_ID
with your actual credentials and project ID.sharpe_results.csv
with columns:ticker,sharpe_ratio
.Let me know if you need help setting up the parameterized algorithm or adjusting any step!
Sources: Backtest Analysis - QuantConnect.com, Probabilistic Sharpe Ratio, Head & Shoulders TA Pattern Detection, Gaussian Naive Bayes Model, Results - QuantConnect.com
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.
Dat En
Thanks@mia-ai . Can you also please include a simple working example of the main.py file containing parameterized QCAlgorithm accepting a ticker parameter (using self.GetParameter("ticker") in Initialize).
S Ray
Hello,
I am getting the following error when running the updated script from above in a QuantConnect Research Notebook:
Â
---------------------------------------------------------------------------
NullReferenceException           Traceback (most recent call last)
Cell In[28], line 20, in run_backtest_for_ticker(ticker)
  18 # 1) Compile the project
  19 print(PROJECT_ID)
---> 20 compile_response = api.create_compile(PROJECT_ID)
  21 if compile_response is None:
  22   raise ValueError("No compile response returned.")
NullReferenceException: Object reference not set to an instance of an object.
 at QuantConnect.Api.Api.CreateCompile(Int32 projectId) in /LeanCloud/CI.Builder/bin/Release/src/QuantConnect/LeanEnterprise/Api/Api.cs:line 434
 at InvokeStub_Api.CreateCompile(Object, Span`1)
 at System.Reflection.MethodBaseInvoker.InvokeWithOneArg(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
------------------------------------------------------------------------
Â
I am using my own credentials. Am I missing something here?
Â
Thank you!
Louis Szeto
Hi S Ray
It is likely an API connection issue. I'd suggest you connect the research notebook then wait for a few seconds first. Then, run the code with the API. If the problem persists, please attach a backtest or code snippet for further assistance.
Best
Louis
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.
Dat En
Thank you
Dat En
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!