0% found this document useful (0 votes)
83 views10 pages

CPR SSL

This document contains the code for an indicator that plots central pivot range levels and provides auto buy/sell signals based on a zigzag reversal algorithm. It calculates daily central pivot levels and resistance/support levels. It also includes options for ATR bands and moving average types to use for baseline, continuation, and exit signals in a SSL Hybrid system. The code defines functions for calculating the zigzag pivots and determining reversal signals, and plots the various indicator values.

Uploaded by

garbage Dump
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views10 pages

CPR SSL

This document contains the code for an indicator that plots central pivot range levels and provides auto buy/sell signals based on a zigzag reversal algorithm. It calculates daily central pivot levels and resistance/support levels. It also includes options for ATR bands and moving average types to use for baseline, continuation, and exit signals in a SSL Hybrid system. The code defines functions for calculating the zigzag pivots and determining reversal signals, and plots the various indicator values.

Uploaded by

garbage Dump
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 10

//@version=5

// ------------The base of this code came from @QuantNomad


// -----------I haven't been able to test this out so feel free to do so yourself
//------------I modifed it to simple CPR.

indicator(title='𝗖𝗣𝗥 𝗦𝗦𝗟 ', shorttitle='𝗖𝗣𝗥 𝗦𝗦𝗟 ', overlay=true)

showCPR = input(title='Show CPR', defval=true)


showAutoBS = input(title='Show Auto Buy Sell', defval=true)
showRainbow = input(title='Show Rainbow', defval=true)

daily_cpr = input.int(title=' Daily CPR ', defval=7, minval=0)

new_bar(res) =>
ta.change(time(res)) != 0
new_period(condition, src) =>
result = 0.0
result := condition ? src : result[1]
result

pivot = (high + low + close) / 3.0


bc = (high + low) / 2.0
tc = pivot - bc + pivot
R1 = 2 * pivot - low
S1 = 2 * pivot - high
R2 = pivot + high - low
S2 = pivot - (high - low)
R3 = high + 2 * (pivot - low)
S3 = low - 2 * (high - pivot)
PH = high
PL = low

//Daily Central Pivot Range


dpp = request.security(syminfo.tickerid, 'D', pivot[1],
lookahead=barmerge.lookahead_on)
dbc = request.security(syminfo.tickerid, 'D', bc[1],
lookahead=barmerge.lookahead_on)
dtc = request.security(syminfo.tickerid, 'D', tc[1],
lookahead=barmerge.lookahead_on)
dR1 = request.security(syminfo.tickerid, 'D', R1[1],
lookahead=barmerge.lookahead_on)
dS1 = request.security(syminfo.tickerid, 'D', S1[1],
lookahead=barmerge.lookahead_on)
dR2 = request.security(syminfo.tickerid, 'D', R2[1],
lookahead=barmerge.lookahead_on)
dS2 = request.security(syminfo.tickerid, 'D', S2[1],
lookahead=barmerge.lookahead_on)
dR3 = request.security(syminfo.tickerid, 'D', R3[1],
lookahead=barmerge.lookahead_on)
dS3 = request.security(syminfo.tickerid, 'D', S3[1],
lookahead=barmerge.lookahead_on)
dPH = request.security(syminfo.tickerid, 'D', PH[1],
lookahead=barmerge.lookahead_on)
dPL = request.security(syminfo.tickerid, 'D', PL[1],
lookahead=barmerge.lookahead_on)

one_day = 1000 * 60 * 60 * 24
new_day = daily_cpr > 0 and timenow - time < one_day * daily_cpr and new_bar('D')
dpp_ = new_period(new_day, dpp)
dtc_ = new_period(new_day, dtc)
dbc_ = new_period(new_day, dbc)
dR1_ = new_period(new_day, dR1)
dS1_ = new_period(new_day, dS1)
dR2_ = new_period(new_day, dR2)
dS2_ = new_period(new_day, dS2)
dR3_ = new_period(new_day, dR3)
dS3_ = new_period(new_day, dS3)
dPH_ = new_period(new_day, dPH)
dPL_ = new_period(new_day, dPL)

