Heikin Ashi RSI Oscillator

JayRogers · study · 164 行 · 点赞 11,097 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

// This source code is free to use, copy, and alter in any way you choose.
// ...but credit is always nice :)
 
//@version=4
//@author=JayRogers
 
study( "Heikin Ashi RSI Oscillator", "HARSI •", false, format.price, 2 )
 
////////////////////////////////////////////////////////////////////////////////
//                                                                            //
// ====== 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!         //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
 
////////////////////////////////////////////////////////////////////////////////
//                                                                            //
//                            ====== INPUTS ======                            //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
 
string GROUP_CAND = "Config » HARSI Candles"
i_lenHARSI  = input( 14,            "Length",               input.integer,  group = GROUP_CAND,
                  minval = 1 )
string INLINE_COL = "Colour Pallette"
i_colUp     = input( color.teal,    "Colour Pallette  ",    input.color,    group = GROUP_CAND, inline = INLINE_COL )
i_colDown   = input( color.red,     " ",                    input.color,    group = GROUP_CAND, inline = INLINE_COL )
i_colWick   = input( color.gray,    " ",                    input.color,    group = GROUP_CAND, inline = INLINE_COL )
i_colBorder = input( #00000088,     " ",                    input.color,    group = GROUP_CAND, inline = INLINE_COL )
 
string GROUP_PLOT = "Config » RSI Plot"
i_source    = input( ohlc4,         "Source",               input.source,   group = GROUP_PLOT )
i_lenRSI    = input( 14,            "Length",               input.integer,  group = GROUP_PLOT,
                  minval = 1 )
i_mode      = input( false,         "Smoothed Mode RSI?",   input.bool,     group = GROUP_PLOT )
i_showPlot  = input( false,         "Show RSI Plot?",       input.bool,     group = GROUP_PLOT )
i_showHist  = input( true,          "Show RSI Histogram?",  input.bool,     group = GROUP_PLOT )
 
string GROUP_CHAN = "Config » OB/OS Boundaries"
i_upper     = input( 20,            "Channel Top",          input.integer,  group = GROUP_CHAN,
                  minval = 1, maxval = 50 )
i_lower     = input( -20,           "Channel Bottom",       input.integer,  group = GROUP_CHAN,
                  minval = -50, maxval = -1 )
 
////////////////////////////////////////////////////////////////////////////////
//                                                                            //
//                          ====== FUNCTIONS ======                           //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
 
//  conversion of color to RGBA components
f_getRGBA( _c ) => [ color.r( _c ), color.g( _c ), color.b( _c ), color.t( _c ) ]
 
//  zero median rsi helper function, just subtracts 50.
f_zrsi( _source, _length ) => rsi( _source, _length ) - 50
 
//  mode selectable rsi function for standard, or smoothed output
f_rsi( _source, _length, _mode ) =>
 
    //  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 ) =>
 
    //  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
    var float _open = na
    _open := na( _open[1] ) ? ( _openRSI + _closeRSI ) / 2 : ( _open[1] + _close[1] ) / 2
 
    //  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 ]
 
////////////////////////////////////////////////////////////////////////////////
//                                                                            //
//                   ====== SERIES, LINES and LABELS ======                   //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
 
//  standard, or ha smoothed rsi for the line plot and/or histogram
float RSI = f_rsi( i_source, i_lenRSI, i_mode )
 
//  get OHLC values to use in the plotcandle()
[ O, H, L, C ] = f_rsiHeikinAshi( i_lenHARSI )
 
//  candle body colouring
color bodyColour    = C > O ? i_colUp : i_colDown
color wickColour    = i_colWick
color borderColour  = i_colBorder
 
////////////////////////////////////////////////////////////////////////////////
//                                                                            //
//                     ====== DRAWING and PLOTTING ======                     //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
 
//  zero median RSI channel hlines
median  = hline( 0,         "Median",       color.orange, hline.style_dotted )
upper   = hline( i_upper,   "Upper Band",   color.new( color.silver, 80 ) )
lower   = hline( i_lower,   "Lower Band",   color.new( color.silver, 80 ) )
 
//  channel fill
fill( upper, lower, color.new( color.fuchsia, 90 ), title = "Background Fill")
 
//  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 Candles", bodyColour, wickColour, bordercolor = borderColour )
 
//  finally draw our choice of standard, or HA smoothed rsi plot. 
plot( i_showPlot ? RSI : na,    "RSI Plot Shadow",  color.rgb( 0, 0, 0, 50 ),     3 )
plot( i_showPlot ? RSI : na,    "RSI Plot Overlay", color.rgb( 250, 200, 50, 0),  1 )
 
// -- PEANUT

← 返回列表