im going to give you a full pinescript study called "Heikin Ashi RSI Oscillator" By JayRogers and i want you to add the following to it and make it a strategy so i can backtest on tradingview:
The core of this strategy is "Heikin Ashi RSI Oscillator (HARSI)" By JayRogers, with multiple confirmation indicators. The buy signal is generated by HARSI when there is green HA, and there is a positive stochastic cross under the line OB line.
To confirm the buys signal, you can use one or all of the following:
EMA: the buy confirmation is positive if the candle closes above the EMA.
Volume Oscillator: the buy confirmation is positive if the volume value is above the zero line.
ADX: the buy confirmation is positive if the D+ is above the D- and the D+ is above the threshold level.
Vortex: the buy confirmation is positive if VI+ is above VI-.
You can enable the take profit, trailing stop loss (TSL), and stop lose (SL), if not, then the strategy will give a sell signal by itself when there is a negative stochastic cross above OB Extreme line.
Note, that the TSL arming value should be lower than the TSL %.
The strategy can give a different result based on your selection. However, the best timeframe is 4 hours without TP.
here is the "Heikin Ashi RSI Oscillator" By JayRogers:
// This source code is free to use, copy, and alter in any way you choose.
// ...but credit is always nice :)
////////////////////////////////////////////////////////////////////////////////
// //
// ====== ABOUT THIS INDICATOR //
// //
// - RSI based Heikin Ashi candle oscillator //
// //
// ====== ARTICLES and FURTHER READING //
// //
// - https://www.investopedia.com/terms/h/heikinashi.asp //
// //
// "Heikin-Ashi is a candlestick pattern technique that aims to reduce //
// some of the market noise, creating a chart that highlights trend //
// direction better than typical candlestick charts" //
// //
// ====== REASON FOR STUDY //
// //
// - Mostly experimental. I wanted to see if I could translate RSI into a //
// Heikin Ashi function and retain it's oscillating nature. That goal //
// was met more easily than I anticipated with quite delightful results. //
// //
// ====== DISCLAIMER //
// //
// Any trade decisions you make are entirely your own responsibility. //
// I've made an effort to squash all the bugs, but you never know! //
// //
////////////////////////////////////////////////////////////////////////////////
string TT_HARSI = "Period for the RSI calculations used to generate the" +
"candles. This seperate from the RSI plot/histogram length."
string TT_PBIAS = "Smoothing feature for the OPEN of the HARSI candles." +
"\n\nIncreases bias toward the prior open value which can" +
" help provide better visualisation of trend strength." +
"\n\n** By changing the Open values, High and Low can also" +
" be distorted - however Close will remain unchanged."
string TT_SMRSI = "This option smoothes the RSI in a manner similar to HA" +
" open, but uses the realtime rsi rather than the prior" +
" close value."
string TT_STOCH = "Uses the RSI generated by the above settings, and as such" +
" will be affected by the smoothing option."
string TT_STFIT = "Adjusts the vertical scaling of the stochastic, can help" +
" to prevent distortion of other data in the channel." +
"\n\nHas no impact cross conditions."
// mode selectable rsi function for standard, or smoothed output
f_rsi( _source, _length, _mode ) =>
code
// get base rsi
float _zrsi = f_zrsi( _source, _length )
// smoothing in a manner similar to HA open, but rather using the realtime
// rsi in place of the prior close value.
var float _smoothed = na
_smoothed := na( _smoothed[1] ) ? _zrsi : ( _smoothed[1] + _zrsi ) / 2
// return the requested mode
_mode ? _smoothed : _zrsi
// RSI Heikin-Ashi generation function
f_rsiHeikinAshi( _length ) =>
code
// get close rsi
float _closeRSI = f_zrsi( close, _length )
// emulate "open" simply by taking the previous close rsi value
float _openRSI = nz( _closeRSI[1], _closeRSI )
// the high and low are tricky, because unlike "high" and "low" by
// themselves, the RSI results can overlap each other. So first we just go
// ahead and get the raw results for high and low, and then..
float _highRSI_raw = f_zrsi( high, _length )
float _lowRSI_raw = f_zrsi( low, _length )
// ..make sure we use the highest for high, and lowest for low
float _highRSI = max( _highRSI_raw, _lowRSI_raw )
float _lowRSI = min( _highRSI_raw, _lowRSI_raw )
// ha calculation for close
float _close = ( _openRSI + _highRSI + _lowRSI + _closeRSI ) / 4
// ha calculation for open, standard, and smoothed/lagged
var float _open = na
_open := na( _open[ i_smoothing ] ) ? ( _openRSI + _closeRSI ) / 2 :
( ( _open[1] * i_smoothing ) + _close[1] ) / ( i_smoothing + 1 )
// ha high and low min-max selections
float _high = max( _highRSI, max( _open, _close ) )
float _low = min( _lowRSI, min( _open, _close ) )
// return the OHLC values
[ _open, _high, _low, _close ]
// channel fill
fill( upper, upperx, color.new( color.red, 90 ), title = "Background Fill OB" )
fill( upper, lower, color.new( color.blue, 90 ), title = "Background Channel" )
fill( lower, lowerx, color.new( color.green, 90 ), title = "Background Fill OS" )
// histogram first, so it is on the bottom of the plot/candle draw stack
plot( i_showHist ? RSI : na, "RSI Histogram", color.new( color.silver, 80 ), 1, plot.style_histogram )
// make our HA rsi candles
plotcandle( O, H, L, C, "HARSI", bodyColour, wickColour, bordercolor = bodyColour )
// Stochastic RSI plots and fill
plot( i_showStoch ? StochK : na, "Stoch K Shadow", not i_ribbon ? colShadow : colNone, 3 )
plot( i_showStoch ? StochD : na, "Stoch D Shadow", not i_ribbon ? colShadow : colNone, 3 )
plot_stochK = plot( i_showStoch ? StochK : na, "Stoch K", not i_ribbon ? colStochK : colNone, 1 )
plot_stochD = plot( i_showStoch ? StochD : na, "Stoch D", not i_ribbon ? colStochD : colNone, 1 )
fill( plot_stochK, plot_stochD, i_ribbon ? colStochFill : na )
// -- PEANUT
Sure! Below is the modified Pine Script that transforms the "Heikin Ashi RSI Oscillator" into a comprehensive trading strategy with multiple confirmation indicators, take profit (TP), trailing stop loss (TSL), and stop loss (SL) functionalities. This strategy can be backtested on TradingView.
Key Features Added:
Strategy Initialization: Converted the script from a study to a strategy.
Buy Signals: Generated when HARSI indicates a green Heikin Ashi candle and the Stochastic RSI crosses below the Overbought (OB) line.
Sell Signals: Triggered either by a Stochastic RSI crossing above the OB Extreme line or through TP, SL, and TSL mechanisms.
Confirmation Indicators: Optional confirmations using EMA, Volume Oscillator, ADX, and Vortex indicators.
Risk Management: Configurable TP, SL, and TSL with validation to ensure TSL arming value is lower than the TSL percentage.
Configured default settings like initial capital, leverage, and calculation on every tick.
Confirmation Indicators:
EMA: Exponential Moving Average to confirm trend direction.
Volume Oscillator: Indicates increasing or decreasing volume.
ADX: Measures trend strength; requires D+ > D- and above a threshold.
Vortex Indicator: Confirms trend direction by comparing VI+ and VI-.
Each confirmation indicator has an enable toggle and configurable parameters. The conditions are only applied if the respective indicator is enabled.
Buy Condition:
Base Buy: HARSI indicates a green candle and Stochastic RSI crosses below the Overbought (OB) line.
Confirmations: All enabled confirmation indicators must satisfy their respective conditions.
Sell Condition:
Base Sell: Stochastic RSI crosses above the OB Extreme line.
Risk Management Exits: Take Profit, Stop Loss, and Trailing Stop Loss are implemented as additional exit strategies.
Risk Management:
Take Profit (TP): Automatically closes the position when a specified percentage gain is achieved.
Stop Loss (SL): Automatically closes the position to prevent excessive loss.
Trailing Stop Loss (TSL): Moves the stop loss level in favor of the trade as the price moves, locking in profits.
The strategy ensures that TSL is only armed after the price has moved by a certain percentage (tsl_arm_level).
Plotting and Visualization:
Confirmation indicators are plotted on the chart for visual reference.
Buy and Sell signals are marked on the chart with labels.
Alerts are set up for Buy and Sell signals to notify the trader in real-time.
Function ta_vortex:
Implemented a basic Vortex Indicator calculation since Pine Script v4 doesn't have a built-in Vortex function. Adjust as necessary based on the specific Vortex calculation you prefer.
Usage Instructions:
Add to TradingView:
Open TradingView and navigate to the Pine Editor.
Paste the entire script above into the editor.
Click Add to Chart to apply the strategy.
Configure Inputs:
Adjust the parameters in the Inputs section according to your trading preferences.
Enable or disable confirmation indicators and risk management features as needed.
Backtest:
Use TradingView's Strategy Tester to backtest the performance of the strategy over historical data.
Analyze metrics like Net Profit, Drawdown, and Win Rate to evaluate effectiveness.
Optimization:
Fine-tune the input parameters to optimize the strategy's performance.
Consider different timeframes and market conditions for robust testing.
Notes:
Timeframe Recommendation: As mentioned, the strategy performs best on the 4-hour timeframe without Take Profit.
Vortex Indicator: Ensure the Vortex calculation aligns with your trading methodology. The provided function is a simplified version.
Risk Management: Always backtest and adjust risk parameters to align with your risk tolerance and trading objectives.
Version Upgrade: Consider upgrading to Pine Script v5 for enhanced features and better performance, if desired.
Feel free to customize and enhance the strategy further based on your specific trading requirements. Happy trading!