You are on page 1of 5

// This source code is subject to the terms of the Mozilla Public License 2.

0 at
https://mozilla.org/MPL/2.0/
// Some source borrowed from � LonesomeTheBlue's Expanded Camarilla script

//@version=5
indicator('Camarilla Levels Trading', overlay=true)
mode = input.string(title='HTF Method', defval='Auto', options=['Auto', 'User
Defined'])
HTFm = input.timeframe('D', title='Time Frame (if HTF Method=User Defined)')
showlast = input(title='Show Only Last Period', defval=true)
lstyle = input.string(title='Line Style', options=['Solid', 'Circles', 'Cross'],
defval='Circles')
showCPROnly = input(defval=false, title='Show CPR Only')
showCPR = input(defval=false, title="Show Central Pivot Range")
showhl1 = input(defval=true, title='Show R1/S1')
showhl2 = input(defval=true, title='Show R2/S2')
showPriorScenarios = input(defval=false, title='Show Prior Scenario')
noChangePCT_Threshold = input(defval=0.005, title='No Change Percentage')

//New Day
is_newbar(res, sess) =>
t = time(res, sess)
na(t[1]) and not na(t) or t[1] < t
// Detect New Day

//auto higher time frame


HTFo = timeframe.period == '1' ? '30' :
timeframe.period == '3' ? '60' :
timeframe.period == '5' ? '240' :
timeframe.period == '15' ? 'D' :
timeframe.period == '30' ? 'D' :
timeframe.period == '45' ? 'D' :
timeframe.period == '60' ? 'D' :
timeframe.period == '120' ? 'D' :
timeframe.period == '180' ? 'D' :
timeframe.period == '240' ? 'D' :
timeframe.period == 'D' ? 'W' :
timeframe.period == 'W' ? '4W' : 'D'

HTF = mode == 'Auto' ? HTFo : HTFm

new_bar = is_newbar(HTF, '1700-1659')

var openValue = 0.00


var longTrigger = false
var shortTrigger = false
var inTrade = false
if new_bar
longTrigger := false
shortTrigger := false
inTrade := false
openValue := new_bar ? open : openValue[1]
highhtf = request.security(syminfo.tickerid, HTF, high[1],
lookahead=barmerge.lookahead_on)
lowhtf = request.security(syminfo.tickerid, HTF, low[1],
lookahead=barmerge.lookahead_on)
closehtf = request.security(syminfo.tickerid, HTF, close[1],
lookahead=barmerge.lookahead_on)

RANGE = highhtf - lowhtf

centralPivot = (highhtf + lowhtf + closehtf) / 3


tempBottomPivot = (highhtf + lowhtf) / 2
tempTopPivot = (centralPivot - tempBottomPivot) + centralPivot

bottomPivot = (tempBottomPivot > tempTopPivot) ? tempTopPivot : tempBottomPivot


topPivot = bottomPivot == tempBottomPivot ? tempTopPivot : tempBottomPivot

cprRange = topPivot - bottomPivot

// is this last bar for HTF?


islast = showlast ? request.security(syminfo.tickerid, HTF, barstate.islast,
lookahead=barmerge.lookahead_on) : true

// Line Style
linestyle = lstyle == 'Solid' ? plot.style_line : lstyle == 'Circle' ?
plot.style_circles : plot.style_cross

R5 = highhtf / lowhtf * closehtf


R4 = closehtf + RANGE * 1.1 / 2
R3 = closehtf + RANGE * 1.1 / 4
R2 = closehtf + RANGE * 1.1 / 6
R1 = closehtf + RANGE * 1.1 / 12
S1 = closehtf - RANGE * 1.1 / 12
S2 = closehtf - RANGE * 1.1 / 6
S3 = closehtf - RANGE * 1.1 / 4
S4 = closehtf - RANGE * 1.1 / 2
S5 = closehtf - (R5 - closehtf)

//plot(islast ? H5 : na, title='H5', color=color.new(color.red, 0), linewidth=1,


style=linestyle)
plot(islast and not showCPROnly ? R4 : na, title='R4', color=color.new(color.red,
0), linewidth=1, style=linestyle)
plotR3 = plot(islast and not showCPROnly ? R3 : na, title='R3',
color=color.new(color.red, 0), linewidth=1, style=linestyle)
plot(islast and showhl2 and not showCPROnly ? R2 : na, title='R2',
color=color.new(color.red, 70), linewidth=1, style=linestyle)
plot(islast and showhl1 and not showCPROnly ? R1 : na, title='R1',
color=color.new(color.red, 80), linewidth=1, style=linestyle)

plotTP = plot(showCPR ? topPivot : na, "Top Pivot", style=plot.style_cross,


color=color.purple)
plotCP = plot(showCPR ? centralPivot : na, "Central Pivot", style=plot.style_cross,
color=color.orange)
plotBP = plot(showCPR ? bottomPivot : na, "Bottom Pivot", style=plot.style_cross,
color=color.purple)

plot(islast and showhl1 and not showCPROnly ? S1 : na, title='S1',


color=color.new(color.lime, 70), linewidth=1, style=linestyle)
plot(islast and showhl2 and not showCPROnly ? S2 : na, title='S2',
color=color.new(color.lime, 80), linewidth=1, style=linestyle)
plotS3 = plot(islast and not showCPROnly ? S3 : na, title='S3',
color=color.new(color.lime, 0), linewidth=1, style=linestyle)
plot(islast and not showCPROnly ? S4 : na, title='S4', color=color.new(color.lime,
0), linewidth=1, style=linestyle)
//plot(islast ? L5 : na, title='L5', color=color.new(color.lime, 0), linewidth=1,
style=linestyle)

