简体   繁体   中英

How to get close price for normal candles in Pine script while using Heikin Ashi candles?

I want to plot difference between current Heikin Ashi (HA) close price and real close price (on normal candles) in Pine script 3.

Seems like it can work if I use normal candles on chart but if I use Heikin Ashi, I get HA close price instead of normal one.

HAclose = security(heikinashi(tickerid), period, close)
NormalClose = security(tickerid, period, close)

plot(HAclose, offset=1, show_last=1)
plot(NormalClose, offset=1, show_last=1)

The code above is expected to draw two lines in front of last candle, showing HA close and normal close. It does if chart is set to normal candles, but on HA candles those have same value -- HA close. Same if I use just close .

Is there a way to explicitly address price on normal candles?

After reading the comments I was able to hack it using syminfo.prefix and ticker

//@version=3
study("Actual price for HA candles") //, overlay=false)
selected_interval = input(title="Interval", defval="D", type=resolution)
// selected_interval = tostring(interval)

actual_close = plot(security(syminfo.prefix + ":" + ticker, selected_interval, close), color=green)
HA_close = plot(security(tickerid, selected_interval, close), color=red)

fill(actual_close, HA_close, color=color(purple,0))

I tried to make the interval automatic as well, but I was not successful since theinterval variable only returns interval multiplier, so it behaved strangely on some timeframes.

I tested this on BITMEX:ETHUSD daily chart - if you have a look at 12th June 2019 with HA candles selected you can see that the actual close is 263.05 (green number) while the HA close is 254.00 (red number).

例子

realPrice = request.security(ticker.new(syminfo.prefix, syminfo.ticker), timeframe.period, close)

v5

Edit Explanation:

When on a Heikin Ashi chart the close price is Heikin Ashi calculation and not the close for normal candles. The security function returns the close for any ticker and any timeframe.

Example: Get the close on a AAPL chart on timeframe 5

realPrice = request.security("AAPL", "5", close)

Instead of typing in "AAPL" get the current ticker with:

currentTicker = ticker.new(syminfo.prefix, syminfo.ticker)

Instead of typing in "5" get the current timeframe with:

currentTimeframe = timeframe.period

Final:

realPrice = request.security(currentTicker, currentTimeframe, close)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM