show bottom
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © slmgtv1

//@version=5
indicator("五分钟K线数据+是否底分型", overlay = true)

pos = input.float(title='显示距离%：', defval=0.0)

title = "5min_K_data"

low_di = low[1]
n = 1

// 处理各类包含关系：
low_2 = low[2]
low_1 = low[1]
low_0 = low

high_2 = high[2]
high_1 = high[1]
high_0 = high

// 1包2
if low[1]<low[2] and high[1]>high[2]
    low_2 := low[3]
    high_2:= high[3]

// 1包2和3
if low[1]<low[2] and high[1]>high[2] and low[1]<low[3] and high[1]>high[3]
    low_2 := low[4]
    high_2:= high[4]

// 1包2、3、4
if low[1]<low[2] and high[1]>high[2] and low[1]<low[3] and high[1]>high[3] and low[1]<low[4] and high[1]>high[4]
    low_2 := low[5]
    high_2:= high[5]

// 1包2、3、4、5
if low[1]<low[2] and high[1]>high[2] and low[1]<low[3] and high[1]>high[3] and low[1]<low[4] and high[1]>high[4] and low[1]<low[5] and high[1]>high[5]
    low_2 := low[6]
    high_2:= high[6]

// 2包1
if low[2]<low[1] and high[2]>high[1]
    low_2 := low[3]
    high_2:= high[3]
    low_1 := low[2]
    high_1:= high[2]
    low_di:= low[2]
    n:=2

// 3包1、2
if low[3]<low[1] and high[3]>high[1] and low[3]<low[2] and high[3]>high[2]
    low_2 := low[4]
    high_2:= high[4]
    low_1 := low[3]
    high_1:= high[3]
    low_di:= low[3]
    n:=3

// 4包1、2、3
if low[4]<low[1] and high[4]>high[1] and low[4]<low[2] and high[4]>high[2] and low[4]<low[3] and high[4]>high[3]
    low_2 := low[5]
    high_2:= high[5]
    low_1 := low[4]
    high_1:= high[4]
    low_di:= low[4]
    n:=4

// 5包1、2、3、4
if low[5]<low[1] and high[5]>high[1] and low[5]<low[2] and high[5]>high[2] and low[5]<low[3] and high[5]>high[3] and low[5]<low[4] and high[5]>high[4]
    low_2 := low[6]
    high_2:= high[6]
    low_1 := low[5]
    high_1:= high[5]
    low_di:= low[5]
    n:=5

var value_di = 0.0
var is_new_di = 0
var ts_di = 0

// 处理完毕，判断是否底分型
is_di = false
if low_1<=low_0 and low_1<=low_2 and high_1<=high_0 and high_1<=high_2
    is_di := true
    is_new_di := 1
    ts_di := time[n]
    label.new(bar_index[n], low_1*(1-pos/100), str.tostring('底'), style=label.style_label_up, textcolor=color.white, size=size.normal)
    value_di := low_1

// 5min bar四价：group=3
msg1='{"ticker":"' + syminfo.ticker + '", "k_time": "'+ timeframe.period + '","type":"' + title + '", "timestamp": "' + str.tostring(time) + '", "open": "'   + str.tostring(open, "#.000") + '", "close": "' + str.tostring(close,  "#.000") + '", "high":"'   + str.tostring(high, "#.000") + '", "low":"'    + str.tostring(low, "#.000") + '", "is_di": "' + str.tostring(is_di) + '", "low_di": "' + str.tostring(low_di, "#.000") + '", "pos_di": "' + str.tostring(n) + '", "group":"'  + str.tostring(3) + '" }'
alert(msg1, alert.freq_once_per_bar_close)


// 判断是否跌破底分型，破底时发送group=5
plot(value_di)

msg2 = '{ "group": "5", "ticker": "'+ syminfo.ticker +'", "timestamp":  "' + str.tostring(time) + '", "price":  "' + str.tostring(close,  "#.000") + '", "type": "passive_take_profit", "ts_di": "' + str.tostring(ts_di) + '" }'

if close < value_di and close[1]>=value_di[1] and is_new_di==1
    label.new(bar_index, high, str.tostring('破底'), style=label.style_label_down, color=color.red, textcolor=color.white, size=size.normal)
    // label.new(bar_index, high, str.tostring(ts_di), style=label.style_label_down, color=color.red, textcolor=color.white, size=size.normal)
    alert(msg2, alert.freq_once_per_bar_close)
    is_new_di := 0
    
    