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

//@version=4

study("Combination", overlay = true, max_lines_count=500,


max_bars_back=500, max_labels_count=500)

// |---------------|
// | MACD 面積計算 |
// |---------------|
// |{---------------

// |--macd inputs--|
fang_da = input(title="enlargeh", type=input.integer, defval=1,
group="MACD Setting")

fast_length = input(title="Fast Length", type=input.integer, defval=12,


group="MACD Setting")
slow_length = input(title="Slow Length", type=input.integer,
defval=26, group="MACD Setting")
src = input(title="Source", type=input.source, defval=close,
group="MACD Setting")
signal_length = input(title="Signal Smoothing", type=input.integer,
minval = 1, maxval = 50, defval = 9, group="MACD Setting")
sma_source = input(title="Oscillator MA Type", type=input.string,
defval="EMA", options=["SMA", "EMA"], group="MACD Setting")
sma_signal = input(title="Signal Line MA Type", type=input.string,
defval="EMA", options=["SMA", "EMA"], group="MACD Setting")

// |--macd colors--|
// col_macd = input(#2962FF, "MACD Line\uc0\u8195 \u8196 ",
input.color, group="Color Settings", inline="MACD")
// col_signal = input(#FF6D00, "Signal Line\uc0\u8195 \u8198 ",
input.color, group="Color Settings", inline="Signal")
// col_grow_above = input(#FF5252, "Above\uc0\u8196 \u8196 \u8197
Grow", input.color, group="Histogram", inline="Above")
// col_fall_above = input(#FFCDD2, "Fall", input.color,
group="Histogram", inline="Above")
// col_grow_below = input(#B2DFDB, "Below\uc0\u8195 Grow",
input.color, group="Histogram", inline="Below")
// col_fall_below = input(#26A69A, "Fall", input.color,
group="Histogram", inline="Below")

// Calculating macd
fast_ma = sma_source == "SMA" ? sma(src, fast_length) : ema(src,
fast_length)
slow_ma = sma_source == "SMA" ? sma(src, slow_length) : ema(src,
slow_length)
macd = fast_ma - slow_ma
signal = sma_signal == "SMA" ? sma(macd, signal_length) :
ema(macd, signal_length)
hist = macd - signal
ba = - hist

// track macd area

col_hist = hist >= 0 ? #B2DFDB : #FFCDD2

my_numberup(hist1, length) =>


sum1 = 0.0
if hist1>0
sum1 := sum1[1] + 1.0
sum1/length

my_numbedown(ba, length2) =>


sum2 = 0.0
if ba > 0
sum2 := sum2[1] + 1.0
sum2/length2

my_sumup(hist2, length2) =>


sum2 = 0.0
if hist2>=0
sum2 := sum2[1] + hist2
sum2/length2

// 面積取小數點第二位
p = ceil(100*my_sumup(hist,1))/100
p_2 = ceil(100*my_sumup(ba,1))/100

// label 面積

area = label.new(bar_index[1], na,


text=tostring(p[1]),
yloc=yloc.abovebar, color=col_hist,
size=size.tiny, style=label.style_triangledown)
label.delete(area[1])

if hist[1] > 0 and hist <= 0


label.new(bar_index[1], na,
text=tostring(p[1]),
yloc=yloc.abovebar, color=col_hist,
size=size.tiny, style=label.style_triangledown)

area_2 = label.new(bar_index[1], na,


text=tostring(p_2[1]),
yloc=yloc.belowbar, color=col_hist,
size=size.tiny, style=label.style_triangleup)

label.delete(area_2[1])

if ba[1] > 0 and ba <= 0


label.new(bar_index[1], na,
text = tostring(p_2[1]),
yloc=yloc.belowbar, color=col_hist,
size=size.tiny, style=label.style_triangleup)

// -------------------------------------------------------------}

// |--------------|
// | SMA 均線 |
// |--------------|

// |--SMA inputs--|

ma_src = input(title="Source", type=input.source, defval=close,


group="MA Setting")

ma_len_1 = input(title="MA1 Length", type=input.integer, defval=5,


group="MA Setting")
ma_len_2 = input(title="MA2 Length", type=input.integer, defval=13,
group="MA Setting")
ma_len_3 = input(title="MA3 Length", type=input.integer, defval=21,
group="MA Setting")
ma_len_4 = input(title="MA4 Length", type=input.integer, defval=34,
group="MA Setting")
ma_len_5 = input(title="MA5 Length", type=input.integer, defval=55,
group="MA Setting")
ma_len_6 = input(title="MA6 Length", type=input.integer, defval=89,
group="MA Setting")
ma_len_7 = input(title="MA7 Length", type=input.integer, defval=144,
group="MA Setting")
ma_len_8 = input(title="MA8 Length", type=input.integer, defval=233,
group="MA Setting")