plot(showCPR ? timeframe.isintraday ? dtc_ >= dbc_ ? dtc_ : dbc_ : na : na,


title='Daily TC', style=plot.style_circles, color=color.new(#2196F3, 0),
linewidth=2)
plot(showCPR ? timeframe.isintraday ? dpp_ : na : na, title='Daily PP',
style=plot.style_circles, color=color.new(#FF5252, 0), linewidth=2)
plot(showCPR ? timeframe.isintraday ? dtc_ >= dbc_ ? dbc_ : dtc_ : na : na,
title='Daily BC', style=plot.style_circles, color=color.new(#2196F3, 0),
linewidth=2)

plot(showCPR ? dR1_ : na, title='R1', style=plot.style_circles,


color=color.new(#FF5252, 0), linewidth=2)
plot(showCPR ? dR2_ : na, title='R2', style=plot.style_circles,
color=color.new(#FF5252, 0), linewidth=2)
plot(showCPR ? dR3_ : na, title='R3', style=plot.style_circles,
color=color.new(#FF5252, 0), linewidth=2)
plot(showCPR ? dS1_ : na, title='S1', style=plot.style_circles,
color=color.new(#4CAF50, 0), linewidth=2)
plot(showCPR ? dS2_ : na, title='S2', style=plot.style_circles,
color=color.new(#4CAF50, 0), linewidth=2)
plot(showCPR ? dS3_ : na, title='S3', style=plot.style_circles,
color=color.new(#4CAF50, 0), linewidth=2)
plot(showCPR ? dPH_ : na, title='PH', style=plot.style_line,
color=color.new(#4CAF50, 0), linewidth=2)
plot(showCPR ? dPL_ : na, title='PL', style=plot.style_line,
color=color.new(#FF5252, 0), linewidth=2)

// Auto Buy sell

string percent_method = input.string(defval='MANUAL', title='Method to use for the


zigzag reversal range:', options=['MANUAL', 'ATR005 * X', 'ATR010 * X', 'ATR020 *
X', 'ATR050 * X', 'ATR100 * X', 'ATR250 * X'])

var float percent = input.float(defval=0.25, title='Percent of last pivot price for


zigzag reversal:', minval=0.0, maxval=99.0) / 100

float percent_multiplier = input(defval=1.0, title='Multiplier to apply to ATR if


applicable:')
if percent_method == 'ATR005 * X'
percent := ta.atr(005) / open * percent_multiplier
percent
if percent_method == 'ATR010 * X'
percent := ta.atr(010) / open * percent_multiplier
percent
if percent_method == 'ATR020 * X'
percent := ta.atr(020) / open * percent_multiplier
percent
if percent_method == 'ATR050 * X'
percent := ta.atr(050) / open * percent_multiplier
percent
if percent_method == 'ATR100 * X'
percent := ta.atr(100) / open * percent_multiplier
percent
if percent_method == 'ATR250 * X'
percent := ta.atr(250) / open * percent_multiplier
percent

// ZigZag options:
bool show_real_pivots = input(defval=true, title='Show real zigzag pivots:')

// ||-------------------------------------------------------------------------||
// ||
// ||-------------------------------------------------------------------------||
// |{
f_zz(_percent) =>

// direction after last pivot


var bool _is_direction_up = na
// track highest price since last lower pivot
var float _htrack = na
// track lowest price since last higher pivot
var float _ltrack = na
// zigzag variable for ploting
var float _pivot = na
// range needed for reaching reversal threshold
float _reverse_range = 0.0
// real pivot time
var int _real_pivot_time = na
var int _htime = na
var int _ltime = na
// reverse line
var float _reverse_line = 0.0
if bar_index >= 1

if na(_is_direction_up)
_is_direction_up := true
_is_direction_up

_reverse_range := nz(_pivot[1]) * _percent

if _is_direction_up
_ltrack := na
_ltime := time

if na(_htrack)
if high > high[1]
_htrack := high
_htime := time
_htime
else
_htrack := high[1]
_htime := time[1]
_htime
else
if high > _htrack
_htrack := high
_htime := time
_htime

_reverse_line := _htrack - _reverse_range

if close <= _reverse_line


_pivot := _htrack
_real_pivot_time := _htime
_is_direction_up := false
_is_direction_up

if not _is_direction_up
_htrack := na
_htime := na

if na(_ltrack)
if low < low[1]
_ltrack := low
_ltime := time
_ltime
else
_ltrack := low[1]
_ltime := time[1]
_ltime
else
if low < _ltrack
_ltrack := low
_ltime := time
_ltime

_reverse_line := _ltrack + _reverse_range

if close >= _reverse_line


_pivot := _ltrack
_real_pivot_time := _ltime
_is_direction_up := true
_is_direction_up

[_pivot, _is_direction_up, _reverse_line, _real_pivot_time]

// || |}---------------------------------------------------------------------<•

// |{
[price_a, is_up, reverse, _rl_time] = f_zz(percent)
alt_time = show_real_pivots and showAutoBS ? _rl_time : time

zz_color = is_up ? color.black : color.black


is_new_zig = ta.change(price_a) != 0 ? price_a : na
//
//plot(is_new_zig, title="Z", color=zz_color, linewidth=1, transp=80)

plot(showAutoBS ? reverse : na, title='R', color=color.new(color.gray, 40),


style=plot.style_stepline, linewidth=1, offset=1)
plot(showAutoBS ? reverse : na, title='R', color=color.new(color.white, 40),
style=plot.style_circles, linewidth=4, offset=1, show_last=1)
// || |}---------------------------------------------------------------------<•

//𝗦𝗦𝗟 𝗛𝗬𝗕𝗥𝗜𝗗

show_Baseline = input(title='Show Baseline', defval=true)


show_SZ = input(title='Show SZ', defval=false)
show_atr = input(title='Show ATR bands', defval=true)
//ATR
atrlen = input(14, 'ATR Period')
mult = input.float(1, 'ATR Multi', step=0.1)
smoothing = input.string(title='ATR Smoothing', defval='WMA', options=['RMA',
'SMA', 'EMA', 'WMA'])

ma_function(source, atrlen) =>


if smoothing == 'RMA'
ta.rma(source, atrlen)
else
if smoothing == 'SMA'
ta.sma(source, atrlen)
else
if smoothing == 'EMA'
ta.ema(source, atrlen)
else
ta.wma(source, atrlen)
atr_slen = ma_function(ta.tr(true), atrlen)
////ATR Up/Low Bands
upper_band = atr_slen * mult + close
lower_band = close - atr_slen * mult

////BASELINE / SZ / SX / EXIT MOVING AVERAGE VALUES


maType = input.string(title='SZ / Baseline Type', defval='HMA', options=['SMA',
'EMA', 'DEMA', 'TEMA', 'LSMA', 'WMA', 'MF', 'VAMA', 'TMA', 'HMA', 'JMA', 'Kijun
v2', 'EDSMA', 'McGinley'])
len = input(title='SZ / Baseline Length', defval=60)

SXType = input.string(title='SX / Continuation Type', defval='JMA', options=['SMA',


'EMA', 'DEMA', 'TEMA', 'WMA', 'MF', 'VAMA', 'TMA', 'HMA', 'JMA', 'McGinley'])
len2 = input(title='SX Length', defval=5)
//
SSL3Type = input.string(title='EXIT Type', defval='HMA', options=['DEMA', 'TEMA',
'LSMA', 'VAMA', 'TMA', 'HMA', 'JMA', 'Kijun v2', 'McGinley', 'MF'])
len3 = input(title='EXIT Length', defval=15)
src = input(title='Source', defval=close)

//
tema(src, len) =>
ema1 = ta.ema(src, len)
ema2 = ta.ema(ema1, len)
ema3 = ta.ema(ema2, len)
3 * ema1 - 3 * ema2 + ema3
kidiv = input.int(defval=1, maxval=4, title='Kijun MOD Divider')

jurik_phase = input(title='* Jurik (JMA) Only - Phase', defval=3)


jurik_power = input(title='* Jurik (JMA) Only - Power', defval=1)
volatility_lookback = input(10, title='* Volatility Adjusted (VAMA) Only -
Volatility lookback length')
//MF
beta = input.float(0.8, minval=0, maxval=1, step=0.1, title='Modular Filter,
General Filter Only - Beta')
feedback = input(false, title='Modular Filter Only - Feedback')
z = input.float(0.5, title='Modular Filter Only - Feedback Weighting', step=0.1,
minval=0, maxval=1)
//EDSMA
ssfLength = input.int(title='EDSMA - Super Smoother Filter Length', minval=1,
defval=20)
ssfPoles = input.int(title='EDSMA - Super Smoother Filter Poles', defval=2,
options=[2, 3])

//----

//EDSMA
get2PoleSSF(src, length) =>
PI = 2 * math.asin(1)
arg = math.sqrt(2) * PI / length
a1 = math.exp(-arg)
b1 = 2 * a1 * math.cos(arg)
c2 = b1
c3 = -math.pow(a1, 2)
c1 = 1 - c2 - c3

ssf = 0.0
ssf := c1 * src + c2 * nz(ssf[1]) + c3 * nz(ssf[2])
ssf

get3PoleSSF(src, length) =>


PI = 2 * math.asin(1)

arg = PI / length
a1 = math.exp(-arg)
b1 = 2 * a1 * math.cos(1.738 * arg)
c1 = math.pow(a1, 2)

coef2 = b1 + c1
coef3 = -(c1 + b1 * c1)
coef4 = math.pow(c1, 2)
coef1 = 1 - coef2 - coef3 - coef4

ssf = 0.0
ssf := coef1 * src + coef2 * nz(ssf[1]) + coef3 * nz(ssf[2]) + coef4 *
nz(ssf[3])
ssf

ma(type, src, len) =>


float result = 0
if type == 'TMA'
result := ta.sma(ta.sma(src, math.ceil(len / 2)), math.floor(len / 2) + 1)
result
if type == 'MF'
ts = 0.
b = 0.
c = 0.
os = 0.
//----
alpha = 2 / (len + 1)
a = feedback ? z * src + (1 - z) * nz(ts[1], src) : src
//----
b := a > alpha * a + (1 - alpha) * nz(b[1], a) ? a : alpha * a + (1 -
alpha) * nz(b[1], a)
c := a < alpha * a + (1 - alpha) * nz(c[1], a) ? a : alpha * a + (1 -
alpha) * nz(c[1], a)
os := a == b ? 1 : a == c ? 0 : os[1]
//----
upper = beta * b + (1 - beta) * c
lower = beta * c + (1 - beta) * b
ts := os * upper + (1 - os) * lower
result := ts
result
if type == 'LSMA'
result := ta.linreg(src, len, 0)
result
if type == 'SMA' // Simple
result := ta.sma(src, len)
result
if type == 'EMA' // Exponential
result := ta.ema(src, len)
result
if type == 'DEMA' // Double Exponential
e = ta.ema(src, len)
result := 2 * e - ta.ema(e, len)
result
if type == 'TEMA' // Triple Exponential
e = ta.ema(src, len)
result := 3 * (e - ta.ema(e, len)) + ta.ema(ta.ema(e, len), len)
result
if type == 'WMA' // Weighted
result := ta.wma(src, len)
result
if type == 'VAMA' // Volatility Adjusted
/// Copyright © 2019 to present, Joris Duyck (JD)
mid = ta.ema(src, len)
dev = src - mid
vol_up = ta.highest(dev, volatility_lookback)
vol_down = ta.lowest(dev, volatility_lookback)
result := mid + math.avg(vol_up, vol_down)
result
if type == 'HMA' // Hull
result := ta.wma(2 * ta.wma(src, len / 2) - ta.wma(src, len),
math.round(math.sqrt(len)))
result
if type == 'JMA' // Jurik
/// Copyright © 2018 Alex Orekhov (everget)
/// Copyright © 2017 Jurik Research and Consulting.
phaseRatio = jurik_phase < -100 ? 0.5 : jurik_phase > 100 ? 2.5 :
jurik_phase / 100 + 1.5
beta = 0.45 * (len - 1) / (0.45 * (len - 1) + 2)
alpha = math.pow(beta, jurik_power)
jma = 0.0
e0 = 0.0
e0 := (1 - alpha) * src + alpha * nz(e0[1])
e1 = 0.0
e1 := (src - e0) * (1 - beta) + beta * nz(e1[1])
e2 = 0.0
e2 := (e0 + phaseRatio * e1 - nz(jma[1])) * math.pow(1 - alpha, 2) +
math.pow(alpha, 2) * nz(e2[1])
jma := e2 + nz(jma[1])
result := jma
result
if type == 'Kijun v2'
kijun = math.avg(ta.lowest(len), ta.highest(len)) //, (open + close)/2)
conversionLine = math.avg(ta.lowest(len / kidiv), ta.highest(len / kidiv))
delta = (kijun + conversionLine) / 2
result := delta
result
if type == 'McGinley'
mg = 0.0
mg := na(mg[1]) ? ta.ema(src, len) : mg[1] + (src - mg[1]) / (len *
math.pow(src / mg[1], 4))
result := mg
result
if type == 'EDSMA'

zeros = src - nz(src[2])


avgZeros = (zeros + zeros[1]) / 2

// Ehlers Super Smoother Filter


ssf = ssfPoles == 2 ? get2PoleSSF(avgZeros, ssfLength) :
get3PoleSSF(avgZeros, ssfLength)

// Rescale filter in terms of Standard Deviations


stdev = ta.stdev(ssf, len)
scaledFilter = stdev != 0 ? ssf / stdev : 0

alpha = 5 * math.abs(scaledFilter) / len

edsma = 0.0
edsma := alpha * src + (1 - alpha) * nz(edsma[1])
result := edsma
result
result

///SSL 1 and SX
emaHigh = ma(maType, high, len)
emaLow = ma(maType, low, len)

maHigh = ma(SXType, high, len2)


maLow = ma(SXType, low, len2)

///EXIT
ExitHigh = ma(SSL3Type, high, len3)
ExitLow = ma(SSL3Type, low, len3)

///Keltner Baseline Channel


BBMC = ma(maType, close, len)
useTrueRange = input(true)
multy = input.float(0.2, step=0.05, title='Base Channel Multiplier')
Keltma = ma(maType, src, len)
range_1 = useTrueRange ? ta.tr : high - low
rangema = ta.ema(range_1, len)
upperk = Keltma + rangema * multy
lowerk = Keltma - rangema * multy

//Baseline Violation Candle


open_pos = open * 1
close_pos = close * 1
difference = math.abs(close_pos - open_pos)
atr_violation = difference > atr_slen
InRange = upper_band > BBMC and lower_band < BBMC
candlesize_violation = atr_violation and InRange
plotshape(showRainbow ? candlesize_violation : na, color=color.new(color.red, 0),
size=size.tiny, style=shape.diamond, location=location.top, title='Diamond')

//SZ VALUES
Hlv = int(na)
Hlv := close > emaHigh ? 1 : close < emaLow ? -1 : Hlv[1]
sslDown = Hlv < 0 ? emaHigh : emaLow

//SX VALUES
Hlv2 = int(na)
Hlv2 := close > maHigh ? 1 : close < maLow ? -1 : Hlv2[1]
sslDown2 = Hlv2 < 0 ? maHigh : maLow

//EXIT VALUES
Hlv3 = int(na)
Hlv3 := close > ExitHigh ? 1 : close < ExitLow ? -1 : Hlv3[1]
sslExit = Hlv3 < 0 ? ExitHigh : ExitLow
base_cross_Long = ta.crossover(close, sslExit)
base_cross_Short = ta.crossover(sslExit, close)
codiff = base_cross_Long ? 1 : base_cross_Short ? -1 : na

//COLORS
show_color_bar = input(title='Color Bars', defval=true)
color_bar = close > upperk ? #00E113 : close < lowerk ? #E16024 : color.gray
color_SZ = close > sslDown ? #00E113 : close < sslDown ? #E16024 : na

//PLOTS
plotarrow(showRainbow ? codiff : na, colorup=color.new(#00E113, 20),
colordown=color.new(#E16024, 20), title='Exit Arrows', maxheight=20, offset=0)
p1 = plot(showRainbow and show_Baseline ? BBMC : na, color=color_bar, linewidth=4,
title='MA Baseline', transp=0)
DownPlot = plot(showRainbow and show_SZ ? sslDown : na, title='SZ', linewidth=3,
color=color_SZ, transp=10)
barcolor(showRainbow and show_color_bar ? color_bar : na)
up_channel = plot(showRainbow and show_Baseline ? upperk : na, color=color_bar,
title='Baseline Upper Channel')
low_channel = plot(showRainbow and show_Baseline ? lowerk : na, color=color_bar,
title='Basiline Lower Channel')
fill(up_channel, low_channel, color=color_bar, transp=90)

////SX Continiuation from ATR


atr_crit = input.float(0.9, step=0.1, title='Continuation ATR Criteria')
upper_half = atr_slen * atr_crit + close
lower_half = close - atr_slen * atr_crit
buy_inatr = lower_half < sslDown2
sell_inatr = upper_half > sslDown2
sell_cont = close < BBMC and close < sslDown2
buy_cont = close > BBMC and close > sslDown2
sell_atr = sell_inatr and sell_cont
buy_atr = buy_inatr and buy_cont
atr_fill = buy_atr ? color.green : sell_atr ? color.purple : color.white
LongPlot = plot(showRainbow ? sslDown2 : na, title='SX', linewidth=2,
color=atr_fill, style=plot.style_circles, transp=0)
u = plot(showRainbow and show_atr ? upper_band : na, '+ATR',
color=color.new(color.white, 80))
l = plot(showRainbow and show_atr ? lower_band : na, '-ATR',
color=color.new(color.white, 80))

//ALERTS
alertcondition(ta.crossover(close, sslDown), title='SSL Cross Alert', message='SZ
has crossed.')
alertcondition(ta.crossover(close, sslDown2), title='SX Cross Alert', message='SX
has crossed.')
alertcondition(sell_atr, title='Sell Continuation', message='Sell Continuation.')
alertcondition(buy_atr, title='Buy Continuation', message='Buy Continuation.')
alertcondition(ta.crossover(close, sslExit), title='Exit Sell', message='Exit Sell
Alert.')
alertcondition(ta.crossover(sslExit, close), title='Exit Buy', message='Exit Buy
Alert.')
alertcondition(ta.crossover(close, upperk), title='Baseline Buy Entry',
message='Base Buy Alert.')
alertcondition(ta.crossover(lowerk, close), title='Baseline Sell Entry',
message='Base Sell Alert.')

You might also like