How to get the candle width to use it as width for a DrawTrendLine ?
How to get the candle width to use it as width for a DrawTrendLine ?
16 Apr 2025, 01:40
Hi,
I use an indicator to draw Heikin Ashi candle for a tick chart. The indicator use a fixed width that is not correct when the chart is zooming dezooming.
I try some combination to use a fixed value depending on the number of candles visible but it is not accurate when the window is small or bigger…it depend on the total pixel size of the chart.
So, is there a way to get the actual candle pixel width or a way to calculate it ?
Thanks
Luc
Replies
JasperWalter99
18 Apr 2025, 03:46
( Updated at: 22 Apr 2025, 23:14 )
In TradingView's Pine Script, you cannot directly get the pixel width of a candle, but you can calculate the candle width using the parameters of the number of candles on the time axis and the window width.
You can Calculates the width of each candle based on the time interval between candles and the current window size ratio.
// Get chart widthint chart_width = WindowWidth();// Get number of bars that can be displayed on the window (based on the time span of visible candles)int bars_on_screen = (Time[0] - Time[Bars-1]) / PeriodSeconds();// Calculate pixel width of the candleint candle_width = chart_width / bars_on_screen;// Print the candle widthPrint("Candle width: ", candle_width, " pixels");
Here Geometry Vibes:
WindowWidth() returns the width of the chart window.
PeriodSeconds() gives the time span between each candle.
@JasperWalter99
firemyst
17 Apr 2025, 00:48
Nope.
If you want to draw candles, draw a vertical trend line for the wicks. That way, you can adjust the width.
@firemyst