You are on page 1of 6

//@version=4

// Copyright (c) 2021-present, Alex Orekhov (everget)


study("half3 (by Mehrab khosravi)", overlay=true)

// Part 1
amplitude_1 = input(title="Amplitude 1", defval=2)
channelDeviation_1 = input(title="Channel Deviation 1", defval=2)
showArrows_1 = input(title="Show Arrows 1", defval=true)
showChannels_1 = input(title="Show Channels 1", defval=true)

var int trend_1 = 0


var int nextTrend_1 = 0
var float maxLowPrice_1 = nz(low[1], low)
var float minHighPrice_1 = nz(high[1], high)

var float up_1 = 0.0


var float down_1 = 0.0
float atrHigh_1 = 0.0
float atrLow_1 = 0.0
float arrowUp_1 = na
float arrowDown_1 = na

atr2_1 = atr(100) / 2
dev_1 = channelDeviation_1 * atr2_1

highPrice_1 = high[abs(highestbars(amplitude_1))]
lowPrice_1 = low[abs(lowestbars(amplitude_1))]
highma_1 = sma(high, amplitude_1)
lowma_1 = sma(low, amplitude_1)

if nextTrend_1 == 1
maxLowPrice_1 := max(lowPrice_1, maxLowPrice_1)

if highma_1 < maxLowPrice_1 and close < nz(low[1], low)


trend_1 := 1
nextTrend_1 := 0
minHighPrice_1 := highPrice_1
else
minHighPrice_1 := min(highPrice_1, minHighPrice_1)

if lowma_1 > minHighPrice_1 and close > nz(high[1], high)


trend_1 := 0
nextTrend_1 := 1
maxLowPrice_1 := lowPrice_1
if trend_1 == 0
if not na(trend_1[1]) and trend_1[1] != 0
up_1 := na(down_1[1]) ? down_1 : down_1[1]
arrowUp_1 := up_1 - atr2_1
else
up_1 := na(up_1[1]) ? maxLowPrice_1 : max(maxLowPrice_1, up_1[1])
atrHigh_1 := up_1 + dev_1
atrLow_1 := up_1 - dev_1
else
if not na(trend_1[1]) and trend_1[1] != 1
down_1 := na(up_1[1]) ? up_1 : up_1[1]
arrowDown_1 := down_1 + atr2_1
else
down_1 := na(down_1[1]) ? minHighPrice_1 : min(minHighPrice_1, down_1[1])
atrHigh_1 := down_1 + dev_1
atrLow_1 := down_1 - dev_1

ht_1 = trend_1 == 0 ? up_1 : down_1

var color buyColor_1 = color.blue


var color sellColor_1 = color.red

htColor_1 = trend_1 == 0 ? buyColor_1 : sellColor_1


htPlot_1 = plot(ht_1, title="HalfTrend 1", linewidth=2, color=htColor_1)

atrHighPlot_1 = plot(showChannels_1 ? atrHigh_1 : na, title="ATR High 1",


style=plot.style_circles, color=sellColor_1)
atrLowPlot_1 = plot(showChannels_1 ? atrLow_1 : na, title="ATR Low 1",
style=plot.style_circles, color=buyColor_1)

fill(htPlot_1, atrHighPlot_1, title="ATR High Ribbon 1", color=sellColor_1)


fill(htPlot_1, atrLowPlot_1, title="ATR Low Ribbon 1", color=buyColor_1)

buySignal_1 = not na(arrowUp_1) and (trend_1 == 0 and trend_1[1] == 1)


sellSignal_1 = not na(arrowDown_1) and (trend_1 == 1 and trend_1[1] == 0)

plotshape(showArrows_1 and buySignal_1 ? atrLow_1 : na, title="Arrow Up 1",


style=shape.triangleup, location=location.absolute, size=size.tiny, color=buyColor_1)
plotshape(showArrows_1 and sellSignal_1 ? atrHigh_1 : na, title="Arrow Down 1",
style=shape.triangledown, location=location.absolute, size=size.tiny, color=sellColor_1)

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


