Trad Pivot Points Alerts

QuantNomad · study · 128 行 · 点赞 1,774 · TradingView 原页

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

Pine Script

//@version=4
study(title="Trad Pivot Points Alerts", shorttitle="Trad Pivot Points Alerts", overlay = true)
 
// 
// author: QuantNomad
// date: 2019-07-28
// Traditional Pivot Points Alerts
// https://www.tradingview.com/u/QuantNomad/
// https://t.me/quantnomad
//
 
// Function outputs 1 when it's the first bar of the D/W/M/Y
is_newbar(res) =>
    ch = 0
    if(res == 'Y')
        t  = year(time('D'))
        ch := change(t) != 0 ? 1 : 0
    else
        t = time(res)
        ch := change(t) != 0 ? 1 : 0
    ch
 
nround(x) => 
    n = x < 10 ? 3 : x < 100 ? 2 : x < 1000 ? 1 : 0
    round(x * pow(10, n)) / pow(10, n)
 
////////////
// INPUTS //
////////////
 
pp_period = input(title = "Period", type=input.string, defval="Day", options = ['Day', 'Week', 'Month', 'Year'])
 
show_levels = input(true, type = input.bool, title = "Show Levels?")
 
pp_res = pp_period == 'Day' ? 'D' : pp_period == 'Week' ? 'W' : pp_period == 'Month' ? 'M' : 'Y' 
 
/////////////////////
// Get HLC from HT //
 
// Calc High
high_cur = 0.0
high_cur := is_newbar(pp_res) ? high : max(high_cur[1], high)
 
phigh = 0.0
phigh := is_newbar(pp_res) ? high_cur[1] : phigh[1]
 
// Calc Low
low_cur = 0.0
low_cur := is_newbar(pp_res) ? low : min(low_cur[1], low)
 
plow = 0.0
plow := is_newbar(pp_res) ? low_cur[1] : plow[1]
 
// Calc Close
pclose = 0.0
pclose := is_newbar(pp_res) ? close[1] : pclose[1]
 
////////////////////////////////
// CALCULATE traditional pivots //
 
vPP = (phigh + plow + pclose) / 3
vR1 = vPP    + (vPP   - plow)
vS1 = vPP    - (phigh - vPP)
vR2 = vPP    + (phigh - plow)
vS2 = vPP    - (phigh - plow)
vR3 = phigh  + 2 * (vPP   - plow) 
vS3 = plow   - 2 * (phigh - vPP) 
  
//////////////
// PLOTTING //
 
bars_sinse = 0
bars_sinse := is_newbar(pp_res) ? 0 : bars_sinse[1] + 1
 
////////////////////////
// PLOT PIVOTS LEVELS //
 
vpp_p = line.new(bar_index[bars_sinse], vPP, bar_index, vPP, color=#f57f17, style =  line.style_solid, extend = extend.right)
vs1_p = line.new(bar_index[bars_sinse], vS1, bar_index, vS1, color=#f57f17, style =  line.style_solid, extend = extend.right)
vs2_p = line.new(bar_index[bars_sinse], vS2, bar_index, vS2, color=#f57f17, style =  line.style_solid, extend = extend.right)
vs3_p = line.new(bar_index[bars_sinse], vS3, bar_index, vS3, color=#f57f17, style =  line.style_solid, extend = extend.right)
vr1_p = line.new(bar_index[bars_sinse], vR1, bar_index, vR1, color=#f57f17, style =  line.style_solid, extend = extend.right)
vr2_p = line.new(bar_index[bars_sinse], vR2, bar_index, vR2, color=#f57f17, style =  line.style_solid, extend = extend.right)
vr3_p = line.new(bar_index[bars_sinse], vR3, bar_index, vR3, color=#f57f17, style =  line.style_solid, extend = extend.right)
 
// delete previous lines in the same period
if (not is_newbar(pp_res))
    line.delete(vpp_p[1])
    line.delete(vs1_p[1]) 
    line.delete(vs2_p[1])  
    line.delete(vs3_p[1])  
    line.delete(vr1_p[1]) 
    line.delete(vr2_p[1])  
    line.delete(vr3_p[1]) 
  
// delete entend for the old lines
if (is_newbar(pp_res))
    line.set_extend(vpp_p[1], extend.none)
    line.set_extend(vs1_p[1], extend.none) 
    line.set_extend(vs2_p[1], extend.none)  
    line.set_extend(vs3_p[1], extend.none)  
    line.set_extend(vr1_p[1], extend.none) 
    line.set_extend(vr2_p[1], extend.none)  
    line.set_extend(vr3_p[1], extend.none) 
 
// Add labels
if (is_newbar(pp_res))  
    label_vpp = label.new(bar_index, vPP, text=show_levels ? ("P"  + " " + tostring(nround(vPP))) : "P",  style= label.style_none)
    label_vs1 = label.new(bar_index, vS1, text=show_levels ? ("S1" + " " + tostring(nround(vS1))) : "S1", style= label.style_none)
    label_vs2 = label.new(bar_index, vS2, text=show_levels ? ("S2" + " " + tostring(nround(vS2))) : "S2", style= label.style_none)
    label_vs3 = label.new(bar_index, vS3, text=show_levels ? ("S3" + " " + tostring(nround(vS3))) : "S3", style= label.style_none)
    label_vr1 = label.new(bar_index, vR1, text=show_levels ? ("R1" + " " + tostring(nround(vR1))) : "R1", style= label.style_none)
    label_vr2 = label.new(bar_index, vR2, text=show_levels ? ("R2" + " " + tostring(nround(vR2))) : "R2", style= label.style_none)
    label_vr3 = label.new(bar_index, vR3, text=show_levels ? ("R3" + " " + tostring(nround(vR3))) : "R3", style= label.style_none)
    
////////////
// ALERTS //
    
alertcondition(not is_newbar(pp_res) and cross(close, vPP), "Cross Trad P",  "Cross Trad P")
alertcondition(not is_newbar(pp_res) and cross(close, vS1), "Cross Trad S1", "Cross Trad S1")
alertcondition(not is_newbar(pp_res) and cross(close, vS2), "Cross Trad S2", "Cross Trad S2")
alertcondition(not is_newbar(pp_res) and cross(close, vS3), "Cross Trad S3", "Cross Trad S3")
alertcondition(not is_newbar(pp_res) and cross(close, vR1), "Cross Trad R1", "Cross Trad R1")
alertcondition(not is_newbar(pp_res) and cross(close, vR2), "Cross Trad R2", "Cross Trad R2")
alertcondition(not is_newbar(pp_res) and cross(close, vR3), "Cross Trad R3", "Cross Trad R3")
 
alertcondition(not is_newbar(pp_res) and (cross(close, vPP) or cross(close, vS1) or cross(close, vS2) or cross(close, vS3) or
   cross(close, vR1) or cross(close, vR2) or cross(close, vR3)), "Cross Trad Any", "Cross Trad Any")

← 返回列表