Candle Color by Volume
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © the_MarketWhisperer
// Version 1.0
// (Based on a code that I found somewhere in the TV world)
// A simple indicator that paints candles with different shades of colors based on the volume transacted. 
// Supports 6 levels of sensitivity at present.
// Darkest indicates the most volume transacted. Lightest, the least.
// Use it for your Volume Spread Analysis and/or in conjunction with any other strategies that you employ.
// Relative volume is calculated based on EMA (set to 20 period by default)

//@version=4
study(title="Candle Color by Volume", shorttitle="VolumeColoredCandles", overlay=true)

emaPeriod = input(20,'Volume EMA Period')
volumeRatio = volume/ema(volume,emaPeriod)
isUp = close > open 

//{
//=============================================================================================================================
//Green and red color repository. 6 is darkest. 1 is lightest.

g_01 = #dff5df, g_02 = #c8e6c9, g_03 = #03f703, g_04 = #44c649, g_05 = #0ca80c, g_06 = #006700

r_01 = #f4e5e5, r_02 = #f9cdcd, r_03 = #f79b9b, r_04 = #e66161, r_05 = #F30000, r_06 = #9F0000
//-----------------------------------------------------------------------------------------------------------------------------
//}

color candleColor = na


if (volume and isUp)
    candleColor := (volumeRatio > 2)? g_06: (volumeRatio > 1.5)? g_05: (volumeRatio > 1.2)? g_04: (volumeRatio > 0.8)? g_03: (volumeRatio > 0.4)? g_02:g_01
else if(volume)
    candleColor := (volumeRatio > 2)? r_06: (volumeRatio > 1.5)? r_05: (volumeRatio > 1.2)? r_04: (volumeRatio > 0.8)? r_03: (volumeRatio > 0.4)? r_02:r_01

barcolor(candleColor)
