本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © allanster
//@version=4
study(title = "Heikin-Ashi Source Function", shorttitle = "HASF", overlay = false)
// Custom source function for toggling traditional Candle sources or Heikin-Ashi sources on a traditional Candles chart.
// Thanks to PineCoders for rounding method: https://www.pinecoders.com/faq_and_code/#how-can-i-round-to-ticks
// Thanks to @LucF and @RicardoSantos for their advice and enlightenment as always.
hknAsh = input(defval = true, title = "Heikin Ashi", type = input.bool)
// === CANDLES ===
src(_src) =>
Close = not hknAsh ? close : ohlc4
Open = float(na)
Open := not hknAsh ? open : na(Open[1]) ? (open + close) / 2 : (nz(Open[1]) + nz(Close[1])) / 2
High = not hknAsh ? high : max(high, max(Open, Close))
Low = not hknAsh ? low : min(low, min(Open, Close))
HL2 = not hknAsh ? hl2 : avg(High, Low)
HLC3 = not hknAsh ? hlc3 : avg(High, Low, Close)
OHLC4 = not hknAsh ? ohlc4 : avg(Open, High, Low, Close)
Price = _src == 'close' ? Close : _src == 'open' ? Open : _src == 'high' ? High : _src == 'low' ? Low : _src == 'hl2' ? HL2 : _src == 'hlc3' ? HLC3 : OHLC4
Source = round(Price / syminfo.mintick) * syminfo.mintick // PineCoders method for aligning Pine prices with chart instrument prices
// === PLOTTING ===
preEvalO = src('open') // pre-Evaluate!
preEvalH = src('high') // pre-Evaluate!
preEvalL = src('low') // pre-Evaluate!
preEvalC = src('close') // pre-Evaluate!
upBar = preEvalC > preEvalO
upBarOpen = upBar ? preEvalO : na
upBarHigh = upBar ? preEvalH : na
upBarLow = upBar ? preEvalL : na
upBarClose = upBar ? preEvalC : na
dnBarOpen = upBar ? na : preEvalO
dnBarHigh = upBar ? na : preEvalH
dnBarLow = upBar ? na : preEvalL
dnBarClose = upBar ? na : preEvalC
plotcandle(upBarOpen, upBarHigh, upBarLow, upBarClose, title = "", color = #26a69a, wickcolor = #26a69a, bordercolor = #26a69a) // plot up bars
plotcandle(dnBarOpen, dnBarHigh, dnBarLow, dnBarClose, title = "", color = #ef5350, wickcolor = #ef5350, bordercolor = #ef5350) // plot dn bars