MTF Outside Bars + Inside Bars

zacromancer · study · 245 行 · 点赞 9 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=6
indicator("Outside Bars + Inside Bars", "OIOI", overlay = true, max_labels_count = 500, max_lines_count = 500, max_boxes_count = 500)
 
// --- User Defined Types ---
type BarData
    float o
    float h
    float l
    float c
    float ph
    float pl
    float po
    float pc
 
type ObjectData
    box bx
    line hi
    line lo
    line mid
    label lbl
    bool active
    float top
    float bottom
 
// --- Inputs ---
// Outside-bar timeframes
showOutside1 = input.bool(true, "Enable", inline = "ob1", group = "Outside Bars · Timeframes")
outsideTf1 = input.timeframe("5", "TF 1", inline = "ob1", group = "Outside Bars · Timeframes")
showOutside2 = input.bool(false, "Enable", inline = "ob2", group = "Outside Bars · Timeframes")
outsideTf2 = input.timeframe("15", "TF 2", inline = "ob2", group = "Outside Bars · Timeframes")
showOutside3 = input.bool(false, "Enable", inline = "ob3", group = "Outside Bars · Timeframes")
outsideTf3 = input.timeframe("60", "TF 3", inline = "ob3", group = "Outside Bars · Timeframes")
 
// Inside-bar timeframes
showInside1 = input.bool(true, "Enable", inline = "ib1", group = "Inside Bars · Timeframes")
insideTf1 = input.timeframe("5", "TF 1", inline = "ib1", group = "Inside Bars · Timeframes")
showInside2 = input.bool(false, "Enable", inline = "ib2", group = "Inside Bars · Timeframes")
insideTf2 = input.timeframe("15", "TF 2", inline = "ib2", group = "Inside Bars · Timeframes")
showInside3 = input.bool(false, "Enable", inline = "ib3", group = "Inside Bars · Timeframes")
insideTf3 = input.timeframe("60", "TF 3", inline = "ib3", group = "Inside Bars · Timeframes")
 
