Thursday, January 30, 2025

Pine Script สำหรับการเรียนรู้เริ่มต้น

Pine Script เป็นภาษาสคริปต์ที่ใช้ใน TradingView สำหรับการสร้างอินดิเคเตอร์ กลยุทธ์ และการแจ้งเตือน (alerts) เพื่อช่วยในการวิเคราะห์ทางเทคนิค

1. เริ่มต้นกับ Pine Script

Pine Script ใช้โครงสร้างคล้าย Python และ JavaScript แต่เรียบง่ายกว่า โดยเริ่มต้นใช้งานได้โดยการเปิด TradingView แล้วเลือก Pine Editor ด้านล่างของหน้าจอ

🔹 ตัวอย่างโค้ดพื้นฐาน

pinescript

//@version=5 indicator("My First Indicator", overlay=true) plot(close, color=color.blue)

อธิบาย:

  • //@version=5 → ระบุว่าใช้ Pine Script เวอร์ชัน 5 (ล่าสุด)
  • indicator("My First Indicator", overlay=true) → สร้างอินดิเคเตอร์ที่แสดงบนกราฟราคา
  • plot(close, color=color.blue) → วาดกราฟของราคาปิด (close) ด้วยเส้นสีน้ำเงิน

2. การสร้าง Indicator พื้นฐาน

ตัวอย่างอินดิเคเตอร์ Moving Average (SMA 14)

pinescript

//@version=5 indicator("Simple Moving Average", overlay=true) length = 14 sma_value = ta.sma(close, length) plot(sma_value, color=color.orange)

อธิบาย:

  • ta.sma(close, length) → คำนวณค่าเฉลี่ยเคลื่อนที่แบบง่าย (Simple Moving Average)
  • plot(sma_value, color=color.orange) → แสดง SMA บนกราฟ

3. การสร้างเงื่อนไขซื้อ-ขาย (Buy/Sell Signal)

pinescript

//@version=5 indicator("Buy/Sell Signal", overlay=true) // คำนวณเส้น SMA 14 และ SMA 50 sma14 = ta.sma(close, 14) sma50 = ta.sma(close, 50) // กำหนดเงื่อนไขซื้อและขาย buySignal = ta.crossover(sma14, sma50) // เส้น SMA14 ตัดขึ้น SMA50 → ซื้อ sellSignal = ta.crossunder(sma14, sma50) // เส้น SMA14 ตัดลง SMA50 → ขาย // วาดสัญลักษณ์ลูกศรบนกราฟ plotshape(series=buySignal, location=location.belowbar, color=color.green, style=shape.labelup, title="Buy") plotshape(series=sellSignal, location=location.abovebar, color=color.red, style=shape.labeldown, title="Sell")

อธิบาย:

  • ta.crossover(sma14, sma50) → ตรวจจับเมื่อเส้น SMA14 ตัดขึ้น SMA50 (ส่งสัญญาณซื้อ)
  • ta.crossunder(sma14, sma50) → ตรวจจับเมื่อเส้น SMA14 ตัดลง SMA50 (ส่งสัญญาณขาย)
  • plotshape(...) → วาดสัญลักษณ์ลูกศรสีเขียว (Buy) และสีแดง (Sell) บนกราฟ

4. การสร้างกลยุทธ์เทรดอัตโนมัติ (Backtest Strategy)

pinescript

//@version=5 strategy("SMA Cross Strategy", overlay=true) // กำหนดเส้น SMA sma14 = ta.sma(close, 14) sma50 = ta.sma(close, 50) // เงื่อนไขเข้าและออกคำสั่งซื้อขาย longCondition = ta.crossover(sma14, sma50) if longCondition strategy.entry("Buy", strategy.long) shortCondition = ta.crossunder(sma14, sma50) if shortCondition strategy.close("Buy")

อธิบาย:

  • strategy.entry("Buy", strategy.long) → เปิดคำสั่งซื้อเมื่อ SMA14 ตัดขึ้น SMA50
  • strategy.close("Buy") → ปิดคำสั่งซื้อเมื่อ SMA14 ตัดลง SMA50
  • นำไปทดสอบย้อนหลัง (Backtest) บน TradingView ได้

5. แหล่งเรียนรู้เพิ่มเติม


สรุป:
1️⃣ Pine Script ใช้งานง่าย เน้นสร้างอินดิเคเตอร์และกลยุทธ์การเทรด
2️⃣ สามารถคำนวณค่า Indicator เช่น SMA, MACD, RSI ได้
3️⃣ ใช้ plot() แสดงเส้น Indicator และ plotshape() แสดงสัญลักษณ์ Buy/Sell
4️⃣ สามารถใช้ strategy.entry() และ strategy.close() สำหรับการเทรดอัตโนมัติ


การเพิ่ม MACD และ RSI ใน Pine Script สามารถทำได้อย่างง่ายดาย โดยใช้คำสั่งพื้นฐานในการคำนวณและแสดงผลดังนี้:

1. เพิ่ม MACD (Moving Average Convergence Divergence)

MACD ใช้ในการตรวจจับความแตกต่างระหว่าง EMA (Exponential Moving Average) 2 เส้น คือ EMA 12 และ EMA 26 พร้อมกับ Signal Line ซึ่งเป็น EMA 9 ของ MACD

pinescript

