標的相對上市
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Morty

//@version=4
study("標的相對上市", resolution="")

s2 = input(title="上市", type=input.symbol, defval="TWSE:TAIEX")

f() => [open, high, low, close]

[o1, h1, l1, c1] = security(syminfo.tickerid, timeframe.period, f())
[o2, h2, l2, c2] = security(s2, timeframe.period, f())

c = o1/o2 < c1/c2 ? #E2DED0 : #4E4F50
plotcandle(o1/o2, h1/h2, l1/l2, c1/c2, color=c, wickcolor=c, bordercolor=c)

// Donchian Channels
length = input(50, "Donchian Channel Length", minval=1)
mid_ma_length = input(5, "Midline moving avarage length", minval=1)
lower = lowest(l1/l2, length)
upper = highest(h1/h2, length)
basis = avg(upper, lower)
pallete = ema(c1/c2, mid_ma_length) > basis ? #76B947 : #F83839
plot(basis, "Basis", color=pallete, linewidth=2)
u = plot(upper, "Upper", color=#2962FF)
l = plot(lower, "Lower", color=#2962FF)
fill(u, l, color=color.rgb(33, 150, 243, 95), title="Background")
