从 0400 (PreMarket) 开始绘制枢轴 TradingView Pinescript

时间:2021-06-15 16:39:41

标签: pine-script

我一直试图让我的脚本在开盘前 (0400) 而非默认的 0930 开始绘制这些枢轴点。下面是我当前的代码以及此代码输出的屏幕截图。

还包括我正在尝试完成的屏幕截图,如橙色线所示。

非常感谢任何帮助。

study(title = "Double Pivot Lines", overlay = true, max_lines_count=500)

show_hist = input(false, title="Show Historical Lines")
extend    = input(false, title="Extend Lines to Right")
show_labels = input(true, title="Show Name Labels")

getstyle(strColor) =>
     iff(strColor=="Solid",line.style_solid,
     iff(strColor=="Dotted",line.style_dotted,
     iff(strColor=="Dashed",line.style_dashed,line.style_solid)))
     

show1 = input(true, title='Show Pivot 1', group="Pivot 1")
type_sw1 = input("Camarilla", title="Pivots 1 Type", options=["Camarilla", "Traditional","Classic","Fibonacci"])
sw1 = input("Daily", title="Pivots Timeframe", options=["Daily","Weekly","Monthly","Yearly"])
tf1 = sw1=="Daily" ? "D" : sw1=="Weekly" ? "W" : sw1=="Monthly" ? "1M" : "12M"
lstyle1 = input(title="Line Style 1", defval="Solid" , options=["Solid", "Dotted", "Dashed"]) 
linewidth1 = input(1, title='Line Width 1')

swr5_1 = input(true, title="", inline="set1_5")
swr4_1 = input(true, title="", inline="set1_4")
swr3_1 = input(true, title="", inline="set1_3")
swr2_1 = input(true, title="", inline="set1_2")
swr1_1 = input(true, title="", inline="set1_1")

sw0_1  = input(false, title="", inline="set1_6")

sws1_1 = input(true, title="", inline="set1_7")
sws2_1 = input(true, title="", inline="set1_8")
sws3_1 = input(true, title="", inline="set1_9")
sws4_1 = input(true, title="", inline="set1_10")
sws5_1 = input(true, title="", inline="set1_11")


colr5_1 = input(color.aqua, title="R 5", inline="set1_5")
colr4_1 = input(color.red, title="R 4", inline="set1_4")
colr3_1 = input(color.red, title="R 3", inline="set1_3")
colr2_1 = input(color.red, title="R 2", inline="set1_2")
colr1_1 = input(color.red, title="R 1", inline="set1_1")
col0_1  = input(color.green , title="Pivot", inline="set1_6")
cols1_1 = input(color.green, title="S 1", inline="set1_7")
cols2_1 = input(color.green, title="S 2", inline="set1_8")
cols3_1 = input(color.green, title="S 3", inline="set1_9")
cols4_1 = input(color.green, title="S 4", inline="set1_10")
cols5_1 = input(color.aqua, title="S 5", inline="set1_11")



show2 = input(true, title='Show Pivot 2', group="Pivot 2")
type_sw2 = input("Classic"  , title="Pivots 2 Type", options=["Camarilla", "Traditional","Classic","Fibonacci"])
sw2 = input("Daily", title="Pivots Timeframe", options=["Daily","Weekly","Monthly","Yearly"])
tf2 = sw2=="Daily" ? "D" : sw2=="Weekly" ? "W" : sw2=="Monthly" ? "1M" : "12M"
lstyle2 = input(title="Line Style 2", defval="Dotted" , options=["Solid", "Dotted", "Dashed"]) 
linewidth2 = input(3, title='Line Width 2')

swr5_2 = input(true, title="", inline="set2_5")
swr4_2 = input(true, title="", inline="set2_4")
swr3_2 = input(true, title="", inline="set2_3")
swr2_2 = input(true, title="", inline="set2_2")
swr1_2 = input(true, title="", inline="set2_1")
sw0_2  = input(true, title="", inline="set2_6")
sws1_2 = input(true, title="", inline="set2_7")
sws2_2 = input(true, title="", inline="set2_8")
sws3_2 = input(true, title="", inline="set2_9")
sws4_2 = input(true, title="", inline="set2_10")
sws5_2 = input(true, title="", inline="set2_11")