//@version=5 indicator("MACD Indicator", overlay=false) // คำนวณ MACD และ Signal Line [macdLine, signalLine, _] = ta.macd(close, 12, 26, 9) // วาด MACD และ Signal Line บนกราฟ plot(macdLine, color=color.blue, title="MACD Line") plot(signalLine, color=color.orange, title="Signal Line") hline(0, "Zero Line", color=color.gray) // เส้นศูนย์ (Zero Line)

อธิบาย:

  • ta.macd(close, 12, 26, 9) → คำนวณ MACD โดยใช้ค่า EMA 12 และ EMA 26 และ Signal Line เป็น EMA 9 ของ MACD
  • plot(macdLine) → วาดเส้น MACD
  • plot(signalLine) → วาดเส้น Signal Line
  • hline(0) → วาดเส้นที่ระดับ 0 บนกราฟ

2. เพิ่ม RSI (Relative Strength Index)

RSI ใช้ในการวิเคราะห์ความแรงของแนวโน้ม โดยวัดจากสัดส่วนของการเพิ่มขึ้นและลดลงในราคา

pinescript

//@version=5 indicator("RSI Indicator", overlay=false) // คำนวณ RSI rsiValue = ta.rsi(close, 14) // วาด RSI plot(rsiValue, color=color.purple, title="RSI Line") hline(70, "Overbought", color=color.red) // เส้น Overbought ที่ระดับ 70 hline(30, "Oversold", color=color.green) // เส้น Oversold ที่ระดับ 30

อธิบาย:

  • ta.rsi(close, 14) → คำนวณค่า RSI โดยใช้ช่วงเวลา 14 วัน
  • plot(rsiValue) → วาดเส้น RSI
  • hline(70) และ hline(30) → วาดเส้น Overbought (ระดับ 70) และ Oversold (ระดับ 30)

3. รวม MACD และ RSI พร้อมเงื่อนไขการซื้อ/ขาย

เราสามารถรวม MACD และ RSI ในการสร้างกลยุทธ์เทรดที่มีกฎซื้อ/ขาย ตัวอย่าง:

pinescript

//@version=5 indicator("MACD & RSI Strategy", overlay=true) // คำนวณ MACD และ RSI [macdLine, signalLine, _] = ta.macd(close, 12, 26, 9) rsiValue = ta.rsi(close, 14) // กำหนดเงื่อนไข Buy และ Sell buySignal = ta.crossover(macdLine, signalLine) and rsiValue < 30 // MACD ตัดขึ้น + RSI ต่ำกว่า 30 sellSignal = ta.crossunder(macdLine, signalLine) and rsiValue > 70 // MACD ตัดลง + RSI สูงกว่า 70 // วาดสัญลักษณ์ Buy และ Sell plotshape(buySignal, location=location.belowbar, color=color.green, style=shape.labelup, title="Buy Signal") plotshape(sellSignal, location=location.abovebar, color=color.red, style=shape.labeldown, title="Sell Signal")

อธิบาย:

  • เงื่อนไขซื้อ (Buy Signal):
    เมื่อ MACD ตัดขึ้น Signal Line (ta.crossover(macdLine, signalLine)) และ RSI ต่ำกว่า 30 (rsiValue < 30)

  • เงื่อนไขขาย (Sell Signal):
    เมื่อ MACD ตัดลง Signal Line (ta.crossunder(macdLine, signalLine)) และ RSI สูงกว่า 70 (rsiValue > 70)

  • ใช้ plotshape() สำหรับการแสดงสัญลักษณ์ซื้อ (ลูกศรเขียว) และขาย (ลูกศรแดง) บนกราฟ


4. เพิ่มการทดสอบย้อนหลัง (Backtesting)

เราสามารถเพิ่มการทดสอบกลยุทธ์โดยใช้ strategy.entry() และ strategy.close() เพื่อทำการทดสอบผลลัพธ์:

pinescript

//@version=5 strategy("MACD & RSI Strategy Test", overlay=true) // คำนวณ MACD และ RSI [macdLine, signalLine, _] = ta.macd(close, 12, 26, 9) rsiValue = ta.rsi(close, 14) // กำหนดเงื่อนไขการเปิดและปิดการเทรด longCondition = ta.crossover(macdLine, signalLine) and rsiValue < 30 shortCondition = ta.crossunder(macdLine, signalLine) and rsiValue > 70 // เปิดและปิดการเทรด if longCondition strategy.entry("Buy", strategy.long) if shortCondition strategy.close("Buy")

อธิบาย:

  • strategy.entry("Buy", strategy.long) → เปิดคำสั่งซื้อเมื่อ MACD ตัดขึ้น Signal Line และ RSI ต่ำกว่า 30
  • strategy.close("Buy") → ปิดคำสั่งซื้อเมื่อ MACD ตัดลง Signal Line และ RSI สูงกว่า 70

สรุป:

  • MACD ใช้ในการวิเคราะห์แนวโน้มราคา
  • RSI ใช้ในการบ่งชี้สภาวะ Overbought หรือ Oversold
  • สามารถสร้าง Buy/Sell Signals ด้วยการรวม MACD และ RSI ได้
  • สามารถทดสอบกลยุทธ์และดูผลลัพธ์การเทรดได้ด้วยการใช้ strategy.entry() และ strategy.close()

ทดลองใช้ใน TradingView และลองปรับแต่งกลยุทธ์ตามที่คุณต้องการ!

Featured Posts

Reversal chart patterns

Master Your Market Entries & Exits! Want to improve your trades? Here are 3 powerful chart patterns that help spot reversal...