//@version=6
strategy('抄底 V2.0', overlay = true, pyramiding = 10, process_orders_on_close = true, default_qty_type = strategy.percent_of_equity, default_qty_value = 10, initial_capital = 1000000, commission_value = 0.1)
dash_loc = input.session('Top Right', 'Dashboard Location', options = ['Top Right', 'Bottom Right', 'Top Left', 'Bottom Left', 'Middle Right', 'Bottom Center'], group = 'Style Settings')
text_size = input.session('Small', 'Dashboard Size', options = ['Tiny', 'Small', 'Normal', 'Large'], group = 'Style Settings')
cell_up = input.color(color.green, 'Up Cell Color', group = 'Style Settings')
cell_dn = input.color(color.red, 'Down Cell Color', group = 'Style Settings')
cell_Neut = input.color(color.gray, 'Nochange Cell Color', group = 'Style Settings')
row_col = color.blue
col_col = color.white
txt_col = color.white
// BACKTESTING RANGE 日期區間設定
startDate = input.int(title = 'Start Date', defval = 1, minval = 1, maxval = 31)
startMonth = input.int(title = 'Start Month', defval = 1, minval = 1, maxval = 12)
startYear = input.int(title = 'Start Year', defval = 1970, minval = 1800, maxval = 2100)
endDate = input.int(title = 'End Date', defval = 15, minval = 1, maxval = 31)
endMonth = input.int(title = 'End Month', defval = 11, minval = 1, maxval = 12)
endYear = input.int(title = 'End Year', defval = 2030, minval = 1800, maxval = 2100)
// === FUNCTION ===
start = timestamp(startYear, startMonth, startDate, 00, 00)
finish = timestamp(endYear, endMonth, endDate, 23, 59)
window() =>
time >= start and time <= finish ? true : false
// // TURN TD 8's & 9's ON / OFF
showBuyTDs = input(true, title = 'Show TD Buy')
showSellTDs = input(true, title = 'Show TD Sell')
len = input.int(10, 'len')
// // TD CALCS
buySignals = 0
buySignals := close < close[len] ? buySignals[1] == 21 ? 1 : buySignals[1] + 1 : 0
sellSignals = 0
sellSignals := close > close[len] ? sellSignals[1] == 21 ? 1 : sellSignals[1] + 1 : 0
BuyOrSell = math.max(buySignals, sellSignals)
TDbuy = showBuyTDs and bool(buySignals) and BuyOrSell < 9
TDbuy_up = showBuyTDs and bool(buySignals) and BuyOrSell >= 9
TD9buy = showBuyTDs and bool(buySignals) and BuyOrSell == 9
TD13buy = showBuyTDs and bool(buySignals) and BuyOrSell == 13
TD17buy = showBuyTDs and bool(buySignals) and BuyOrSell == 17
TD21buy = showBuyTDs and bool(buySignals) and BuyOrSell == 21
TDsell = showSellTDs and bool(sellSignals) and BuyOrSell < 9
TDsell_down = showSellTDs and bool(sellSignals) and BuyOrSell >= 9
TD9sell = showSellTDs and bool(sellSignals) and BuyOrSell == 9
TD13sell = showSellTDs and bool(sellSignals) and BuyOrSell == 13
TD17sell = showSellTDs and bool(sellSignals) and BuyOrSell == 17
TD21sell = showSellTDs and bool(sellSignals) and BuyOrSell == 21
// TD LABELS
plotshape(TD9buy, style = shape.labelup, text = '9', color = color.new(color.green, 0), textcolor = color.new(color.white, 0), size = size.tiny, location = location.belowbar)
plotshape(TD13buy, style = shape.labelup, text = '13', color = color.new(color.green, 0), textcolor = color.new(color.white, 0), size = size.tiny, location = location.belowbar)
plotshape(TD17buy, style = shape.labelup, text = '17', color = color.new(#4286d9, 0), textcolor = color.new(color.white, 0), size = size.tiny, location = location.belowbar)
plotshape(TD21buy, style = shape.labelup, text = '低', color = color.new(#2c2fff, 0), textcolor = color.new(color.white, 0), size = size.tiny, location = location.belowbar)
plotshape(TD9sell, style = shape.labeldown, text = '9', color = color.new(color.red, 0), textcolor = color.new(color.white, 0), size = size.tiny, location = location.abovebar)
plotshape(TD13sell, style = shape.labeldown, text = '13', color = color.new(color.red, 0), textcolor = color.new(color.white, 0), size = size.tiny, location = location.abovebar)
plotshape(TD17sell, style = shape.labeldown, text = '17', color = color.new(#ff0000, 0), textcolor = color.new(color.white, 0), size = size.tiny, location = location.abovebar)
plotshape(TD21sell, style = shape.labeldown, text = '高', color = color.new(#ff0ce7, 0), textcolor = color.new(color.white, 0), size = size.tiny, location = location.abovebar)
//
//---- Table Position & Size code start {----//
bullishpin = TD9buy or TD13buy or TD17buy or TD21buy
bearishpin = TD17sell
//
// 追蹤進場狀態
var bool enteredTD9 = false
var bool enteredTD13 = false
if window() and TD9buy and not enteredTD9
strategy.entry('TD9 Long', strategy.long)
enteredTD9 := true // 設定已進場
if window() and TD13buy and not enteredTD13
strategy.entry('TD13 Long', strategy.long)
enteredTD13 := true // 設定已進場
if window() and (TD17buy or TD21buy) // TD17 和 TD21 沒有限制
strategy.entry('TD17/21 Long', strategy.long)
// 條件重置邏輯(可選)
if window() and bearishpin and close > entryPrice
// 如果出場,重置進場狀態
enteredTD9 := false
enteredTD13 := false
//
if window() and bearishpin and close > entryPrice
strategy.close_all() //多單出場 ID 條件
//