Search code examples
pine-script

Plot only when defined condition is met in Pinescript


I am trying to improve this code, TD Sequential, by Tom Demark. Firstly, rules for the "TD Buy Setup"

  1. start counting when you have a close that is lower than the close four bars earlier. Continue counting as long as each subsequent close is lower than the close four bars earlier.
  2. If the count reaches nine, the TD Setup is complete. Thus, the code is here below
/@version=5
indicator("My script")

//Buy Setup
var int buySetup = na
buySetup := if close < close[4]
    if close[1] > close[5] or 0 < nz(buySetup[1])
        nz(buySetup[1]) + 1
    else
        0
    if nz(buySetup[1]) == 9
        1
    else
        nz(buySetup[1]) + 1
else
    0

plotchar(buySetup == 1, char='1', color=color.red, location=location.abovebar)
plotchar(buySetup == 2, char='2', color=color.red, location=location.abovebar)
plotchar(buySetup == 3, char='3', color=color.red, location=location.abovebar)
plotchar(buySetup == 4, char='4', color=color.red, location=location.abovebar)
plotchar(buySetup == 5, char='5', color=color.red, location=location.abovebar)
plotchar(buySetup == 6, char='6', color=color.red, location=location.abovebar)
plotchar(buySetup == 7, char='7', color=color.red, location=location.abovebar)
plotchar(buySetup == 8, char='8', color=color.red, location=location.abovebar)
plotchar(buySetup == 9, char='9', color=color.red, location=location.abovebar)

Now, what I want is I'm trying to plot the counts 1 through 9 ONLY if "Buy Setup" has completed - that is when the count reaches 9. I don't want it to plot rest of the numbers if "Buy Setup" is incomplete (when it doesn't reach 9)

Here is the picture below to clearly illustrate what I mean [Example](https://i.sstatic.net/OFM0Y.png)

I've tried this (new to coding)

var bool plotCounts = false
if (buySetup == 9)
    plotCounts := true

but it did not work.

Thanks


Solution

  • You can change the ploshape of your code to only plot if buySetup==9 and then use offset to draw the preceding numbers :

    plotchar(buySetup == 9, char='9', color=color.red, location=location.abovebar)
    plotchar(buySetup == 9, char='8', color=color.red, location=location.abovebar, offset = -1)
    plotchar(buySetup == 9, char='7', color=color.red, location=location.abovebar, offset = -2)
    plotchar(buySetup == 9, char='6', color=color.red, location=location.abovebar, offset = -3)
    plotchar(buySetup == 9, char='4', color=color.red, location=location.abovebar, offset = -4)
    plotchar(buySetup == 9, char='5', color=color.red, location=location.abovebar, offset = -5)
    plotchar(buySetup == 9, char='3', color=color.red, location=location.abovebar, offset = -6)
    plotchar(buySetup == 9, char='2', color=color.red, location=location.abovebar, offset = -7)
    plotchar(buySetup == 9, char='1', color=color.red, location=location.abovebar, offset = -8)
    

    This way, the numbers won't appear until you reach BuySetup=9 : enter image description here