HalfTrend Strategy
HalfTrend Strategy
// Setting
//StopLoss
atr = ta.atr(atr1)
//HalfTrend
atr2 = ta.atr(100) / 2
dev = channelDeviation * atr2
highPrice = high[math.abs(ta.highestbars(amplitude))]
lowPrice = low[math.abs(ta.lowestbars(amplitude))]
highma = ta.sma(high, amplitude)
lowma = ta.sma(low, amplitude)
if nextTrend == 1
maxLowPrice := math.max(lowPrice, maxLowPrice)
if trend == 0
if not na(trend[1]) and trend[1] != 0
up := na(down[1]) ? down : down[1]
arrowUp := up - atr2
arrowUp
else
up := na(up[1]) ? maxLowPrice : math.max(maxLowPrice, up[1])
up
atrHigh := up + dev
atrLow := up - dev
atrLow
else
if not na(trend[1]) and trend[1] != 1
down := na(up[1]) ? up : up[1]
arrowDown := down + atr2
arrowDown
else
down := na(down[1]) ? minHighPrice : math.min(minHighPrice, down[1])
down
atrHigh := down + dev
atrLow := down - dev
atrLow
ht = trend == 0 ? up : down
//EMA
//Superichi
avg(src,length,mult)=>
atr = ta.atr(length)*mult
up = hl2 + atr
dn = hl2 - atr
upper = 0.,lower = 0.
upper := src[1] < upper[1] ? math.min(up,upper[1]) : up
lower := src[1] > lower[1] ? math.max(dn,lower[1]) : dn
os = 0,max = 0.,min = 0.
os := src > upper ? 1 : src < lower ? 0 : os[1]
spt = os == 1 ? lower : upper
max := ta.cross(src,spt) ? math.max(src,max[1]) : os == 1 ?
math.max(src,max[1]) : spt
min := ta.cross(src,spt) ? math.min(src,min[1]) : os == 0 ?
math.min(src,min[1]) : spt
math.avg(max,min)
tenkan = avg(close,tenkan_len,tenkan_mult)
kijun = avg(close,kijun_len,kijun_mult)
senkouA = math.avg(kijun,tenkan)
senkouB = avg(close,spanB_len,spanB_mult)
tenkan_css = #2157f3
kijun_css = #ff5d00
cloud_a = color.new(color.teal,50)
cloud_b = color.new(color.red,50)
//Alert
getpips() =>
syminfo.mintick * (syminfo.type == "forex" ? 10 : 1)
// Strategy
float risk_long = na
float risk_short = na
float stopLoss = na
float takeProfit1 = na
float takeProfit2 = na
float entry_price = na
bool longcondition = na
bool shortcondition = na
if trendA == "Superichi"
longcondition := buySignal and close > senkouA[26] and senkouA[26] >
senkouB[26]
shortcondition := sellSignal and close < senkouA[26] and senkouA[26] <
senkouB[26]
if trendA == "EMA"
longcondition := buySignal and close > ema
shortcondition := sellSignal and close < ema
risk_long := risk_long[1]
risk_short := risk_short[1]
if typeSL == "ATR"
if _x
if tlpc == "Telegram"
strategy.entry("long", strategy.long, qty=lotB1, comment="Buy "
+ str.tostring(close) + "", alert_message = mess_buyAMT, when = minp > security)
if tlpc == "PineConnector"
strategy.entry("long", strategy.long, qty=lotB1, comment="Buy "
+ str.tostring(close) + "", alert_message = mess_buyAMP1, when = minp > security)
alert(mess_sellAMP2, alert.freq_once_per_bar_close)
else
if tlpc == "Telegram"
strategy.entry("long", strategy.long, qty=lotB1, comment="Buy "
+ str.tostring(close) + "", alert_message = mess_buyAMT)
if tlpc == "PineConnector"
strategy.entry("long", strategy.long, qty=lotB1, comment="Buy "
+ str.tostring(close) + "", alert_message = mess_buyAMP1)
alert(mess_sellAMP2, alert.freq_once_per_bar_close)
if tlpc == "PineConnector"
strategy.entry("short", strategy.short, qty=lotS1,
comment="Sell " + str.tostring(close) + "", alert_message = mess_sellAMP1, when =
minp > security)
alert(mess_sellAMP2, alert.freq_once_per_bar_close)
else
if tlpc == "Telegram"
strategy.entry("short", strategy.short, qty=lotS1,
comment="Sell " + str.tostring(close) + "", alert_message = mess_sellAMT)
if tlpc == "PineConnector"
strategy.entry("short", strategy.short, qty=lotS1,
comment="Sell " + str.tostring(close) + "", alert_message = mess_sellAMP1)
alert(mess_sellAMP2, alert.freq_once_per_bar_close)
if typeTP == "R:R"
if _x
if tlpc == "Telegram"
strategy.entry("long", strategy.long, qty=lotB1, comment="Buy "
+ str.tostring(close) + "", alert_message = mess_buyAT, when = minp > security)
if tlpc == "PineConnector"
strategy.entry("long", strategy.long, qty=lotB1, comment="Buy "
+ str.tostring(close) + "", alert_message = mess_buyAP, when = minp > security)
else
if tlpc == "Telegram"
strategy.entry("long", strategy.long, qty=lotB1, comment="Buy "
+ str.tostring(close) + "", alert_message = mess_buyAT)
if tlpc == "PineConnector"
strategy.entry("long", strategy.long, qty=lotB1, comment="Buy "
+ str.tostring(close) + "", alert_message = mess_buyAP)
if _x
if tlpc == "Telegram"
strategy.entry("short", strategy.short, qty=lotS1,
comment="Sell " + str.tostring(close) + "", alert_message = mess_sellAT, when =
minp > security)
if tlpc == "PineConnector"
strategy.entry("short", strategy.short, qty=lotS1,
comment="Sell " + str.tostring(close) + "", alert_message = mess_sellAP, when =
minp > security)
else
if tlpc == "Telegram"
strategy.entry("short", strategy.short, qty=lotS1,
comment="Sell " + str.tostring(close) + "", alert_message = mess_sellAT)
if tlpc == "PineConnector"
strategy.entry("short", strategy.short, qty=lotS1,
comment="Sell " + str.tostring(close) + "", alert_message = mess_sellAP)
if typeSL == "Swing"
if _x
if tlpc == "Telegram"
strategy.entry("long", strategy.long, qty=lotB, comment="Buy "
+ str.tostring(close) + "", alert_message = mess_buyMT, when = minp > security)
if tlpc == "PineConnector"
strategy.entry("long", strategy.long, qty=lotB, comment="Buy "
+ str.tostring(close) + "", alert_message = mess_buyMP1, when = minp > security)
alert(mess_buyMP2, alert.freq_once_per_bar_close)
else
if tlpc == "Telegram"
strategy.entry("long", strategy.long, qty=lotB, comment="Buy "
+ str.tostring(close) + "", alert_message = mess_buyMT)
if tlpc == "PineConnector"
strategy.entry("long", strategy.long, qty=lotB, comment="Buy "
+ str.tostring(close) + "", alert_message = mess_buyMP1)
alert(mess_buyMP2, alert.freq_once_per_bar_close)
if _x
if tlpc == "Telegram"
strategy.entry("short", strategy.short, qty=lotS, comment="Sell
" + str.tostring(close) + "", alert_message = mess_sellMT, when = minp > security)
if tlpc == "PineConnector"
strategy.entry("short", strategy.short, qty=lotS, comment="Sell
" + str.tostring(close) + "", alert_message = mess_sellMP1, when = minp > security)
alert(mess_sellMP2, alert.freq_once_per_bar_close)
else
if tlpc == "Telegram"
strategy.entry("short", strategy.short, qty=lotS, comment="Sell
" + str.tostring(close) + "", alert_message = mess_sellMT)
if tlpc == "PineConnector"
strategy.entry("short", strategy.short, qty=lotS, comment="Sell
" + str.tostring(close) + "", alert_message = mess_sellMP1)
alert(mess_sellMP2, alert.freq_once_per_bar_close)
if typeTP == "R:R"
if strategy.position_size == 0 and longcondition and inTradeWindow
risk_long := (close - swingLow) / close
minp = close - swingLow
if _x
if tlpc == "Telegram"
strategy.entry("long", strategy.long, qty=lotB, comment="Buy "
+ str.tostring(close) + "", alert_message = mess_buyT, when = minp > security)
if tlpc == "PineConnector"
strategy.entry("long", strategy.long, qty=lotB, comment="Buy "
+ str.tostring(close) + "", alert_message = mess_buyP, when = minp > security)
else
if tlpc == "Telegram"
strategy.entry("long", strategy.long, qty=lotB, comment="Buy "
+ str.tostring(close) + "", alert_message = mess_buyT)
if tlpc == "PineConnector"
strategy.entry("long", strategy.long, qty=lotB, comment="Buy "
+ str.tostring(close) + "", alert_message = mess_buyP)
if _x
if tlpc == "Telegram"
strategy.entry("short", strategy.short, qty=lotS, comment="Sell
" + str.tostring(close) + "", alert_message = mess_sellT, when = minp > security)
if tlpc == "PineConnector"
strategy.entry("short", strategy.short, qty=lotS, comment="Sell
" + str.tostring(close) + "", alert_message = mess_sellP, when = minp > security)
else
if tlpc == "Telegram"
strategy.entry("short", strategy.short, qty=lotS, comment="Sell
" + str.tostring(close) + "", alert_message = mess_sellT)
if tlpc == "PineConnector"
strategy.entry("short", strategy.short, qty=lotS, comment="Sell
" + str.tostring(close) + "", alert_message = mess_sellP)
if tlpc == "Telegram"
if strategy.position_size > 0
if ta.crossover(high, takeProfit1)
mess_profit2 = "𝗔 𝗖𝗛𝗔𝗧 🔵 " + str.tostring(syminfo.ticker) +
if strategy.position_size < 0
if tlpc == "PineConnector"
if strategy.position_size > 0
if ta.crossover(high, takeProfit1)
strategy.exit("Exit 2", "long", stop = entry_price, limit =
takeProfit2, qty_percent = 100, comment_profit = "TP2 ✅", comment_loss = "BE ❎")
else
strategy.exit("Exit 1", "long", stop = stopLoss, qty_percent = 100,
comment_loss = "SL ❌")
if strategy.position_size < 0
if ta.crossunder(low, takeProfit1)
strategy.exit("Exit 2", "short", stop = entry_price, limit =
takeProfit2, qty_percent = 100, comment_profit = "TP2 ✅", comment_loss = "BE ❎")
else
strategy.exit("Exit 1", "short", stop = stopLoss, qty_percent =
100, comment_loss = "SL ❌")
if typeTP == "R:R"
if strategy.position_size > 0
if tlpc == "PineConnector"
strategy.exit("Exit long", "long", stop = stopLoss, limit =
takeProfit1, comment_profit="TP ✅", comment_loss="SL ❌")
if strategy.position_size < 0
if tlpc == "PineConnector"
strategy.exit("Exit short", "short", stop = stopLoss, limit =
takeProfit1, comment_profit="TP ✅" , comment_loss="SL ❌")
//plot