Skip to content

Instantly share code, notes, and snippets.

@princefr
Last active June 10, 2020 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save princefr/aa1ab893f8d3ec3de7fdf55028715da9 to your computer and use it in GitHub Desktop.
Save princefr/aa1ab893f8d3ec3de7fdf55028715da9 to your computer and use it in GitHub Desktop.
gist bot
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © prince_ond
//@version=4
study("mon script", shorttitle="Spartiate")
// === INPUTS ===
uDiv = input(true,"Show Divergence Channel")
multi = input(0.5, minval=0.0, maxval=3.0, title="Divergence Channel Width Factor (Stddev)")
uHid = input(true, title="Show Hidden Divergence")
uReg = input(true, title="Show Regular Divergence")
rsi_length = input(14, title="RSI_period")
uDiv := uReg or uHid ? uDiv : false
fast_length = input(title="Fast Length", type=input.integer, defval=10)
slow_length = input(title="Slow Length", type=input.integer, defval=26)
src = input(title="Source", type=input.source, defval=close)
signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 50, defval = 3)
sma_source = input(title="Simple MA(Oscillator)", type=input.bool, defval=false)
sma_signal = input(title="Simple MA(Signal Line)", type=input.bool, defval=false)
stoch_os_level = input(-60, minval=1)
stoch_ob_level = input(60, minval=1)
rsi_A = (rsi(close, rsi_length) - 50) * 3
k = sma(rsi_A, 23)
k2 = ema(rsi_A, 2)
p4 = plot(rsi_A, color=color.blue, title="rsi")
p5 = plot(k, color=color.orange, title="sma_A")
p6 = plot(k2, color=color.purple, title="sma_B")
//fill(p4, p5, color=k > rsi_A ? color.red : color.green, transp=10, title='Stoch BG')
// === HLine ===
h0 = hline(stoch_ob_level)
h1 = hline(stoch_os_level)
middle_level = (stoch_os_level + stoch_ob_level) / 2
// === Colors ===
col_grow_above = #26A69A
col_grow_below = #FFCDD2
col_fall_above = #B2DFDB
col_fall_below = #EF5350
col_macd = #0094ff
col_signal = #ff6a00
// === Calculating Historigram ===
fast_ma = sma_source ? sma(src, fast_length) : ema(src, fast_length)
slow_ma = sma_source ? sma(src, slow_length) : ema(src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal ? sma(macd, signal_length) : ema(macd, signal_length)
hist = (macd - k) / 5
plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below) ), transp=0 )
// === Utilities ===
// || Functions:
f_top_fractal(_src)=>_src[4] < _src[2] and _src[3] < _src[2] and _src[2] > _src[1] and _src[2] > _src[0]
f_bot_fractal(_src)=>_src[4] > _src[2] and _src[3] > _src[2] and _src[2] < _src[1] and _src[2] < _src[0]
f_fractalize(_src)=>f_top_fractal(_src) ? 1 : f_bot_fractal(_src) ? -1 : 0
// === /BASE FUNCTIONS ===
rsi_high = rsi_A
rsi_low = rsi_A
offset_ = multi*stdev(rsi_A, 20)
fractal_top_rsi = f_fractalize(rsi_high) > 0 ? rsi_high[2] : na
fractal_bot_rsi = f_fractalize(rsi_low) < 0 ? rsi_low[2] : na
rsi_high_prev = valuewhen(fractal_top_rsi, rsi_high[2], 1)
rsi_high_price = valuewhen(fractal_top_rsi, high[2], 1)
rsi_low_prev = valuewhen(fractal_bot_rsi, rsi_low[2], 1)
rsi_low_price = valuewhen(fractal_bot_rsi, low[2], 1)
regular_bearish_div = fractal_top_rsi and high[2] > rsi_high_price and rsi_high[2] < rsi_high_prev and rsi_high>0
hidden_bearish_div = fractal_top_rsi and high[2] < rsi_high_price and rsi_high[2] > rsi_high_prev and rsi_high>0
regular_bullish_div = fractal_bot_rsi and low[2] < rsi_low_price and rsi_low[2] > rsi_low_prev and rsi_low<0
hidden_bullish_div = fractal_bot_rsi and low[2] > rsi_low_price and rsi_low[2] < rsi_low_prev and rsi_low<0
plot(title='RSI High', series=uDiv? rsi_high:na, color=color.gray)
plot(title='RSI Low', series=uDiv? rsi_low:na, color=color.gray)
plot(title='H F', series=uDiv ? fractal_top_rsi+offset_ : na, color=regular_bearish_div or hidden_bearish_div ? color.red : not uDiv ? na : color.silver, offset=-2)
plot(title='L F', series=uDiv ? fractal_bot_rsi-offset_ : na, color=regular_bullish_div or hidden_bullish_div ? color.green : not uDiv ? na : color.silver, offset=-2)
plot(title='H D', series=uDiv ? fractal_top_rsi+offset_ : na, style=plot.style_circles, color=regular_bearish_div or hidden_bearish_div ? color.red : not uDiv ? na : color.silver, linewidth=3, offset=-2)
plot(title='L D', series=uDiv ? fractal_bot_rsi-offset_ : na, style=plot.style_circles, color=regular_bullish_div or hidden_bullish_div ? color.green : not uDiv ? na : color.silver, linewidth=3, offset=-2)
plotshape(title='+RBD', series=regular_bearish_div and uReg? rsi_high[2]+offset_ : na, text='Regular', style=shape.labeldown, location=location.absolute, color=color.red, textcolor=color.white, offset=-2)
plotshape(title='+HBD', series=hidden_bearish_div and uHid? rsi_high[2]+offset_ : na, text='hidden', style=shape.labeldown, location=location.absolute, color=color.red, textcolor=color.white, offset=-2)
plotshape(title='-RBD', series=regular_bullish_div and uReg? rsi_low[2]-offset_ : na, text='Regular', style=shape.labelup, location=location.absolute, color=color.green, textcolor=color.white, offset=-2)
plotshape(title='-HBD', series=hidden_bullish_div and uHid? rsi_low[2]-offset_ : na, text='hidden', style=shape.labelup, location=location.absolute, color=color.green, textcolor=color.white, offset=-2)
buy = (rsi_A[1] < k [1] and rsi_A > k)
sell = (rsi_A[1] >k [1] and rsi_A < k)
exitLong = crossover(k, k2)
exitShort = crossunder(k, k2)
alertcondition(buy, title='Buy Signal {{exchange}} ', message='{"exchange" : "{{exchange}}" , "pairs": "{{ticker}}" , "direction": "buy", "price": "{{close}}", "type": "enter_long"}')
alertcondition(sell, title='Sell Signal {{exchange}}', message='{"exchange" : "{{exchange}}" , "pairs": "{{ticker}}" , "direction": "sell", "price": "{{close}}", "type": "enter_short"}')
alertcondition(exitLong, title='Close long Signal {{exchange}}', message='{"exchange" : "{{exchange}}" , "pairs": "{{ticker}}" , "direction": "sell", "price": "{{close}}", "type": "close_long"}')
alertcondition(exitShort, title='Close Sell Signal {{exchange}}', message='{"exchange" : "{{exchange}}" , "pairs": "{{ticker}}" , "direction": "sell", "price": "{{close}}", "type": "close_short"}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment