## RSI Strategy Query

Hi All,

I want to realize a function by using RSI.

When RSI is over 80, short EURUSD, Take Profit when RSI hits 50, Stop Loss =100pips;

When RSI is below 20 long EURUSD, Take Profit when RSI hits 50, Stop Loss =100pips;

Each trade, i want to use half of the money for trading. Could you help on the code? Thanks.

import numpy as np

class RSIAlgorithm(QCAlgorithm):

def Initialize(self):

# Set our main strategy parameters
self.SetStartDate(2015,1, 1)   # Set Start Date
self.SetEndDate(2018,1,1)      # Set End Date
self.SetCash(10000)            # Set Strategy Cash

RSI_Period    = 14                # RSI Look back period
self.RSI_OB   = 80                # RSI Overbought level
self.RSI_OS   = 20                # RSI Oversold level
self.Allocate = 20             # Percentage of captital to allocate

# Find more symbols here: http://quantconnect.com/data
self.RSI_Ind = self.RSI("EURUSD", RSI_Period)

# Ensure that the Indicator has enough data before trading,.
self.SetWarmUp(RSI_Period)

def OnData(self, data):
if not self.Portfolio.Invested:
# If not, we check the RSI Indicator
if self.RSI_Ind.Current.Value < self.RSI_OS:
self.SetHoldings("EURUSD", self.Allocate)
else:
if self.RSI_Ind.Current.Value > self.RSI_OB:
self.short("EURUSD")

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 Yao, I was able to draft up a quick algorithm. You can see in the backtest below I implemented most of the strategy except the stop loss, but that can be learned here. I commented out some code in the algorithm that should help as well.

I use half the money by  investing half of my portfolio. SetHoldings sets a fraction of unlevered equity. So pick a value between -1 (short) and 1 (long).

Just an observation: there weren't as many trading signals as when I used 70/30 as the OB/OS values.

1

Hi Yao.

There are useful examples and tutorials on how to implement algorithms in the Bootcamp section and also in this documentation section on Initializing Algorithms.

I've attached a backtest where I demonstrate how to implement your algorithm.  Also, I've added a "Trade Plot" where I plot the value of the RSI indicator along with overbought/oversold levels. The algorithm uses StopMarketOrder() to open a position, and it sets a Stop-Loss level 100 pips away from the exchange rate. The weight of each opened position is 50% of the portfolio value.

1

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.