SPX Fly Zone

punitusingh · study · 205 行 · 点赞 9 · TradingView 原页

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

Pine Script

//@version=6
indicator("SPX Fly", shorttitle="SPX Fly", overlay=true, max_boxes_count=50, max_lines_count=50, max_labels_count=50)
 
// SPX Fly
// Draws up to 15 butterfly zones on an SPX chart. For each fly:
//   rectangle top    = Fly Price + Premium
//   middle line      = Fly Price
//   rectangle bottom = Fly Price - Premium
// Each fly has its own rectangle color (a different default for each).
// Each fly starts 30 minutes after the previous one (New York time): Fly 1 at 10:30, Fly 2 at 11:00, and so on.
// Fly Price is text. A time such as "10:30" (the default) is the fly's start time and uses the price at that time,
// "Open" uses today's open price, and a number such as 7720 uses that price. These prices stay fixed once set;
// they are listed in the Data Window.
// Width is shown in the label only.
// A fly is drawn when its checkbox is on and its Premium is above 0.
// Rectangles run from the fly's start time to the current session close. A fly that starts at or after
// the close is not drawn.
 
GRP_STYLE = "Style"
GRP_FLY   = "SPX Flies   [on | Px = Fly Price (start time like 10:30, Open, or a number) | Wd = Width | Pr = Premium | C = Color]"
 
fillTransp  = input.int(85, "Fill transparency", minval=0, maxval=100, group=GRP_STYLE)
borderWidth = input.int(2, "Border width", minval=1, maxval=5, group=GRP_STYLE)
showLabels  = input.bool(true, "Show labels", group=GRP_STYLE)
 
on1  = input.bool(false, "Fly 1", inline="f1", group=GRP_FLY)
px1  = input.string("10:30", "Px", inline="f1", group=GRP_FLY)
wd1  = input.float(50.0, "Wd", minval=0.0, step=5.0, inline="f1", group=GRP_FLY)
pr1  = input.float(20.0, "Pr", minval=0.0, step=0.05, inline="f1", group=GRP_FLY)
cl1  = input.color(color.blue, "C", inline="f1", group=GRP_FLY)
 
on2  = input.bool(false, "Fly 2", inline="f2", group=GRP_FLY)
px2  = input.string("11:00", "Px", inline="f2", group=GRP_FLY)
wd2  = input.float(50.0, "Wd", minval=0.0, step=5.0, inline="f2", group=GRP_FLY)
pr2  = input.float(20.0, "Pr", minval=0.0, step=0.05, inline="f2", group=GRP_FLY)
cl2  = input.color(color.red, "C", inline="f2", group=GRP_FLY)
 
on3  = input.bool(false, "Fly 3", inline="f3", group=GRP_FLY)
px3  = input.string("11:30", "Px", inline="f3", group=GRP_FLY)
wd3  = input.float(50.0, "Wd", minval=0.0, step=5.0, inline="f3", group=GRP_FLY)
pr3  = input.float(20.0, "Pr", minval=0.0, step=0.05, inline="f3", group=GRP_FLY)
cl3  = input.color(color.green, "C", inline="f3", group=GRP_FLY)
 
on4  = input.bool(false, "Fly 4", inline="f4", group=GRP_FLY)
px4  = input.string("12:00", "Px", inline="f4", group=GRP_FLY)
wd4  = input.float(50.0, "Wd", minval=0.0, step=5.0, inline="f4", group=GRP_FLY)
pr4  = input.float(20.0, "Pr", minval=0.0, step=0.05, inline="f4", group=GRP_FLY)
cl4  = input.color(color.orange, "C", inline="f4", group=GRP_FLY)
 
on5  = input.bool(false, "Fly 5", inline="f5", group=GRP_FLY)
px5  = input.string("12:30", "Px", inline="f5", group=GRP_FLY)
wd5  = input.float(50.0, "Wd", minval=0.0, step=5.0, inline="f5", group=GRP_FLY)
pr5  = input.float(20.0, "Pr", minval=0.0, step=0.05, inline="f5", group=GRP_FLY)
cl5  = input.color(color.purple, "C", inline="f5", group=GRP_FLY)
 
