Half Trend Other With VWAP MA Alerts

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 9

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

0 at
https://mozilla.org/MPL/2.0/
// © Sandesh
//@version=4

//***************GUIDE***********************************
//CPR - Applicable only for daily pivots
//CPR - All 3 lines display enabled by default
//CPR - Central Pivot line display cannot changed
//CPR - Central Pivot is a blue line by default and can be changed from settings
//CPR - Top Range & Bottom Ranage display can be changed from settings
//CPR - Top Range & Bottom Ranage are Yellow lines by default and can be chaned
from settings
//Daily pivots - Pivot line and CPR Central line are samea
//Daily pivots - level 1 & 2 (S1, R1, S2 R2) display enabled by default and can be
changed from settings
//Daily pivots - level 3 (S3 & R3) is availale and can be seleted from settings
//Daily pivots - Resistance(R) lines are Red lines and can be changed from settings
//Daily pivots - Support(S) lines are Green lines and can be changed from settings
//Weekly pivots - Pivot is a blue line by default and can be changed from settings
//Weekly pivots - 3 levels (S1, R1, S2, R2, S3 & R3) availale and can be seleted
from settings
//Weekly pivots - Resistance(R) lines are crossed (+) Red lines and can be changed
from settings
//Weekly pivots - Support(S) lines are crossed (+) Green lines and can be changed
from settings
//Monthly pivots - Pivot is a blue line by default and can be changed from settings
//Monthly pivots - 3 levels (S1, R1, S2, R2, S3 & R3) availale and can be seleted
from settings
//Monthly pivots - Resistance(R) lines are circled (o) Red lines and can be changed
from settings
//Monthly pivots - Support(S) lines are circled (o) Green lines and can be changed
from settings

study("CPR with MAs, halfTrend & VWAP by Sandesh", overlay = true,


shorttitle="CPR",precision=1)
//******************LOGICS**************************
//cenral pivot range
pivot = (high + low + close) /3 //Central Povit
BC = (high + low) / 2 //Below Central povit
TC = (pivot - BC) + pivot //Top Central povot

//3 support levels


S1 = (pivot * 2) - high
S2 = pivot - (high - low)
S3 = low - 2 * (high - pivot)

//3 resistance levels


R1 = (pivot * 2) - low
R2 = pivot + (high - low)
R3 = high + 2 * (pivot-low)

//Checkbox inputs
CPRPlot = input(title = "Plot CPR?", type=input.bool, defval=true)
DayS1R1 = input(title = "Plot Daiy S1/R1?", type=input.bool, defval=true)
DayS2R2 = input(title = "Plot Daiy S2/R2?", type=input.bool, defval=true)
DayS3R3 = input(title = "Plot Daiy S3/R3?", type=input.bool, defval=true)
WeeklyPivotInclude = input(title = "Plot Weekly Pivot?", type=input.bool,
defval=true)
WeeklyS1R1 = input(title = "Plot weekly S1/R1?", type=input.bool, defval=true)
WeeklyS2R2 = input(title = "Plot weekly S2/R2?", type=input.bool, defval=true)
WeeklyS3R3 = input(title = "Plot weekly S3/R3?", type=input.bool, defval=false)
MonthlyPivotInclude = input(title = "Plot Montly Pivot?", type=input.bool,
defval=true)
MonthlyS1R1 = input(title = "Plot Monthly S1/R1?", type=input.bool, defval=true)
MonthlyS2R2 = input(title = "Plot Monthly S2/R2?", type=input.bool, defval=true)
//MonthlyS3R3 = input(title = "Plot Montly S3/R3?", type=input.bool, defval=false)

//******************DAYWISE CPR & PIVOTS**************************


// Getting daywise CPR
DayPivot = security(syminfo.tickerid, "D", pivot[1], barmerge.gaps_off,
barmerge.lookahead_on)
DayBC = security(syminfo.tickerid, "D", BC[1], barmerge.gaps_off,
barmerge.lookahead_on)
DayTC = security(syminfo.tickerid, "D", TC[1], barmerge.gaps_off,
barmerge.lookahead_on)

