Raja OI Grid Lines

nawadoln · study · 70 行 · 点赞 931 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
indicator("Raja OI Grid Lines", overlay=true)
 
// ==========================================
// 1. INPUTS
// ==========================================
basisOffset    = input.float(30.0, title="Basis Offset (USD)", tooltip="ค่าส่วนต่าง Basis ที่นำไปบวกเพิ่มเข้ากับราคา Spot เพื่อคำนวณราคา Future/Option")
stepSize       = input.float(25.0, title="Grid Step Size (USD)", minval=0.1, tooltip="ระยะห่างของ Strike Price")
linesCount     = input.int(20, title="Number of Lines (Above/Below)", minval=1, maxval=100, tooltip="จำนวนเส้นที่จะแสดงด้านบนและด้านล่าง")
lineColor      = input.color(color.blue, title="Line Color")
lineStyleInput = input.string("Solid", title="Line Style", options=["Solid", "Dashed", "Dotted"])
lineWidth      = input.int(1, title="Line Width", minval=1, maxval=4)
showLabels     = input.bool(true, title="Show Strike Price Labels", tooltip="เปิด/ปิด การแสดงป้ายตัวเลขราคา Future")
 
// ==========================================
// 2. HELPER FUNCTIONS
// ==========================================
getLineStyle(string styleStr) =>
    switch styleStr
        "Dashed" => line.style_dashed
        "Dotted" => line.style_dotted
        => line.style_solid
 
// ==========================================
// 3. CALCULATIONS
// ==========================================
// 1. ดึงราคาเปิด (Open) ของแท่งวันปัจจุบันบนกราฟ Spot (XAUUSD)
float dailyOpenSpot = request.security(syminfo.tickerid, "D", open, barmerge.gaps_off, barmerge.lookahead_on)
 
// 2. คำนวณราคา Future อ้างอิงชั่วคราว เพื่อหา Center Strike Price ที่ลงตัว
float rawFuturePrice = dailyOpenSpot + basisOffset
float centerFutureStrike = math.round(rawFuturePrice / stepSize) * stepSize
 
// 3. คำนวณราคา Center Spot สำหรับวาดเส้นลงบนกราฟจริง
float centerSpotLevel = centerFutureStrike - basisOffset
 
// ==========================================
// 4. DRAW GRID LINES & LABELS
// ==========================================
// ฟังก์ชันวาดเส้นที่ราคา Spot แต่แสดงป้ายตัวเลขเป็นราคา Future
drawLineWithLabel(float spotPrice, float futurePrice, color col, string style, int width) =>
    // วาดเส้น Grid ที่ระดับราคา Spot บนกราฟ
    line.new(bar_index - 500, spotPrice, bar_index + 100, spotPrice, 
             color=col, style=style, width=width, extend=extend.both)
    
    // แสดงป้ายตัวเลขราคา Future (Spot + Basis) ทางด้านขวา
    if showLabels
        label.new(x=bar_index + 10, y=spotPrice, 
                  text=str.tostring(futurePrice, "#.##"), 
                  style=label.style_none, 
                  textcolor=col, 
                  textalign=text.align_left)
 
if barstate.islast
    style = getLineStyle(lineStyleInput)
    
    // 1. วาดเส้น Center Line
    drawLineWithLabel(centerSpotLevel, centerFutureStrike, lineColor, style, lineWidth + 1)
    
    // 2. วาดเส้นระดับราคาด้านบนและด้านล่าง (+- ทีละ $25 ตาม Strike Price)
    for i = 1 to linesCount
        // ระดับราคาด้านบน
        float upperSpotLevel   = centerSpotLevel + (i * stepSize)
        float upperFuturePrice = centerFutureStrike + (i * stepSize)
        drawLineWithLabel(upperSpotLevel, upperFuturePrice, lineColor, style, lineWidth)
        
        // ระดับราคาด้านล่าง
        float lowerSpotLevel   = centerSpotLevel - (i * stepSize)
        float lowerFuturePrice = centerFutureStrike - (i * stepSize)
        drawLineWithLabel(lowerSpotLevel, lowerFuturePrice, lineColor, style, lineWidth)

← 返回列表