I have a historical set of data that I need to show as candles in tradingView. For this, I’m thinking of store in arrays each open,high,low and close data and next use plotCandle function to plot the data. The thing is that I haven’t found how to loop that arrays to use plotCandle on each iteration and synchronize the data with the date it was generated.
How can I do this ?
I tried this as an example:
//@version=5
indicator("CandlesTest", overlay=true)
// Configuración de la gráfica
symbol = "BTCUSDT"
interval = "5m"
var float [] openArray = array.new<float>()
array.push(openArray,63468.00000000)
array.push(openArray,63527.47000000)
array.push(openArray,63464.01000000)
var float [] high_Array = array.new<float>()
array.push(high_Array,63528.62000000)
array.push(high_Array,63550.00000000)
array.push(high_Array,63490.09000000)
var float [] low_Array = array.new<float>()
array.push(low_Array,63467.99000000)
array.push(low_Array,63464.00000000)
array.push(low_Array,63447.36000000)
var float [] closeArray = array.new<float>()
array.push(closeArray,63527.48000000)
array.push(closeArray,63464.00000000)
array.push(closeArray,63457.99000000)
//date = input.time(timestamp("19 Feb 2024 00:00 +0000"), "Shift Origin To", tooltip = 'When enabled use this offset for origin bar of data range.', inline = '1')
// Fecha de inicio
var fechaInicio = input.time(defval = timestamp("10 Sep 2024 00:00 +0000"), title = "Initial data", tooltip = "Init data")
// Declarar variables
var float openPrice = na
var float highPrice = na
var float lowPrice = na
var float closePrice = na
var color colorAlcista = na
var color colorBajista = na
var color colorNeutro = na
//definir colores
colorAlcista := closePrice > openPrice ? color.green : na
colorBajista := closePrice < openPrice ? color.red : na
colorNeutro := closePrice == openPrice ? color.gray : na
var Painting = time >= fechaInicio ? true : false
for i = 0 to 2 by 1
openPrice := array.get(openArray,i)
highPrice := array.get(high_Array,i)
lowPrice := array.get(low_Array,i)
closePrice := array.get(closeArray,i)
// This line throws the error
plotcandle(Painting?openPrice: na,highPrice,lowPrice,closePrice,color=colorAlcista,wickcolor =colorAlcista,bordercolor = colorAlcista, display = display.none)
//plotcandle(Painting?openPrice:na,Painting?highPrice:na,Painting?lowPrice:na,Painting?closePrice:na,color=Painting?colorBajista : na,wickcolor = Painting?colorBajista:na,bordercolor = Painting?colorBajista:na)
//plotcandle(Painting?openPrice:na,Painting?highPrice:na,Painting?lowPrice:na,Painting?closePrice:na,color=Painting?colorNeutro : na,wickcolor = Painting?colorNeutro:na,bordercolor = Painting?colorNeutro:na)
This code doesn’t works because of the Cannot use ‘plotcandle’ in local scope error