Introduction to Stochastic RSI (S) Strategy
The Stochastic RSI (S) strategy, developed by MachinaLabs Ltd., is a powerful trading algorithm designed to identify potential buy signals based on the Stochastic RSI indicator in financial markets. The Stochastic RSI is a combination of two popular technical indicators, the Relative Strength Index (RSI) and the Stochastic Oscillator.
How it Works:
The strategy calculates the Stochastic RSI indicator using user-configurable parameters, including the lookback period, period for fast %K, and period for fast %D. The Stochastic RSI measures the RSI values’ relative position within their recent trading range, helping to identify overbought and oversold conditions.
Trading Logic:
The strategy generates a “buy” signal when the Stochastic RSI crosses above a specified “Oversold” threshold and the fast %K value is below the same threshold. This combination indicates a potential buying opportunity after the market has been oversold.
When no buy signal is detected, the strategy holds its position in the market, waiting for the next opportunity.
Customizability:
The Stochastic RSI (S) strategy offers customization options through various user-defined parameters. Traders can adjust the lookback period and other indicator settings, tailoring the strategy to suit different market conditions and trading preferences.
Visualization:
To aid in decision-making, the strategy visualizes the Stochastic RSI values on the chart, highlighting the fast %K and %D lines. Additionally, the strategy plots horizontal lines on the chart to represent the “Overbought” and “Oversold” thresholds.
Usage:
The Stochastic RSI (S) strategy is available on the MachinaTrader platform, providing traders with an insightful tool for identifying potential entry points in the markets. By incorporating the Stochastic RSI indicator into their trading approach, users can enhance their decision-making process and optimize their trading strategies.
Note:
As with any trading strategy, users are encouraged to backtest the Stochastic RSI (S) strategy on historical data and exercise caution when applying it to live trading. Past performance does not guarantee future results, and risk management is essential in trading.
# Strategy Name: Stochastic RSI (S) # Author: MachinaLabs Ltd. # Update Date: 2022-06-19 # Importing required libraries import sys, talib, requests, json, numpy, base64, json, enum, datetime, array, math, mtCommon # Define strategy parameters and constants STRAT_NAME = 'Stochastic RSI (S)' VERSION = '1.0' STRAT_TYPE = mtCommon.SimpleStrategy # Strategy Parameters NRCANDLES = '#Candles lookback' PFK = 'Period Fast K' PFD = 'Period Fast D' MATYPE = 'MA_Type' OVERBOUGHT = 'Overbought' OVERSOLD = 'Oversold' BOUGHT = True # ------------------------------------------------------------------- # Global methods - Not specific to any market # ------------------------------------------------------------------- def onSendParams(): # Build up parameters to send to MT _mt.addNumericParameter(NRCANDLES, 30, True) _mt.addNumericParameter(PFK, 30, True) _mt.addNumericParameter(PFD, 3, True) _mt.addNumericParameter(MATYPE, 1, True) #MA_Type: 0=SMA, 1=EMA, 2=WMA, 3=DEMA, 4=TEMA, 5=TRIMA, 6=KAMA, 7=MAMA, 8=T3 (Default=SMA) _mt.addNumericParameter(OVERSOLD, 21, True) _mt.addNumericParameter(OVERBOUGHT, 75, True) # ------------------------------------------------------------------- # Per market methods - Execution triggered per market (as per machina configuration) # ------------------------------------------------------------------- def onInit(): _mt.logInfoP('onInit called [' + STRAT_NAME + ', v' + VERSION + ']') BOUGHT = False def onTick(currentDate, candles, config): # Create an instance of the Calcs class to perform calculations calcs = Calcs(candles) if len(candles.C) == 0: _mt.logError('No candles returned for ' + config.MARKET) return # Implement the trading logic based on the calculated indicators if calcs.shouldBuy(_mt): _mt.buy(config.MARKET) BOUGHT = True _mt.logInfoP('buy') else: _mt.hold(config.MARKET) _mt.logInfoP('hold') def onSendIndicatorModels(candles): # Create an instance of the Calcs class to perform calculations calcs = Calcs(candles) # Add Stochastic RSI indicators to the chart stochrsi = _mt.createIndicatorModel('StochRSI', True) stochrsi.addSimpleIndicator('FastK', calcs.fastk, 'green', mtCommon.Line, mtCommon.Solid, mtCommon.Slim) stochrsi.addSimpleIndicator('FastD', calcs.fastd, 'red', mtCommon.Line, mtCommon.Solid, mtCommon.Slim) stochrsi.addHorizontalLine(OVERSOLD, yAxis=int(_mt.getParameter(OVERSOLD)), color='white', style=mtCommon.Solid, width=mtCommon.Slim) stochrsi.addHorizontalLine(OVERBOUGHT, yAxis=_mt.getParameter(OVERBOUGHT), color='white', style=mtCommon.Solid, width=mtCommon.Slim) _mt.addIndicatorModel(stochrsi) xovers = _mt.createIndicatorModel('Cross Overs') xovers.addShapeIndicator('xUnder', calcs.crossOver, 'rgba(45, 188, 32, 0.5)', mtCommon.TriangleUp, mtCommon.BelowBar, mtCommon.Small) _mt.addIndicatorModel(xovers) class Calcs: # Calculation class used to keep all calculations within a re-usable module def __init__(self, candles): # Calculate values self.onCalculate(candles, _mt) def onCalculate(self, candles, mt): self.IDX = len(candles.T) - 1 # Calculate Stochastic RSI values self.fastk, self.fastd = talib.func.STOCHRSI(candles.C, timeperiod=int(mt.getParameter(NRCANDLES)), fastk_period=int(mt.getParameter(PFK)), fastd_period=int(mt.getParameter(PFD)), fastd_matype=int(mt.getParameter(MATYPE))) # Determine cross over events self.crossOver = _mt.crossOver(self.fastk, self.fastd) def shouldBuy(self, mt): # Check if a "buy" condition is met return self.crossOver[len(self.crossOver) - 1] and (self.fastk[len(self.crossOver) - 1] < mt.getParameter(OVERSOLD))