0

edit: Used the code from vgladkov. Thankyou. Seems to be working just how I wanted. Thanks again.

This indicator will place a bar overlay on the candlesticks if the volume meets with the criteria. Volume at 30,000 or less, no overlay just default bar colours, volume at 30,000 to 100,000 the bar changes to bullish yellow and bearish blue, volume over 100,001 will be white, for both bullish and bearish. This helps to show that volume is picking up for the stock. I’ve got so far, but having trouble adding the code for the 100,001+ volume. Can someone shed some light on what I need to add. Thanks

//@version=5
indicator("Cadle Stick Assistant", shorttitle = "CSA", overlay=true)

//inputs
bullishcolor = input(color.rgb(234, 238, 20), "Bullish Color")
bearishcolor = input(color.rgb(59, 177, 255), "Bearish Color")
uvcolor = input(color.rgb(255, 255, 255), "Ultra Volume Color")

percentbreakoutcolor = input(color.rgb(255, 0, 255), "Breakdown Color")
percentthreshold = input(4.0, "Percentage Threshold", tooltip="The minimum percentage decrease for a candle to be highlighted.")

minvol = input(30000, "Min Volume", tooltip = "The minimum volume needed to generate a bar colour change.")
maxvol = input(100000, "Max Volume", tooltip = "The maximum volume needed to generate a bar colour change.")
ultravolume = input(100001, "Ultra Volume", tooltip = "Default volume set at 100,000 shares.")

// calculations
percentchange = math.abs(close - open) / open * 100
truepercent = (close - open) / open * 100

color barcolor = na

// conditions
bullishCond = close >= open
bearishCond = close < open

ultravolCond = volume > ultravolume
mediumvolCond = volume > minvol and volume <= maxvol
breakdownvolCond = percentchange > percentthreshold

switch
    breakdownvolCond and bearishCond => barcolor := percentbreakoutcolor
    ultravolCond => barcolor := uvcolor
    mediumvolCond and bullishCond => barcolor := bullishcolor
    mediumvolCond and bearishCond => barcolor := bullishcolor

// Plot volume indication
barcolor(barcolor)````

I'm not sure where to add the 100,001+ code in the if statements and also in the barclolor statement.

2 Answers2

1

You need some if checks for the color variable.

color col = na

if (bullish and (volume > minvol))  // Check if it is a bullish bar and the volume is greater than the min limit. Otherwise don't do anything
    if (volume < maxvol)            // This is the case where volume > minvol and volume < maxvol
        col := bullishcolor
    else                            // This is the case where volume >= maxvol
        col := uvcolor

// Then use col to color the bars
vitruvius
  • 15,740
  • 3
  • 16
  • 26
1

If you continue to complicate the conditions through the ternary operator, then the construction in which the color is defined will become unreadable. Most likely your problem comes from this, it is already difficult to find all the chains of the color selection logic. You have separate definitions of states, additional variables are introduced that are redundant. Simplify your logic to the following criteria:

  1. set of boolean states: boolish bar, bearish bar, very high volume, medium volume
  2. based on these states, make a simple logical construction, in this case, switch seems to be the most appropriate

Here is an example based on your code:

color barcolor = na

// conditions
bullishCond = close >= open
bearishCond = close < open

ultravolCond = volume > ultravol
mediumvolCond = volume > minvol and volume <= maxvol

switch
    ultravolCond => barcolor := uvcolor
    mediumvolCond and bullishCond => barcolor := bullishcolor
    mediumvolCond and bearishCond => barcolor := bearishcolor

// Plot volume indication
barcolor(barcolor)
vgladkov
  • 346
  • 2
  • 6