Chart VWAP

TradingView · study · 108 行 · 点赞 4,053 · TradingView 原页

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

Pine Script

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TradingView
 
//@version=5
indicator("Chart VWAP", overlay = true)
 
// Visible Chart Range
// v1 2022.07.11
 
// This code was written using the recommendations from the Pine Script™ User Manual's Style Guide:
//   https://www.tradingview.com/pine-script-docs/en/v5/writing/Style_guide.html
 
 
 
import PineCoders/VisibleChart/1 as PCvc
 
 
 
// ———————————————————— Constants and Inputs {
 
 
// Constants
string AVG_TT  = "Enables the average price line for all visible chart bars"
string OHLC_TT = "'Show chart OHLC levels' displays 4 lines at the open, high, low, and close of the visible chart range.
  \n\n'Show HL and % to Close values' displays values on the high and low levels when 'Show Chart OHLC' is also enabled.
  The percentage is the distance from the current price to the level."
 
// Inputs
bool showAvgInput   = input.bool(true,  "Show Bar Average",              tooltip = AVG_TT)
bool showOhlcInput  = input.bool(false, "Show chart OHLC levels ",       inline = "11")
bool showLevelInput = input.bool(true,  "Show HL and % to Close values", inline = "11", tooltip = OHLC_TT)
// }
 
 
 
// ———————————————————— Functions {
 
 
// @function            Sets a horizontal line to the visible chart width at a specified level.
// @param lineId        (line) The line identifier. 
// @param level         (series float) The price level for the line. 
// @returns             (void) Alters the left and right points of `lineId` to the visible chart width at a specified `level`.
setChartLine(line lineId, series float level) =>
    line.set_xy1(lineId, chart.left_visible_bar_time,  level)
    line.set_xy2(lineId, chart.right_visible_bar_time, level)
 
 
// @function            Calculates the percent difference between two prices.
// @param startPrice    (series float) The begining point to calculate percent from.
// @param endPrice      (series float) The end point to calculate percent to. 
// @returns             (float) A difference in percent between the `startPrice` and the `endPrice`
percent(series float startPrice, series float endPrice) => 
    float result = (startPrice - endPrice) / endPrice * 100
// }
 
 
 
// ———————————————————— Calculations {
 
 
// VWAP anchored on the leftmost visible bar.
float vwap = PCvc.vVwap()
 
// Average `close` of all visible bars.
float barAvg = PCvc.avg()
 
// OHLC values for the visible chart time range.
[chartOpen, chartHigh, chartLow, chartClose, chartVolume] = PCvc.ohlcv()
// }
 
 
 
// ———————————————————— Visuals {
 
 
// Plot the VWAP and avg bar price. 
plot(PCvc.barIsVisible() and showAvgInput ? barAvg : na, "Visible Avg.", color.new(color.gray, 50), 1)
plot(vwap, "Chart VWAP", color.orange, 2)
 
// Relative x-coordinate time for the price/percent label (25% of the visible space's width, starting from the left).
int labelTime  = PCvc.chartXTimePct(25)
 
// Show OHLC levels and HL values when enabled. 
if showOhlcInput and barstate.islast 
    // Declare drawing objects once and set later. 
    var line  chartHighLine  =  line.new(na, na, na, na, xloc.bar_time, extend.right, #4CAF5075,  line.style_solid,  1)
    var line  chartLowLine   =  line.new(na, na, na, na, xloc.bar_time, extend.right, #f2364580,  line.style_solid,  1)
    var line  chartOpenLine  =  line.new(na, na, na, na, xloc.bar_time, extend.right, color.gray, line.style_dotted, 1)
    var line  chartCloseLine =  line.new(na, na, na, na, xloc.bar_time, extend.right, #787B8675,  line.style_solid,  1)
    var label chartHighLabel = label.new(na, na, "",     xloc.bar_time, yloc.price,   color(na),  label.style_label_down, #4CAF5075)
    var label chartLowLabel  = label.new(na, na, "",     xloc.bar_time, yloc.price,   color(na),  label.style_label_up,   #f2364580)
    
    // OHLC lines.
    setChartLine(chartOpenLine,  chartOpen)
    setChartLine(chartHighLine,  chartHigh)
    setChartLine(chartLowLine,   chartLow)
    setChartLine(chartCloseLine, chartClose)
 
    // Display high, low and percent to `close` values when enabled.
    if showLevelInput
        string labelFormat = "{0} ({1, number, #.##}%)"
        // Reposition labels.
        label.set_xy(chartHighLabel, labelTime, chartHigh)
        label.set_xy(chartLowLabel,  labelTime, chartLow)
        // Update label values with each chart update.
        label.set_text(chartHighLabel, str.format(labelFormat, str.tostring(chartHigh, format.mintick), percent(chartHigh, chartClose)))
        label.set_text(chartLowLabel,  str.format(labelFormat, str.tostring(chartLow,  format.mintick), percent(chartLow,  chartClose)))
// }

← 返回列表