//Trade
int scenario = switch
(R3 > openValue) and (openValue > S3) => 1
(R3 < openValue) and (openValue < R4) => 2
(S3 > openValue) and (openValue > S4) => 3
(openValue > R4) => 4
(openValue < S4) => 5

longTrigger := ((scenario == 1) and (close[1] < S3) and (high > S3))
or (scenario == 2 and (close[1] < R3) and (high > R3))
or (scenario == 3 and (high > S3))
or (scenario == 5 and (high > S3))

shortTrigger := (scenario == 1 and (close[1] > R3) and (low < R3))
or (scenario == 2 and (close[1] > S3) and (low < S3))
or (scenario == 3 and (low < S4))
or (scenario == 4 and (low < R3))

float entryValue = switch


(longTrigger and scenario == 1) => S3
(longTrigger and scenario == 2) => R3
(longTrigger and scenario == 3) => S3
(longTrigger and scenario == 5) => S3
(shortTrigger and scenario == 1) => R3
(shortTrigger and scenario == 2) => S3
(shortTrigger and scenario == 3) => S4
(shortTrigger and scenario == 4) => R3

float tpValue = switch


(longTrigger and scenario == 1) => R1
(longTrigger and scenario == 2) => R5
(longTrigger and scenario == 3) => R1
(longTrigger and scenario == 5) => R1
(shortTrigger and scenario == 1) => S1
(shortTrigger and scenario == 2) => S5
(shortTrigger and scenario == 3) => S1
(shortTrigger and scenario == 4) => S1

float stopValue = switch


(longTrigger and scenario == 1) => S4
(longTrigger and scenario == 2) => R3
(longTrigger and scenario == 3) => S4
(longTrigger and scenario == 5) => (S4 + S3)/2
(shortTrigger and scenario == 1) => R4
(shortTrigger and scenario == 2) => R4
(shortTrigger and scenario == 3) => S3
(shortTrigger and scenario == 4) => (R4 + R3)/2

plotshape(longTrigger and not inTrade, style=shape.triangleup,


location=location.abovebar, color=color.green)
plotshape(shortTrigger and not inTrade, style=shape.triangledown,
location=location.belowbar, color=color.red)

if (longTrigger or shortTrigger) and not inTrade[1]


