Current Timeframe Support & Resistance v7.1

mikeyc747 · study · 1260 行 · 点赞 55 · TradingView 原页

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

Pine Script

//@version=6
indicator("Current Timeframe Support & Resistance v7.1", overlay=true, max_boxes_count=40, max_labels_count=40, max_bars_back=5000)
 
//====================================================================
// INPUTS
//====================================================================
 
atrLen = input.int(14, "ATR length", minval=2, group="Detection")
lookbackBars = input.int(500, "Zone lookback bars", minval=50, maxval=5000, group="Detection")
maxStoredZones = input.int(80, "Maximum stored zones", minval=10, maxval=200, group="Detection")
 
swingReversalAtr = input.float(1.00, "Swing confirmation x ATR", minval=0.25, maxval=5.0, step=0.05, group="Detection")
mergeAtrMult = input.float(0.15, "Zone merge distance x ATR", minval=0.01, maxval=1.0, step=0.01, group="Detection")
 
zoneHalfAtrMult = input.float(0.10, "Fog half-width x ATR", minval=0.01, maxval=1.0, step=0.01, group="Zones")
nearestZonesEachSide = input.int(2, "Nearest zones each side", minval=1, maxval=4, group="Zones")
majorZonesEachSide = input.int(2, "Major zones each side", minval=0, maxval=4, group="Zones")
majorMaxDistanceAtr = input.float(6.0, "Major maximum distance x ATR", minval=2.0, maxval=30.0, step=0.5, group="Zones")
displayBars = input.int(200, "Fog display length", minval=25, maxval=500, group="Zones")
 
eventCooldown = input.int(2, "Bars between zone visits", minval=1, maxval=20, group="Scoring")
 
swingWeight = input.float(8.0, "Swing weight", minval=0.0, step=0.5, group="Scoring")
interactionWeight = input.float(3.0, "Zone visit weight", minval=0.0, step=0.5, group="Scoring")
bounceWeight = input.float(5.0, "Bounce weight", minval=0.0, step=0.5, group="Scoring")
breakWeight = input.float(2.0, "Break / flip importance", minval=0.0, step=0.5, group="Scoring")
rejectionWeight = input.float(3.0, "Rejection strength weight", minval=0.0, step=0.5, group="Scoring")
recencyWeight = input.float(3.0, "Recency weight", minval=0.0, step=0.5, group="Scoring")
 
supportColor = input.color(color.rgb(0, 170, 110), "Support color", group="Display")
resistanceColor = input.color(color.rgb(220, 70, 70), "Resistance color", group="Display")
 
zoneTransparency = input.int(86, "Confirmed fog transparency", minval=0, maxval=100, group="Display")
liveTransparency = input.int(80, "Live fog transparency", minval=0, maxval=100, group="Display")
 
showLiveZone = input.bool(true, "Show developing live zone", group="Display")
showLabels = input.bool(true, "Show labels", group="Display")
showStats = input.bool(true, "Show zone statistics", group="Display")
 
//====================================================================
// CURRENT TIMEFRAME DATA
//====================================================================
 
float atrValue = ta.atr(atrLen)
 
//====================================================================
// ZONE STORAGE
//====================================================================
 
var array<float> zoneLevels = array.new<float>()
var array<float> zoneWidths = array.new<float>()
 
var array<int> zoneSwings = array.new<int>()
var array<int> zoneInteractions = array.new<int>()
var array<int> zoneBounces = array.new<int>()
var array<int> zoneBreaks = array.new<int>()
 
var array<float> zoneRejection = array.new<float>()
 
var array<int> zoneInside = array.new<int>()
var array<int> zoneEntrySide = array.new<int>()
 
var array<int> zoneLastInteractionBar = array.new<int>()
var array<int> zoneLastActivityBar = array.new<int>()
 
var array<box> drawnBoxes = array.new<box>()
var array<label> drawnLabels = array.new<label>()
 
//====================================================================
// REMOVE ZONE
//====================================================================
 
