Back

Need Help on Analysing Strategy

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.

  1. Strategy - 
    1. RSI based buy and sell
    2. if RSI crosses 30 mark on a daily basis -  BUY (from low to high)
    3. if RSI crosses 70 mark on a daily basis - SELL (from high to low)
  2. Liquidate
    1. When BUY order RSI more than 50 mark
    2. When SELL order RSI less than 50 mark
    3. When LOSS on a position is 5% Close it
My backtesting is attached Now QUESTIONS -
  1. How can I add SPY benchmark in the same chart as Equity chart?
  2. After algo stops on end date, I still have holdings. How can I liquidate all after end date is reached?
  3. What should I check next to improve this or any strategy, once I have started doing basic order placement and liquidating?
THANK you in advance for your help. Ray 
Update Backtest








0

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.


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

1

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.


Update Backtest





0

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.


Loading...

This discussion is closed