// Outside-bar style
colorOB = input.color(#B05CFF, "Color", group = "Outside Bars · Style")
maxBoxOB = input.int(20, "Max patterns per timeframe", minval = 1, maxval = 25, group = "Outside Bars · Style")
borderWidthOB = input.int(2, "Border width", minval = 1, maxval = 10, group = "Outside Bars · Style")
showLinesOB = input.bool(true, "Show high/low", inline = "ob_lines", group = "Outside Bars · Style")
lineWidthOB = input.int(1, "Width", minval = 1, maxval = 10, inline = "ob_lines", group = "Outside Bars · Style")
lineStyleOB = input.string("dotted", "Style", options = ["solid", "dashed", "dotted"], inline = "ob_lines", group = "Outside Bars · Style")
lineColorOB = input.color(#B05CFF, "Line color", group = "Outside Bars · Style")
showMidOB = input.bool(true, "Show mid line", inline = "ob_mid", group = "Outside Bars · Style")
midWidthOB = input.int(1, "Width", minval = 1, maxval = 10, inline = "ob_mid", group = "Outside Bars · Style")
midStyleOB = input.string("dotted", "Style", options = ["solid", "dashed", "dotted"], inline = "ob_mid", group = "Outside Bars · Style")
midColorOB = input.color(#B05CFF, "Mid color", group = "Outside Bars · Style")
showLabelOB = input.bool(true, "Show label", inline = "ob_label", group = "Outside Bars · Style")
labelColorOB = input.color(color.gray, "Text color", inline = "ob_label", group = "Outside Bars · Style")
 
// Inside-bar style
colorIB = input.color(#FF7A2F, "Color", group = "Inside Bars · Style")
maxBoxIB = input.int(20, "Max patterns per timeframe", minval = 1, maxval = 25, group = "Inside Bars · Style")
borderWidthIB = input.int(2, "Border width", minval = 1, maxval = 10, group = "Inside Bars · Style")
showLinesIB = input.bool(true, "Show high/low", inline = "ib_lines", group = "Inside Bars · Style")
lineWidthIB = input.int(1, "Width", minval = 1, maxval = 10, inline = "ib_lines", group = "Inside Bars · Style")
lineStyleIB = input.string("dotted", "Style", options = ["solid", "dashed", "dotted"], inline = "ib_lines", group = "Inside Bars · Style")
lineColorIB = input.color(#FF7A2F, "Line color", group = "Inside Bars · Style")
showMidIB = input.bool(true, "Show mid line", inline = "ib_mid", group = "Inside Bars · Style")
midWidthIB = input.int(1, "Width", minval = 1, maxval = 10, inline = "ib_mid", group = "Inside Bars · Style")
midStyleIB = input.string("dashed", "Style", options = ["solid", "dashed", "dotted"], inline = "ib_mid", group = "Inside Bars · Style")
midColorIB = input.color(#FF7A2F, "Mid color", group = "Inside Bars · Style")
showLabelIB = input.bool(true, "Show label", inline = "ib_label", group = "Inside Bars · Style")
labelColorIB = input.color(color.gray, "Text color", inline = "ib_label", group = "Inside Bars · Style")
 
labelSize = input.string(size.small, "Label size", options = [size.tiny, size.small, size.normal], group = "Labels")
 
// Session filter
enableSessionFilter = input.bool(false, "Enable session filter", group = "Session Filter")
sessionTz = "America/New_York"
session1On = input.bool(true, "", inline = "sess1", group = "Session Filter")
session1 = input.session("1930-2130", "Session 1", inline = "sess1", group = "Session Filter")
session2On = input.bool(true, "", inline = "sess2", group = "Session Filter")
session2 = input.session("0200-0500", "Session 2", inline = "sess2", group = "Session Filter")
session3On = input.bool(true, "", inline = "sess3", group = "Session Filter")
session3 = input.session("0700-0900", "Session 3", inline = "sess3", group = "Session Filter")
 
// --- Helper Functions ---
getLineStyle(string style) =>
    style == "dashed" ? line.style_dashed : style == "dotted" ? line.style_dotted : line.style_solid
 
f_formatTf(string tf) =>
    int seconds = timeframe.in_seconds(tf)
    string formattedTf = na
    if seconds < 60
        formattedTf := str.tostring(seconds) + "s"
    else if seconds < 3600
        formattedTf := str.tostring(seconds / 60) + "min"
    else if seconds < 86400
        formattedTf := str.tostring(seconds / 3600) + "H"
    else if seconds < 604800
        formattedTf := str.tostring(seconds / 86400) + "D"
    else if seconds < 2592000
        formattedTf := str.tostring(seconds / 604800) + "W"
    else
        formattedTf := str.tostring(seconds / 2592000) + "M"
    formattedTf
 
f_inSession(string session) =>
    not na(time(timeframe.period, session, sessionTz))
 
f_getHtfData() =>
    BarData.new(open, high, low, close, high[1], low[1], open[1], close[1])
 
f_deleteObject(ObjectData obj) =>
    if not na(obj.bx)
        box.delete(obj.bx)
    if not na(obj.hi)
        line.delete(obj.hi)
    if not na(obj.lo)
        line.delete(obj.lo)
    if not na(obj.mid)
        line.delete(obj.mid)
    if not na(obj.lbl)
        label.delete(obj.lbl)
 
f_addPattern(array<ObjectData> objects, int leftIdx, int rightIdx, float top, float bottom, string patternLabel, color patternColor, int maxCount, int borderWidth, bool showLines, int lineWidth, string lineStyle, color lineColor, bool showMid, int midWidth, string midStyle, color midColor, bool showLabel, color labelColor, string labelSize, bool drawBox) =>
    int width = math.max(1, rightIdx - leftIdx + 1)
    int endIdx = rightIdx + math.min(width * 2, 500)
    float midpoint = math.avg(top, bottom)
    box bx = drawBox ? box.new(leftIdx, top, rightIdx, bottom, bgcolor = color.new(patternColor, 90), border_color = color.new(patternColor, 40), border_width = borderWidth) : na
    line hi = showLines ? line.new(rightIdx, top, endIdx, top, color = lineColor, width = lineWidth, style = getLineStyle(lineStyle)) : na
    line lo = showLines ? line.new(rightIdx, bottom, endIdx, bottom, color = lineColor, width = lineWidth, style = getLineStyle(lineStyle)) : na
    line mid = showMid ? line.new(rightIdx, midpoint, endIdx, midpoint, color = midColor, width = midWidth, style = getLineStyle(midStyle)) : na
    label lbl = showLabel ? label.new(rightIdx, top, patternLabel, xloc = xloc.bar_index, yloc = yloc.price, style = label.style_label_left, color = color.new(color.black, 100), textcolor = labelColor, size = labelSize) : na
    array.push(objects, ObjectData.new(bx, hi, lo, mid, lbl, true, top, bottom))
    if array.size(objects) > maxCount
        ObjectData oldest = array.shift(objects)
        f_deleteObject(oldest)
 
f_process(bool enabled, string tf, BarData currentData, bool newTfBar, int startIdx, int priorStartIdx, array<ObjectData> objects, bool isOutside, bool sessionActive, color patternColor, int maxCount, int borderWidth, bool showLines, int lineWidth, string lineStyle, color lineColor, bool showMid, int midWidth, string midStyle, color midColor, bool showLabel, color labelColor, string labelSize) =>
    int nextStart = startIdx
    int nextPriorStart = priorStartIdx
    bool completed = false
    if newTfBar
        nextPriorStart := startIdx
        nextStart := bar_index
        BarData priorData = currentData[1]
        if not na(priorData)
            completed := isOutside ? priorData.h > priorData.ph and priorData.l < priorData.pl : priorData.h < priorData.ph and priorData.l > priorData.pl
            bool timeframeAllowed = timeframe.in_seconds(tf) >= timeframe.in_seconds()
            if completed and enabled and timeframeAllowed and sessionActive
                string patternText = f_formatTf(tf) + (isOutside ? " OSB" : " ISB")
                f_addPattern(objects, startIdx, bar_index - 1, priorData.h, priorData.l, patternText, patternColor, maxCount, borderWidth, showLines, lineWidth, lineStyle, lineColor, showMid, midWidth, midStyle, midColor, showLabel, labelColor, labelSize, timeframe.in_seconds(tf) > timeframe.in_seconds())
    [nextStart, nextPriorStart, completed]
 
f_checkBreakout(array<ObjectData> objects) =>
    if array.size(objects) > 0
        for i = 0 to array.size(objects) - 1
            ObjectData obj = array.get(objects, i)
            if obj.active and (close > obj.top or close < obj.bottom)
                obj.active := false
 
// --- Session State ---
sessionActive = not enableSessionFilter or (session1On and f_inSession(session1)) or (session2On and f_inSession(session2)) or (session3On and f_inSession(session3))
 
// --- HTF Data ---
obData1 = request.security(syminfo.tickerid, outsideTf1, f_getHtfData())
obData2 = request.security(syminfo.tickerid, outsideTf2, f_getHtfData())
obData3 = request.security(syminfo.tickerid, outsideTf3, f_getHtfData())
ibData1 = request.security(syminfo.tickerid, insideTf1, f_getHtfData())
ibData2 = request.security(syminfo.tickerid, insideTf2, f_getHtfData())
ibData3 = request.security(syminfo.tickerid, insideTf3, f_getHtfData())
 
newOB1 = ta.change(time(outsideTf1)) > 0
newOB2 = ta.change(time(outsideTf2)) > 0
newOB3 = ta.change(time(outsideTf3)) > 0
newIB1 = ta.change(time(insideTf1)) > 0
newIB2 = ta.change(time(insideTf2)) > 0
newIB3 = ta.change(time(insideTf3)) > 0
 
// --- Object Storage ---
var obObjects1 = array.new<ObjectData>()
var obObjects2 = array.new<ObjectData>()
var obObjects3 = array.new<ObjectData>()
var ibObjects1 = array.new<ObjectData>()
var ibObjects2 = array.new<ObjectData>()
var ibObjects3 = array.new<ObjectData>()
 
var int obStart1 = bar_index
var int obStart2 = bar_index
var int obStart3 = bar_index
var int ibStart1 = bar_index
var int ibStart2 = bar_index
var int ibStart3 = bar_index
var int obPriorStart1 = bar_index
var int obPriorStart2 = bar_index
var int obPriorStart3 = bar_index
var int ibPriorStart1 = bar_index
var int ibPriorStart2 = bar_index
var int ibPriorStart3 = bar_index
 
// --- Process Each Timeframe ---
[obStart1Next, obPriorStart1Next, obComplete1] = f_process(showOutside1, outsideTf1, obData1, newOB1, obStart1, obPriorStart1, obObjects1, true, sessionActive, colorOB, maxBoxOB, borderWidthOB, showLinesOB, lineWidthOB, lineStyleOB, lineColorOB, showMidOB, midWidthOB, midStyleOB, midColorOB, showLabelOB, labelColorOB, labelSize)
[obStart2Next, obPriorStart2Next, obComplete2] = f_process(showOutside2, outsideTf2, obData2, newOB2, obStart2, obPriorStart2, obObjects2, true, sessionActive, colorOB, maxBoxOB, borderWidthOB, showLinesOB, lineWidthOB, lineStyleOB, lineColorOB, showMidOB, midWidthOB, midStyleOB, midColorOB, showLabelOB, labelColorOB, labelSize)
[obStart3Next, obPriorStart3Next, obComplete3] = f_process(showOutside3, outsideTf3, obData3, newOB3, obStart3, obPriorStart3, obObjects3, true, sessionActive, colorOB, maxBoxOB, borderWidthOB, showLinesOB, lineWidthOB, lineStyleOB, lineColorOB, showMidOB, midWidthOB, midStyleOB, midColorOB, showLabelOB, labelColorOB, labelSize)
[ibStart1Next, ibPriorStart1Next, ibComplete1] = f_process(showInside1, insideTf1, ibData1, newIB1, ibStart1, ibPriorStart1, ibObjects1, false, sessionActive, colorIB, maxBoxIB, borderWidthIB, showLinesIB, lineWidthIB, lineStyleIB, lineColorIB, showMidIB, midWidthIB, midStyleIB, midColorIB, showLabelIB, labelColorIB, labelSize)
[ibStart2Next, ibPriorStart2Next, ibComplete2] = f_process(showInside2, insideTf2, ibData2, newIB2, ibStart2, ibPriorStart2, ibObjects2, false, sessionActive, colorIB, maxBoxIB, borderWidthIB, showLinesIB, lineWidthIB, lineStyleIB, lineColorIB, showMidIB, midWidthIB, midStyleIB, midColorIB, showLabelIB, labelColorIB, labelSize)
[ibStart3Next, ibPriorStart3Next, ibComplete3] = f_process(showInside3, insideTf3, ibData3, newIB3, ibStart3, ibPriorStart3, ibObjects3, false, sessionActive, colorIB, maxBoxIB, borderWidthIB, showLinesIB, lineWidthIB, lineStyleIB, lineColorIB, showMidIB, midWidthIB, midStyleIB, midColorIB, showLabelIB, labelColorIB, labelSize)
 
obStart1 := obStart1Next
obStart2 := obStart2Next
obStart3 := obStart3Next
ibStart1 := ibStart1Next
ibStart2 := ibStart2Next
ibStart3 := ibStart3Next
obPriorStart1 := obPriorStart1Next
obPriorStart2 := obPriorStart2Next
obPriorStart3 := obPriorStart3Next
ibPriorStart1 := ibPriorStart1Next
ibPriorStart2 := ibPriorStart2Next
ibPriorStart3 := ibPriorStart3Next
 
f_checkBreakout(obObjects1)
f_checkBreakout(obObjects2)
f_checkBreakout(obObjects3)
f_checkBreakout(ibObjects1)
f_checkBreakout(ibObjects2)
f_checkBreakout(ibObjects3)
 
// --- Same-Timeframe Bar Coloring ---
isOutsideNow1 = not na(obData1) and obData1.h > obData1.ph and obData1.l < obData1.pl
isOutsideNow2 = not na(obData2) and obData2.h > obData2.ph and obData2.l < obData2.pl
isOutsideNow3 = not na(obData3) and obData3.h > obData3.ph and obData3.l < obData3.pl
isInsideNow1 = not na(ibData1) and ibData1.h < ibData1.ph and ibData1.l > ibData1.pl
isInsideNow2 = not na(ibData2) and ibData2.h < ibData2.ph and ibData2.l > ibData2.pl
isInsideNow3 = not na(ibData3) and ibData3.h < ibData3.ph and ibData3.l > ibData3.pl
 
bool outsideColorSignal = sessionActive and ((showOutside1 and timeframe.in_seconds(outsideTf1) == timeframe.in_seconds() and isOutsideNow1) or (showOutside2 and timeframe.in_seconds(outsideTf2) == timeframe.in_seconds() and isOutsideNow2) or (showOutside3 and timeframe.in_seconds(outsideTf3) == timeframe.in_seconds() and isOutsideNow3))
bool insideColorSignal = sessionActive and ((showInside1 and timeframe.in_seconds(insideTf1) == timeframe.in_seconds() and isInsideNow1) or (showInside2 and timeframe.in_seconds(insideTf2) == timeframe.in_seconds() and isInsideNow2) or (showInside3 and timeframe.in_seconds(insideTf3) == timeframe.in_seconds() and isInsideNow3))
color barColorValue = outsideColorSignal ? color.new(colorOB, 50) : insideColorSignal ? color.new(colorIB, 50) : na
barcolor(barColorValue)
 
// --- Alerts ---
anyOutsideCompleted = sessionActive and ((showOutside1 and obComplete1) or (showOutside2 and obComplete2) or (showOutside3 and obComplete3))
anyInsideCompleted = sessionActive and ((showInside1 and ibComplete1) or (showInside2 and ibComplete2) or (showInside3 and ibComplete3))
alertcondition(anyOutsideCompleted, "Outside Bar", "Outside Bar detected")
alertcondition(anyInsideCompleted, "Inside Bar", "Inside Bar detected")

← 返回列表