f_removeZone(int index) =>
    array.remove(zoneLevels, index)
    array.remove(zoneWidths, index)
 
    array.remove(zoneSwings, index)
    array.remove(zoneInteractions, index)
    array.remove(zoneBounces, index)
    array.remove(zoneBreaks, index)
 
    array.remove(zoneRejection, index)
 
    array.remove(zoneInside, index)
    array.remove(zoneEntrySide, index)
 
    array.remove(zoneLastInteractionBar, index)
    array.remove(zoneLastActivityBar, index)
 
    0
 
//====================================================================
// ADD OR MERGE CONFIRMED SWING
//====================================================================
 
f_addSwing(float price, float width, int originBar, float mergeDistance) =>
 
    int foundIndex = -1
    float nearestDistance = 1e20
 
    int count = array.size(zoneLevels)
 
    if count > 0
        for i = 0 to count - 1
 
            float existing = array.get(zoneLevels, i)
            float distance = math.abs(price - existing)
 
            if distance <= mergeDistance and distance < nearestDistance
                foundIndex := i
                nearestDistance := distance
 
    //============================================================
    // NEW ZONE
    //============================================================
 
    if foundIndex == -1
 
        array.push(zoneLevels, price)
        array.push(zoneWidths, width)
 
        // The swing establishes the zone.
        // It does NOT count as a later bounce/test.
        array.push(zoneSwings, 1)
        array.push(zoneInteractions, 0)
        array.push(zoneBounces, 0)
        array.push(zoneBreaks, 0)
 
        array.push(zoneRejection, 0.0)
 
        array.push(zoneInside, 0)
        array.push(zoneEntrySide, 0)
 
        array.push(zoneLastInteractionBar, originBar)
        array.push(zoneLastActivityBar, originBar)
 
    //============================================================
    // EXISTING ZONE REINFORCED BY ANOTHER CONFIRMED SWING
    //============================================================
 
    else
 
        float oldLevel = array.get(zoneLevels, foundIndex)
        float oldWidth = array.get(zoneWidths, foundIndex)
 
        int oldSwings = array.get(zoneSwings, foundIndex)
        int newSwings = oldSwings + 1
 
        float newLevel = ((oldLevel * oldSwings) + price) / newSwings
        float newWidth = ((oldWidth * oldSwings) + width) / newSwings
 
        array.set(zoneLevels, foundIndex, newLevel)
        array.set(zoneWidths, foundIndex, newWidth)
 
        // Reinforces the structural zone,
        // but does not fake an interaction/bounce.
        array.set(zoneSwings, foundIndex, newSwings)
 
        array.set(
             zoneLastActivityBar,
             foundIndex,
             math.max(array.get(zoneLastActivityBar, foundIndex), originBar)
        )
 
    0
 
//====================================================================
// IMPORTANCE SCORE
//====================================================================
 
f_zoneScore(int index) =>
 
    int swings = array.get(zoneSwings, index)
    int interactions = array.get(zoneInteractions, index)
    int bounces = array.get(zoneBounces, index)
    int breaks = array.get(zoneBreaks, index)
 
    float rejection = array.get(zoneRejection, index)
 
    int lastActivity = array.get(zoneLastActivityBar, index)
    int age = bar_index - lastActivity
 
    float ageRatio = math.min(1.0, age * 1.0 / lookbackBars)
    float recencyScore = (1.0 - ageRatio) * recencyWeight
 
    float score = swings * swingWeight
    score += interactions * interactionWeight
    score += bounces * bounceWeight
 
    // A break still proves the zone mattered.
    // It does not erase the level.
    score += breaks * breakWeight
 
    score += rejection * rejectionWeight
    score += recencyScore
 
    score
 
//====================================================================
// ACTUAL RETEST RELIABILITY
//====================================================================
 
f_reliability(int index) =>
 
    int bounces = array.get(zoneBounces, index)
    int breaks = array.get(zoneBreaks, index)
 
    int outcomes = bounces + breaks
 
    float result = na
 
    if outcomes > 0
        result := bounces * 1.0 / outcomes
 
    result
 
//====================================================================
// CURRENT ROLE OF ZONE
//
// 1  = SUPPORT
// -1 = RESISTANCE
//====================================================================
 
f_zoneRole(int index, float referencePrice) =>
 
    float level = array.get(zoneLevels, index)
    float width = array.get(zoneWidths, index)
 
    float zoneTop = level + width
    float zoneBottom = level - width
 
    int role = 0
 
    if referencePrice > zoneTop
        role := 1
 
    else if referencePrice < zoneBottom
        role := -1
 
    else
        role := referencePrice >= level ? 1 : -1
 
    role
 
