Tracking Buy and Sell Ticks Within a Single Candlestick

07 May 2019
0 comment
0 attachment

This ProBuilder code snippet is designed to count the number of positive and negative price ticks within the same candlestick. It uses array variables to maintain count across ticks during the candlestick’s formation, which is crucial for analyzing intra-candle price movements.

once $lastclose[0]=close
if not isset($ticks[barindex]) then
    $ticks[barindex] = 0
    $positivetick[barindex]=0
    $negativetick[barindex]=0
endif
if islastbarupdate then
    if close>$lastclose[0] then
        $ticks[barindex]=$ticks[barindex]+1
        $positivetick[barindex]=$positivetick[barindex]+1
    else
        $ticks[barindex]=$ticks[barindex]-1
        $negativetick[barindex]=$negativetick[barindex]+1
    endif
    $lastclose[0]=close
endif
return 0,$positivetick[barindex] as "positivetick" coloured("green"), $negativetick[barindex] as "negativetick" coloured("crimson")

Explanation of the Code:

  • Initialization: The code starts by setting the initial last close price of the candlestick to the current close price using the once keyword, which ensures this line runs only once.
  • Array Variable Setup: It checks if the array variables for the current bar index are set. If not, it initializes them. This includes arrays for storing the total ticks, positive ticks, and negative ticks.
  • Tick Counting: Within the islastbarupdate condition, which checks if the bar is the last updated one (i.e., the current candlestick), the code compares the current close price with the last recorded close price. If the current close is higher, it increments both the total and positive tick counters. If lower, it decrements the total tick counter and increments the negative tick counter.
  • Updating Last Close: The last close price is updated at the end of the condition to the current close price for comparison in the next tick.
  • Output: The function returns zero (common in indicator scripts where the main output is graphical), and it also outputs the count of positive and negative ticks with respective colors for visual distinction in the charting software.

This snippet is particularly useful for traders and analysts who need to track how many times the price moved up or down within the same candlestick, providing insights into intra-candle strength or weakness.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/tick-indicator/#post-209765

Visit Link
What is a Snippet? A snippet is a small, reusable chunk of code designed to solve specific tasks quickly. Think of it as a shortcut that helps you achieve your coding goals without reinventing the wheel. How to Use: Simply copy the snippet and paste it into your project where needed. Don't forget to tweak it to fit your context. Snippets are not just time-savers; they're also learning tools to help you become a more efficient coder.
Nicolas Master
I created ProRealCode because I believe in the power of shared knowledge. I spend my time coding new tools and helping members solve complex problems. If you are stuck on a code or need a fresh perspective on a strategy, I am always willing to help. Welcome to the community!
Author’s Profile

Comments

Search Snippets

Showing some results...
Sorry, no result found!

Snippets Categories

global
35
indicator
133
strategy
171

Recent Snippets

How to Create a Simple MTF Trend Dashboard with EMA and SMA
indicator
This indicator builds a compact multi-timeframe (MTF) dashboard that shows whether price is trading above or below a [...]
How to Display Per-Bar Volume Accumulation in Real Time (Intrabar Updates)
global
This snippet tracks and displays the current bar’s accumulated volume while the bar is still forming, instead of only [...]
Ticks Counter: Count Tick Updates Per Bar on Tick or Time Charts
global
This snippet counts how many tick updates have occurred for the current bar by incrementing a per-bar counter on each [...]
How to Build a Step-Based Trailing Stop That Moves to Break-Even First
strategy
This snippet implements a step trailing stop that advances in fixed increments once price reaches predefined profit [...]
Utilizing Arrays to Track and Compare Indicator Values Within the Same Bar in ProBuilder
indicator
This ProBuilder code snippet demonstrates how to use arrays to compare the values of an indicator (RSI in this case) [...]
Logo Logo
Loading...