colr5_2 = input(color.aqua, title="R 5", inline="set2_5")
colr4_2 = input(color.red, title="R 4", inline="set2_4")
colr3_2 = input(color.red, title="R 3", inline="set2_3")
colr2_2 = input(color.red, title="R 2", inline="set2_2")
colr1_2 = input(color.red, title="R 1", inline="set2_1")
col0_2 = input(color.purple , title="Pivot", inline="set2_6")
cols1_2 = input(color.green, title="S 1", inline="set2_7")
cols2_2 = input(color.green, title="S 2", inline="set2_8")
cols3_2 = input(color.green, title="S 3", inline="set2_9")
cols4_2 = input(color.green, title="S 4", inline="set2_10")
cols5_2 = input(color.aqua, title="S 5", inline="set2_11")

lineStyle1 = getstyle(lstyle1)
lineStyle2 = getstyle(lstyle2)


pivot_f (type_sw, sw, tf) =>
    xHigh  = security(syminfo.tickerid, tf, high[1] ,barmerge.gaps_off, barmerge.lookahead_on)
    xLow   = security(syminfo.tickerid, tf, low[1]  ,barmerge.gaps_off, barmerge.lookahead_on)
    xClose = security(syminfo.tickerid, tf, close[1],barmerge.gaps_off, barmerge.lookahead_on)
    xpivot = (xHigh+xLow+xClose) / 3
    xrange = (xHigh-xLow)
    
    xR1 = type_sw=="Camarilla"?  xClose + 1.1 * (xHigh - xLow) / 12  :  type_sw=="Fibonacci"   ? xpivot + 0.382*xrange                     : xpivot+(xpivot-xLow)   
    xS1 = type_sw=="Camarilla"?  xClose - 1.1 * (xHigh - xLow) / 12  :  type_sw=="Fibonacci"   ? xpivot - 0.382*xrange                     : xpivot-(xHigh - xpivot)
    
    xR2 = type_sw=="Camarilla"?  xClose + 1.1 * (xHigh - xLow) / 6   :  type_sw=="Traditional" ? xpivot + (xHigh - xLow)                   : type_sw=="Classic"? xpivot +   xrange  :              xpivot + 0.618*xrange
    xS2 = type_sw=="Camarilla"?  xClose - 1.1 * (xHigh - xLow) / 6   :  type_sw=="Traditional" ? xpivot - (xHigh - xLow)                   : type_sw=="Classic"? xpivot -   xrange  :              xpivot - 0.618*xrange
    
    xR3 = type_sw=="Camarilla"?  xClose + 1.1 * (xHigh - xLow) / 4   :  type_sw=="Traditional" ? xHigh + 2 * (xpivot - xLow)               : type_sw=="Classic"? xpivot + 2*xrange  :              xpivot + 1.000*xrange
    xS3 = type_sw=="Camarilla"?  xClose - 1.1 * (xHigh - xLow) / 4   :  type_sw=="Traditional" ? xLow - 2 * (xHigh - xpivot)               : type_sw=="Classic"? xpivot - 2*xrange  :              xpivot - 1.000*xrange
    
    xR4 = type_sw=="Camarilla"?  xClose + 1.1 * (xHigh - xLow) / 2   :  type_sw=="Traditional" ? xHigh + 3 * (xpivot - xLow)               : type_sw=="Classic"? xpivot + 3*xrange  :              xpivot + 1.618*xrange 
    xS4 = type_sw=="Camarilla"?  xClose - 1.1 * (xHigh - xLow) / 2   :  type_sw=="Traditional" ? xLow - 3 * (xHigh - xpivot)               : type_sw=="Classic"? xpivot - 3*xrange  :              xpivot - 1.618*xrange 
    
    xR5 = type_sw=="Camarilla"?  (xHigh / xLow) * xClose             :  type_sw=="Traditional" ? xHigh + 4 * (xpivot - xLow)               : type_sw=="Classic"? xpivot + 4*xrange  :              xpivot + 2.618*xrange 
    xS5 = type_sw=="Camarilla"?  (xClose - (xR5 - xClose))           :  type_sw=="Traditional" ? xLow - 4 * (xHigh - xpivot)               : type_sw=="Classic"? xpivot - 4*xrange  :              xpivot - 2.618*xrange 

    [xpivot, xR1, xS1, xR2, xS2, xR3, xS3, xR4, xS4, xR5, xS5]


[xpivot1, xR11, xS11, xR21, xS21, xR31, xS31, xR41, xS41, xR51, xS51] = pivot_f (type_sw1, sw1, tf1)
[xpivot2, xR12, xS12, xR22, xS22, xR32, xS32, xR42, xS42, xR52, xS52] = pivot_f (type_sw2, sw2, tf2)



