Crude Oil CL1 / CL2 Spread Box WW

wyndmillwillie · study · 82 行 · 点赞 9 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © wyndmillwillie
//@version=6
indicator("CL1 / CL2 Spread Box WW", overlay = true)
 
cl1Symbol = input.symbol("NYMEX:CL1!", "Front Month Symbol")
cl2Symbol = input.symbol("NYMEX:CL2!", "Back Month Symbol")
boxPosition = input.string("Top Right", "Box Position", options = ["Top Right", "Top Left", "Bottom Right", "Bottom Left"])
textSizeInput = input.string("Normal", "Text Size", options = ["Auto", "Tiny", "Small", "Normal", "Large", "Huge"])
verticalOffset = input.int(0, "Vertical Offset (Rows)", minval = 0, maxval = 10, tooltip = "Adds buffer rows to shift the box away from top status line or bottom TV logo.")
showHeader = input.bool(true, "Show Header")
showStructure = input.bool(true, "Show Curve Structure")
highlightSpread = input.bool(true, "Highlight Spread Row")
 
tablePosition = boxPosition == "Top Left" ? position.top_left : boxPosition == "Bottom Right" ? position.bottom_right : boxPosition == "Bottom Left" ? position.bottom_left : position.top_right
textSize = textSizeInput == "Tiny" ? size.tiny : textSizeInput == "Small" ? size.small : textSizeInput == "Large" ? size.large : textSizeInput == "Huge" ? size.huge : textSizeInput == "Auto" ? size.auto : size.normal
 
isTop = boxPosition == "Top Right" or boxPosition == "Top Left"
topOffset = isTop ? verticalOffset : 0
 
cl1Close = request.security(cl1Symbol, timeframe.period, close, gaps = barmerge.gaps_off)
cl2Close = request.security(cl2Symbol, timeframe.period, close, gaps = barmerge.gaps_off)
spread = cl1Close - cl2Close
 
spreadColor = spread > 0 ? color.lime : spread < 0 ? color.red : color.silver
panelColor = color.new(color.black, 18)
labelColor = color.new(color.white, 0)
headerColor = color.new(color.blue, 70)
spreadBgColor = highlightSpread ? color.new(spread > 0 ? color.lime : spread < 0 ? color.red : color.gray, 82) : panelColor
curveState = spread > 0 ? "Backwardation" : spread < 0 ? "Contango" : "Flat"
curveStateColor = spread > 0 ? color.lime : spread < 0 ? color.orange : color.silver
 
formatPrice(value) => na(value) ? "n/a" : str.tostring(value, format.mintick)
 
contentRowCount = (showHeader ? 1 : 0) + 3 + (showStructure ? 1 : 0)
totalRowCount = contentRowCount + verticalOffset
 
var table spreadTable = table.new(tablePosition, 2, totalRowCount, bgcolor = panelColor, frame_color = color.new(color.white, 70), frame_width = 1, border_color = color.new(color.white, 85))
 
if barstate.isfirst
    table.set_position(spreadTable, tablePosition)
 
if barstate.islast
    table.set_position(spreadTable, tablePosition)
    table.clear(spreadTable, 0, 0, 1, totalRowCount - 1)
    
    // Fill top offset spacer rows with transparent cells if placed at top margin
    if topOffset > 0
        for i = 0 to topOffset - 1
            table.cell(spreadTable, 0, i, "", bgcolor = color.new(color.black, 100))
            table.cell(spreadTable, 1, i, "", bgcolor = color.new(color.black, 100))
 
    rowOffset = topOffset + (showHeader ? 1 : 0)
 
    if showHeader
        table.cell(spreadTable, 0, topOffset, "Crude Curve", text_color = color.white, text_size = textSize, text_halign = text.align_center, bgcolor = headerColor)
        table.cell(spreadTable, 1, topOffset, "Live", text_color = color.white, text_size = textSize, text_halign = text.align_center, bgcolor = headerColor)
 
    table.cell(spreadTable, 0, 0 + rowOffset, "CL1", text_color = labelColor, text_size = textSize, text_halign = text.align_left)
    table.cell(spreadTable, 1, 0 + rowOffset, formatPrice(cl1Close), text_color = color.white, text_size = textSize, text_halign = text.align_right)
 
    table.cell(spreadTable, 0, 1 + rowOffset, "CL2", text_color = labelColor, text_size = textSize, text_halign = text.align_left)
    table.cell(spreadTable, 1, 1 + rowOffset, formatPrice(cl2Close), text_color = color.white, text_size = textSize, text_halign = text.align_right)
 
    table.cell(spreadTable, 0, 2 + rowOffset, "Spread", text_color = labelColor, text_size = textSize, text_halign = text.align_left, bgcolor = spreadBgColor)
    table.cell(spreadTable, 1, 2 + rowOffset, formatPrice(spread), text_color = spreadColor, text_size = textSize, text_halign = text.align_right, bgcolor = spreadBgColor)
 
    if showStructure
        table.cell(spreadTable, 0, 3 + rowOffset, "Structure", text_color = labelColor, text_size = textSize, text_halign = text.align_left)
        table.cell(spreadTable, 1, 3 + rowOffset, curveState, text_color = curveStateColor, text_size = textSize, text_halign = text.align_right)
 
    // Fill bottom offset spacer rows with transparent cells if placed at bottom margin
    if not isTop and verticalOffset > 0
        for i = 0 to verticalOffset - 1
            table.cell(spreadTable, 0, contentRowCount + i, "", bgcolor = color.new(color.black, 100))
            table.cell(spreadTable, 1, contentRowCount + i, "", bgcolor = color.new(color.black, 100))
 
spreadCrossUp = ta.crossover(spread, 0)
spreadCrossDown = ta.crossunder(spread, 0)
 
alertcondition(spreadCrossUp, title = "CL Spread Crossed Above Zero", message = "CL1-CL2 spread crossed above zero. Curve shifted into backwardation.")
alertcondition(spreadCrossDown, title = "CL Spread Crossed Below Zero", message = "CL1-CL2 spread crossed below zero. Curve shifted into contango.")

← 返回列表