Ultimate Pivot Points Alerts

QuantNomad · study · 232 行 · 点赞 1,646 · TradingView 原页

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

Pine Script

//@version=4
study(title="Ultimate Pivot Points Alerts", shorttitle="Ultim. Piv. Points Alerts", overlay = true)
 
// 
// author: QuantNomad
// date: 2019-08-13
// Ultimate Pivot Points Alerts
// https://www.tradingview.com/u/QuantNomad/
// https://t.me/quantnomad
//
 
///////////////
// FUNCTIONS //
///////////////
 
// 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
 
// Rounding levels to min tick
nround(x) => 
    n = round(x / syminfo.mintick) * syminfo.mintick
 
////////////
// INPUTS //
////////////
 
pp_type                = input(title = 'Pivot Type',               defval = "Traditional", options = ["Traditional", "Fibonacci", "Woodie", "Camarilla"]) 
pp_period              = input(title = "Period",                   defval = "Day",  type = input.string, options = ['Day', 'Week', 'Month', 'Year'])
show_historical_levels = input(title = "Show Historical Levels?",  defval = false,  type = input.bool)
show_level_value       = input(title = "Show Levels Value?",       defval = true,   type = input.bool)
show_current_levels    = input(title = "Show Current Levels",      defval = false,  type = input.bool)
 
pp_res = pp_period == 'Day' ? 'D' : pp_period == 'Week' ? 'W' : pp_period == 'Month' ? 'M' : 'Y' 
 
/////////////////////
// Get HLC from HT //
 
// Calc Open
open_cur = 0.0
open_cur := is_newbar(pp_res) ? open : open_cur[1]
 
popen = 0.0
popen := is_newbar(pp_res) ? open_cur[1] : popen[1]
 
// 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 PIVOT POINTS //
////////////////////////////
 
PP = 0.0
R1 = 0.0, R2 = 0.0, R3 = 0.0
S1 = 0.0, S2 = 0.0, S3 = 0.0
 
if (pp_type == "Traditional")
    PP := (phigh + plow + pclose) / 3
    R1 := PP     + (PP   - plow)
    S1 := PP     - (phigh - PP)
    R2 := PP     + (phigh - plow)
    S2 := PP     - (phigh - plow)
    R3 := phigh  + 2 * (PP   - plow) 
    S3 := plow   - 2 * (phigh - PP) 
  
if (pp_type == "Fibonacci")
    PP := (phigh + plow + pclose) / 3
    R1 := PP + (phigh - plow) * 0.382
    S1 := PP - (phigh - plow) * 0.382
    R2 := PP + (phigh - plow) * 0.618
    S2 := PP - (phigh - plow) * 0.618
    R3 := PP + (phigh - plow) * 1.000
    S3 := PP - (phigh - plow) * 1.000
  
if (pp_type == "Woodie")
    PP := (phigh + plow + 2 * popen) / 4
    R1 := PP + (PP - plow)
    S1 := PP - (phigh - PP)
    R2 := PP + (phigh - plow)
    S2 := PP - (phigh - plow)
    R3 := phigh + 2 * (PP - plow) 
    S3 := plow  - 2 * (phigh - PP)
    
if (pp_type == "Camarilla")
    PP := (phigh + plow + pclose) / 3
    R1 := pclose + (phigh - plow) * 1.1/12
    S1 := pclose - (phigh - plow) * 1.1/12
    R2 := pclose + (phigh - plow) * 1.1/6
    S2 := pclose - (phigh - plow) * 1.1/6
    R3 := pclose + (phigh - plow) * 1.1/4
    S3 := pclose - (phigh - plow) * 1.1/4
 
// Projected levels
 
prPP = 0.0
prR1 = 0.0, prR2 = 0.0, prR3 = 0.0
prS1 = 0.0, prS2 = 0.0, prS3 = 0.0
  
if (pp_type == "Traditional")
    prPP := (high_cur + low_cur + close) / 3
    prR1 := prPP     + (prPP   - low_cur)
    prS1 := prPP     - (high_cur - PP)
    prR2 := prPP     + (high_cur - low_cur)
    prS2 := prPP     - (high_cur - low_cur)
    prR3 := phigh  + 2 * (prPP   - low_cur) 
    prS3 := plow   - 2 * (high_cur - PP)   
  
//////////////
// PLOTTING //
 
bars_sinse = 0
bars_sinse := is_newbar(pp_res) ? 0 : bars_sinse[1] + 1
 
////////////////////////
// PLOT PIVOTS LEVELS //
 
vpp_p = line.new(bar_index[min(bars_sinse, 300)], PP, bar_index, PP, color=color.gray,  style =  line.style_solid, extend = extend.none)
vs1_p = line.new(bar_index[min(bars_sinse, 300)], S1, bar_index, S1, color=color.red,   style =  line.style_solid, extend = extend.none)
vs2_p = line.new(bar_index[min(bars_sinse, 300)], S2, bar_index, S2, color=color.red,   style =  line.style_solid, extend = extend.none)
vs3_p = line.new(bar_index[min(bars_sinse, 300)], S3, bar_index, S3, color=color.red,   style =  line.style_solid, extend = extend.none)
vr1_p = line.new(bar_index[min(bars_sinse, 300)], R1, bar_index, R1, color=color.green, style =  line.style_solid, extend = extend.none)
vr2_p = line.new(bar_index[min(bars_sinse, 300)], R2, bar_index, R2, color=color.green, style =  line.style_solid, extend = extend.none)
vr3_p = line.new(bar_index[min(bars_sinse, 300)], R3, bar_index, R3, color=color.green,   style =  line.style_solid, extend = extend.none)
 
