//@version=5
indicator("Multi-TF Prob-Up (MACD·RSI·Vol)", shorttitle="MU-PROB", overlay=false)
// ────────────── 1. 사용자 입력 ──────────────
lookback = input.int(200, "통계 샘플 수", minval = 50)
src = close
showTable = input.bool(true, "확률 테이블 표시")
// ────────────── 2. 멀티타임프레임 데이터 가져오기 ──────────────
// 5분봉
[macd5, sig5, hist5 ] = request.security(syminfo.tickerid, "5", ta.macd(src, 12, 26, 9))
rsi5 = request.security(syminfo.tickerid, "5", ta.rsi(src, 14))
vol5 = request.security(syminfo.tickerid, "5", volume)
// 15분봉
[macd15, sig15, hist15] = request.security(syminfo.tickerid, "15", ta.macd(src, 12, 26, 9))
rsi15 = request.security(syminfo.tickerid, "15", ta.rsi(src, 14))
vol15 = request.security(syminfo.tickerid, "15", volume)
// 1시간봉
[macd60, sig60, hist60] = request.security(syminfo.tickerid, "60", ta.macd(src, 12, 26, 9))
rsi60 = request.security(syminfo.tickerid, "60", ta.rsi(src, 14))
vol60 = request.security(syminfo.tickerid, "60", volume)
// 4시간봉
[macd240, sig240, hist240] = request.security(syminfo.tickerid, "240", ta.macd(src, 12, 26, 9))
rsi240 = request.security(syminfo.tickerid, "240", ta.rsi(src, 14))
vol240 = request.security(syminfo.tickerid, "240", volume)
// ────────────── 3. 조건 벡터 만들기 ──────────────
// 각 조건은 1(불리시), 0(베어링)으로 이진화
cond5 = (macd5 > sig5 and hist5 > hist5[1]) or rsi5 < 30
cond15 = (macd15 > sig15 and hist15 > hist15[1]) or rsi15 < 30
cond60 = (macd60 > sig60 and hist60 > hist60[1]) or rsi60 < 30
cond240= (macd240> sig240 and hist240> hist240[1])or rsi240< 30
// 거래량 급증 조건 (현재 봉이 평균 대비 1.5배 이상)
volUp5 = vol5 > ta.sma(vol5 , 20) * 1.5
volUp15 = vol15 > ta.sma(vol15, 20) * 1.5
volUp60 = vol60 > ta.sma(vol60, 20) * 1.5
volUp240= vol240> ta.sma(vol240,20) * 1.5
// 최종 스코어 (0~8)
score = (cond5 ? 1 : 0) + (cond15 ? 1 : 0) + (cond60 ? 1 : 0) + (cond240 ? 1 : 0) +
(volUp5 ? 1 : 0) + (volUp15? 1 : 0) + (volUp60? 1 : 0) + (volUp240? 1 : 0)
// ────────────── 4. 확률 계산 (과거 샘플 기반) ──────────────
var float[] probArr = array.new<float>()
var int[] cntArr = array.new<int>()
if barstate.isconfirmed
// 다음 봉 상승 여부
nextUp = src > src[1]
// score 값에 따라 버킷에 누적
idx = math.round(score)
while array.size(cntArr) <= idx
array.push(cntArr, 0)
array.push(probArr, 0.0)
prevCnt = array.get(cntArr, idx)
prevProb = array.get(probArr, idx)
newCnt = prevCnt + 1
newProb = (prevProb * prevCnt + (nextUp ? 100.0 : 0.0)) / newCnt
array.set(cntArr, idx, newCnt)
array.set(probArr, idx, newProb)
// 현재 score에 해당하는 확률
currentProb = score <= array.size(probArr) - 1 ? array.get(probArr, score) : 50
// ────────────── 5. 시각화 ──────────────
plot(currentProb, title="상승 확률(%)", color=color.new(color.blue, 0), linewidth=2)
hline(50, "50%", color=color.gray)
// 테이블
if showTable
var table t = table.new(position.top_right, 2, 5, bgcolor=color.black)
if barstate.islastconfirmedhistory
table.cell(t, 0, 0, "타임프레임", text_color=color.white)
table.cell(t, 0, 1, "5분", text_color=color.white)
table.cell(t, 0, 2, "15분", text_color=color.white)
table.cell(t, 0, 3, "1H", text_color=color.white)
table.cell(t, 0, 4, "4H", text_color=color.white)
table.cell(t, 1, 0, "상승확률(%)", text_color=color.white)
table.cell(t, 1, 1, str.tostring(math.round(currentProb)), text_color=color.white)
table.cell(t, 1, 2, "", text_color=color.white)
table.cell(t, 1, 3, "", text_color=color.white)
table.cell(t, 1, 4, "", text_color=color.white)