คำเตือน: เนื้อหาด้านล่างเพื่อความบันเทิงและการศึกษาเท่านั้น! ไม่ใช่คำแนะนำทางการเงิน หรือคำแนะนำการเขียนโค้ด (AI หรืออย่างอื่น) นี่เป็นเพียงเพื่อสาธิตเท่านั้น!
ขอให้สร้างพรอมต์สำหรับ SMA Clouds
ทำหน้าที่เป็นผู้พัฒนา TradingView Pine Script v5 ระดับอาวุโส
สร้างตัวบ่งชี้แบบ overlay ที่ชื่อว่า:
“Technical Indicator Coding 4.0 — SMA Cloud Setup”
วัตถุประสงค์ (PURPOSE)
สร้างตัวบ่งชี้หลายไทม์เฟรมแบบไม่ repaint ที่ให้บริบทแนวโน้ม และทำหน้าที่คัดกรองสัญญาณที่อาจเกิดขึ้น ต้องไม่ทำการเทรดหรือให้คำแนะนำทางการเงิน
กรอบงาน (FRAMEWORK)
ตัวบ่งชี้ใช้ 3 ชั้นการวิเคราะห์:
โครงสร้าง 8 ชั่วโมง
- กำหนดโครงสร้างภาพรวมของตลาดบนไทม์เฟรม 8H
- จัดประเภทเป็นขาขึ้น ขาลง หรือกลางๆ
- แสดงการจัดประเภทปัจจุบันในแดชบอร์ดเล็กๆ
2. เมฆ SMA ขนาด 1 ชั่วโมง
- คำนวณค่า Simple Moving Average เร็วและช้า บนไทม์เฟรม 1H
- ทำให้ทั้งความยาว SMA สามารถตั้งค่าได้
- ค่าเริ่มต้น: [INSERT FAST SMA] และ [INSERT SLOW SMA]
- เติมพื้นที่ระหว่างค่าเฉลี่ย:
• สีเขียวเมื่อ SMA เร็วอยู่เหนือ SMA ช้า
• สีแดงเมื่อ SMA เร็วอยู่ต่ำกว่า SMA ช้า
• สีเทากลางๆเมื่อความสัมพันธ์ยังไม่ชัด
- เเมฆ 1H เป็นตัวกรองทิศทางแนวโน้มหลัก
3. ทริกเกอร์ 15 นาที และการยืนยัน IMACD
- คำนวณทริกเกอร์เข้า (entry) บนไทม์เฟรม 15M
- ยืนยันทริกเกอร์ขาขึ้นได้เฉพาะเมื่อโมเมนตัมของ IMACD เป็นบวกเท่านั้น
- ยืนยันสัญญาณขาลงได้เฉพาะเมื่อโมเมนตัมของ IMACD เป็นลบ
- ใช้สเปก IMACD ต่อไปนี้: [INSERT EXACT IMACD FORMULA AND SETTINGS].
- วาดสามเหลี่ยมชี้ขึ้นสีเขียวสำหรับทริกเกอร์ขาขึ้นที่ยืนยันแล้ว
- วาดสามเหลี่ยมชี้ลงสีแดงสำหรับทริกเกอร์ขาลงที่ยืนยันแล้ว
ลำดับความสำคัญของสัญญาณ (SIGNAL HIERARCHY)
คุณสมบัติขาขึ้น (bullish qualification) ต้องเป็นไปตามเงื่อนไข:
- โครงสร้าง 8H ที่ยอมรับได้คือแบบขาขึ้นหรือเป็นกลาง
- เมฆ SMA 1H ขาขึ้น
- ทริกเกอร์ 15M ขาขึ้น
- การยืนยัน IMACD เป็นบวก
เงื่อนไขการยืนยันขาลงต้องเป็นส่วนกลับ (inverse) แบบเป๊ะทุกประการ
ข้อกำหนดทางเทคนิค
- ใช้ request.security() โดยกำหนด barmerge.lookahead_off
- ประเมินสัญญาณเฉพาะบนแท่งที่ปิดแล้วยืนยันแล้วเท่านั้น
- ห้าม repaint สัญญาณในอดีต
- ห้ามใช้ข้อมูลในอนาคต
- ให้ 3 ไทม์เฟรมและความยาวการคำนวณทั้งหมดสามารถเปลี่ยนผ่านอินพุตได้
- ใส่สวิตช์เพื่อแสดงหรือซ่อนเมฆ, ป้ายโครงสร้าง, มาร์กเกอร์ทริกเกอร์ และแดชบอร์ด
- เพิ่มกฎ alertcondition() สำหรับการยืนยันคุณสมบัติขาขึ้นและขาลง
- ทำให้กราฟดูสะอาดตา
- ห้ามรวมคำสั่งอัตโนมัติ คำกล่าวอ้างด้านผลการเทรด P&L หรือคำแนะนำการลงทุน
- ใส่คอมเมนต์โค้ดให้ชัดเจนและอธิบายว่าแต่ละไทม์เฟรมมีส่วนต่อผลลัพธ์อย่างไร
ก่อนที่จะเขียนโค้ด ให้ระบุว่ามี กฎการคำนวณหรือพารามิเตอร์ใดที่ขาดหายไปหรือไม่ อย่าสร้างค่าที่เป็นการตั้งค่าสิทธิ์เฉพาะ (proprietary) ขึ้นมา ขอให้ฉันให้ค่าที่ขาดหายมาก่อน
หลังจากได้รับค่าดังกล่าวแล้ว โปรดให้:
1. เขียนโค้ด Pine Script v5 ครบถ้วน
2. คำอธิบายตรรกะแบบกระชับ
3. เช็กลิสต์สำหรับทดสอบว่าตัวบ่งชี้ไม่ repaint
CODE (COPY & PASTE)
//@version=6
indicator("John Wick v 4.0", overlay=true)
//==================================================
// MACD Inputs
//==================================================
fastLength = input.int(12, "MACD Fast Length")
slowLength = input.int(26, "MACD Slow Length")
signalLength = input.int(9, "MACD Signal Length")
atrLength = input.int(14, "ATR Length")
showExits = input.bool(true)
showCrosses = input.bool(true)
minProfitATR = input.float(0.30)
//==================================================
// MACD Core
//==================================================
fastMA = ta.ema(close, fastLength)
slowMA = ta.ema(close, slowLength)
macdLine = fastMA - slowMA
signalLine = ta.ema(macdLine, signalLength)
hist = macdLine - signalLine
//==================================================
// เหตุการณ์การตัดกัน (Cross Events)
//==================================================
bullCross = ta.crossover(macdLine, signalLine)
bearCross = ta.crossunder(macdLine, signalLine)
//==================================================
// ระยะห่างของมาร์กเกอร์
//==================================================
atrValue = ta.atr(atrLength)
dotOffset = atrValue * 0.28
triOffset = atrValue * 0.70
longExitY = high + dotOffset
shortExitY = low - dotOffset
triBullY = low - triOffset
triBearY = high + triOffset
//==================================================
// Trade State Engine
//==================================================
var int activeDir = 0
var float entryPrice = na
var float bestHist = na
var bool partialTaken = false
if bullCross
activeDir := 1
entryPrice := close
bestHist := hist
partialTaken := false
if bearCross
activeDir := -1
entryPrice := close
bestHist := hist
partialTaken := false
if activeDir == 1
bestHist := na(bestHist) ? hist : math.max(bestHist, hist)
if activeDir == -1
bestHist := na(bestHist) ? hist : math.min(bestHist, hist)
//==================================================
// ลอจิกการออก (Exit Logic)
//==================================================
longInProfit =
activeDir == 1
and not na(entryPrice)
and high >= entryPrice + atrValue * minProfitATR
shortInProfit =
activeDir == -1
and not na(entryPrice)
and low <= entryPrice - atrValue * minProfitATR
longMomentumFade =
activeDir == 1
and bestHist > 0
and hist < hist[1]
shortMomentumFade =
activeDir == -1
and bestHist < 0
and hist > hist[1]
yellowLongExit =
showExits
and activeDir == 1
and not partialTaken
and longInProfit
and longMomentumFade
yellowShortExit =
showExits
and activeDir == -1
and not partialTaken
and shortInProfit
and shortMomentumFade
if yellowLongExit or yellowShortExit
partialTaken := true
redLongExit =
showExits
and activeDir == 1
and partialTaken
and bearCross
redShortExit =
showExits
and activeDir == -1
and partialTaken
and bullCross
if redLongExit or redShortExit
activeDir := 0
entryPrice := na
bestHist := na
partialTaken := false
//==================================================
// EMA STACK (เมฆ 5 ชั้น)
//==================================================
ema5 = ta.ema(close, 5)
ema9 = ta.ema(close, 9)
ema12 = ta.ema(close, 12)
ema26 = ta.ema(close, 26)
ema50 = ta.ema(close, 50)
//==================================================
// พล็อต EMAs (บาง / เงียบ)
//==================================================
p5 = plot(ema5, color=color.new(color.aqua, 20), linewidth=1)
p9 = plot(ema9, color=color.new(color.blue, 20), linewidth=1)
p12 = plot(ema12, color=color.new(color.gray, 20), linewidth=1)
p26 = plot(ema26, color=color.new(color.orange,20), linewidth=1)
p50 = plot(ema50, color=color.new(color.red, 20), linewidth=1)
//==================================================
// CLOUDS
//==================================================
// ปฏิกิริยาเร็ว 5–9
fill(p5, p9, color=color.new(color.aqua, 80))
// โมเมนตัม 9–12
fill(p9, p12, color=color.new(color.blue, 82))
// โครงสร้าง 12–26
fill(p12, p26, color=color.new(color.gray, 84))
// แนวโน้ม 26–50
fill(p26, p50, color=color.new(color.orange, 86))
//==================================================
// Exit Dots
//==================================================
plotshape(
yellowLongExit ? longExitY : na,
style=shape.circle,
location=location.absolute,
color=color.new(color.yellow, 20),
size=size.tiny
)
plotshape(
yellowShortExit ? shortExitY : na,
style=shape.circle,
location=location.absolute,
color=color.new(color.yellow, 20),
size=size.tiny
)
plotshape(
redLongExit ? longExitY : na,
style=shape.circle,
location=location.absolute,
color=color.red,
size=size.tiny
)
plotshape(
redShortExit ? shortExitY : na,
style=shape.circle,
location=location.absolute,
color=color.red,
size=size.tiny
)
//==================================================
// Entry Triangles
//==================================================
plotshape(
showCrosses and bullCross ? triBullY : na,
style=shape.triangleup,
location=location.absolute,
color=color.lime,
size=size.small
)
plotshape(
showCrosses and bearCross ? triBearY : na,
style=shape.triangledown,
location=location.absolute,
color=color.red,
size=size.small
)
ATR Prompt and Code
พรอมต์
ทำหน้าที่เป็นผู้พัฒนา TradingView Pine Script v5 ระดับอาวุโส
สร้างตัวบ่งชี้แบบ overlay ที่ชื่อว่า:
“Technical Indicator Coding 5.2 — Average True Range”
วัตถุประสงค์ (PURPOSE)
สร้างตัวบ่งชี้ทิศทางแนวโน้มที่ปรับตามความผันผวน โดยใช้ระดับ trailing ของ Average True Range แบบ ratcheting ตัวบ่งชี้นี้เพื่อการวิเคราะห์เชิงการศึกษาเท่านั้นและต้องไม่ทำการเทรดหรืออ้างอัตราผลตอบแทน
ค่าตั้งค่าหลัก (CORE SETTINGS)
- คาบ ATR: 10
- ตัวคูณ ATR: 1.9
- ตราบใดที่ราคายังคงอยู่ต่ำกว่าระดับ trailing ก่อนหน้า ระดับอาจขยับลงได้ แต่ไม่ให้ขยับขึ้น
- ทำให้ช่วง ATR, ตัวคูณ และแหล่งที่มาสามารถตั้งค่าได้
การคำนวณ
1. คำนวณ Average True Range เฉลี่ยเหนือ 10 ช่วง
2. คำนวณระยะ trailing:
trailingDistance = 1.9 × ATR(10)
3. สร้างระดับ trailing แบบ recursive ที่เลื่อนไปตามราคา:
- ตราบใดที่ราคายังคงอยู่เหนือระดับ trailing ก่อนหน้า ระดับอาจขยับขึ้นได้ แต่ไม่ให้ขยับลง
shortInProfit =
- เมื่อราคาตัดผ่านระดับ trailing ให้เปลี่ยนทิศทางที่โค้ดกำหนด
ตรรกะทิศทาง (DIRECTION LOGIC)
- เปลี่ยนทิศทางเป็นขาขึ้น เมื่อราคาปิดตัดผ่านเหนือระดับ trailing
- เปลี่ยนทิศทางเป็นขาลงเมื่อราคาปิดตัดผ่านต่ำกว่าระดับ trailing
- สร้างสัญญาณเมื่อทิศทางเปลี่ยนเท่านั้น
- ห้ามสร้างสัญญาณที่ซ้ำทุกแท่ง
ผลลัพธ์เชิงภาพ (VISUAL OUTPUT)
- พล็อตระดับ ATR trailing
- ใช้สีเขียวเมื่อทิศทางที่โค้ดกำหนดเป็นขาขึ้น
- ใช้สีแดงเมื่อทิศทางที่โค้ดกำหนดเป็นขาลง
- วาดสามเหลี่ยมชี้ขึ้นสีเขียวเมื่อทิศทางขาขึ้นมีการเปลี่ยนแปลง
- วาดสามเหลี่ยมชี้ลงสีแดงเมื่อเกิดการเปลี่ยนแปลงทิศทางฝั่งขาลง
- (ไม่บังคับ) ระบายสีแท่งเทียนตามทิศทางปัจจุบัน
EMA CLOUD
- เพิ่มเมฆของ Exponential Moving Average ช่วง 12 และ 26
- เติมเมฆเป็นสีเขียวเมื่อ EMA 12 อยู่เหนือ EMA 26
- ระบายสีเป็นสีแดงเมื่อ EMA 12 ต่ำกว่า EMA 26
- มองเมฆ EMA เป็นบริบทของโมเมนตัม; มันต้องไม่เปลี่ยนการคำนวณการเปลี่ยนทิศทางของ ATR
- เพิ่มอินพุตเพื่อซ่อนเมฆ EMA
ข้อกำหนดทางเทคนิค
- ใช้การปิดแท่งเทียนที่ยืนยันแล้ว
- ห้าม repaint
- ห้ามใช้ข้อมูลในอนาคตหรือการมองล่วงหน้า (look-ahead)
- รักษาระดับ trailing ผ่านตรรกะของตัวแปรแบบ recursive
- เพิ่มกฎ alertcondition() สำหรับการเปลี่ยนทิศทางขาขึ้นและขาลง
- เพิ่มสวิตช์สำหรับ marker, สีแท่งเทียน, ระดับ trailing และเมฆ EMA
- ห้ามสร้างคำสั่ง strategy, ตารางผลลัพธ์, การคำนวณ P&L หรือคำแนะนำทางการเงิน
- จัดระเบียบและใส่คอมเมนต์ให้โค้ดอ่านง่าย
Provide:
1. เขียนโค้ด Pine Script v5 ครบถ้วน
คำอธิบายด้วยภาษาธรรมดาเกี่ยวกับ ATR และกลไกแบบ “ratcheting”
3. คำอธิบายเงื่อนไขที่สร้างตัวทำเครื่องหมายแต่ละแบบ
4. เช็กลิสต์ตรวจสอบแบบไม่ repaint สำหรับการยืนยันช่วงสั้น
โค้ด (CODE)
//@version=6
indicator("JW V5.2", overlay=true)
// ── Inputs ──────────────────────────────────────────
atrPeriod = input.int(10, "ATR Period")
keyValue = input.float(1.9, "Key Value / ATR Multiplier", step=0.1)
warnZoneATR = input.float(0.25, "Warning Zone (x ATR)", step=0.05)
cloudSmooth = input.int(3, "Cloud Smoothness", minval=1, tooltip="ทำให้แถบที่แสดงผลเรียบขึ้นเท่านั้น — ไม่ได้มีผลต่อเวลาของสัญญาณ")
src = close
// ── แกนหลัก ATR trailing stop — ต้นทางดิบ ขับเคลื่อนทุกอย่างจริง ──
xATR = ta.atr(atrPeriod)
nLoss = keyValue * xATR
var float trailStop = 0.0
trailStop := src > nz(trailStop[1], 0) and src[1] > nz(trailStop[1], 0) ? math.max(nz(trailStop[1]), src - nLoss) :
src < nz(trailStop[1], 0) and src[1] < nz(trailStop[1], 0) ? math.min(nz(trailStop[1]), src + nLoss) :
src > nz(trailStop[1], 0) ? src - nLoss : src + nLoss
var int pos = 0
pos := src[1] < nz(trailStop[1], 0) and src > nz(trailStop[1], 0) ? 1 :
src[1] > nz(trailStop[1], 0) and src < nz(trailStop[1], 0) ? -1 : nz(pos[1], 0)
trendColor = pos == 1 ? color.new(color.green, 0) : pos == -1 ? color.new(color.red, 0) : color.gray
// ── เมฆ — ปรับให้ลื่นไหลด้วยการทำให้เรียบด้วยภาพ (cosmetic) ──
plotSrc = ta.ema(src, cloudSmooth)
plotStop = ta.ema(trailStop, cloudSmooth)
pricePlot = plot(plotSrc, title="Price (smoothed)", color=trendColor, linewidth=1)
stopPlot = plot(plotStop, title="Trail Stop (smoothed)", color=trendColor, linewidth=1)
fill(pricePlot, stopPlot, color=color.new(trendColor, 75), title="Trend Cloud")
// ── สัญญาณ — ขับเคลื่อนด้วยค่าดิบ ไม่ได้รับผลจากการทำให้เรียบ ──
buySignal = pos == 1 and pos[1] == -1
sellSignal = pos == -1 and pos[1] == 1
plotshape(buySignal, title="Buy", text="buy", style=shape.triangleup, location=location.belowbar, color=color.green, textcolor=color.white, size=size.small)
plotshape(sellSignal, title="Sell", text="sell", style=shape.triangledown, location=location.abovebar, color=color.red, textcolor=color.white, size=size.small)
alertcondition(buySignal, "Buy Signal", "ATR Trail flipped bullish")
alertcondition(sellSignal, "Sell Signal", "ATR Trail flipped bearish")
// ── Flip proximity — ช่องว่างดิบ เหมือนคณิตสัญญาณ ──
gapATR = pos == 1 ? (src - trailStop) / xATR : (trailStop - src) / xATR
pctToFlip = math.max(0, math.min(100, 100 * (1 - gapATR / keyValue)))
dirText = pos == 1 ? "LONG" : pos == -1 ? "SHORT" : "FLAT"
nearFlip = gapATR <= warnZoneATR
labelColor = nearFlip ? color.new(color.orange, 0) : trendColor
labelText = dirText + " — " + str.tostring(gapATR, "#.##") + " ATR to flip (" + str.tostring(pctToFlip, "#") + "%)"
var label proximityLabel = na
if barstate.islast
label.delete(proximityLabel)
proximityLabel := label.new(bar_index + 2, src, labelText, style=label.style_label_left, color=labelColor, textcolor=color.white, size=size.normal)