alertcondition(sellSignal_1, title="Alert: HalfTrend Sell 1", message="HalfTrend Sell 1")
// Part 2
amplitude_2 = input(title="Amplitude 2", defval=2)
channelDeviation_2 = input(title="Channel Deviation 2", defval=2)
showArrows_2 = input(title="Show Arrows 2", defval=true)
showChannels_2 = input(title="Show Channels 2", defval=true)

var int trend_2 = 0


var int nextTrend_2 = 0
var float maxLowPrice_2 = nz(low[1], low)
var float minHighPrice_2 = nz(high[1], high)

var float up_2 = 0.0


var float down_2 = 0.0
float atrHigh_2 = 0.0
float atrLow_2 = 0.0
float arrowUp_2 = na
float arrowDown_2 = na

atr2_2 = atr(100) / 2
dev_2 = channelDeviation_2 * atr2_2

highPrice_2 = high[abs(highestbars(amplitude_2))]
lowPrice_2 = low[abs(lowestbars(amplitude_2))]
highma_2 = sma(high, amplitude_2)
lowma_2 = sma(low, amplitude_2)

if nextTrend_2 == 1
maxLowPrice_2 := max(lowPrice_2, maxLowPrice_2)

if highma_2 < maxLowPrice_2 and close < nz(low[1], low)


trend_2 := 1
nextTrend_2 := 0
minHighPrice_2 := highPrice_2
else
minHighPrice_2 := min(highPrice_2, minHighPrice_2)

if lowma_2 > minHighPrice_2 and close > nz(high[1], high)


trend_2 := 0
nextTrend_2 := 1
maxLowPrice_2 := lowPrice_2
if trend_2 == 0
if not na(trend_2[1]) and trend_2[1] != 0
up_2 := na(down_2[1]) ? down_2 : down_2[1]
arrowUp_2 := up_2 - atr2_2
else
up_2 := na(up_2[1]) ? maxLowPrice_2 : max(maxLowPrice_2, up_2[1])
atrHigh_2 := up_2 + dev_2
atrLow_2 := up_2 - dev_2
else
if not na(trend_2[1]) and trend_2[1] != 1
down_2 := na(up_2[1]) ? up_2 : up_2[1]
arrowDown_2 := down_2 + atr2_2
else
down_2 := na(down_2[1]) ? minHighPrice_2 : min(minHighPrice_2, down_2[1])
atrHigh_2 := down_2 + dev_2
atrLow_2 := down_2 - dev_2

ht_2 = trend_2 == 0 ? up_2 : down_2

var color buyColor_2 = color.blue


var color sellColor_2 = color.red

htColor_2 = trend_2 == 0 ? buyColor_2 : sellColor_2


htPlot_2 = plot(ht_2, title="HalfTrend 2", linewidth=2, color=htColor_2)

atrHighPlot_2 = plot(showChannels_2 ? atrHigh_2 : na, title="ATR High 2",


style=plot.style_circles, color=sellColor_2)
atrLowPlot_2 = plot(showChannels_2 ? atrLow_2 : na, title="ATR Low 2",
style=plot.style_circles, color=buyColor_2)

fill(htPlot_2, atrHighPlot_2, title="ATR High Ribbon 2", color=sellColor_2)


fill(htPlot_2, atrLowPlot_2, title="ATR Low Ribbon 2", color=buyColor_2)

buySignal_2 = not na(arrowUp_2) and (trend_2 == 0 and trend_2[1] == 1)


sellSignal_2 = not na(arrowDown_2) and (trend_2 == 1 and trend_2[1] == 0)

plotshape(showArrows_2 and buySignal_2 ? atrLow_2 : na, title="Arrow Up 2",


style=shape.triangleup, location=location.absolute, size=size.tiny, color=buyColor_2)
plotshape(showArrows_2 and sellSignal_2 ? atrHigh_2 : na, title="Arrow Down 2",
style=shape.triangledown, location=location.absolute, size=size.tiny, color=sellColor_2)

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