// sma_source_1 = input(title="Oscillator MA 1 Type",


type=input.string, defval="SMA", options=["SMA", "EMA"],
group="MA Setting")
// sma_source_2 = input(title="Oscillator MA 2 Type",
type=input.string, defval="SMA", options=["SMA", "EMA"],
group="MA Setting")
// sma_source_3 = input(title="Oscillator MA 3 Type",
type=input.string, defval="SMA", options=["SMA", "EMA"],
group="MA Setting")
// sma_source_4 = input(title="Oscillator MA 4 Type",
type=input.string, defval="SMA", options=["SMA", "EMA"],
group="MA Setting")
// sma_source_5 = input(title="Oscillator MA 5 Type",
type=input.string, defval="SMA", options=["SMA", "EMA"],
group="MA Setting")
// sma_source_6 = input(title="Oscillator MA 6 Type",
type=input.string, defval="SMA", options=["SMA", "EMA"],
group="MA Setting")
// sma_source_7 = input(title="Oscillator MA 7 Type",
type=input.string, defval="SMA", options=["SMA", "EMA"],
group="MA Setting")
// sma_source_8 = input(title="Oscillator MA 8 Type",
type=input.string, defval="SMA", options=["SMA", "EMA"],
group="MA Setting")

// |---plot MA---|
// ma1 = sma_source_1 == "SMA" ? sma(ma_src, ma_len_1) :
ema(ma_src, ma_len_1)
// ma2 = sma_source_2 == "SMA" ? sma(ma_src, ma_len_2) :
ema(ma_src, ma_len_2)
// ma3 = sma_source_3 == "SMA" ? sma(ma_src, ma_len_3) :
ema(ma_src, ma_len_3)
// ma4 = sma_source_4 == "SMA" ? sma(ma_src, ma_len_4) :
ema(ma_src, ma_len_4)
// ma5 = sma_source_5 == "SMA" ? sma(ma_src, ma_len_5) :
ema(ma_src, ma_len_5)
// ma6 = sma_source_6 == "SMA" ? sma(ma_src, ma_len_6) :
ema(ma_src, ma_len_6)
// ma7 = sma_source_7 == "SMA" ? sma(ma_src, ma_len_7) :
ema(ma_src, ma_len_7)
// ma8 = sma_source_8 == "SMA" ? sma(ma_src, ma_len_8) :
ema(ma_src, ma_len_8)

ma1 = sma(ma_src,ma_len_1)
ma2 = sma(ma_src,ma_len_2)
ma3 = sma(ma_src,ma_len_3)
ma4 = sma(ma_src,ma_len_4)
ma5 = sma(ma_src,ma_len_5)
ma6 = sma(ma_src,ma_len_6)
ma7 = sma(ma_src,ma_len_7)
ma8 = sma(ma_src,ma_len_8)

plot(ma1, title="MA 5", color=color.blue


// ,display=display.none
)
plot(ma2, title="MA 13", color=color.orange
// ,display=display.none
)
plot(ma3, title="MA 21", color=color.olive
,display=display.none
)
plot(ma4, title="MA 34", color=color.maroon
,display=display.none
)
plot(ma5, title="MA 55", color=color.teal
,display=display.none
)
plot(ma6, title="MA 89", color=color.gray
,display=display.none
)
plot(ma7, title="MA 144", color=color.aqua
,display=display.none
)
plot(ma8, title="MA 233", color=color.silver
,display=display.none
)

// -------------------------------------------------------------------------------}
// |--------------------------------------------|
// | Bollinger Band |
// |--------------------------------------------|
// |{--------------------------------------------

// Bollinger Band Inputs


bb_len = input(title="BB Length", type=input.integer, defval=26,
group="Bollinger Band Setting")
bb_src = input(title="BB Source", type=input.source, defval=close,
group="Bollinger Band Setting")
bb_color = #B0C4DE

[middle_band, ub, lb] = bb(bb_src, bb_len, 2)


mid_p = plot(middle_band, title="middle band", color=bb_color
// ,display=display.none
)
ubp = plot(ub, title="upper band", color=bb_color
// ,display=display.none
)
lbp = plot(lb, title="lower band", color=bb_color
// ,display=display.none
)

