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

//@version=4

strategy(title="UT stc Combined tpsl", overlay=true)

// Indicator 1 Settings
keyvalue1 = input(2, title="Key Value (Indicator 1)", step=0.5)
atrperiod1 = input(1, title="ATR Period (Indicator 1)")

// Indicator 2 Settings
keyvalue2 = input(2, title="Key Value (Indicator 2)", step=0.5)
atrperiod2 = input(300, title="ATR Period (Indicator 2)")

// Indicator 1
src1 = close
xATR1 = atr(atrperiod1)
nLoss1 = keyvalue1 * xATR1

xATRTrailingStop1 = 0.0
xATRTrailingStop1 := iff(src1 > nz(xATRTrailingStop1[1], 0) and src1[1] >
nz(xATRTrailingStop1[1], 0), max(nz(xATRTrailingStop1[1]), src1 - nLoss1),
iff(src1 < nz(xATRTrailingStop1[1], 0) and src1[1] < nz(xATRTrailingStop1[1],
0), min(nz(xATRTrailingStop1[1]), src1 + nLoss1),
iff(src1 > nz(xATRTrailingStop1[1], 0), src1 - nLoss1, src1 + nLoss1)))

pos1 = 0
pos1 := iff(src1[1] < nz(xATRTrailingStop1[1], 0) and src1 >
nz(xATRTrailingStop1[1], 0), 1,
iff(src1[1] > nz(xATRTrailingStop1[1], 0) and src1 < nz(xATRTrailingStop1[1],
0), -1, nz(pos1[1], 0)))

xcolor1 = pos1 == -1 ? color.red : pos1 == 1 ? color.green : color.blue

plot(xATRTrailingStop1, color=xcolor1, title="Trailing Stop 1")


sell = crossunder(src1, xATRTrailingStop1)
barcolor1 = src1 > xATRTrailingStop1

// Indicator 2
src2 = close
xATR2 = atr(atrperiod2)
nLoss2 = keyvalue2 * xATR2

xATRTrailingStop2 = 0.0
xATRTrailingStop2 := iff(src2 > nz(xATRTrailingStop2[1], 0) and src2[1] >
nz(xATRTrailingStop2[1], 0), max(nz(xATRTrailingStop2[1]), src2 - nLoss2),
iff(src2 < nz(xATRTrailingStop2[1], 0) and src2[1] < nz(xATRTrailingStop2[1],
0), min(nz(xATRTrailingStop2[1]), src2 + nLoss2),
iff(src2 > nz(xATRTrailingStop2[1], 0), src2 - nLoss2, src2 + nLoss2)))

pos2 = 0
pos2 := iff(src2[1] < nz(xATRTrailingStop2[1], 0) and src2 >
nz(xATRTrailingStop2[1], 0), 1,
iff(src2[1] > nz(xATRTrailingStop2[1], 0) and src2 < nz(xATRTrailingStop2[1],
0), -1, nz(pos2[1], 0)))

xcolor2 = pos2 == -1 ? color.red : pos2 == 1 ? color.green : color.blue

plot(xATRTrailingStop2, color=xcolor2, title="Trailing Stop 2")


buy = crossover(src2, xATRTrailingStop2)

// [SHK] STC
EEEEEE = input(80, 'Length')
BBBB = input(27, 'FastLength')
BBBBB = input(50, 'SlowLength')

AAAA(BBB, BBBB, BBBBB) =>


fastMA = ema(BBB, BBBB)
slowMA = ema(BBB, BBBBB)
AAAA = fastMA - slowMA
AAAA

AAAAA(EEEEEE, BBBB, BBBBB) =>


AAA = input(0.5)
var CCCCC = 0.0
var DDD = 0.0
var DDDDDD = 0.0
var EEEEE = 0.0
BBBBBB = AAAA(close, BBBB, BBBBB)
CCC = lowest(BBBBBB, EEEEEE)
CCCC = highest(BBBBBB, EEEEEE) - CCC
CCCCC := CCCC > 0 ? (BBBBBB - CCC) / CCCC * 100 : nz(CCCCC[1])
DDD := na(DDD[1]) ? CCCCC : DDD[1] + AAA * (CCCCC - DDD[1])
DDDD = lowest(DDD, EEEEEE)
DDDDD = highest(DDD, EEEEEE) - DDDD
DDDDDD := DDDDD > 0 ? (DDD - DDDD) / DDDDD * 100 : nz(DDDDDD[1])
EEEEE := na(EEEEE[1]) ? DDDDDD : EEEEE[1] + AAA * (DDDDDD - EEEEE[1])
EEEEE

mAAAAA = AAAAA(EEEEEE, BBBB, BBBBB)

buy_filtered = buy and mAAAAA < 25


sell_filtered = sell and mAAAAA > 75

xcolor_buy = buy_filtered ? color.green : color.gray


xcolor_sell = sell_filtered ? color.red : color.gray

// Stop Loss and Take Profit


stopLossBuy = input(1.0, title="Stop Loss % for Buy", step=0.1)
takeProfitBuy = input(2.0, title="Take Profit % for Buy", step=0.1)
stopLossSell = input(1.0, title="Stop Loss % for Sell", step=0.1)
takeProfitSell = input(2.0, title="Take Profit % for Sell", step=0.1)

// Buy Strategy
buyCondition = buy_filtered and strategy.position_size <= 0
strategy.entry("Buy", strategy.long, when=buyCondition)
strategy.exit("Sell", "Buy", stop=strategy.position_avg_price * (1 - stopLossBuy /
100), limit=strategy.position_avg_price * (1 + takeProfitBuy / 100))

// Sell Strategy
sellCondition = sell_filtered and strategy.position_size >= 0
strategy.entry("Sell", strategy.short, when=sellCondition)
strategy.exit("Cover", "Sell", stop=strategy.position_avg_price * (1 + stopLossSell
/ 100), limit=strategy.position_avg_price * (1 - takeProfitSell / 100))

plotshape(buy_filtered, title="Buy", text="Buy", style=shape.labelup,


location=location.belowbar, color=xcolor_buy, textcolor=color.white, transp=0,
size=size.tiny)
plotshape(sell_filtered, title="Sell", text="Sell", style=shape.labeldown,
color=xcolor_sell, textcolor=color.white, transp=0, size=size.tiny)
// Color bar based on Indicator 1 or 2
barcolor(barcolor1 ? color.green : color.red)

// Alert conditions
alertcondition(sell_filtered, title="UT BOT Sell", message="UT BOT Sell")
alertcondition(buy_filtered, title="UT BOT Buy", message="UT BOT Buy")

You might also like