new_line(cond, val, col, style, wid) => 
    if cond 
        newper = change(val)!=0
        newper_n  = valuewhen(newper, bar_index, 1)
        newper_n0 = valuewhen(newper, bar_index, 0)
        
        if newper and show_hist
            line.new(bar_index[1], val[1], newper_n, val[1], color=col, style=style, width=wid)
            
        l = line.new(bar_index[0], val, newper_n0, val, color=col, style=style, width=wid, extend=extend? extend.left : extend.none)
        line.delete(l[1])

transp=color.new(color.orange,90)

plotshape(show1 and sw0_1  and show_labels and change(xpivot1)!=0? xpivot1 :na, color=transp, textcolor=col0_1 , text="PP" , style=shape.diamond,size=size.tiny, location=location.absolute, offset=0)
plotshape(show1 and swr1_1 and show_labels and change(xR11)!=0? xR11 :na      , color=transp, textcolor=colr1_1, text="R1" , style=shape.diamond,size=size.tiny, location=location.absolute, offset=0)
plotshape(show1 and sws1_1 and show_labels and change(xS11)!=0? xS11 :na      , color=transp, textcolor=cols1_1, text="S1" , style=shape.diamond,size=size.tiny, location=location.absolute, offset=0)
plotshape(show1 and swr2_1 and show_labels and change(xR21)!=0? xR21 :na      , color=transp, textcolor=colr2_1, text="R2" , style=shape.diamond,size=size.tiny, location=location.absolute, offset=0)
plotshape(show1 and sws2_1 and show_labels and change(xS21)!=0? xS21 :na      , color=transp, textcolor=cols2_1, text="S2" , style=shape.diamond,size=size.tiny, location=location.absolute, offset=0)
plotshape(show1 and swr3_1 and show_labels and change(xR31)!=0? xR31 :na      , color=transp, textcolor=colr3_1, text="R3" , style=shape.diamond,size=size.tiny, location=location.absolute, offset=0)
plotshape(show1 and sws3_1 and show_labels and change(xS31)!=0? xS31 :na      , color=transp, textcolor=cols3_1, text="S3" , style=shape.diamond,size=size.tiny, location=location.absolute, offset=0)
plotshape(show1 and swr4_1 and show_labels and change(xR41)!=0? xR41 :na      , color=transp, textcolor=colr4_1, text="R4" , style=shape.diamond,size=size.tiny, location=location.absolute, offset=0)
plotshape(show1 and sws4_1 and show_labels and change(xS41)!=0? xS41 :na      , color=transp, textcolor=cols4_1, text="S4" , style=shape.diamond,size=size.tiny, location=location.absolute, offset=0)
plotshape(show1 and swr5_1 and show_labels and change(xR51)!=0? xR51 :na      , color=transp, textcolor=colr5_1, text="R5" , style=shape.diamond,size=size.tiny, location=location.absolute, offset=0)
plotshape(show1 and sws5_1 and show_labels and change(xS51)!=0? xS51 :na      , color=transp, textcolor=cols5_1, text="S5" , style=shape.diamond,size=size.tiny, location=location.absolute, offset=0)

plotshape(show2 and sw0_2  and show_labels and change(xpivot2)!=0? xpivot2 :na, color=transp, textcolor=col0_2 , text="PP" , style=shape.diamond,size=size.tiny, location=location.absolute, offset=0)
plotshape(show2 and swr1_2 and show_labels and change(xR12)!=0? xR12 :na      , color=transp, textcolor=colr1_2, text="R1" , style=shape.diamond,size=size.tiny, location=location.absolute, offset=0)
plotshape(show2 and sws1_2 and show_labels and change(xS12)!=0? xS12 :na      , color=transp, textcolor=cols1_2, text="S1" , style=shape.diamond,size=size.tiny, location=location.absolute, offset=0)
plotshape(show2 and swr2_2 and show_labels and change(xR22)!=0? xR22 :na      , color=transp, textcolor=colr2_2, text="R2" , style=shape.diamond,size=size.tiny, location=location.absolute, offset=0)
plotshape(show2 and sws2_2 and show_labels and change(xS22)!=0? xS22 :na      , color=transp, textcolor=cols2_2, text="S2" , style=shape.diamond,size=size.tiny, location=location.absolute, offset=0)
plotshape(show2 and swr3_2 and show_labels and change(xR32)!=0? xR32 :na      , color=transp, textcolor=colr3_2, text="R3" , style=shape.diamond,size=size.tiny, location=location.absolute, offset=0)
plotshape(show2 and sws3_2 and show_labels and change(xS32)!=0? xS32 :na      , color=transp, textcolor=cols3_2, text="S3" , style=shape.diamond,size=size.tiny, location=location.absolute, offset=0)
plotshape(show2 and swr4_2 and show_labels and change(xR42)!=0? xR42 :na      , color=transp, textcolor=colr4_2, text="R4" , style=shape.diamond,size=size.tiny, location=location.absolute, offset=0)
plotshape(show2 and sws4_2 and show_labels and change(xS42)!=0? xS42 :na      , color=transp, textcolor=cols4_2, text="S4" , style=shape.diamond,size=size.tiny, location=location.absolute, offset=0)
plotshape(show2 and swr5_2 and show_labels and change(xR52)!=0? xR52 :na      , color=transp, textcolor=colr5_2, text="R5" , style=shape.diamond,size=size.tiny, location=location.absolute, offset=0)
plotshape(show2 and sws5_2 and show_labels and change(xS52)!=0? xS52 :na      , color=transp, textcolor=cols5_2, text="S5" , style=shape.diamond,size=size.tiny, location=location.absolute, offset=0)


