本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("NS Model 2.0", shorttitle="NS Model 2.0", overlay=true, max_boxes_count=100, max_lines_count=300, max_labels_count=500)
// ===================== INPUTS =====================
tz = input.string("Asia/Dhaka", "Timezone", group="Session")
boxSess = input.session("1800-1930", "Box window (BDT)", group="Session")
fullSess = input.session("1800-0300", "Full NY session (BDT)", group="Session")
maxSig = input.int(1, "Max signals per session", minval=1, maxval=10, group="Session")
breakBy = input.string("Wick", "Candle 1 breaks the box when", options=["Wick", "Candle close"], group="Signal")
c3Level = input.string("Candle 2 extreme", "Candle 3 must close beyond", options=["Candle 2 extreme", "Candle 2 close"], group="Signal")
strict = input.bool(false, "Candles must be consecutive (no gaps)", group="Signal")
expiry = input.int(60, "Cancel setup after X bars", minval=5, group="Signal")
showLbl = input.bool(true, "Show candle 1 / 2 / 3 labels", group="Signal")
slMode = input.string("Since break", "Stop loss at", options=["Since break", "Opposite box edge"], group="Trade")
useSL = input.bool(false, "Use stop loss (if ON, SL hit counts as loss)", group="Trade")
tpMode = input.string("Fixed pct", "Take profit by", options=["RR", "Fixed pct"], group="Trade")
tpPct = input.float(2.5, "Take profit percent", minval=0.1, step=0.1, group="Trade")
rr = input.float(2.0, "Take profit RR (if RR mode)", minval=0.5, step=0.1, group="Trade")
life = input.string("Until next session", "Trade lifetime", options=["Until next session", "24 hours from entry", "Until target or stop"], group="Trade")
slBuf = input.float(0.1, "SL buffer (x ATR)", minval=0.0, step=0.05, group="Trade")
showDays = input.bool(true, "Show day-by-day status table (bottom right)", group="Day Stats")
// ===================== SESSIONS =====================
atr = ta.atr(14)
inBox = not na(time(timeframe.period, boxSess, tz))
inFull = not na(time(timeframe.period, fullSess, tz))
startBox = inBox and not inBox[1]
endFull = (not inFull) and inFull[1]
afterBox = inFull and not inBox
// ===================== STATE =====================
var float bHi = na
var float bLo = na
var box rBox = na
var box eBox = na
var int upStg = 0
var int upC1 = 0
var int upLast = 0
var float upMark = na
var float upC2 = na
var float upRunLo = na
var bool upFlip = false
var line upLine = na
var bool needUp = false
var int dnStg = 0
var int dnC1 = 0
var int dnLast = 0
var float dnMark = na
var float dnC2 = na
var float dnRunHi = na
var bool dnFlip = false
var line dnLine = na
var bool needDn = false
var int sigCnt = 0
var int nSess = 0
var int nC1 = 0
var int nC2 = 0
var int nExp = 0
var int nSig = 0
var int nFlipSig = 0
var int nExpT = 0
var int tDir = 0
var float tE = na
var float tTP = na
var float tSL = na
var float tRisk = na
var int tOpenT = 0
var int tDay = 0
var line entryLine = na
var line tpLine = na
var line slLine = na
var int wins = 0
var int losses = 0
var float sumRisk = 0.0
// Day-of-week stats (index 1 = Sunday ... 7 = Saturday, day = day the session started, in BDT)
var int sessDay = 0
var int[] dayW = array.new_int(8, 0)
var int[] dayL = array.new_int(8, 0)
// ===================== BOX =====================
if startBox
bHi := high
bLo := low
upStg := 0
dnStg := 0
sigCnt := 0
needUp := false
needDn := false
nSess += 1
sessDay := dayofweek(time, tz)
rBox := box.new(time, bHi, time, bLo, xloc=xloc.bar_time, border_color=color.blue, bgcolor=color.new(color.blue, 80))
eBox := box.new(time, bHi, time, bLo, xloc=xloc.bar_time, border_color=na, bgcolor=color.new(color.orange, 90))
else if inBox and not na(rBox)
bHi := math.max(bHi, high)
bLo := math.min(bLo, low)
box.set_top(rBox, bHi)
box.set_bottom(rBox, bLo)
box.set_right(rBox, time)
if inFull and not na(eBox)
box.set_top(eBox, bHi)
box.set_bottom(eBox, bLo)
box.set_right(eBox, time)
if endFull
upStg := 0
dnStg := 0
// ===================== TRADE MANAGEMENT =====================
// Rule: TP touched before the next session starts = WIN. Not touched = LOSS.
// A new session starts and the old trade never touched TP -> LOSS
if tDir != 0 and startBox and life == "Until next session"
losses += 1
sumRisk += tRisk
nExpT += 1
array.set(dayL, tDay, array.get(dayL, tDay) + 1)
tDir := 0
bool hadTrade = tDir != 0
bool longSL = useSL and tDir == 1 and low <= tSL
bool longTP = tDir == 1 and not longSL and high >= tTP
bool shortSL = useSL and tDir == -1 and high >= tSL
bool shortTP = tDir == -1 and not shortSL and low <= tTP
if hadTrade
if not na(entryLine)
line.set_x2(entryLine, bar_index)
if not na(tpLine)
line.set_x2(tpLine, bar_index)
if not na(slLine)
line.set_x2(slLine, bar_index)
if longSL or shortSL
losses += 1
sumRisk += tRisk
array.set(dayL, tDay, array.get(dayL, tDay) + 1)
tDir := 0
if longTP or shortTP
wins += 1
sumRisk += tRisk
array.set(dayW, tDay, array.get(dayW, tDay) + 1)
tDir := 0
// 24h mode: TP not touched within 24h -> LOSS
if tDir != 0 and life == "24 hours from entry" and time - tOpenT >= 86400000
losses += 1
sumRisk += tRisk
nExpT += 1
array.set(dayL, tDay, array.get(dayL, tDay) + 1)
tDir := 0
// ===================== CANDLE 1 -> 2 -> 3 (both sides tracked) =====================
bool buyOk = false
bool sellOk = false
bool buyTrig = false
bool sellTrig = false
bool insideBox = not na(bHi) and close <= bHi and close >= bLo
if needUp and insideBox
needUp := false
if needDn and insideBox
needDn := false
if afterBox and sigCnt < maxSig and not na(bHi)
bool upBreak = breakBy == "Wick" ? high > bHi : close > bHi
bool dnBreak = breakBy == "Wick" ? low < bLo : close < bLo
// ----- Progress of unfinished attempts (checked before a new candle 1 can start) -----
if upStg > 0 and bar_index > upLast
upRunLo := math.min(upRunLo, low)
line.set_x2(upLine, bar_index)
bool upGap = strict and bar_index > upLast + 1
if bar_index - upC1 > expiry or upGap
upStg := 0
needUp := true
nExp += 1
else if upStg == 1
if close > upMark
upStg := 2
upLast := bar_index
nC2 += 1
upC2 := high
if c3Level == "Candle 2 close"
upC2 := close
if showLbl
label.new(bar_index, high, "2", style=label.style_label_down, color=color.new(color.gray, 20), textcolor=color.white, size=size.small)
else
if close > upC2
buyTrig := true
if dnStg > 0 and bar_index > dnLast
dnRunHi := math.max(dnRunHi, high)
line.set_x2(dnLine, bar_index)
bool dnGap = strict and bar_index > dnLast + 1
if bar_index - dnC1 > expiry or dnGap
dnStg := 0
needDn := true
nExp += 1
else if dnStg == 1
if close < dnMark
dnStg := 2
dnLast := bar_index
nC2 += 1
dnC2 := low
if c3Level == "Candle 2 close"
dnC2 := close
if showLbl
label.new(bar_index, low, "2", style=label.style_label_up, color=color.new(color.gray, 20), textcolor=color.white, size=size.small)
else
if close < dnC2
sellTrig := true
if buyTrig and sellTrig
buyTrig := false
sellTrig := false
// ----- START: candle 1 on each side (only if no signal on this bar) -----
bool startUp = upStg == 0 and not needUp and upBreak and not dnBreak and not buyTrig and not sellTrig
bool startDn = dnStg == 0 and not needDn and dnBreak and not upBreak and not buyTrig and not sellTrig
if startUp
upFlip := dnStg > 0
dnStg := 0
upStg := 1
upC1 := bar_index
upLast := bar_index
upMark := high
upRunLo := low
nC1 += 1
upLine := line.new(bar_index, upMark, bar_index, upMark, color=color.orange, style=line.style_dashed)
if showLbl
label.new(bar_index, high, "1", style=label.style_label_down, color=color.new(color.gray, 20), textcolor=color.white, size=size.small)
if startDn
dnFlip := upStg > 0
upStg := 0
dnStg := 1
dnC1 := bar_index
dnLast := bar_index
dnMark := low
dnRunHi := high
nC1 += 1
dnLine := line.new(bar_index, dnMark, bar_index, dnMark, color=color.orange, style=line.style_dashed)
if showLbl
label.new(bar_index, low, "1", style=label.style_label_up, color=color.new(color.gray, 20), textcolor=color.white, size=size.small)
// ----- SIGNAL on candle 3 -----
if buyTrig or sellTrig
bool isBuy = buyTrig
if tDir == 0
float slP = 0.0
if slMode == "Since break"
if isBuy
slP := math.min(upRunLo, low) - atr * slBuf
else
slP := math.max(dnRunHi, high) + atr * slBuf
else
if isBuy
slP := bLo - atr * slBuf
else
slP := bHi + atr * slBuf
float riskD = isBuy ? close - slP : slP - close
if riskD > 0
float tpD = close * tpPct / 100.0
if tpMode == "RR"
tpD := riskD * rr
tDir := isBuy ? 1 : -1
tE := close
tSL := slP
tTP := isBuy ? close + tpD : close - tpD
tRisk := riskD * 100.0 / close
tOpenT := time
tDay := sessDay
buyOk := isBuy
sellOk := not isBuy
sigCnt += 1
nSig += 1
if isBuy and upFlip
nFlipSig += 1
if not isBuy and dnFlip
nFlipSig += 1
if showLbl
if isBuy
label.new(bar_index, high, "3", style=label.style_label_down, color=color.new(color.gray, 20), textcolor=color.white, size=size.small)
else
label.new(bar_index, low, "3", style=label.style_label_up, color=color.new(color.gray, 20), textcolor=color.white, size=size.small)
entryLine := line.new(bar_index, tE, bar_index, tE, color=color.gray, style=line.style_dashed)
tpLine := line.new(bar_index, tTP, bar_index, tTP, color=color.lime, width=2)
label.new(bar_index, tTP, str.tostring(tpPct, "#.0#") + "% TP", style=label.style_label_left, color=color.new(color.lime, 20), textcolor=color.black, size=size.tiny)
slLine := na
if useSL
slLine := line.new(bar_index, tSL, bar_index, tSL, color=color.red, style=line.style_dotted)
upStg := 0
dnStg := 0
needUp := true
needDn := true
plotshape(buyOk, title="BUY", style=shape.labelup, location=location.belowbar, color=color.green, text="BUY", textcolor=color.white, size=size.normal)
plotshape(sellOk, title="SELL", style=shape.labeldown, location=location.abovebar, color=color.red, text="SELL", textcolor=color.white, size=size.normal)
alertcondition(buyOk, "NY Box 3C BUY", "NY Box 3-Candle: BUY signal")
alertcondition(sellOk, "NY Box 3C SELL", "NY Box 3-Candle: SELL signal")
bgcolor(inFull ? color.new(color.gray, 95) : na)
// ===================== STATS TABLE =====================
var table stats = table.new(position.top_right, 2, 13, bgcolor=color.new(color.black, 20), border_width=1)
if barstate.islast
int totalT = wins + losses
float wr = 0.0
float avgRisk = 0.0
if totalT > 0
wr := wins * 100.0 / totalT
avgRisk := sumRisk / totalT
string beTxt = "-"
float beWr = 0.0
if useSL
if tpMode == "RR"
beWr := 100.0 / (1.0 + rr)
beTxt := str.tostring(beWr, "#.0") + "%"
else if totalT > 0
beWr := avgRisk * 100.0 / (tpPct + avgRisk)
beTxt := str.tostring(beWr, "#.0") + "%"
string wrTxt = "-"
if totalT > 0
wrTxt := str.tostring(wr, "#.0") + "%"
color wrCol = color.orange
if wr >= beWr
wrCol := color.lime
table.cell(stats, 0, 0, "Sessions found", text_color=color.white)
table.cell(stats, 1, 0, str.tostring(nSess), text_color=color.white)
table.cell(stats, 0, 1, "Candle 1 (box break)", text_color=color.white)
table.cell(stats, 1, 1, str.tostring(nC1), text_color=color.white)
table.cell(stats, 0, 2, "Candle 2 found", text_color=color.white)
table.cell(stats, 1, 2, str.tostring(nC2), text_color=color.white)
table.cell(stats, 0, 3, " - setup expired", text_color=color.gray)
table.cell(stats, 1, 3, str.tostring(nExp), text_color=color.gray)
table.cell(stats, 0, 4, "Signals (candle 3)", text_color=color.yellow)
table.cell(stats, 1, 4, str.tostring(nSig), text_color=color.yellow)
table.cell(stats, 0, 5, " - after opposite fakeout", text_color=color.gray)
table.cell(stats, 1, 5, str.tostring(nFlipSig), text_color=color.gray)
table.cell(stats, 0, 6, "Wins (TP touched)", text_color=color.lime)
table.cell(stats, 1, 6, str.tostring(wins), text_color=color.lime)
table.cell(stats, 0, 7, "Losses (not touched / SL)", text_color=color.red)
table.cell(stats, 1, 7, str.tostring(losses), text_color=color.red)
table.cell(stats, 0, 8, " - TP not touched in time", text_color=color.gray)
table.cell(stats, 1, 8, str.tostring(nExpT), text_color=color.gray)
table.cell(stats, 0, 9, "Win rate", text_color=color.white)
table.cell(stats, 1, 9, wrTxt, text_color=wrCol)
table.cell(stats, 0, 10, "Break-even", text_color=color.white)
table.cell(stats, 1, 10, beTxt, text_color=color.white)
table.cell(stats, 0, 11, "Target %", text_color=color.white)
table.cell(stats, 1, 11, str.tostring(tpPct, "#.0#"), text_color=color.white)
table.cell(stats, 0, 12, "Lifetime", text_color=color.white)
table.cell(stats, 1, 12, life, text_color=color.white)
// ===================== DAY-BY-DAY TABLE (bottom right) =====================
var table dayTbl = table.new(position.bottom_right, 4, 9, bgcolor=color.new(color.black, 20), border_width=1)
if barstate.islast and showDays
int[] ord = array.from(7, 1, 2, 3, 4, 5, 6)
string[] nm = array.from("Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
table.cell(dayTbl, 0, 0, "Day", text_color=color.white)
table.cell(dayTbl, 1, 0, "TP hit", text_color=color.lime)
table.cell(dayTbl, 2, 0, "Not hit / SL", text_color=color.red)
table.cell(dayTbl, 3, 0, "Win %", text_color=color.white)
int totW = 0
int totL = 0
for i = 0 to 6
int d = array.get(ord, i)
int w = array.get(dayW, d)
int l = array.get(dayL, d)
totW += w
totL += l
int n = w + l
string pTxt = n > 0 ? str.tostring(w * 100.0 / n, "#") + "%" : "-"
color rowCol = n == 0 ? color.gray : (w >= l ? color.lime : color.orange)
table.cell(dayTbl, 0, i + 1, array.get(nm, i), text_color=color.white)
table.cell(dayTbl, 1, i + 1, str.format("{0,number,00}", w), text_color=color.lime)
table.cell(dayTbl, 2, i + 1, str.format("{0,number,00}", l), text_color=color.red)
table.cell(dayTbl, 3, i + 1, pTxt, text_color=rowCol)
int totN = totW + totL
table.cell(dayTbl, 0, 8, "Total", text_color=color.yellow)
table.cell(dayTbl, 1, 8, str.format("{0,number,00}", totW), text_color=color.yellow)
table.cell(dayTbl, 2, 8, str.format("{0,number,00}", totL), text_color=color.yellow)
table.cell(dayTbl, 3, 8, totN > 0 ? str.tostring(totW * 100.0 / totN, "#") + "%" : "-", text_color=color.yellow)