on6  = input.bool(false, "Fly 6", inline="f6", group=GRP_FLY)
px6  = input.string("13:00", "Px", inline="f6", group=GRP_FLY)
wd6  = input.float(50.0, "Wd", minval=0.0, step=5.0, inline="f6", group=GRP_FLY)
pr6  = input.float(20.0, "Pr", minval=0.0, step=0.05, inline="f6", group=GRP_FLY)
cl6  = input.color(color.teal, "C", inline="f6", group=GRP_FLY)
 
on7  = input.bool(false, "Fly 7", inline="f7", group=GRP_FLY)
px7  = input.string("13:30", "Px", inline="f7", group=GRP_FLY)
wd7  = input.float(50.0, "Wd", minval=0.0, step=5.0, inline="f7", group=GRP_FLY)
pr7  = input.float(20.0, "Pr", minval=0.0, step=0.05, inline="f7", group=GRP_FLY)
cl7  = input.color(color.fuchsia, "C", inline="f7", group=GRP_FLY)
 
on8  = input.bool(false, "Fly 8", inline="f8", group=GRP_FLY)
px8  = input.string("14:00", "Px", inline="f8", group=GRP_FLY)
wd8  = input.float(50.0, "Wd", minval=0.0, step=5.0, inline="f8", group=GRP_FLY)
pr8  = input.float(20.0, "Pr", minval=0.0, step=0.05, inline="f8", group=GRP_FLY)
cl8  = input.color(color.maroon, "C", inline="f8", group=GRP_FLY)
 
on9  = input.bool(false, "Fly 9", inline="f9", group=GRP_FLY)
px9  = input.string("14:30", "Px", inline="f9", group=GRP_FLY)
wd9  = input.float(50.0, "Wd", minval=0.0, step=5.0, inline="f9", group=GRP_FLY)
pr9  = input.float(20.0, "Pr", minval=0.0, step=0.05, inline="f9", group=GRP_FLY)
cl9  = input.color(color.navy, "C", inline="f9", group=GRP_FLY)
 
on10 = input.bool(false, "Fly 10", inline="f10", group=GRP_FLY)
px10 = input.string("15:00", "Px", inline="f10", group=GRP_FLY)
wd10 = input.float(50.0, "Wd", minval=0.0, step=5.0, inline="f10", group=GRP_FLY)
pr10 = input.float(20.0, "Pr", minval=0.0, step=0.05, inline="f10", group=GRP_FLY)
cl10 = input.color(color.olive, "C", inline="f10", group=GRP_FLY)
 
on11 = input.bool(false, "Fly 11", inline="f11", group=GRP_FLY)
px11 = input.string("15:30", "Px", inline="f11", group=GRP_FLY)
wd11 = input.float(50.0, "Wd", minval=0.0, step=5.0, inline="f11", group=GRP_FLY)
pr11 = input.float(20.0, "Pr", minval=0.0, step=0.05, inline="f11", group=GRP_FLY)
cl11 = input.color(color.gray, "C", inline="f11", group=GRP_FLY)
 
on12 = input.bool(false, "Fly 12", inline="f12", group=GRP_FLY)
px12 = input.string("16:00", "Px", inline="f12", group=GRP_FLY)
wd12 = input.float(50.0, "Wd", minval=0.0, step=5.0, inline="f12", group=GRP_FLY)
pr12 = input.float(20.0, "Pr", minval=0.0, step=0.05, inline="f12", group=GRP_FLY)
cl12 = input.color(color.aqua, "C", inline="f12", group=GRP_FLY)
 