//Adding linebreaks daywse for CPR


CPColour = DayPivot != DayPivot[1] ? na : color.blue
BCColour = DayBC != DayBC[1] ? na : color.black
TCColour = DayTC != DayTC[1] ? na : color.black

//Plotting daywise CPR


plot(DayPivot, title = "CP" , color = CPColour, style = plot.style_linebr,
linewidth =3)
plot(CPRPlot ? DayBC : na , title = "BC" , color = BCColour, style =
plot.style_circles, linewidth =2)
plot(CPRPlot ? DayTC : na , title = "TC" , color = TCColour, style =
plot.style_circles, linewidth =2)

// Getting daywise Support levels


DayS1 = security(syminfo.tickerid, "D", S1[1], barmerge.gaps_off,
barmerge.lookahead_on)
DayS2 = security(syminfo.tickerid, "D", S2[1], barmerge.gaps_off,
barmerge.lookahead_on)
DayS3 = security(syminfo.tickerid, "D", S3[1], barmerge.gaps_off,
barmerge.lookahead_on)

//Adding linebreaks for daywise Support levels


DayS1Color =DayS1 != DayS1[1] ? na : color.green
DayS2Color =DayS2 != DayS2[1] ? na : color.green
DayS3Color =DayS3 != DayS3[1] ? na : color.green

//Plotting daywise Support levels


plot(DayS1R1 ? DayS1 : na, title = "D-S1" , color = DayS1Color, style =
plot.style_cross, linewidth =1)
plot(DayS2R2 ? DayS2 : na, title = "D-S2" , color = DayS2Color, style =
plot.style_cross, linewidth =1)
plot(DayS3R3 ? DayS3 : na, title = "D-S3" , color = DayS3Color, style =
plot.style_cross, linewidth =1)

// Getting daywise Resistance levels


DayR1 = security(syminfo.tickerid, "D", R1[1], barmerge.gaps_off,
barmerge.lookahead_on)
DayR2 = security(syminfo.tickerid, "D", R2[1], barmerge.gaps_off,
barmerge.lookahead_on)
DayR3 = security(syminfo.tickerid, "D", R3[1], barmerge.gaps_off,
barmerge.lookahead_on)

//Adding linebreaks for daywise Support levels


DayR1Color =DayR1 != DayR1[1] ? na : color.red
DayR2Color =DayR2 != DayR2[1] ? na : color.red
DayR3Color =DayR3 != DayR3[1] ? na : color.red

//Plotting daywise Resistance levels


plot(DayS1R1 ? DayR1 : na, title = "D-R1" , color = DayR1Color, style =
plot.style_cross, linewidth =1)
plot(DayS2R2 ? DayR2 : na, title = "D-R2" , color = DayR2Color, style =
plot.style_cross, linewidth =1)
plot(DayS3R3 ? DayR3 : na, title = "D-R3" , color = DayR3Color, style =
plot.style_cross, linewidth =1)

//******************WEEKLY PIVOTS**************************

// Getting Weely Pivot


WPivot = security(syminfo.tickerid, "W", pivot[1], barmerge.gaps_off,
barmerge.lookahead_on)

//Adding linebreaks for Weely Pivot


WeeklyPivotColor =WPivot != WPivot[1] ? na : #fbc02d

//Plotting Weely Pivot


plot(WeeklyPivotInclude ? WPivot:na, title = "W-P" , color = WeeklyPivotColor,
transp=50, style = plot.style_circles, linewidth =2)

// Getting Weely Support levels


WS1 = security(syminfo.tickerid, "W", S1[1],barmerge.gaps_off,
barmerge.lookahead_on)
WS2 = security(syminfo.tickerid, "W", S2[1],barmerge.gaps_off,
barmerge.lookahead_on)
WS3 = security(syminfo.tickerid, "W", S3[1],barmerge.gaps_off,
barmerge.lookahead_on)

//Adding linebreaks for weekly Support levels