//====================================================================
// PROJECTED ZONE OVERLAP
//====================================================================
 
f_projectedOverlaps(float price, float width) =>
 
    bool overlaps = false
 
    int count = array.size(zoneLevels)
 
    if count > 0
 
        for i = 0 to count - 1
 
            float level = array.get(zoneLevels, i)
            float existingWidth = array.get(zoneWidths, i)
 
            float allowedDistance = width + existingWidth
 
            if math.abs(price - level) <= allowedDistance
                overlaps := true
                break
 
    overlaps
 
//====================================================================
// DRAW CONFIRMED ZONE
//====================================================================
 
f_drawZone(int index, string prefix, color zoneColor, int leftBar) =>
 
    float level = array.get(zoneLevels, index)
    float width = array.get(zoneWidths, index)
 
    float zoneTop = level + width
    float zoneBottom = level - width
 
    box zoneBox = box.new(
         left=leftBar,
         top=zoneTop,
         right=bar_index + 1,
         bottom=zoneBottom,
         xloc=xloc.bar_index,
         extend=extend.right,
         border_color=zoneColor,
         border_width=1,
         bgcolor=color.new(zoneColor, zoneTransparency)
    )
 
    array.push(drawnBoxes, zoneBox)
 
    if showLabels
 
        string labelText =
             prefix +
             " " +
             str.tostring(level, format.mintick)
 
        if showStats
 
            int swings = array.get(zoneSwings, index)
            int bounces = array.get(zoneBounces, index)
            int breaks = array.get(zoneBreaks, index)
 
            float reliability = f_reliability(index)
 
            labelText :=
                 labelText +
                 " | S" +
                 str.tostring(swings) +
                 " B" +
                 str.tostring(bounces) +
                 " X" +
                 str.tostring(breaks)
 
            if not na(reliability)
 
                labelText :=
                     labelText +
                     " " +
                     str.tostring(reliability * 100.0, "#") +
                     "%"
 
        label zoneLabel = label.new(
             bar_index + 1,
             level,
             labelText,
             xloc=xloc.bar_index,
             yloc=yloc.price,
             style=label.style_label_left,
             color=zoneColor,
             textcolor=color.white,
             size=size.tiny
        )
 
        array.push(drawnLabels, zoneLabel)
 
    0
 
//====================================================================
// ATR REVERSAL SWING ENGINE
//
// 1  = DEVELOPING UPSWING
// -1 = DEVELOPING DOWNSWING
//====================================================================
 
var int swingDirection = 0
var float projectedPrice = na
var float projectedAtr = na
var int projectedBar = na
 
if barstate.isconfirmed and not na(atrValue)
 
    //================================================================
    // INITIALIZE
    //================================================================
 
    if swingDirection == 0
 
        if not na(close[1])
 
            swingDirection :=
                 close >= close[1] ? 1 : -1
 
            if swingDirection == 1
                projectedPrice := high
            else
                projectedPrice := low
 
            projectedAtr := atrValue
            projectedBar := bar_index
 
    //================================================================
    // DEVELOPING UPSWING
    //================================================================
 
    else if swingDirection == 1
 
        if na(projectedPrice) or high >= projectedPrice
 
            projectedPrice := high
            projectedAtr := atrValue
            projectedBar := bar_index
 
        float reversalThreshold =
             atrValue * swingReversalAtr
 
        float reversalDistance =
             projectedPrice - close
 
        // Confirm swing high
        if reversalDistance >= reversalThreshold
 
            float confirmedAtr =
                 math.max(
                     nz(projectedAtr, atrValue),
                     syminfo.mintick * 20.0
                 )
 
            float confirmedWidth =
                 math.max(
                     confirmedAtr * zoneHalfAtrMult,
                     syminfo.mintick * 2.0
                 )
 
            float mergeDistance =
                 math.max(
                     confirmedAtr * mergeAtrMult,
                     syminfo.mintick * 4.0
                 )
 
            f_addSwing(
                 projectedPrice,
                 confirmedWidth,
                 projectedBar,
                 mergeDistance
            )
 
            swingDirection := -1
            projectedPrice := low
            projectedAtr := atrValue
            projectedBar := bar_index
 
    //================================================================
    // DEVELOPING DOWNSWING
    //================================================================
 
    else if swingDirection == -1
 
        if na(projectedPrice) or low <= projectedPrice
 
            projectedPrice := low
            projectedAtr := atrValue
            projectedBar := bar_index
 
        float reversalThreshold =
             atrValue * swingReversalAtr
 
        float reversalDistance =
             close - projectedPrice
 
        // Confirm swing low
        if reversalDistance >= reversalThreshold
 
            float confirmedAtr =
                 math.max(
                     nz(projectedAtr, atrValue),
                     syminfo.mintick * 20.0
                 )
 
            float confirmedWidth =
                 math.max(
                     confirmedAtr * zoneHalfAtrMult,
                     syminfo.mintick * 2.0
                 )
 
            float mergeDistance =
                 math.max(
                     confirmedAtr * mergeAtrMult,
                     syminfo.mintick * 4.0
                 )
 
            f_addSwing(
                 projectedPrice,
                 confirmedWidth,
                 projectedBar,
                 mergeDistance
            )
 
            swingDirection := 1
            projectedPrice := high
            projectedAtr := atrValue
            projectedBar := bar_index
 
