Hello All,
I am pretty new to python and algo trading, however, I have been trading for some time based on TA.
I did not realize it will be such a steep learning path and converting simple strategies to working algo strategies. However, I am thankful for this platform, so that there is a nice way to learn algo trading.
So I started learning python some 3 weeks ago and wanted to implement a simple strategy.
- Strategy -
- RSI based buy and sell
- if RSI crosses 30 mark on a daily basis - BUY (from low to high)
- if RSI crosses 70 mark on a daily basis - SELL (from high to low)
- Liquidate
- When BUY order RSI more than 50 mark
- When SELL order RSI less than 50 mark
- When LOSS on a position is 5% Close it
- How can I add SPY benchmark in the same chart as Equity chart?
- After algo stops on end date, I still have holdings. How can I liquidate all after end date is reached?
- What should I check next to improve this or any strategy, once I have started doing basic order placement and liquidating?
Derek Melchin
Hi Ray,
(1) We can plot the SPY benchmark on the Strategy Equity chart by adding the following lines to Initialize
self.benchmarkTicker = 'SPY' self.SetBenchmark(self.benchmarkTicker) self.initBenchmarkPrice = None
We then define a method to calculate the benchmark value
def UpdateBenchmarkValue(self): if self.initBenchmarkPrice is None: self.initBenchmarkCash = self.Portfolio.Cash self.initBenchmarkPrice = self.Benchmark.Evaluate(self.Time) self.benchmarkValue = self.initBenchmarkCash else: currentBenchmarkPrice = self.Benchmark.Evaluate(self.Time) self.benchmarkValue = (currentBenchmarkPrice / self.initBenchmarkPrice) * self.initBenchmarkCash
Lastly, we plot the value in OnData
self.UpdateBenchmarkValue() self.Plot('Strategy Equity', self.benchmarkTicker, self.benchmarkValue)
(2) To liquidate all the holdings at the end of the backtest, we can add the following snippet to the top of OnData
if (data.Time > self.EndDate - timedelta(1)): self.Liquidate() return
Note that the timedelta duration would need to be adjusted if the backtest ends on a Monday or weekend. See the attached backtest for a working example of these first 2 points.
(3) The next best step would be to adjust the algorithm so that it adheres to the Algorithm Framework. We have great documentation and a Bootcamp lesson on the topic I'd recommend checking out.
Best,
Derek Melchin
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.
Ray Trader
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!