OPEN-SOURCE SCRIPT

RSI & Backed-Weighted MA Strategy

RSI & MA Strategy :


INTRODUCTION :

This strategy is based on two well-known indicators that work best together: the Relative Strength Index (RSI) and the Moving Average (MA). We're going to use the RSI as a trend-follower indicator, rather than a reversal indicator as most are used to. To the signals sent by the RSI, we'll add a condition on the chart's MA, filtering out irrelevant signals and considerably increasing our winning rate. This is a medium/long-term strategy. There's also a money management method enabling us to reinvest part of the profits or reduce the size of orders in the event of substantial losses.


RSI :

The RSI is one of the best-known and most widely used indicators in trading. Its purpose is to warn traders when an asset is overbought or oversold. It was designed to send reversal signals, but we're going to use it as a trend indicator by increasing its length to 20. The RSI formula is as follows :

RSI (n) = 100 - (100 / (1 + (H (n)/L (n))))

With n the length of the RSI, H(n) the average of days closing above the open and L(n) the average of days closing below the open.


MA :

The Moving Average is also widely used in technical analysis, to smooth out variations in an asset. The SMA formula is as follows :

SMA (n) = (P1 + P2 + ... + Pn) / n

where n is the length of the MA.

However, an SMA does not weight any of its terms, which means that the price 10 days ago has the same importance as the price 2 days ago or today's price... That's why in this strategy we use a RWMA, i.e. a back-weighted moving average. It weights old prices more heavily than new ones. This will enable us to limit the impact of short-term variations and focus on the trend that was dominating. The RWMA used weights :
  • The 4 most recent terms by : 100 / (4+(n-4)*1.30)
  • The other oldest terms by : weight_4_first_term*1.30

So the older terms are weighted 1.30 more than the more recent ones. The moving average thus traces a trend that accentuates past values and limits the noise of short-term variations.


PARAMETERS :

  • RSI Length : Lenght of RSI. Default is 20.
  • MA Type : Choice between a SMA or a RWMA which permits to minimize the impact of short term reversal. Default is RWMA.
  • MA Length : Length of the selected MA. Default is 19.
  • RSI Long Signal : Minimum value of RSI to send a LONG signal. Default is 60.
  • RSI Short signal : Maximum value of RSI to send a SHORT signal. Default is 40.
  • ROC MA Long Signal : Maximum value of Rate of Change MA to send a LONG signal. Default is 0.
  • ROC MA Short signal : Minimum value of Rate of Change MA to send a SHORT signal. Default is 0.
  • TP activation in multiple of ATR : Threshold value to trigger trailing stop Take Profit. This threshold is calculated as multiple of the ATR (Average True Range). Default value is 5 meaning that to trigger the trailing TP the price need to move 5*ATR in the right direction.
  • Trailing TP in percentage : Percentage value of trailing Take Profit. This Trailing TP follows the profit if it increases, remaining selected percentage below it, but stops if the profit decreases. Default is 3%.
  • Fixed Ratio : This is the amount of gain or loss at which the order quantity is changed. Default is 400, which means that for each $400 gain or loss, the order size is increased or decreased by a user-selected amount.
  • Increasing Order Amount : This is the amount to be added to or subtracted from orders when the fixed ratio is reached. The default is $200, which means that for every $400 gain, $200 is reinvested in the strategy. On the other hand, for every $400 loss, the order size is reduced by $200.
  • Initial capital : $1000
  • Fees : Interactive Broker fees apply to this strategy. They are set at 0.18% of the trade value.
  • Slippage : 3 ticks or $0.03 per trade. Corresponds to the latency time between the moment the signal is received and the moment the order is executed by the broker.
  • Important : A bot has been used to test the different parameters and determine which ones maximize return while limiting drawdown. This strategy is the most optimal on ETHUSD with a timeframe set to 6h. Parameters are set as follows :

MA type: RWMA
MA Length: 19
RSI Long Signal: >60
RSI Short Signal : <40
ROC MA Long Signal : <0
ROC MA Short Signal : >0
TP Activation in multiple ATR : 5
Trailing TP in percentage : 3


ENTER RULES :

The principle is very simple:
  • If the asset is overbought after a bear market, we are LONG.
  • If the asset is oversold after a bull market, we are SHORT.

We have defined a bear market as follows : Rate of Change (20) RWMA < 0
We have defined a bull market as follows : Rate of Change (20) RWMA > 0
The Rate of Change is calculated using this formula : (RWMA/RWMA(20) - 1)*100
Overbought is defined as follows : RSI > 60
Oversold is defined as follows : RSI < 40

LONG CONDITION :
RSI > 60 and (RWMA/RWMA(20) - 1)*100 < -1

SHORT CONDITION :
RSI < 40 and (RWMA/RWMA(20) - 1)*100 > 1


EXIT RULES FOR WINNING TRADE :

We have a trailing TP allowing us to exit once the price has reached the "TP Activation in multiple ATR" parameter, i.e. 5*ATR by default in the profit direction. TP trailing is triggered at this point, not limiting our gains, and securing our profits at 3% below this trigger threshold.

Remember that the True Range is : maximum(H-L, H-C(1), C-L(1))
with C : Close, H : High, L : Low
The Average True Range is therefore the average of these TRs over a length defined by default in the strategy, i.e. 20.


RISK MANAGEMENT :

This strategy may incur losses. The method for limiting losses is to set a Stop Loss equal to 3*ATR. This means that if the price moves against our position and reaches three times the ATR, we exit with a loss.
Sometimes the ATR can result in a SL set below 10% of the trade value, which is not acceptable. In this case, we set the SL at 10%, limiting losses to a maximum of 10%.


MONEY MANAGEMENT :

The fixed ratio method was used to manage our gains and losses. For each gain of an amount equal to the value of the fixed ratio, we increase the order size by a value defined by the user in the "Increasing order amount" parameter. Similarly, each time we lose an amount equal to the value of the fixed ratio, we decrease the order size by the same user-defined value. This strategy increases both performance and drawdown.


Enjoy the strategy and don't forget to take the trade :)
Average True Range (ATR)Moving AveragesRelative Strength Index (RSI)

Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in publication is governed by House rules. You can favorite it to use it on a chart.

Want to use this script on a chart?

Disclaimer