WS1Color =WS1 != WS1[1] ? na : color.green
WS2Color =WS2 != WS2[1] ? na : color.green
WS3Color =WS3 != WS3[1] ? na : color.green

//Plotting Weely Support levels


plot(WeeklyS1R1 ? WS1 : na, title = "W-S1" , color = WS1Color, transp=50, style =
plot.style_circles, linewidth =1)
plot(WeeklyS2R2 ? WS2 : na, title = "W-S2" , color = WS2Color, transp=50, style =
plot.style_circles, linewidth =1)
plot(WeeklyS3R3 ? WS3 : na, title = "W-S3" , color = WS3Color, transp=50, style =
plot.style_circles, linewidth =1)

// Getting Weely Resistance levels


WR1 = security(syminfo.tickerid, "W", R1[1], barmerge.gaps_off,
barmerge.lookahead_on)
WR2 = security(syminfo.tickerid, "W", R2[1], barmerge.gaps_off,
barmerge.lookahead_on)
WR3 = security(syminfo.tickerid, "W", R3[1], barmerge.gaps_off,
barmerge.lookahead_on)

//Adding linebreaks for weekly Resistance levels


WR1Color = WR1 != WR1[1] ? na : color.red
WR2Color = WR2 != WR2[1] ? na : color.red
WR3Color = WR3 != WR3[1] ? na : color.red

//Plotting Weely Resistance levels


plot(WeeklyS1R1 ? WR1 : na , title = "W-R1" , color = WR1Color, transp=50, style =
plot.style_circles, linewidth =1)
plot(WeeklyS2R2 ? WR2 : na , title = "W-R2" , color = WR2Color, transp=50, style =
plot.style_circles, linewidth =1)
plot(WeeklyS3R3 ? WR3 : na , title = "W-R3" , color = WR3Color, transp=50, style =
plot.style_circles, linewidth =1)

//******************MONTHLY PIVOTS**************************

// Getting Monhly Pivot


MPivot = security(syminfo.tickerid, "M", pivot[1],barmerge.gaps_off,
barmerge.lookahead_on)

//Adding linebreaks for Monthly Support levels


MonthlyPivotColor =MPivot != MPivot[1] ? na : #4caf50

//Plotting Monhly Pivot


plot(MonthlyPivotInclude? MPivot:na, title = " M-P" , color = MonthlyPivotColor,
transp=50, style = plot.style_circles, linewidth =2)

// Getting Monhly Support levels


MS1 = security(syminfo.tickerid, "M", S1[1],barmerge.gaps_off,
barmerge.lookahead_on)
MS2 = security(syminfo.tickerid, "M", S2[1],barmerge.gaps_off,
barmerge.lookahead_on)
MS3 = security(syminfo.tickerid, "M", S3[1],barmerge.gaps_off,
barmerge.lookahead_on)

//Adding linebreaks for Montly Support levels


MS1Color =MS1 != MS1[1] ? na : color.green
MS2Color =MS2 != MS2[1] ? na : color.green
MS3Color =MS3 != MS3[1] ? na : color.green

//Plotting Monhly Support levels


plot(MonthlyS1R1 ? MS1 : na, title = "M-S1" , color = MS1Color, transp=50, style =
plot.style_circles, linewidth =1)
plot(MonthlyS2R2 ? MS2 : na, title = "M-S2" , color = MS2Color, transp=50, style =
plot.style_circles, linewidth =1)
//plot(MonthlyS3R3 ? MS3 : na, title = "M-S3" , color = MS3Color, transp=50, style
= plot.style_circles, linewidth =1)

// Getting Monhly Resistance levels


MR1 = security(syminfo.tickerid, "M", R1[1],barmerge.gaps_off,
barmerge.lookahead_on)
MR2 = security(syminfo.tickerid, "M", R2[1],barmerge.gaps_off,
barmerge.lookahead_on)
MR3 = security(syminfo.tickerid, "M", R3[1],barmerge.gaps_off,
barmerge.lookahead_on)

MR1Color =MR1 != MR1[1] ? na : color.red


