本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Commodity in Gold", overlay=false, precision=6)
pick = input.string("Oil (WTI)", "Commodity", options=["Oil (WTI)", "Oil (Brent)", "Heating oil", "Gasoline", "Natural gas", "Copper", "Silver", "Platinum", "Palladium", "Aluminium", "Nickel", "Zinc", "Iron ore", "Uranium", "Wheat", "Corn", "Soybeans", "Rice", "Sugar", "Coffee", "Cocoa", "Cotton", "Live cattle", "Lean hogs", "S&P 500", "Chart symbol", "Custom"])
customSym = input.symbol("", "Custom symbol (if Custom)")
customCol = input.color(color.yellow, "Colour for chart symbol / custom")
gold = input.symbol("TVC:GOLD", "Gold")
mode = input.string("Ratio", "Mode", options=["Ratio", "Gap to trend %"])
showAvg = input.bool(true, "Show average (Ratio mode)")
showTrend = input.bool(true, "Show log trend (Ratio mode)")
gapLen = input.int(240, "Trend length for gap (bars)", minval=12)
sym0 = switch pick
"Oil (WTI)" => "TVC:USOIL"
"Oil (Brent)" => "TVC:UKOIL"
"Heating oil" => "NYMEX:HO1!"
"Gasoline" => "NYMEX:RB1!"
"Natural gas" => "NYMEX:NG1!"
"Copper" => "COMEX:HG1!"
"Silver" => "TVC:SILVER"
"Platinum" => "TVC:PLATINUM"
"Palladium" => "TVC:PALLADIUM"
"Aluminium" => "FRED:PALUMUSDM"
"Nickel" => "FRED:PNICKUSDM"
"Zinc" => "FRED:PZINCUSDM"
"Iron ore" => "FRED:PIORECRUSDM"
"Uranium" => "FRED:PURANUSDM"
"Wheat" => "CBOT:ZW1!"
"Corn" => "CBOT:ZC1!"
"Soybeans" => "CBOT:ZS1!"
"Rice" => "CBOT:ZR1!"
"Sugar" => "ICEUS:SB1!"
"Coffee" => "ICEUS:KC1!"
"Cocoa" => "ICEUS:CC1!"
"Cotton" => "ICEUS:CT1!"
"Live cattle" => "CME:LE1!"
"Lean hogs" => "CME:HE1!"
"S&P 500" => "SP:SPXTR"
"Chart symbol" => syminfo.tickerid
=> customSym
sym = sym0 == "" ? syminfo.tickerid : sym0
col = switch pick
"Oil (WTI)" => #757575
"Oil (Brent)" => #5E6A71
"Heating oil" => #B0B0B0
"Gasoline" => #E57373
"Natural gas" => #4FC3F7
"Copper" => #B87333
"Silver" => #E0E0E0
"Platinum" => #A9B8C9
"Palladium" => #7FA7A0
"Aluminium" => #C9D6DF
"Nickel" => #8E9A6E
"Zinc" => #7E8CA0
"Iron ore" => #B5523B
"Uranium" => #9ACD32
"Wheat" => #E3C16F
"Corn" => #F5D90A
"Soybeans" => #6B8E23
"Rice" => #F2EBD3
"Sugar" => #FFFFFF
"Coffee" => #A0785A
"Cocoa" => #D2A679
"Cotton" => #DCE3F0
"Live cattle" => #D2691E
"Lean hogs" => #F48FB1
"S&P 500" => #2962FF
=> customCol
a = request.security(sym, timeframe.period, close)
g = request.security(gold, timeframe.period, close)
r = a / g
isRatio = mode == "Ratio"
// gap mode: % distance from rolling log trend
lr = ta.linreg(math.log(r), gapLen, 0)
gap = (r / math.exp(lr) - 1) * 100
plot(isRatio ? r : na, "Ratio", color=col, linewidth=2)
plot(isRatio ? na : gap, "Gap %", color=col, linewidth=2, format=format.percent, precision=0)
plot(isRatio ? na : 0, "Zero", color=color.gray, linewidth=1)
// full-history average and log regression (ratio mode)
var float s = 0.0
var int n = 0
var float sx = 0.0
var float sy = 0.0
var float sxx = 0.0
var float sxy = 0.0
var int x0 = na
if not na(r) and r > 0
float x = bar_index
float y = math.log(r)
s += r
n += 1
sx += x
sy += y
sxx += x * x
sxy += x * y
if na(x0)
x0 := bar_index
var line avgLine = na
var line trendLine = na
var label avgLbl = na
var label trendLbl = na
if barstate.islast and n > 1
line.delete(avgLine)
line.delete(trendLine)
label.delete(avgLbl)
label.delete(trendLbl)
if isRatio and showAvg
avg = s / n
avgLine := line.new(bar_index - 1, avg, bar_index, avg, extend=extend.both, color=color.new(col, 40), style=line.style_dashed)
avgLbl := label.new(bar_index, avg, "Avg", style=label.style_label_left, color=color(na), textcolor=col, size=size.large)
if isRatio and showTrend
slope = (n * sxy - sx * sy) / (n * sxx - sx * sx)
icpt = (sy - slope * sx) / n
tNow = math.exp(icpt + slope * bar_index)
trendLine := line.new(x0, math.exp(icpt + slope * x0), bar_index, tNow, extend=extend.right, color=color.new(col, 20), style=line.style_dotted, width=2)
trendLbl := label.new(bar_index, tNow, "Trend", style=label.style_label_left, color=color(na), textcolor=col, size=size.large)