//====================================================================
// EVENT ENGINE
//
// OUTSIDE -> ENTER ZONE -> BOUNCE OR BREAK
//====================================================================
 
if barstate.isconfirmed and not na(atrValue)
 
    float currentAtrEvents =
         math.max(
             atrValue,
             syminfo.mintick * 20.0
         )
 
    int count = array.size(zoneLevels)
 
    if count > 0
 
        for i = 0 to count - 1
 
            float level =
                 array.get(
                     zoneLevels,
                     i
                 )
 
            float width =
                 array.get(
                     zoneWidths,
                     i
                 )
 
            float zoneTop =
                 level + width
 
            float zoneBottom =
                 level - width
 
            int inside =
                 array.get(
                     zoneInside,
                     i
                 )
 
            int entrySide =
                 array.get(
                     zoneEntrySide,
                     i
                 )
 
            int lastInteraction =
                 array.get(
                     zoneLastInteractionBar,
                     i
                 )
 
            bool overlaps =
                 high >= zoneBottom and
                 low <= zoneTop
 
            bool cooldownPassed =
                 bar_index - lastInteraction >= eventCooldown
 
            //========================================================
            // NEW VISIT
            //========================================================
 
            if inside == 0 and overlaps and cooldownPassed
 
                int approachSide = 0
 
                // Approached from above = testing support
                if not na(close[1]) and close[1] > zoneTop
                    approachSide := 1
 
                // Approached from below = testing resistance
                if not na(close[1]) and close[1] < zoneBottom
                    approachSide := -1
 
                if approachSide != 0
 
                    array.set(zoneInside, i, 1)
                    array.set(zoneEntrySide, i, approachSide)
 
                    array.set(
                         zoneInteractions,
                         i,
                         array.get(zoneInteractions, i) + 1
                    )
 
                    array.set(
                         zoneLastInteractionBar,
                         i,
                         bar_index
                    )
 
                    array.set(
                         zoneLastActivityBar,
                         i,
                         bar_index
                    )
 
                    //================================================
                    // SAME-BAR SUPPORT BOUNCE
                    //================================================
 
                    if approachSide == 1 and close > zoneTop
 
                        float rejectionStrength =
                             math.min(
                                 math.max(
                                     (close - low) /
                                     currentAtrEvents,
                                     0.0
                                 ),
                                 3.0
                             )
 
                        array.set(
                             zoneBounces,
                             i,
                             array.get(zoneBounces, i) + 1
                        )
 
                        array.set(
                             zoneRejection,
                             i,
                             array.get(zoneRejection, i) +
                             rejectionStrength
                        )
 
                        array.set(zoneInside, i, 0)
                        array.set(zoneEntrySide, i, 0)
 
                    //================================================
                    // SAME-BAR SUPPORT BREAK
                    //================================================
 
                    else if approachSide == 1 and close < zoneBottom
 
                        array.set(
                             zoneBreaks,
                             i,
                             array.get(zoneBreaks, i) + 1
                        )
 
                        array.set(zoneInside, i, 0)
                        array.set(zoneEntrySide, i, 0)
 
                    //================================================
                    // SAME-BAR RESISTANCE BOUNCE
                    //================================================
 
                    else if approachSide == -1 and close < zoneBottom
 
                        float rejectionStrength =
                             math.min(
                                 math.max(
                                     (high - close) /
                                     currentAtrEvents,
                                     0.0
                                 ),
                                 3.0
                             )
 
                        array.set(
                             zoneBounces,
                             i,
                             array.get(zoneBounces, i) + 1
                        )
 
                        array.set(
                             zoneRejection,
                             i,
                             array.get(zoneRejection, i) +
                             rejectionStrength
                        )
 
                        array.set(zoneInside, i, 0)
                        array.set(zoneEntrySide, i, 0)
 
                    //================================================
                    // SAME-BAR RESISTANCE BREAK
                    //================================================
 
                    else if approachSide == -1 and close > zoneTop
 
                        array.set(
                             zoneBreaks,
                             i,
                             array.get(zoneBreaks, i) + 1
                        )
 
                        array.set(zoneInside, i, 0)
                        array.set(zoneEntrySide, i, 0)
 
            //========================================================
            // EXISTING VISIT WAITING FOR OUTCOME
            //========================================================
 
            else if inside == 1
 
                //====================================================
                // SUPPORT TEST
                //====================================================
 
                if entrySide == 1
 
                    // Bounced back above
                    if close > zoneTop
 
                        float rejectionStrength =
                             math.min(
                                 math.max(
                                     (close - low) /
                                     currentAtrEvents,
                                     0.0
                                 ),
                                 3.0
                             )
 
                        array.set(
                             zoneBounces,
                             i,
                             array.get(zoneBounces, i) + 1
                        )
 
                        array.set(
                             zoneRejection,
                             i,
                             array.get(zoneRejection, i) +
                             rejectionStrength
                        )
 
                        array.set(zoneInside, i, 0)
                        array.set(zoneEntrySide, i, 0)
 
                        array.set(
                             zoneLastActivityBar,
                             i,
                             bar_index
                        )
 
                    // Broke underneath
                    else if close < zoneBottom
 
                        array.set(
                             zoneBreaks,
                             i,
                             array.get(zoneBreaks, i) + 1
                        )
 
                        array.set(zoneInside, i, 0)
                        array.set(zoneEntrySide, i, 0)
 
                        array.set(
                             zoneLastActivityBar,
                             i,
                             bar_index
                        )
 
                //====================================================
                // RESISTANCE TEST
                //====================================================
 
                else if entrySide == -1
 
                    // Rejected back underneath
                    if close < zoneBottom
 
                        float rejectionStrength =
                             math.min(
                                 math.max(
                                     (high - close) /
                                     currentAtrEvents,
                                     0.0
                                 ),
                                 3.0
                             )
 
                        array.set(
                             zoneBounces,
                             i,
                             array.get(zoneBounces, i) + 1
                        )
 
                        array.set(
                             zoneRejection,
                             i,
                             array.get(zoneRejection, i) +
                             rejectionStrength
                        )
 
                        array.set(zoneInside, i, 0)
                        array.set(zoneEntrySide, i, 0)
 
                        array.set(
                             zoneLastActivityBar,
                             i,
                             bar_index
                        )
 
                    // Broke above
                    else if close > zoneTop
 
                        array.set(
                             zoneBreaks,
                             i,
                             array.get(zoneBreaks, i) + 1
                        )
 
                        array.set(zoneInside, i, 0)
                        array.set(zoneEntrySide, i, 0)
 
                        array.set(
                             zoneLastActivityBar,
                             i,
                             bar_index
                        )
 
