HLF
Pine Script Rookie
Pine Script Rookie
Posts: 2
Joined: April 27th, 2021

Cumulative Total Count of Tick Movements in a Bar?

Hi everyone, I'm a bit stuck and looking for some help. I'm working on a script where I'm trying to count every tick movement, whether up or down, and show the cumulative total movement in a bar as a count. I'm stuck because so far all I can figure out how to do is calculate the range, but I want to count each individual up tick/down tick rather the the range within a candle. So everytime the price changes (no matter the direction) I want to add 1 to a count. I'm using this for ES with a minimum tick value of 0.25

The following code calculates the range just fine, but how would I modify it to count each up tick/down tick individually for a cumulative total within the candle? :

Code: Select all

//@version=4
study(title="Tick Travel", shorttitle="Tick Travel")
open_pos = open*1
high_pos = high*1
low_pos = low*1
highdiff = abs(high_pos-open_pos)
lowdiff = abs(low_pos-open_pos)
selected = max(highdiff, lowdiff)
pricelevel = input(title="Tick Highlighter >=", type=float, defval=0.25)
plot(selected, style=columns, color=selected >= pricelevel ? white : silver)

Any help is much appreciated, I've been stuck on this for a while and it's probably something really silly I'm not thinking of.

User avatar
Matthew
Site Admin
Site Admin
Posts: 92
Joined: July 1st, 2020
Location: Australia
Contact: Website Facebook Twitter TradingView Profile

Re: Cumulative Total Count of Tick Movements in a Bar?

Hi HLF!

This is an interesting goal that I've tried in the past but was unable to figure out a solution... until today haha.

Your question reminded me of a recent blog post I read on the TradingView website which explained a new feature that the team recently added to Pine which finally makes this possible.

Normally each new tick resets everything you're doing in your script, and "var" only saves your data across each bar on your chart, not each tick. But the TV team recently added a new variable type called "varip" which does not reset on each new tick.

Here's the blog post: What's New in Pine?

And here's some code which will do exactly what you are trying to do. This code will count price ticks (the blue column) and volume changes (the purple histogram). Keep in mind that a new bar tick is detected by Pine whenever price OR volume changes, so if you want to count price movement ticks vs trades executed at a given price, this is how you do it:

Code: Select all

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ZenAndTheArtOfTrading / www.PineScriptMastery.com
// @version=4
study("Tick Counter")

// Prepare tick counter variables (varip makes them persistent on real-time bar)
varip ticks = 0
varip volumeChanges = 0
varip lastPrice = close

// Check if price has changed (price tick)
if barstate.isrealtime and lastPrice != close
    ticks := ticks + 1
    lastPrice := close
    
// Check if volume has changed (trade executed at current price)
if barstate.isrealtime and lastPrice == close
    volumeChanges := volumeChanges + 1
    
// Reset counters on each new bar
if barstate.isnew
    ticks := 0
    volumeChanges := 0
    lastPrice := close
    
// Draw data to chart
plot(ticks, style=plot.style_columns, title="Price Ticks")
plot(volumeChanges, style=plot.style_histogram, color=color.purple, title="Volume Ticks")

Have fun, good luck with your coding & trading, and thanks for reminding me of this new feature as I'm sure it'll come in handy for my own scripts someday!

User avatar
Matthew
Site Admin
Site Admin
Posts: 92
Joined: July 1st, 2020
Location: Australia
Contact: Website Facebook Twitter TradingView Profile

Re: Cumulative Total Count of Tick Movements in a Bar?

PS. Here's a YouTube video lesson on this subject if you're interested in learning more: https://youtu.be/CPPKcwXWVFM

HLF
Pine Script Rookie
Pine Script Rookie
Posts: 2
Joined: April 27th, 2021

Re: Cumulative Total Count of Tick Movements in a Bar?

Wow thanks for the prompt response and sample code, that's very helpful! The problem I ran into with varip was that the tally of ticks resets when the indicator is added/removed. What I'm trying to accomplish is something that would keep a total count for the day, and a historical account.

For example, I'd like to be able to see:
- On Monday, ES moved 10000 total ticks
- On Tuesday, ES moved 8000 total ticks
- On Wednesday, ES moved 20000 ticks

etc, etc.

Does that make sense? How do I incorporate varip without it resetting when I remove the indicator?

Return to “Pine Script Q&A”