// |--------------------------------------------}

// |--------------------------------------------|
// | 纏論 K 線 |
// |--------------------------------------------|
// |{-------------------------------------------

// 分型
// 分型 inputs
// K_legal = input(title = "K legal", type = input.bool, defval=true,
group="頂底分型")
// K_situ = input(title = "K situation", type = input.bool, defval=true,
group="頂底分型")
// K_pre = input(title = "previous K", type = input.bool, defval=true,
group="頂底分型")
// K_Z = input(title= "KZ", type = input.bool, defval=true, group="頂底
分型")
// K_frac = input(title="K fractal", type=input.bool, defval=true,
group="頂底分型")

newHigh = high
newLow = low

// 包含關係
include_1 = high > newHigh[1] and low < newLow[1]
include_2 = high < newHigh[1] and low > newLow[1]
include_3 = high > newHigh[1] and low == newLow[1]
include_4 = high < newHigh[1] and low == newLow[1]
include_5 = high == newHigh[1] and low < newLow[1]
include_6 = high == newHigh[1] and low > newLow[1]
include_7 = high == newHigh[1] and low == newLow[1]

directup = (newHigh[1] > newHigh[2] and newLow[1] > newLow[2])


or (newHigh[1] == newHigh[2] and newLow[1] > newLow[2])
or (newHigh[1] > newHigh[2] and newLow[1] == newLow[2])

directdown = (newHigh[1] < newHigh[2] and newLow[1] <


newLow[2])
or (newHigh[1] == newHigh[2] and newLow[1] < newLow[2])
or (newHigh[1] < newHigh[2] and newLow[1] == newLow[2])

equalhl = (newHigh[1] == newHigh[2] and newLow[1] == newLow[2])

//var plotshape la = na

dir_up = false
dir_down = false
not_include = false

if include_1 or include_2 or include_3 or include_4 or include_5 or


include_6 or include_7
for i = 0 to 300
if directup[i] and not equalhl[i]
newHigh := max(high,newHigh[1])
newLow := max(low,newLow[1])
dir_up := true
dir_down := false
else if directdown[i] and not equalhl[i]
newHigh := min(high,newHigh[1])
newLow := min(low,newLow[1])
dir_up := false
dir_down := true
else if equalhl[i]
continue
if directup[i+1]
newHigh := max(high,newHigh[1])
newLow := max(low,newLow[1])
dir_up := true
dir_down := false
else if directdown[i+1]
newHigh := min(high,newHigh[1])
newLow := min(low,newLow[1])
dir_up := false
dir_down := true
break
newHigh
newLow
else
if (newHigh > newHigh[1] and newLow > newLow[1])
or (newHigh == newHigh[1] and newLow > newLow[1])
or (newHigh > newHigh[1] and newLow == newLow[1])
newHigh := high
newLow := low
dir_up := true
dir_down := false
not_include := true
else if (newHigh < newHigh[1] and newLow < newLow[1])
or (newHigh == newHigh[1] and newLow < newLow[1])
or (newHigh < newHigh[1] and newLow == newLow[1])
newHigh := high
newLow := low
dir_up := false
dir_down := true
not_include := true
// else
// label.new(bar_index, na,
// yloc=yloc.abovebar, color=color.blue,
// size=size.auto, style=label.style_triangledown)
plotshape(dir_up, title="方向向上", style=shape.triangleup,
color=color.red, size=size.auto, location=location.top,
display=display.none)
plotshape(dir_down, title="方向向下", style=shape.triangledown,
color=color.green, size=size.auto, location=location.bottom,
display=display.none)
plotshape(not_include, title="合法 K 線", style=shape.triangledown,
color=color.blue, size=size.auto, location=location.abovebar,
display=display.none)

// plot newHigh and newLow


plot(newHigh, title="包含處理高", color=color.lime
,display=display.none
)
plot(newLow, title="包含處理低", color=color.red
,display=display.none
)

// 頂底分型判斷:

