TradingView
Trendoscope
Dec 28, 2023 12:30 PM

Thinking in Pine - Time Series Special Cases Education

Bitcoin / TetherUSBinance

Description

Hello 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 -
, 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[1]) showLogInLoop := false // Time series of variables within a loop arrayOfX = array.new<int>() arrayOfLastX = array.new<int>() for i = 1 to 5 x = i*10 arrayOfX.push(x) arrayOfLastX.push(x[1]) if(barstate.islastconfirmedhistory) log.info('Array of X : {0}', arrayOfX) log.info('Array of last X : {0}', arrayOfLastX)


🎲 References:

Comments
basictradingtv
I watched the mid 3 minutes and it is certainly too complicated for me - good work though (probably lol)🤣🤣
Trendoscope
@basictradingtv, if you watch the previous video on time series basics - it may become easier to understand. Ultimately what you need to remember is not to use the historical referencing operator within if condition and for loop using any of the variables defined in local scope :)
MyCryptoParadise_Simon
For those new to this session, it’s a good idea to check out the previous video on time series in Pine Script to build a foundational understanding
Setupsfx_
Great work mate
Trendoscope
@Setupsfx_, thanks 🙏
TR421
Is it possible to find the most recent bar that matches the current value of input 1? And then fetch the value of input 2 from that historical bar for comparison to input 2 current value? Thanks
Trendoscope
@TR421, Can you explain further. Not able to understand the requirement.
TR421
Sorry, I’m curious if it is possible to have pine script identify the most recent bar where the historical value matches the current value (input source 1). Next step would be to retrieve the value of input 2 source from that same historical bar if possible. Thanks for the videos!
Trendoscope
@TR421, You can use ta.barssince to check when was the last bar same thing happened.
TR421
@Trendoscope thank you I appreciate it
More