Hello geniuses
i'd like to backtest this strategy if anyone can help me. i'd be grateful. thank you.
Strategy with XOM (Exxon Mobil) and IBB
Analysis periods :
Four-year Reference Period for XOM and IBB: For example, from 2020 to 2023 to assess historical performance.
Two consecutive 4-year periods for IBB: From 2016 to 2019 and from 2020 to 2023, to assess IBB's upward trend.
Conditions for entering a Buy Position on IBB :
Condition 1 (XOM): Exxon Mobil must reach a new high in the current period (2024-2027) relative to its previous high in the four-year reference period (2020-2023).
Condition 2 (IBB): IBB must also reach a new high in the current period (2024-2027) in relation to its previous high in the same four-year reference period.
Condition 3 (IBB - Uptrend): IBB must have shown an uptrend for two consecutive three-year periods (2016-2019 and 2020-2023), defined by a closing price at the end of each period that is higher than the closing price at the end of the previous period.
Buy IBB: If all the above conditions are met, a buy position is taken on IBB at its new high.
With a 10% target
calculation of drawdown
and sharpe ratio calculation
the dates given are just to illustrate what I want to do. but you have to understand that I know when it's going to happen. so four-year reference period for XOM and IBB. Two consecutive 4-year periods for IBB.
thanks
Mia Alissi
To backtest the strategy described by Fares, we'll need to implement a few key components in QuantConnect's LEAN framework. This includes handling historical data for both XOM and IBB, checking the conditions for entering a buy position on IBB, and calculating the target, drawdown, and Sharpe ratio. Below is a simplified version of how you might start to implement this strategy in Python using QuantConnect. Note that this example focuses on the structure and key elements of the strategy rather than a complete, ready-to-run solution.
This code outlines the basic structure you'd need to start implementing Fares' strategy. Key points to expand upon include:
Remember, this is a starting point. Developing a fully functional and tested strategy will require further work, especially in handling historical data and implementing the specific conditions and calculations you've outlined.
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.
Fares amine benrahal
thank you milla but it doesn't really work, your code has errors
Mia Alissi
I understand the initial example might not have been fully functional or clear on how to implement the specific conditions and calculations required for your strategy. Let's refine the approach, focusing on a more detailed and corrected structure for implementing the strategy, especially regarding the historical data handling and condition checks. This example will still be somewhat simplified but will aim to provide a clearer direction.
Key Points to Note and Implement:
CheckConditionsAndTrademethod requests historical data for XOM and IBB to find previous highs. Adjust thelookbackPeriodas needed to match your specific time frames.CheckIBBUptrendmethod is a placeholder where you should implement the logic to verify IBB's uptrend condition.SetHoldingsfor simplicity. You might want to useMarketOrderor other order types depending on your strategy's specifics, including handling the exit based on a 10% target or other criteria.This example aims to provide
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.
Fares amine benrahal
thank you mia but it doesn't work someone else please ?
Mia Alissi
I understand the previous examples might not have fully met your needs or contained complexities that made them difficult to directly apply. Let's simplify the approach to focus on the core elements of your strategy, breaking it down into more manageable steps. This will not be a complete solution but should offer a clearer starting point for developing your strategy on QuantConnect.
Step 1: Define Your Universe and Initialize Variables
First, you need to set up your algorithm's initial configuration, including setting the start and end dates for the backtest, adding the securities you're interested in (XOM and IBB), and initializing any variables you'll need.
Step 2: Implement the Logic to Check Conditions
You'll need to periodically check the conditions you've outlined for entering a trade. This involves comparing the current price to previous highs and assessing the uptrend condition for IBB.
Step 3: Implement Exit Strategy and Performance Metrics
You mentioned a 10% target, drawdown calculation, and Sharpe ratio calculation. These will require additional logic to implement. For the 10% target, you could set a limit order at 10% above your buy price. Drawdown and Sharpe ratio are typically analyzed post-backtest using the backtest results.
Additional Considerations
Historymethod.Schedulefunction to periodically check your conditions instead of doing it every day, depending on your strategy's needs.This simplified approach should
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.
Sebastien M. Laignel
Hi
Not quite sure about what do you want but this code is running. Please check carefully that the trading logic matches your wishes.
Brgds
SL
Fares amine benrahal
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!