//====================================================================
// REMOVE STALE ZONES
//====================================================================
 
int pruneIndex =
     array.size(zoneLevels) - 1
 
while pruneIndex >= 0
 
    int lastActivity =
         array.get(
             zoneLastActivityBar,
             pruneIndex
         )
 
    int age =
         bar_index - lastActivity
 
    if age > lookbackBars
        f_removeZone(pruneIndex)
 
    pruneIndex -= 1
 
//====================================================================
// LIMIT STORED ZONES
//====================================================================
 
while array.size(zoneLevels) > maxStoredZones
 
    int weakestIndex = 0
 
    float weakestScore =
         f_zoneScore(0)
 
    int count =
         array.size(zoneLevels)
 
    if count > 1
 
        for i = 1 to count - 1
 
            float candidateScore =
                 f_zoneScore(i)
 
            if candidateScore < weakestScore
 
                weakestIndex := i
                weakestScore := candidateScore
 
    f_removeZone(weakestIndex)
 
//====================================================================
// DRAW
//====================================================================
 
if barstate.islast
 
    //================================================================
    // DELETE OLD DRAWINGS
    //================================================================
 
    if array.size(drawnBoxes) > 0
 
        for i = 0 to array.size(drawnBoxes) - 1
            box.delete(array.get(drawnBoxes, i))
 
    array.clear(drawnBoxes)
 
    if array.size(drawnLabels) > 0
 
        for i = 0 to array.size(drawnLabels) - 1
            label.delete(array.get(drawnLabels, i))
 
    array.clear(drawnLabels)
 
    //================================================================
    // USE MOST RECENT CLOSED BAR
    //================================================================
 
    float referencePrice =
         barstate.isconfirmed ?
         close :
         close[1]
 
    float currentAtrDraw =
         barstate.isconfirmed ?
         atrValue :
         atrValue[1]
 
    currentAtrDraw :=
         math.max(
             nz(
                 currentAtrDraw,
                 syminfo.mintick * 20.0
             ),
             syminfo.mintick * 20.0
         )
 
    int leftBar =
         math.max(
             0,
             bar_index - displayBars
         )
 
    float majorMaxDistance =
         currentAtrDraw *
         majorMaxDistanceAtr
 
    //================================================================
    // DEVELOPING LIVE SWING
    //================================================================
 
    if showLiveZone and swingDirection != 0 and not na(projectedPrice)
 
        float projectedBaseAtr =
             math.max(
                 nz(
                     projectedAtr,
                     currentAtrDraw
                 ),
                 syminfo.mintick * 20.0
             )
 
        float projectedWidth =
             math.max(
                 projectedBaseAtr *
                 zoneHalfAtrMult,
                 syminfo.mintick * 2.0
             )
 
        bool overlapsConfirmed =
             f_projectedOverlaps(
                 projectedPrice,
                 projectedWidth
             )
 
        if not overlapsConfirmed
 
            color projectedColor =
                 swingDirection == 1 ?
                 resistanceColor :
                 supportColor
 
            float projectedTop =
                 projectedPrice +
                 projectedWidth
 
            float projectedBottom =
                 projectedPrice -
                 projectedWidth
 
            box projectedBox = box.new(
                 left=leftBar,
                 top=projectedTop,
                 right=bar_index + 1,
                 bottom=projectedBottom,
                 xloc=xloc.bar_index,
                 extend=extend.right,
                 border_color=projectedColor,
                 border_width=2,
                 bgcolor=color.new(
                     projectedColor,
                     liveTransparency
                 )
            )
 
            array.push(
                 drawnBoxes,
                 projectedBox
            )
 
            if showLabels
 
                string projectedLabelText =
                     swingDirection == 1 ?
                     "LIVE RES " :
                     "LIVE SUP "
 
                projectedLabelText :=
                     projectedLabelText +
                     str.tostring(
                         projectedPrice,
                         format.mintick
                     )
 
                label projectedLabel = label.new(
                     bar_index + 1,
                     projectedPrice,
                     projectedLabelText,
                     xloc=xloc.bar_index,
                     yloc=yloc.price,
                     style=label.style_label_left,
                     color=projectedColor,
                     textcolor=color.white,
                     size=size.tiny
                )
 
                array.push(
                     drawnLabels,
                     projectedLabel
                )
 
    //================================================================
    // CONFIRMED ZONES
    //================================================================
 
    int zoneCount =
         array.size(zoneLevels)
 
    if zoneCount > 0
 
        array<bool> used =
             array.new<bool>(
                 zoneCount,
                 false
             )
 
        //============================================================
        // NEAREST SUPPORT
        //============================================================
 
        for slot = 0 to nearestZonesEachSide - 1
 
            int bestIndex = -1
            float bestDistance = 1e20
 
            for i = 0 to zoneCount - 1
 
                if not array.get(used, i)
 
                    int role =
                         f_zoneRole(
                             i,
                             referencePrice
                         )
 
                    if role == 1
 
                        float level =
                             array.get(
                                 zoneLevels,
                                 i
                             )
 
                        float width =
                             array.get(
                                 zoneWidths,
                                 i
                             )
 
                        float zoneTop =
                             level + width
 
                        float distance =
                             math.max(
                                 referencePrice -
                                 zoneTop,
                                 0.0
                             )
 
                        if distance < bestDistance
 
                            bestDistance := distance
                            bestIndex := i
 
            if bestIndex != -1
 
                array.set(
                     used,
                     bestIndex,
                     true
                )
 
                f_drawZone(
                     bestIndex,
                     "NEAR SUP",
                     supportColor,
                     leftBar
                )
 
        //============================================================
        // NEAREST RESISTANCE
        //============================================================
 
        for slot = 0 to nearestZonesEachSide - 1
 
            int bestIndex = -1
            float bestDistance = 1e20
 
            for i = 0 to zoneCount - 1
 
                if not array.get(used, i)
 
                    int role =
                         f_zoneRole(
                             i,
                             referencePrice
                         )
 
                    if role == -1
 
                        float level =
                             array.get(
                                 zoneLevels,
                                 i
                             )
 
                        float width =
                             array.get(
                                 zoneWidths,
                                 i
                             )
 
                        float zoneBottom =
                             level - width
 
                        float distance =
                             math.max(
                                 zoneBottom -
                                 referencePrice,
                                 0.0
                             )
 
                        if distance < bestDistance
 
                            bestDistance := distance
                            bestIndex := i
 
            if bestIndex != -1
 
                array.set(
                     used,
                     bestIndex,
                     true
                )
 
                f_drawZone(
                     bestIndex,
                     "NEAR RES",
                     resistanceColor,
                     leftBar
                )
 
        //============================================================
        // MAJOR SUPPORT
        //============================================================
 
        if majorZonesEachSide > 0
 
            for slot = 0 to majorZonesEachSide - 1
 
                int bestIndex = -1
                float bestScore = -1e20
 
                for i = 0 to zoneCount - 1
 
                    if not array.get(used, i)
 
                        int role =
                             f_zoneRole(
                                 i,
                                 referencePrice
                             )
 
                        float level =
                             array.get(
                                 zoneLevels,
                                 i
                             )
 
                        float distance =
                             referencePrice -
                             level
 
                        bool usefulDistance =
                             distance >= 0 and
                             distance <= majorMaxDistance
 
                        if role == 1 and usefulDistance
 
                            float score =
                                 f_zoneScore(i)
 
                            if score > bestScore
 
                                bestScore := score
                                bestIndex := i
 
                if bestIndex != -1
 
                    array.set(
                         used,
                         bestIndex,
                         true
                    )
 
                    f_drawZone(
                         bestIndex,
                         "MAJOR SUP",
                         supportColor,
                         leftBar
                    )
 
        //============================================================
        // MAJOR RESISTANCE
        //============================================================
 
        if majorZonesEachSide > 0
 
            for slot = 0 to majorZonesEachSide - 1
 
                int bestIndex = -1
                float bestScore = -1e20
 
                for i = 0 to zoneCount - 1
 
                    if not array.get(used, i)
 
                        int role =
                             f_zoneRole(
                                 i,
                                 referencePrice
                             )
 
                        float level =
                             array.get(
                                 zoneLevels,
                                 i
                             )
 
                        float distance =
                             level -
                             referencePrice
 
                        bool usefulDistance =
                             distance >= 0 and
                             distance <= majorMaxDistance
 
                        if role == -1 and usefulDistance
 
                            float score =
                                 f_zoneScore(i)
 
                            if score > bestScore
 
                                bestScore := score
                                bestIndex := i
 
                if bestIndex != -1
 
                    array.set(
                         used,
                         bestIndex,
                         true
                    )
 
                    f_drawZone(
                         bestIndex,
                         "MAJOR RES",
                         resistanceColor,
                         leftBar
                    )

← 返回列表