on13 = input.bool(false, "Fly 13", inline="f13", group=GRP_FLY)
px13 = input.string("16:30", "Px", inline="f13", group=GRP_FLY)
wd13 = input.float(50.0, "Wd", minval=0.0, step=5.0, inline="f13", group=GRP_FLY)
pr13 = input.float(20.0, "Pr", minval=0.0, step=0.05, inline="f13", group=GRP_FLY)
cl13 = input.color(color.lime, "C", inline="f13", group=GRP_FLY)
 
on14 = input.bool(false, "Fly 14", inline="f14", group=GRP_FLY)
px14 = input.string("17:00", "Px", inline="f14", group=GRP_FLY)
wd14 = input.float(50.0, "Wd", minval=0.0, step=5.0, inline="f14", group=GRP_FLY)
pr14 = input.float(20.0, "Pr", minval=0.0, step=0.05, inline="f14", group=GRP_FLY)
cl14 = input.color(color.yellow, "C", inline="f14", group=GRP_FLY)
 
on15 = input.bool(false, "Fly 15", inline="f15", group=GRP_FLY)
px15 = input.string("17:30", "Px", inline="f15", group=GRP_FLY)
wd15 = input.float(50.0, "Wd", minval=0.0, step=5.0, inline="f15", group=GRP_FLY)
pr15 = input.float(20.0, "Pr", minval=0.0, step=0.05, inline="f15", group=GRP_FLY)
cl15 = input.color(color.silver, "C", inline="f15", group=GRP_FLY)
 
var array<bool>  flyOn    = array.from(on1, on2, on3, on4, on5, on6, on7, on8, on9, on10, on11, on12, on13, on14, on15)
var array<string> flyPrice = array.from(px1, px2, px3, px4, px5, px6, px7, px8, px9, px10, px11, px12, px13, px14, px15)
var array<float> flyWidth = array.from(wd1, wd2, wd3, wd4, wd5, wd6, wd7, wd8, wd9, wd10, wd11, wd12, wd13, wd14, wd15)
var array<float> flyPrem  = array.from(pr1, pr2, pr3, pr4, pr5, pr6, pr7, pr8, pr9, pr10, pr11, pr12, pr13, pr14, pr15)
var array<color> flyColors = array.from(cl1, cl2, cl3, cl4, cl5, cl6, cl7, cl8, cl9, cl10, cl11, cl12, cl13, cl14, cl15)
 
dayOpenTs  = nz(time("D"), time)
dayCloseTs = nz(time_close("D"), time)
 
var float openPx = na
if na(openPx) or timeframe.change("D")
    openPx := open
plot(openPx, "Day open", display=display.status_line + display.data_window)
 
// start time of each fly in minutes after midnight New York time: the time typed in its Px box,
// otherwise 10:30 + 30 minutes per fly
var array<int> flyStartMin = array.new_int(15, 0)
if barstate.isfirst
    for i = 0 to array.size(flyPrice) - 1
        parts = str.split(str.replace_all(array.get(flyPrice, i), " ", ""), ":")
        hh = array.size(parts) == 2 ? str.tonumber(array.get(parts, 0)) : na
        mm = array.size(parts) == 2 ? str.tonumber(array.get(parts, 1)) : na
        timeOk = not na(hh) and not na(mm) and hh >= 0 and hh < 24 and mm >= 0 and mm < 60
        array.set(flyStartMin, i, timeOk ? int(hh) * 60 + int(mm) : 630 + 30 * i)
 
// price at each fly's start time: open of the first bar starting in the 30 minutes from that time,
// reset each day
etMin = hour(time, "America/New_York") * 60 + minute(time, "America/New_York")
var array<float> flyStartPx = array.new_float(15, na)
newDay = timeframe.change("D")
for i = 0 to array.size(flyStartMin) - 1
    sMin = array.get(flyStartMin, i)
    if newDay
        array.set(flyStartPx, i, float(na))
    if na(array.get(flyStartPx, i)) and etMin >= sMin and etMin < sMin + 30
        array.set(flyStartPx, i, open)
