Average Daily Range (ADR) Levels

EricaLorrai · study · 247 行 · 点赞 6 · TradingView 原页

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

Pine Script

//@version=6
indicator("ADR by Trade Tribe HQ", shorttitle="ADR", overlay=true)
 
//=============================================================================
// INPUTS
//=============================================================================
 
adrLength = input.int(
     14,
     title="ADR Length",
     minval=1,
     group="ADR Settings"
)
 
showRemaining = input.bool(
     true,
     title="Show Remaining %",
     group="ADR Settings"
)
 
tablePositionInput = input.string(
     "Top Right",
     title="Table Position",
     options=[
         "Top Right",
         "Top Left",
         "Bottom Right",
         "Bottom Left"
     ],
     group="Table Settings"
)
 
tableTextSizeInput = input.string(
     "Normal",
     title="Text Size",
     options=[
         "Small",
         "Normal",
         "Large"
     ],
     group="Table Settings"
)
 
//=============================================================================
// TABLE POSITION
//=============================================================================
 
tablePosition =
     tablePositionInput == "Top Left" ? position.top_left :
     tablePositionInput == "Bottom Right" ? position.bottom_right :
     tablePositionInput == "Bottom Left" ? position.bottom_left :
     position.top_right
 
tableTextSize =
     tableTextSizeInput == "Small" ? size.small :
     tableTextSizeInput == "Large" ? size.large :
     size.normal
 
//=============================================================================
// PIP SIZE
//=============================================================================
 
// Forex pip size:
// Most pairs = 0.0001
// JPY pairs  = 0.01
//
// TradingView commonly quotes forex with fractional pips,
// so syminfo.mintick is often 0.00001 or 0.001.
 
bool isForex = syminfo.type == "forex"
 
bool isJPYPair =
     str.contains(syminfo.ticker, "JPY")
 
float pipSize =
     isForex ?
         (isJPYPair ? 0.01 : 0.0001) :
         syminfo.mintick
 
//=============================================================================
// DAILY DATA
//=============================================================================
 
// Previous completed daily candle ranges.
// Using [1] keeps the current unfinished day out of the ADR average.
 
float dailyADRPrice = request.security(
     syminfo.tickerid,
     "D",
     ta.sma(high - low, adrLength)[1],
     lookahead=barmerge.lookahead_on
)
 
// Current day's running high and low.
 
float currentDayHigh = request.security(
     syminfo.tickerid,
     "D",
     high,
     lookahead=barmerge.lookahead_on
)
 
float currentDayLow = request.security(
     syminfo.tickerid,
     "D",
     low,
     lookahead=barmerge.lookahead_on
)
 
//=============================================================================
// ADR CALCULATIONS
//=============================================================================
 
float adrPips =
     dailyADRPrice / pipSize
 
float currentRangePrice =
     currentDayHigh - currentDayLow
 
float currentRangePips =
     currentRangePrice / pipSize
 
float adrUsedPercent =
     not na(dailyADRPrice) and dailyADRPrice > 0
     ? (currentRangePrice / dailyADRPrice) * 100
     : na
 
float adrRemainingPercent =
     not na(adrUsedPercent)
     ? math.max(100 - adrUsedPercent, 0)
     : na
 
//=============================================================================
// DISPLAY VALUES
//=============================================================================
 
string adrText =
     not na(adrPips)
     ? str.tostring(math.round(adrPips)) + " pips"
     : "—"
 
string usedText =
     not na(adrUsedPercent)
     ? str.tostring(math.round(adrUsedPercent)) + "%"
     : "—"
 
string remainingText =
     not na(adrRemainingPercent)
     ? str.tostring(math.round(adrRemainingPercent)) + "%"
     : "—"
 
//=============================================================================
// TABLE
//=============================================================================
 
int tableColumns =
     showRemaining ? 3 : 2
 
var table adrTable =
     table.new(
         tablePosition,
         3,
         2,
         border_width=1,
         frame_width=1
     )
 
if barstate.islast
 
    // Clear old cells before rebuilding.
    table.clear(
         adrTable,
         0,
         0,
         2,
         1
     )
 
    //------------------------------------------------------------------------
    // HEADER ROW
    //------------------------------------------------------------------------
 
    table.cell(
         adrTable,
         0,
         0,
         "ADR",
         text_color=color.white,
         bgcolor=color.new(color.black, 15),
         text_size=tableTextSize
     )
 
    table.cell(
         adrTable,
         1,
         0,
         "Used",
         text_color=color.white,
         bgcolor=color.new(color.black, 15),
         text_size=tableTextSize
     )
 
    if showRemaining
        table.cell(
             adrTable,
             2,
             0,
             "Remaining",
             text_color=color.white,
             bgcolor=color.new(color.black, 15),
             text_size=tableTextSize
         )
 
    //------------------------------------------------------------------------
    // VALUE ROW
    //------------------------------------------------------------------------
 
    table.cell(
         adrTable,
         0,
         1,
         adrText,
         text_color=color.white,
         bgcolor=color.new(color.black, 35),
         text_size=tableTextSize
     )
 
    table.cell(
         adrTable,
         1,
         1,
         usedText,
         text_color=color.white,
         bgcolor=color.new(color.black, 35),
         text_size=tableTextSize
     )
 
    if showRemaining
        table.cell(
             adrTable,
             2,
             1,
             remainingText,
             text_color=color.white,
             bgcolor=color.new(color.black, 35),
             text_size=tableTextSize
         )

← 返回列表