// 頂分型判斷
topfractal = ((newHigh[1] > newHigh and newHigh[1] > newHigh[2]
and newLow[1] > newLow and newLow[1] > newLow[2])
or (newHigh[1] >= newHigh and newHigh[1] > newHigh[2] and
newLow[1] > newLow and newLow[1] > newLow[2])
or (newHigh[1] > newHigh and newHigh[1] >= newHigh[2] and
newLow[1] > newLow and newLow[1] > newLow[2])
or (newHigh[1] > newHigh and newHigh[1] > newHigh[2] and
newLow[1] >= newLow and newLow[1] > newLow[2])
or (newHigh[1] > newHigh and newHigh[1] > newHigh[2] and
newLow[1] > newLow and newLow[1] >= newLow[2])
or (newHigh[1] >= newHigh and newHigh[1] >= newHigh[2] and
newLow[1] > newLow and newLow[1] > newLow[2])
// or (newHigh[1] >= newHigh and newHigh[1] > newHigh[2] and
newLow[1] >= newLow and newLow[1] > newLow[2])
or (newHigh[1] >= newHigh and newHigh[1] > newHigh[2] and
newLow[1] > newLow and newLow[1] >= newLow[2])
or (newHigh[1] > newHigh and newHigh[1] >= newHigh[2] and
newLow[1] >= newLow and newLow[1] > newLow[2])
or (newHigh[1] > newHigh and newHigh[1] >= newHigh[2] and
newLow[1] > newLow and newLow[1] >= newLow[2])
or (newHigh[1] > newHigh and newHigh[1] > newHigh[2] and
newLow[1] >= newLow and newLow[1] >= newLow[2])
// or (newHigh[1] >= newHigh and newHigh[1] >= newHigh[2] and
newLow[1] >= newLow and newLow[1] > newLow[2])
or (newHigh[1] >= newHigh and newHigh[1] >= newHigh[2] and
newLow[1] > newLow and newLow[1] >= newLow[2])
// or (newHigh[1] >= newHigh and newHigh[1] > newHigh[2] and
newLow[1] >= newLow and newLow[1] >= newLow[2])
or (newHigh[1] > newHigh and newHigh[1] >= newHigh[2] and
newLow[1] >= newLow and newLow[1] >= newLow[2])) and dir_up[1]

// 底分型判斷
buttomfractal = ((newHigh[1] < newHigh and newHigh[1] <
newHigh[2] and newLow[1] < newLow and newLow[1] < newLow[2])
or (newHigh[1] <= newHigh[2] and newHigh[1] < newHigh and
newLow[1] <= newLow[2] and newLow[1] <= newLow)
// or (newHigh[1] < newHigh[2] and newHigh[1] <= newHigh and
newLow[1] <= newLow[2] and newLow[1] <= newLow)
// or (newHigh[1] <= newHigh[2] and newHigh[1] <= newHigh and
newLow[1] < newLow[2] and newLow[1] <= newLow)
or (newHigh[1] <= newHigh[2] and newHigh[1] <= newHigh and
newLow[1] <= newLow[2] and newLow[1] < newLow)
or (newHigh[1] < newHigh[2] and newHigh[1] < newHigh and
newLow[1] <= newLow[2] and newLow[1] <= newLow)
// or (newHigh[1] < newHigh and newHigh[1] <= newHigh[2] and
newLow[1] < newLow and newLow[1] <= newLow[2])
or (newHigh[1] <= newHigh and newHigh[1] < newHigh[2] and
newLow[1] < newLow and newLow[1] <= newLow[2])
or (newHigh[1] < newHigh and newHigh[1] <= newHigh[2] and
newLow[1] <= newLow and newLow[1] < newLow[2])
// or (newHigh[1] <= newHigh and newHigh[1] < newHigh[2] and
newLow[1] <= newLow and newLow[1] < newLow[2])
or (newHigh[1] <= newHigh and newHigh[1] <= newHigh[2] and
newLow[1] < newLow and newLow[1] < newLow[2])
or (newHigh[1] < newHigh and newHigh[1] < newHigh[2] and
newLow[1] < newLow and newLow[1] <= newLow[2])
or (newHigh[1] < newHigh and newHigh[1] < newHigh[2] and
newLow[1] <= newLow and newLow[1] < newLow[2])
or (newHigh[1] < newHigh and newHigh[1] <= newHigh[2] and
newLow[1] < newLow and newLow[1] < newLow[2])
or (newHigh[1] <= newHigh and newHigh[1] < newHigh[2] and
newLow[1] < newLow and newLow[1] < newLow[2])) and dir_down[1]
// 標記頂底分型
if topfractal
labeltop = label.new(bar_index[1], na,
yloc=yloc.abovebar, color=color.red,
size=size.auto, style=label.style_triangledown)
if buttomfractal
labelbottom = label.new(bar_index[1], na,
yloc=yloc.belowbar, color=color.green,
size=size.auto, style=label.style_triangleup)

You might also like