MR2Color =MR2 != MR2[1] ? na : color.red
MR3Color =MR3 != MR3[1] ? na : color.red

//Plotting Monhly Resistance levels


plot(MonthlyS1R1 ? MR1 : na , title = "M-R1", color = MR1Color, transp=50, style =
plot.style_circles , linewidth =1)
plot(MonthlyS2R2 ? MR2 : na , title = "M-R2" , color = MR2Color, transp=50, style =
plot.style_circles, linewidth =1)
//plot(MonthlyS3R3 ? MR3 : na, title = "M-R3" , color = MR3Color, transp=50, style
= plot.style_circles, linewidth =1)

//*****************************INDICATORs**************************

//EMA 20
PlotSMA = input(title = "Plot EMA?", type=input.bool, defval=true)
SMALength = input(title="EMA Length", type=input.integer, defval=20)
SMASource = input(title="EMA Source", type=input.source, defval=close)
SMAvg = ema (SMASource, SMALength)
plot(PlotSMA ? SMAvg : na, color= color.blue, title="EMA 20")

//EMA 50
PlotEMA50 = input(title = "Plot EMA?", type=input.bool, defval=true)
EMALength50 = input(title="EMA Length", type=input.integer, defval=50)
EMASource50 = input(title="EMA Source", type=input.source, defval=close)
EMAvg50 = ema (EMASource50, EMALength50)
plot(PlotEMA50 ? EMAvg50 : na, color= color.red, title="EMA 50")

//EMA 200
PlotEMA200 = input(title = "Plot EMA?", type=input.bool, defval=true)
EMALength200 = input(title="EMA Length", type=input.integer, defval=200)
EMASource200 = input(title="EMA Source", type=input.source, defval=close)
EMAvg200 = ema (EMASource200, EMALength200)
plot(PlotEMA200 ? EMAvg200 : na, color= color.black, title="EMA 200")

//VWAP
PlotVWAP = input(title = "Plot VWAP?", type=input.bool, defval=true)
VWAPSource = input(title="VWAP Source", type=input.source, defval=close)
VWAPrice = vwap (VWAPSource)
plot(PlotVWAP ? VWAPrice : na, color= #fbc02d, title="VWAP", style =
plot.style_stepline, linewidth =2)

//PDH
PrevBars = input(title = "Show previous highs and lows?", type =input.bool,
defval=false)
condition = not(timeframe.isweekly or timeframe.ismonthly)

h = security(syminfo.tickerid,"D",high,barmerge.gaps_off,barmerge.lookahead_on)
l = security(syminfo.tickerid,"D",low,barmerge.gaps_off,barmerge.lookahead_on)

