Harsh Bhut
Harsh Bhut

Reputation: 238

How to get green and red volume bar count from specific date in Pine Script (Tradingview)?

//@version=4
study("Volume bar Count")
var g_count = 0 
var r_count = 0
isGreen = close >= open
if isGreen
    g_count := g_count + 1
else
    r_count := r_count + 1

I can get the count of a green and red bar but not able to get it from a specific date.

Upvotes: 0

Views: 2456

Answers (1)

PineCoders-LucF
PineCoders-LucF

Reputation: 8789

Version 1

You can set the date through the script's Inputs with this:

//@version=4
study("")
i_fromYear      = input(1900,   "From Year",    minval = 1900)
i_fromMonth     = input(1,      "From Month",   minval = 1, maxval = 12)
i_fromDay       = input(1,      "From Day",     minval = 1, maxval = 31)
fromDate        = timestamp(i_fromYear, i_fromMonth, i_fromDay, 00, 00)

var g_count = 0 
var r_count = 0
isGreen = close >= open
if time > fromDate
    if isGreen
        g_count := g_count + 1
    else
        r_count := r_count + 1
plot(g_count, "", color.green)
plot(r_count, "", color.red)

Version 2

[2021.01.12 21:09 — LucF]

This is a more elegant way of doing the same thing:

//@version=4
study("1")
i_fromYear      = input(1900,   "From Year",    minval = 1900)
i_fromMonth     = input(1,      "From Month",   minval = 1, maxval = 12)
i_fromDay       = input(1,      "From Day",     minval = 1, maxval = 31)
fromDate        = timestamp(i_fromYear, i_fromMonth, i_fromDay, 00, 00)

isGreen = close >= open
gCount = cum(time > fromDate and isGreen     ? 1 : 0) 
rCount = cum(time > fromDate and not isGreen ? 1 : 0) 

plot(gCount, "", color.green)
plot(rCount, "", color.red)

Upvotes: 3

Related Questions