plot(array.get(flyStartPx, 0), "Fly 1 start price", display=display.status_line + display.data_window)
plot(array.get(flyStartPx, 1), "Fly 2 start price", display=display.data_window)
plot(array.get(flyStartPx, 2), "Fly 3 start price", display=display.data_window)
plot(array.get(flyStartPx, 3), "Fly 4 start price", display=display.data_window)
plot(array.get(flyStartPx, 4), "Fly 5 start price", display=display.data_window)
plot(array.get(flyStartPx, 5), "Fly 6 start price", display=display.data_window)
plot(array.get(flyStartPx, 6), "Fly 7 start price", display=display.data_window)
plot(array.get(flyStartPx, 7), "Fly 8 start price", display=display.data_window)
plot(array.get(flyStartPx, 8), "Fly 9 start price", display=display.data_window)
plot(array.get(flyStartPx, 9), "Fly 10 start price", display=display.data_window)
plot(array.get(flyStartPx, 10), "Fly 11 start price", display=display.data_window)
plot(array.get(flyStartPx, 11), "Fly 12 start price", display=display.data_window)
plot(array.get(flyStartPx, 12), "Fly 13 start price", display=display.data_window)
plot(array.get(flyStartPx, 13), "Fly 14 start price", display=display.data_window)
plot(array.get(flyStartPx, 14), "Fly 15 start price", display=display.data_window)
 
var array<box>   boxes  = array.new<box>()
var array<line>  mids   = array.new<line>()
var array<label> labels = array.new<label>()
 
if barstate.islast
    for b in boxes
        box.delete(b)
    for l in mids
        line.delete(l)
    for lb in labels
        label.delete(lb)
    array.clear(boxes)
    array.clear(mids)
    array.clear(labels)
 
    for i = 0 to array.size(flyOn) - 1
        pxTxt = str.lower(str.replace_all(str.replace_all(array.get(flyPrice, i), ",", ""), " ", ""))
        pxIn  = str.tonumber(pxTxt)
        startPx = nz(array.get(flyStartPx, i), openPx)
        fallbackPx = pxTxt == "open" ? openPx : startPx
        c = (na(pxIn) or pxIn <= 0) ? nz(fallbackPx, close) : pxIn
        w = array.get(flyWidth, i)
        p = array.get(flyPrem, i)
        clr = array.get(flyColors, i)
        sMin    = array.get(flyStartMin, i)
        startTs = timestamp("America/New_York", year(time, "America/New_York"), month(time, "America/New_York"), dayofmonth(time, "America/New_York"), math.floor(sMin / 60), sMin % 60, 0)
        leftTs  = math.min(math.max(startTs, dayOpenTs), dayCloseTs)
        rightTs = dayCloseTs
        if array.get(flyOn, i) and p > 0 and leftTs < rightTs
            fillClr = color.new(clr, fillTransp)
            array.push(boxes, box.new(left=leftTs, top=c + p, right=rightTs, bottom=c - p, xloc=xloc.bar_time, border_color=clr, border_width=borderWidth, bgcolor=fillClr))
            array.push(mids, line.new(x1=leftTs, y1=c, x2=rightTs, y2=c, xloc=xloc.bar_time, color=clr, width=1, style=line.style_dashed))
            if showLabels
                lum    = 0.299 * color.r(clr) + 0.587 * color.g(clr) + 0.114 * color.b(clr)
                txtClr = lum > 160 ? color.black : color.white
                txt = str.tostring(c, "#.##") + "  ±" + str.tostring(p, "#.##") + "  (width " + str.tostring(w, "#.##") + ")"
                tip = "Top " + str.tostring(c + p, "#.##") + "\nCenter " + str.tostring(c, "#.##") + "\nBottom " + str.tostring(c - p, "#.##") + "\nPremium " + str.tostring(p, "#.##") + "\nWidth " + str.tostring(w, "#.##")
                array.push(labels, label.new(x=rightTs, y=c, xloc=xloc.bar_time, text=txt, style=label.style_label_right, color=clr, textcolor=txtClr, size=size.small, tooltip=tip))

← 返回列表