inTrade := true
alertText = (longTrigger ? "Go Long\n" : "Go Short\n") +
"Entry: " + str.tostring(entryValue, "#.##") + "\n" +
"TP: " + str.tostring(tpValue, "#.##") + "\n" +
"SL: " + str.tostring(stopValue, "#.##")
labelStyle = longTrigger ? label.style_label_up : label.style_label_down
yPlacement = longTrigger ? S4 : R4
labelColor = longTrigger ? color.new(color.green, 50) : color.new(color.red,
50)
label.new(x=bar_index, y=yPlacement, color=labelColor,
textcolor=color.white, size=size.small,
style=labelStyle,
text=alertText)

//Status
priorCentralPivot = request.security(syminfo.tickerid, HTF, centralPivot[1],
lookahead=barmerge.lookahead_on)
priorTopPivot = request.security(syminfo.tickerid, HTF, topPivot[1],
lookahead=barmerge.lookahead_on)
priorBottomPivot = request.security(syminfo.tickerid, HTF, bottomPivot[1],
lookahead=barmerge.lookahead_on)
priorCPRRange = request.security(syminfo.tickerid, HTF, cprRange[1],
lookahead=barmerge.lookahead_on)
priorR4 = request.security(syminfo.tickerid, HTF, R4[1],
lookahead=barmerge.lookahead_on)
priorS4 = request.security(syminfo.tickerid, HTF, S4[1],
lookahead=barmerge.lookahead_on)

//Highlight pressure zones when present


fill(plotR3, plotTP, color=R3 <= topPivot ? color.new(color.red,50) : na)
fill(plotS3, plotBP, color=S3 >= bottomPivot ? color.new(color.green,50) : na)

//CPR Range Percentile


cprRangePercentile = cprRange/math.max(cprRange, 22) * 100

//2-Day CPR Movement


cprStatus = if (bottomPivot > topPivot)
-1
else if (bottomPivot > priorTopPivot)
1
else if (topPivot < priorBottomPivot)
2
else if (centralPivot <= priorCentralPivot * (1 + noChangePCT_Threshold)
and centralPivot >= priorCentralPivot * (1 - noChangePCT_Threshold))
3
else if (topPivot < priorTopPivot and bottomPivot > priorBottomPivot)
4
else if (topPivot > priorTopPivot and bottomPivot < priorBottomPivot)
5
else if (centralPivot > priorCentralPivot)
6
else
7

string cprBias = switch cprStatus


1 => "Bullish | Higher CPR"
2 => "Bearish | Lower CPR"
3 => "Sideways | Unchanged CPR"
4 => "Trending/Breakout Inside CPR"
5 => "Sideways | Engulfing CPR"
6 => "Moderately Bullish | Overlapping CPR"
7 => "Moderately Bearish | Overlapping CPR"

string cprRangeStatus = switch


cprRangePercentile >= 90 => "Wide | Sideways Trend"
cprRangePercentile >= 80 => "Somewhat Wide | Sideways Trend"
cprRangePercentile >= 20 => "Mid-Range | No Bias"
cprRangePercentile >= 10 => "Somewhat Narrow | Trending"
cprRangePercentile >= 0 => "Narrow | Trending"

// We use `var` to only initialize the table on the first bar.


var table cprDisplay = table.new(position.bottom_right, 1, 4)
if barstate.islast
// We only populate the table on the last bar.
table.cell(cprDisplay, 0, 0, str.tostring(cprBias), text_color=color.white)
table.cell(cprDisplay, 0, 1, 'Range Width Percentile:'
+str.tostring(cprRangePercentile, '#.#'), text_color=color.white)
table.cell(cprDisplay, 0, 2, str.tostring(cprRangeStatus) ,
text_color=color.white)
table.cell(cprDisplay, 0, 3, "Trading Scenario " + str.tostring(scenario,
'#') , text_color=color.white)

if new_bar and showPriorScenarios


labelText = 'Scenario ' + str.tostring(scenario, '#')
label.new(x=bar_index, y=high,
text=labelText,
size = size.normal,
style=label.style_label_lower_left,
textcolor=color.white)

You might also like