if show1
    new_line(sw0_1 , xpivot1 , col0_1 , lineStyle1, linewidth1)
    new_line(swr1_1, xR11    , colr1_1, lineStyle1, linewidth1)
    new_line(sws1_1, xS11    , cols1_1, lineStyle1, linewidth1)
    new_line(swr2_1, xR21    , colr2_1, lineStyle1, linewidth1)
    new_line(sws2_1, xS21    , cols2_1, lineStyle1, linewidth1)
    new_line(swr3_1, xR31    , colr3_1, lineStyle1, linewidth1)
    new_line(sws3_1, xS31    , cols3_1, lineStyle1, linewidth1)
    new_line(swr4_1, xR41    , colr4_1, lineStyle1, linewidth1)
    new_line(sws4_1, xS41    , cols4_1, lineStyle1, linewidth1)
    new_line(swr5_1, xR51    , colr5_1, lineStyle1, linewidth1)
    new_line(sws5_1, xS51    , cols5_1, lineStyle1, linewidth1)

if show2
    new_line(sw0_2 , xpivot2 , col0_2 , lineStyle2, linewidth2)
    new_line(swr1_2, xR12    , colr1_2, lineStyle2, linewidth2)
    new_line(sws1_2, xS12    , cols1_2, lineStyle2, linewidth2)
    new_line(swr2_2, xR22    , colr2_2, lineStyle2, linewidth2)
    new_line(sws2_2, xS22    , cols2_2, lineStyle2, linewidth2)
    new_line(swr3_2, xR32    , colr3_2, lineStyle2, linewidth2)
    new_line(sws3_2, xS32    , cols3_2, lineStyle2, linewidth2)    
    new_line(swr4_2, xR42    , colr4_2, lineStyle2, linewidth2)
    new_line(sws4_2, xS42    , cols4_2, lineStyle2, linewidth2)
    new_line(swr5_2, xR52    , colr5_2, lineStyle2, linewidth2)
    new_line(sws5_2, xS52    , cols5_2, lineStyle2, linewidth2)

What I have

What I want

1 个答案:

答案 0 :(得分:0)

这是一个简化的示例,但您应该能够从中构建您想要的内容。

newDay = change(time("D")) != 0
//newDay = time == timestamp(year, month, dayofmonth, 9, 30) // if you want to use 9:30 as the start for calculating pivots

ts = timestamp(year, month, dayofmonth, 15, 55)

var float closeD = na
var float highD = na
var float lowD = na
var float xPivot = na

var line xPivotLine = line.new(x1 = na, y1 = na, x2 = na, y2 = na, extend = extend.right)

if newDay
    closeD := close
    highD := high
    lowD := low
else
    closeD := close
    if high > highD
        highD := high
    if low < lowD
        lowD := low

if time == ts and barstate.isconfirmed
    xPivot := (highD + lowD + closeD) / 3
    line.set_xy1(xPivotLine, x = bar_index - 1, y = xPivot)
    line.set_xy2(xPivotLine, x = bar_index, y = xPivot)
    
plot(closeD, color = color.yellow)
plot(lowD, color = color.red)
plot(highD, color = color.green)

yesterdayxPivot = valuewhen(time == ts, xPivot, 1)

plot(yesterdayxPivot, style = plot.style_circles)