alertcondition(sellSignal_2, title="Alert: HalfTrend Sell 2", message="HalfTrend Sell 2")
// Part 3
amplitude_3 = input(title="Amplitude 3", defval=2)
channelDeviation_3 = input(title="Channel Deviation 3", defval=2)
showArrows_3 = input(title="Show Arrows 3", defval=true)
showChannels_3 = input(title="Show Channels 3", defval=true)

var int trend_3 = 0


var int nextTrend_3 = 0
var float maxLowPrice_3 = nz(low[1], low)
var float minHighPrice_3 = nz(high[1], high)

var float up_3 = 0.0


var float down_3 = 0.0
float atrHigh_3 = 0.0
float atrLow_3 = 0.0
float arrowUp_3 = na
float arrowDown_3 = na

atr2_3 = atr(100) / 2
dev_3 = channelDeviation_3 * atr2_3

highPrice_3 = high[abs(highestbars(amplitude_3))]
lowPrice_3 = low[abs(lowestbars(amplitude_3))]
highma_3 = sma(high, amplitude_3)
lowma_3 = sma(low, amplitude_3)

if nextTrend_3 == 1
maxLowPrice_3 := max(lowPrice_3, maxLowPrice_3)

if highma_3 < maxLowPrice_3 and close < nz(low[1], low)


trend_3 := 1
nextTrend_3 := 0
minHighPrice_3 := highPrice_3
else
minHighPrice_3 := min(highPrice_3, minHighPrice_3)

if lowma_3 > minHighPrice_3 and close > nz(high[1], high)


trend_3 := 0
nextTrend_3 := 1
maxLowPrice_3 := lowPrice_3

if trend_3 == 0
if not na(trend_3[1]) and trend_3[1] != 0
up_3 := na(down_3[1]) ? down_3 : down_3[1]
arrowUp_3 := up_3 - atr2_3
else
up_3 := na(up_3[1]) ? maxLowPrice_3 : max(maxLowPrice_3, up_3[1])
atrHigh_3 := up_3 + dev_3
atrLow_3 := up_3 - dev_3
else
if not na(trend_3[1]) and trend_3[1] != 1
down_3 := na(up_3[1]) ? up_3 : up_3[1]
arrowDown_3 := down_3 + atr2_3
else
down_3 := na(down_3[1]) ? minHighPrice_3 : min(minHighPrice_3, down_3[1])
atrHigh_3 := down_3 + dev_3
atrLow_3 := down_3 - dev_3

ht_3 = trend_3 == 0 ? up_3 : down_3

var color buyColor_3 = color.blue


var color sellColor_3 = color.red

htColor_3 = trend_3 == 0 ? buyColor_3 : sellColor_3


htPlot_3 = plot(ht_3, title="HalfTrend 3", linewidth=2, color=htColor_3)

atrHighPlot_3 = plot(showChannels_3 ? atrHigh_3 : na, title="ATR High 3",


style=plot.style_circles, color=sellColor_3)
atrLowPlot_3 = plot(showChannels_3 ? atrLow_3 : na, title="ATR Low 3",
style=plot.style_circles, color=buyColor_3)

fill(htPlot_3, atrHighPlot_3, title="ATR High Ribbon 3", color=sellColor_3)


fill(htPlot_3, atrLowPlot_3, title="ATR Low Ribbon 3", color=buyColor_3)

buySignal_3 = not na(arrowUp_3) and (trend_3 == 0 and trend_3[1] == 1)


sellSignal_3 = not na(arrowDown_3) and (trend_3 == 1 and trend_3[1] == 0)

plotshape(showArrows_3 and buySignal_3 ? atrLow_3 : na, title="Arrow Up 3",


style=shape.triangleup, location=location.absolute, size=size.tiny, color=buyColor_3)
plotshape(showArrows_3 and sellSignal_3 ? atrHigh_3 : na, title="Arrow Down 3",
style=shape.triangledown, location=location.absolute, size=size.tiny, color=sellColor_3)

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


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

You might also like