Thinking in Pine - Study References Too Many Bars In the HistoryWelcome to "Thinking in Pine" short video series on the topics of Pine Script.
Today's topic is handling the error - "The study references too many candles in the history" and other bar_index related issues.
If you are not familiar with Pine Script time series concepts, please go through our previous videos.
Thinking in Pine - Time Series
Thinking in Pine - Special Cases of Time Series
🎲 Points Discussed
When do we get the error "Study references too many candles in the history" and how to overcome this issue.
bar_index limitations with drawing objects on the chart.
🎯 Example Program - Historical Reference Alternative Implementation
// When we are trying to refer to history more than or equal to 10000 bars older
// float value = close
// plot(value)
// Alternative implementation
var values = array.new()
values.unshift(close)
// Throws error on first bar since the number of items is less than 10000
// float valueFromArray = values.get(10000)
// plot(valueFromArray)
//Option 1 - Get the last available value when the number of bars available is less than 10000
float valueFromArray1 = values.get(math.min(bar_index, 10000))
plot(valueFromArray1)
// Option 2 - If number of available bars less than 10000, then set value to na else get the value of 10000 bars back
float valueFromArray2 = values.size() <= 10000? na : values.get(10000)
plot(valueFromArray2)
🎯 Example Program - Drawing Object Limitations with Bar Index
// Trying to create a line too far in history or in future
// if(barstate.islast)
// Throws error as can only draw upto 9999 bars before
// ln1 = line.new(bar_index, high, bar_index-10000, high)
// Throws error as we can only draw upto 500 bars in the future.
// ln2 = line.new(bar_index, high, bar_index+501, high)
startingPoint = ta.valuewhen(bar_index == last_bar_index-10000, time, 0)
float price = 0.0
if(barstate.islast)
// However, we can draw more than 10000 bars back or more than 500 bars in the future using time instead of bar _index
ln = line.new(time, high, startingPoint, high, xloc=xloc.bar_time)
// Cannot use line.get_price when the line is drawn using xloc = xloc.bar_time
// price := ln.get_price(last_bar_index-5000)
Thinkinginpine
Thinking in Pine - Tricks and Tips of DebuggingWelcome to "Thinking in Pine" short video series on the topics of Pine Script.
Today's discussion point is debugging and tools that Pine script provide us to debug our programs.
🎲 Points Discussed
Debugging using `plot` for numerical series values.
Using Pine Logs to debug for cases where `plot` is not suitable
🎯 Example Program - Debugging using plot
length = input.int(14, 'Length')
multiplier = input.float(4, 'Multiplier')
var dir = 1
var stopDistance = ta.atr(length) * multiplier
// Remove var to fix the first issue
// stopDistance = ta.atr(length) * multiplier
buyStopCurrent = close-stopDistance
sellStopCurrent = close + stopDistance
var buyStop = buyStopCurrent
var sellStop = sellStopCurrent
buyStop := dir > 0? math.max(buyStop, buyStopCurrent) : buyStopCurrent
sellStop := dir < 0? math.min(sellStop, sellStopCurrent) : sellStopCurrent
// Use nz to overcome na issues with math.max/ math.min
// buyStop := dir > 0? math.max(nz(buyStop, buyStopCurrent), buyStopCurrent) : buyStopCurrent
// sellStop := dir < 0? math.min(nz(sellStop, sellStopCurrent), sellStopCurrent) : sellStopCurrent
dir := dir == 1 and close < buyStop? -1 : dir == -1 and close > sellStop ? 1 : dir
// Plot statements used for debugging. display is set to display.data_window as the debugging plots do not need to appear on the charts
plot(stopDistance, 'Stop Distance', color.purple, display = display.data_window)
plot(buyStopCurrent, 'buyStopCurrent', color.red, display = display.data_window)
plot(sellStopCurrent, 'sellStopCurrent', color.green, display = display.data_window)
plot(buyStop, 'buyStop', color.red, display = display.data_window)
plot(sellStop, 'sellStop', color.green, display = display.data_window)
plot(dir, 'Direction', color.white, display = display.data_window)
plot(dir > 0? buyStop : sellStop, 'Supertrend', dir > 0? color.red : color.green)
🎯 Example Program - Pine Logs
sum = 0
arr = array.new()
for i = 0 to 10
sum+=i
// Log on every bar and also on every tick on real time
// log.info('Sum at iteration {0} is {1}', i, sum)
// Run only for the first bar
// if(barstate.isfirst)
// log.info('Sum at iteration {0} is {1}', i, sum)
// Run on the last confirmed bar
// if(barstate.islastconfirmedhistory)
// log.warning('Sum at iteration {0} is {1}', i, sum)
// if(barstate.isrealtime)
// log.warning('Sum at iteration {0} is {1}', i, sum)
// Display only once
// varip showLog = true
// if(barstate.isrealtime and showLog)
// log.warning('Sum at iteration {0} is {1}', i, sum)
// if (i == 10)
// showLog := false
// Extract through the array and log outside the loop
arr.push(sum)
// Log the extracted array on real time and print only once
varip showLog = true
if(barstate.isrealtime and showLog)
log.error('Sum at different iterations : {0}', arr)
showLog := false
plot(sum)
🎲 References
Pine Logs - Reference Manual
plot - Reference Manual
Thinking in Pine - Execution Model and Rollback on Realtime BarsHello All,
Welcome to another session of "Thinking in Pine" - short video tutorials on Pine Script.
Before continuing with this video, if you are not familiar with var, varip and regular variables, please watch our previous video - "Thinking in Pine - var, varip and regular variables"
🎲 Today's discussion points
How var, varip and regular variable modification code works with historical and real time bar updates.
Rollback concept of var variables
🎯 Example Program Used
// The statements execute on every tick
count = 0.0
count+=1
varip varipcount = 0 //executes only once on the first bar
varipcount+=1
// Reset counter on every bar
// if(barstate.isconfirmed)
// varipcount:=0
// Rollbacks and assigns on every tick
var varcount = 0.0 //executes only once on the first bar
varcount+=1
// varcount:=varcount -- Rollback
// varcount := varcount + 1 -- execute again
plot(varipcount, 'Varip Count')
plot(varcount, 'Var Count')
plot(count, 'Çount')
arrRegular = array.new()
var arrVar = array.new()
varip arrVarip = array.new()
if(bar_index >= last_bar_index -5)
arrRegular.push(close)
arrVar.push(close)
arrVarip.push(close)
log.info('Regular : {0}', arrRegular)
log.info('Var : {0}', arrVar)
log.info('Varip : {0}', arrVarip)
🎲 References
Pine Script® User Manual - Execution Model
Thinking in Pine - Functions Containing Var VariablesHello everyone, welcome back to "Thinking in Pine" short video tutorials. In this video, we have discussed special cases of using var variables inside function definitions.
If you are not familiar with var variables, please take a step back and watch our earlier video - "Thinking in Pine - var, varip and regular variables"
🎲 Summary
Using var within a function scope and how it behaves with multiple invocations.
Using the functions containing var variable definitions within a loop.
🎯 Example Program Used
increment()=>
var i = 0
i+=1
var1 = increment()
var2 = increment()
var3 = increment()
// The code above is equivalent to
// var i1 = 0
// i1+=1
// var1 = i1
// var i2 = 0
// i2+=1
// var2 = i2
// var i3 = 0
// i3+=1
// var3 = i3
plot(var1, "Counter 1", color=color.blue)
plot(var2, "Counter 2", color=color.red)
plot(var3, "Counter 3", color=color.purple)
arr = array.from(var1, var2, var3)
for i=1 to 3
arr.push(increment())
// The code above is equivalent to
// var i4 = 0
// i4+=1
// arr.push(i4)
if(bar_index == 4)
log.info('Value of array containing incremental values : {0}', arr)
🎲 References
Pine Script® User Manual - Variable declarations
Pine Script® Reference Manual - var
Thinking in Pine - Time Series Special CasesHello Everyone,
Welcome back to "Thinking in Pine" short video series. In this session, we have discussed few special cases of time series variables and using historical operator within local scope.
If you have not watched our previous video - "Thinking in Pine - Time Series" , request you to do that before continuing this video.
🎲 Summary of our today's discussion
How historical operator works for variables defined inside an conditional block
How historical operator works for variables defined in a loop.
🎯 Example Program Used
// Time series for variables within a condition
varip showLogInLoop = true
if(bar_index%3 == 0)
specialBarIndex = bar_index
if(bar_index > last_bar_index-3 and showLogInLoop)
log.info('Current and Previous special bar index are : {0} and {1}', specialBarIndex, specialBarIndex )
showLogInLoop := false
// Time series of variables within a loop
arrayOfX = array.new()
arrayOfLastX = array.new()
for i = 1 to 5
x = i*10
arrayOfX.push(x)
arrayOfLastX.push(x )
if(barstate.islastconfirmedhistory)
log.info('Array of X : {0}', arrayOfX)
log.info('Array of last X : {0}', arrayOfLastX)
🎲 References:
Pine Script® User Manual - Execution Model
Pine Script® User Manual - Time Series
Pine Script® User Manual - History Referencing Operator
Pine Script® Reference Manual - History Referencing Operator
Thinking in Pine - Time SeriesHello everyone,
Welcome back to "Thinking in Pine" short video series. In this video, we discuss the concept of time series variables in Pinescript.
If you are not familiar with var and varip type of variables - please step back and watch this video before continuing - "Thinking in Pine - var, varip and regular variables"
🎲 Summary of our discussion is as follows
What are time series variables, and how are they used in Pinescript?
How do we access historical values of time series?
Limitations of accessing historical values
🎯 Example Program Used
currentBar = bar_index
var currentBarUsingVar = 0
currentBarUsingVar := bar_index
varip showLog = true
valueAt200thBar = ta.valuewhen(bar_index == 500, currentBar, 0)
if(barstate.islast and showLog)
log.info("Current Bar Values using regular and var variables : {0}, {1}", currentBar, currentBarUsingVar)
log.info("Last Bar Values using regular and var variables : {0}, {1}", currentBar , currentBarUsingVar )
log.info("Values 500 bars ago using regular and var variables : {0}, {1}", currentBar , currentBarUsingVar )
offset = bar_index-25000
log.info("Values at 25000th bar using regular and var variables : {0}, {1}", currentBar , currentBarUsingVar )
showLog := false
plot(bar_index, "Bar Index", color = color.blue, display = display.data_window)
There are pitfalls of using historical operators within loop or under if condition. We will discuss that in our next video.
🎲 References:
Pine Script® User Manual - Execution Model
Pine Script® User Manual - Time Series
Pine Script® User Manual - History Referencing Operator
]Pine Script® Reference Manual - History Referencing Operator