// delete previous lines in the same period
if (not is_newbar(pp_res) or not show_historical_levels)
    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]) 
 
 
 
// Add labels
label_vpp = label.new(bar_index, PP, text=show_level_value ? ("P"  + " " + tostring(nround(PP))) : "P",  style= label.style_none)
label_vs1 = label.new(bar_index, S1, text=show_level_value ? ("S1" + " " + tostring(nround(S1))) : "S1", style= label.style_none)
label_vs2 = label.new(bar_index, S2, text=show_level_value ? ("S2" + " " + tostring(nround(S2))) : "S2", style= label.style_none)
label_vs3 = label.new(bar_index, S3, text=show_level_value ? ("S3" + " " + tostring(nround(S3))) : "S3", style= label.style_none)
label_vr1 = label.new(bar_index, R1, text=show_level_value ? ("R1" + " " + tostring(nround(R1))) : "R1", style= label.style_none)
label_vr2 = label.new(bar_index, R2, text=show_level_value ? ("R2" + " " + tostring(nround(R2))) : "R2", style= label.style_none)
label_vr3 = label.new(bar_index, R3, text=show_level_value ? ("R3" + " " + tostring(nround(R3))) : "R3", style= label.style_none)
    
label.delete(label_vpp[1])
label.delete(label_vs1[1])
label.delete(label_vs2[1])
label.delete(label_vs3[1])
label.delete(label_vr1[1])
label.delete(label_vr2[1])
label.delete(label_vr3[1])    
    
// Projected levels    
line vpp_pr = na
line vs1_pr = na
line vs2_pr = na
line vs3_pr = na
line vr1_pr = na
line vr2_pr = na
line vr3_pr = na
 
if (show_current_levels)
    
    vpp_pr = line.new(bar_index[1], prPP, bar_index, prPP, color=color.gray,  style =  line.style_dashed, extend = extend.right)
    vs1_pr = line.new(bar_index[1], prS1, bar_index, prS1, color=color.red,   style =  line.style_dashed, extend = extend.right)
    vs2_pr = line.new(bar_index[1], prS2, bar_index, prS2, color=color.red,   style =  line.style_dashed, extend = extend.right)
    vs3_pr = line.new(bar_index[1], prS3, bar_index, prS3, color=color.red,   style =  line.style_dashed, extend = extend.right)
    vr1_pr = line.new(bar_index[1], prR1, bar_index, prR1, color=color.green, style =  line.style_dashed, extend = extend.right)
    vr2_pr = line.new(bar_index[1], prR2, bar_index, prR2, color=color.green, style =  line.style_dashed, extend = extend.right)
    vr3_pr = line.new(bar_index[1], prR3, bar_index, prR3, color=color.green, style =  line.style_dashed, extend = extend.right)
        
    line.delete(vpp_pr[1])
    line.delete(vs1_pr[1])
    line.delete(vs2_pr[1])
    line.delete(vs3_pr[1])
    line.delete(vr1_pr[1])
    line.delete(vr2_pr[1])
    line.delete(vr3_pr[1])
 
////////////
// ALERTS //
    
alertcondition(not is_newbar(pp_res) and cross(close, PP), "Cross P",  "Cross P")
alertcondition(not is_newbar(pp_res) and cross(close, S1), "Cross S1", "Cross S1")
alertcondition(not is_newbar(pp_res) and cross(close, S2), "Cross S2", "Cross S2")
alertcondition(not is_newbar(pp_res) and cross(close, S3), "Cross S3", "Cross S3")
alertcondition(not is_newbar(pp_res) and cross(close, R1), "Cross R1", "Cross R1")
alertcondition(not is_newbar(pp_res) and cross(close, R2), "Cross R2", "Cross R2")
alertcondition(not is_newbar(pp_res) and cross(close, R3), "Cross R3", "Cross R3")
 
alertcondition(not is_newbar(pp_res) and crossover(close, PP), "Crossover P",  "Crossover P")
alertcondition(not is_newbar(pp_res) and crossover(close, S1), "Crossover S1", "Crossover S1")
alertcondition(not is_newbar(pp_res) and crossover(close, S2), "Crossover S2", "Crossover S2")
alertcondition(not is_newbar(pp_res) and crossover(close, S3), "Crossover S3", "Crossover S3")
alertcondition(not is_newbar(pp_res) and crossover(close, R1), "Crossover R1", "Crossover R1")
alertcondition(not is_newbar(pp_res) and crossover(close, R2), "Crossover R2", "Crossover R2")
alertcondition(not is_newbar(pp_res) and crossover(close, R3), "Crossover R3", "Crossover R3")
 
alertcondition(not is_newbar(pp_res) and crossunder(close, PP), "Crossunder P",  "Crossunder P")
alertcondition(not is_newbar(pp_res) and crossunder(close, S1), "Crossunder S1", "Crossunder S1")
alertcondition(not is_newbar(pp_res) and crossunder(close, S2), "Crossunder S2", "Crossunder S2")
alertcondition(not is_newbar(pp_res) and crossunder(close, S3), "Crossunder S3", "Crossunder S3")
alertcondition(not is_newbar(pp_res) and crossunder(close, R1), "Crossunder R1", "Crossunder R1")
alertcondition(not is_newbar(pp_res) and crossunder(close, R2), "Crossunder R2", "Crossunder R2")
alertcondition(not is_newbar(pp_res) and crossunder(close, R3), "Crossunder R3", "Crossunder R3")
 
alertcondition(not is_newbar(pp_res) and (cross(close, PP) or cross(close, S1) or cross(close, S2) or cross(close, S3) or
   cross(close, R1) or cross(close, R2) or cross(close, R3)), "Cross Any", "Cross Any")

← 返回列表