plotshape(series = condition and PrevBars ? h[1] : na, title = "Previous


high",color = #0E6720,location=location.absolute ,transp=0,editable = true)
plotshape(series = condition and PrevBars ? l[1] : na, title = "Previous low",color
=#AF0D26,location = location.absolute ,transp=0, editable = true)

// Draw lines from the previous highs and lows


newSession = change(time('D'))
count = barssince(newSession)

var line PrevHigh = na


var line PrevLow = na

if (newSession)
PrevHigh := line.new(x1=bar_index, y1=h[1],
x2=bar_index, y2=h[1], color=#0E6720,width=1)
PrevLow := line.new(x1=bar_index,y1=l[1],
x2=bar_index,y2=l[1],color=#AF0D26,width = 1)
line.delete(id = PrevHigh[1])
line.delete(id = PrevLow[1])

if (not barstate.islast)
line.set_x2(id= PrevHigh, x=bar_index)
line.set_x2(id= PrevLow, x=bar_index)
else
line.set_xloc(id=PrevHigh, x1=time[count + 1],
x2=time_close + (1 * 86400000), xloc=xloc.bar_time)
line.set_xloc(id=PrevLow, x1=time[count + 1],
x2=time_close + (1 * 86400000), xloc=xloc.bar_time)

prevwkHL = input(true, title="Show previous week high and low?")

//previous week
prevWeekHigh = security(syminfo.tickerid, 'W', high[1], lookahead=true)
prevWeekLow = security(syminfo.tickerid, 'W', low[1], lookahead=true)

//previous Week Plots


plot(prevwkHL ? prevWeekHigh : na, title="Prev Week High",
style=plot.style_stepline, linewidth=1, color=color.fuchsia, transp=20)
plot(prevwkHL ? prevWeekLow : na, title="Prev Week Low", style=plot.style_stepline,
linewidth=1, color=color.fuchsia, transp=20)

//halftrend starts here

showArrows = input(title="Show halftrend signal Arrows", defval=false)

amplitude = 1
channelDeviation = 2

var color buyColor = color.blue


var color sellColor = color.red

var int trend = 0


var int nextTrend = 0
var float maxLowPrice = nz(low[1], low)
var float minHighPrice = nz(high[1], high)

var float up = 0.0


var float down = 0.0
float atrHigh = 0.0
float atrLow = 0.0
float arrowUp = na
float arrowDown = na

atr2 = atr(100) / 2
dev = channelDeviation * atr2

highPrice = high[abs(highestbars(1))]
lowPrice = low[abs(lowestbars(1))]
highma = sma(high, 1)
lowma = sma(low, 1)
if nextTrend == 1
maxLowPrice := max(lowPrice, maxLowPrice)

if highma < maxLowPrice and close < nz(low[1], low)


trend := 1
nextTrend := 0
minHighPrice := highPrice
else
minHighPrice := min(highPrice, minHighPrice)

if lowma > minHighPrice and close > nz(high[1], high)


trend := 0
nextTrend := 1
maxLowPrice := lowPrice

if trend == 0
if not na(trend[1]) and trend[1] != 0
up := na(down[1]) ? down : down[1]
arrowUp := up - atr2
else
up := na(up[1]) ? maxLowPrice : max(maxLowPrice, up[1])
atrHigh := up + dev
atrLow := up - dev
else
if not na(trend[1]) and trend[1] != 1
down := na(up[1]) ? up : up[1]
arrowDown := down + atr2
else
down := na(down[1]) ? minHighPrice : min(minHighPrice, down[1])
atrHigh := down + dev
atrLow := down - dev

ht = trend == 0 ? up : down

htColor = trend == 0 ? buyColor : sellColor


plot(ht, title="HalfTrend 1", linewidth=1, color=htColor)

//halftrend2
highPriceHT2 = high[abs(highestbars(2))]
lowPriceHT2 = low[abs(lowestbars(2))]
highmaHT2 = sma(high, 2)
lowmaHT2 = sma(low, 2)

var int trendHT2 = 0


var int nextTrendHT2 = 0
var float maxLowPriceHT2 = nz(low[1], low)
var float minHighPriceHT2 = nz(high[1], high)

var float upHT2 = 0.0


var float downHT2 = 0.0
float atrHighHT2 = 0.0
float atrLowHT2 = 0.0
float arrowUpHT2 = na
float arrowDownHT2 = na

if nextTrendHT2 == 1
maxLowPriceHT2 := max(lowPriceHT2, maxLowPriceHT2)
if highmaHT2 < maxLowPriceHT2 and close < nz(low[1], low)
trendHT2 := 1
nextTrendHT2 := 0
minHighPriceHT2 := highPriceHT2
else
minHighPriceHT2 := min(highPriceHT2, minHighPriceHT2)

if lowmaHT2 > minHighPriceHT2 and close > nz(high[1], high)


trendHT2 := 0
nextTrendHT2 := 1
maxLowPriceHT2 := lowPriceHT2

if trendHT2 == 0
if not na(trendHT2[1]) and trendHT2[1] != 0
upHT2 := na(downHT2[1]) ? downHT2 : downHT2[1]
arrowUpHT2 := upHT2 - atr2
else
upHT2 := na(upHT2[1]) ? maxLowPriceHT2 : max(maxLowPriceHT2, upHT2[1])
atrHighHT2 := upHT2 + dev
atrLowHT2 := upHT2 - dev
else
if not na(trendHT2[1]) and trendHT2[1] != 1
downHT2 := na(upHT2[1]) ? upHT2 : upHT2[1]
arrowDownHT2 := downHT2 + atr2
else
downHT2 := na(downHT2[1]) ? minHighPriceHT2 : min(minHighPriceHT2,
downHT2[1])
atrHighHT2 := downHT2 + dev
atrLowHT2 := downHT2 - dev

ht2 = trendHT2 == 0 ? upHT2 : downHT2

htColor2 = trendHT2 == 0 ? buyColor : sellColor


htPlot2 = plot(ht2, title="HalfTrend 2", linewidth=1, color=htColor2)

//halftrend3
highPriceHT3 = high[abs(highestbars(3))]
lowPriceHT3 = low[abs(lowestbars(3))]
highmaHT3 = sma(high, 3)
lowmaHT3 = sma(low, 3)

var int trendHT3 = 0


var int nextTrendHT3 = 0
var float maxLowPriceHT3 = nz(low[1], low)
var float minHighPriceHT3 = nz(high[1], high)

var float upHT3 = 0.0


var float downHT3 = 0.0
float atrHighHT3 = 0.0
float atrLowHT3 = 0.0
float arrowUpHT3 = na
float arrowDownHT3 = na

if nextTrendHT3 == 1
maxLowPriceHT3 := max(lowPriceHT3, maxLowPriceHT3)
if highmaHT3 < maxLowPriceHT3 and close < nz(low[1], low)
trendHT3 := 1
nextTrendHT3 := 0
minHighPriceHT3 := highPriceHT3
else
minHighPriceHT3 := min(highPriceHT3, minHighPriceHT3)

if lowmaHT3 > minHighPriceHT3 and close > nz(high[1], high)


trendHT3 := 0
nextTrendHT3 := 1
maxLowPriceHT3 := lowPriceHT3

if trendHT3 == 0
if not na(trendHT3[1]) and trendHT3[1] != 0
upHT3 := na(downHT3[1]) ? downHT3 : downHT3[1]
arrowUpHT3 := upHT3 - atr2
else
upHT3 := na(upHT3[1]) ? maxLowPriceHT3 : max(maxLowPriceHT3, upHT3[1])
atrHighHT3 := upHT3 + dev
atrLowHT3 := upHT3 - dev
else
if not na(trendHT3[1]) and trendHT3[1] != 1
downHT3 := na(upHT3[1]) ? upHT3 : upHT3[1]
arrowDownHT3 := downHT3 + atr2
else
downHT3 := na(downHT3[1]) ? minHighPriceHT3 : min(minHighPriceHT3,
downHT3[1])
atrHighHT3 := downHT3 + dev
atrLowHT3 := downHT3 - dev

ht3 = trendHT3 == 0 ? upHT3 : downHT3

htColor3 = trendHT3 == 0 ? buyColor : sellColor


htPlot3 = plot(ht3, title="HalfTrend 3", linewidth=1, color=htColor3)

buySignal = not na(arrowUp) and (trend == 0 and trend[1] == 1 and trendHT2 == 0 and
trendHT2[1] == 1 and trendHT3 == 0 and trendHT3[1] == 1 )
sellSignal = not na(arrowDown) and (trend == 1 and trend[1] == 0 and trendHT2 == 1
and trendHT2[1] == 0 and trendHT3 == 1 and trendHT3[1] == 0)

plotshape(showArrows and buySignal ? atrLow : na, title="Arrow Up",


style=shape.triangleup, location=location.absolute, size=size.tiny, color=buyColor)
plotshape(showArrows and sellSignal ? atrHigh : na, title="Arrow Down",
style=shape.triangledown, location=location.absolute, size=size.tiny,
color=sellColor)

alertcondition(buySignal, title="Alert: HalfTrend Buy", message="HalfTrend Buy")


alertcondition(sellSignal, title="Alert: HalfTrend Sell", message="HalfTrend Sell")

You might also like