Woodies CCI (SafeDay) v1.1
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © SafeDayTrading

//@Version=6
// Fixed the Histogram to only change trend color after gold bar condition to align with Woodies specifications
// Added ability for user to select colors for Turbo, CCI and Histogram

//@version=5
// Fixed Sidewinder calculations to include syminfo.mintick correction and compare correctly to Radians
// Made one adjustment to SideWWinder to align more closely with technical specs
// Notes on Indicator
// SideWinder (+/- 200 Line ) --> Trend Indication.  Green - Strong Trend, Yellow --> Trending, Red --> Not Trending.  Looks at absolute value of sum of rate of change for LSMA and ema34
// ChopZone (+/- 100 Line) --> Trend Direction.  Turquoise --> UpTrend, Red --> Downtrend, Any Other Color --> Not Trending.  Comparing price to ema 34
// LSMA (Zero Line) --> Price Direction (rate of change of LSMA)  --> Green --> Up, Red --> Down, White --> near Std Dev.

// Buy Direction
//   Sidewinder (Trending?)- Green (Stong Trend, Yellow - Trend, Red - Not Trending)
//   ChopZone (Trend Direction?) = Turquoise (Price above ema34)
//   LSMA (Direction of Avg Price Change) = Green  (Green - Up, Red - Down, White - Near Std Dev)
    
// Sell Direction
//   Sidewinder (Trending?)- Green (Stong Trend, Yellow - Trend, Red - Not Trending)
//   ChopZone (Trend Direction?) = Red (Price above ema34, Red - price below ema34, Green - price above ema34)
//   LSMA (Direction of Avg Price Change) = Red  (Red - Down, Green - Up, White - Near Std Dev)

indicator(title='Woodies CCI (SafeDay) v1.2', shorttitle="Woodies CCI (SafeDay) v1.2", format=format.price, precision=2, timeframe="", timeframe_gaps=true)

//declaration of variables
source = input(title='Source for CCI/Turbo/EMA', defval=close, group="CCI and Turbo Setup")
cci14Length = input.int(title='CCI Length', defval=14, minval=7, maxval=20, group= "CCI and Turbo Setup")
cciTurboLength = input.int(title='Turbo Length', defval=6, minval=3, maxval=14, group= "CCI and Turbo Setup")
cciColor = input(title="CCI Color", defval=#FF00FF, group= "CCI and Turbo Setup")
turboColor = input(title="Turbo Color", defval=#ffeb3b, group= "CCI and Turbo Setup")

// Histogram Setup
barsbt = input(title='Bars Before Trend', defval=4, group="Histogram Setup")
histBull = input(title="Histogram Bull Trend Color", defval = #008000, group="Histogram Setup")
histBear = input(title = "Histogram Bear Trend Color", defval=#FF0000, group="Histogram Setup")
histTrendStart = input(title = "Histogram Trend Start Color", defval= #ffff00, group="Histogram Setup")
histNoTrend = input(title = "Histogram Non Trend Color", defval = #c0c0c0, group="Histogram Setup")

// LSMA Setup for ZeroLine
src = input(defval=open, title='LSMA Source',  group="LSMA Setup For ZeroLine")
length = input(title='LSMA Length', defval=25, group="LSMA Setup For ZeroLine")
offset = input(title='LSMA Offset', defval=0,  group="LSMA Setup For ZeroLine")

// Chopzone and Sidewinder Setup
emal = input(title='EMA Length for ChopZone and SideWinder', defval=34, group="ChopZone and SideWinder Setup")
swhigh = input(title='Sidewinder Radians Higher Threshold ', defval=1.7453, group="ChopZone and SideWinder Setup")	// 100 Degrees
swmid = input(title='Sidewinder Radians Middle Threshold', defval=1.0471, group="ChopZone and SideWinder Setup")	//  60 Degrees -- Not Used
swlow = input(title='Sidewinder Radians Lower Threshold', defval=0.5235, group="ChopZone and SideWinder Setup")	// 30 Degrees

// Setup of non user selectable colors for chopzone, lsma, sidewinder
colorTurquoise = #34dddd
colorDarkGreen = #006400
colorPaleGreen = #98fb98
colorDarkRed = #8B0000
colorLightOrange = #ffc04c
colorOrange = color.orange
colorBlack = color.black
colorLime = color.lime
colorRed = #FF0000
colorSilver = #c0c0c0
colorYellow = #ffff00
colorGreen = #008000
colorWhite = color.white


//Chopzone Calculations
avg = hlc3
pi = math.atan(1) * 4
periods = 30
highestHigh = ta.highest(periods)
lowestLow = ta.lowest(periods)
range_1 = 25 / (highestHigh - lowestLow) * lowestLow

ema34 = ta.ema(source, emal)
x1_ema34 = 0
x2_ema34 = 1
y1_ema34 = 0
y2_ema34 = (ema34[1] - ema34) / avg * range_1
c_ema34 = math.sqrt((x2_ema34 - x1_ema34) * (x2_ema34 - x1_ema34) + (y2_ema34 - y1_ema34) * (y2_ema34 - y1_ema34))
emaAngle_1 = math.round(180 * math.acos((x2_ema34 - x1_ema34) / c_ema34) / pi)
emaAngle = y2_ema34 > 0 ? -emaAngle_1 : emaAngle_1
chopZoneColor = emaAngle >= 5 ? colorTurquoise : emaAngle < 5 and emaAngle >= 3.57 ? colorDarkGreen : emaAngle < 3.57 and emaAngle >= 2.14 ? colorPaleGreen : emaAngle < 2.14 and emaAngle >= .71 ? colorLime : emaAngle <= -1 * 5 ? colorDarkRed : emaAngle > -1 * 5 and emaAngle <= -1 * 3.57 ? colorRed : emaAngle > -1 * 3.57 and emaAngle <= -1 * 2.14 ? colorOrange : emaAngle > -1 * 2.14 and emaAngle <= -1 * .71 ? colorLightOrange : colorYellow

// CCI and Turbo Calculations
cciTurbo = ta.cci(source, cciTurboLength)
cci14 = ta.cci(source, cci14Length)

// Histogram Calculations
BullGoldBar = false
BearGoldBar = false
var Trend = ""

BullCondition1 = ta.lowest(cci14, barsbt+1)  >= 0 
BearCondition1 = ta.highest(cci14, barsbt+1) < 0

BullGoldBar := cci14 >= 0 ? ((BullCondition1 and not(BullGoldBar[1]) and not(Trend == "BullTrend"))) : false
BearGoldBar := cci14 < 0 ? ((BearCondition1 and not(BearGoldBar[1]) and not(Trend == "BearTrend"))) : false

BullTrend = (cci14 >= 0 and BullGoldBar[1]) or (Trend[1] == "BullTrend")
BearTrend = (cci14 < 0 and BearGoldBar[1]) or (Trend[1] == "BearTrend")
Trend := cci14 >= 0 and BullTrend ? "BullTrend" : cci14 < 0 and BearTrend ?"BearTrend" : cci14 >= 0 and BearTrend ? "BearTrend" : "BullTrend"

histogramColor = (cci14 > 0) and (BullTrend) ? histBull : (cci14 < 0) and (BearTrend) ? histBear : (BullGoldBar or BearGoldBar) ? histTrendStart : histNoTrend

//LSMA Calculations
lsma = ta.linreg(src, length, offset)
lsmastd = ta.stdev(math.abs(lsma[1] - lsma), length)
colorLine = math.abs(lsma - lsma[1]) < lsmastd ? colorWhite : lsma < lsma[1] ? colorRed : colorGreen

//Sidewinder Calculations
sum = (ta.change(lsma) + ta.change(ema34)) / syminfo.mintick
//** radian angles
colorLineSW = math.abs(sum) > swhigh ? colorGreen : math.abs(sum) > swmid and math.abs(sum) < swhigh ? colorYellow : colorRed


//plots of CCI and Histogram
plot(cci14, title='Histogram', color=color.new(histogramColor, 50), style=plot.style_columns)
plot(cciTurbo, title='Turbo', color=color.new(turboColor, 0), style=plot.style_line, linewidth=3)
plot(cci14, title='CCI', color=color.new(cciColor, 0), style=plot.style_line, linewidth=3)

//plots of Chopzones
plot(100, title='plus chop zone', color=chopZoneColor, style=plot.style_line, linewidth=3, editable=false)
plot(-100, title='minus chop zone', color=chopZoneColor, style=plot.style_line, linewidth=3, editable=false)

//plot of LSMA
plot(0, title='LSMA line', color=colorLine, style=plot.style_line, linewidth=3, editable=false)

//plot of Sidewinder
plot(-200, title='minus Sidewinder', color=colorLineSW, style=plot.style_line, linewidth=3, editable=false)
plot(200, title='plus Sidewinder', color=colorLineSW, style=plot.style_line, linewidth=3, editable=false)

//hlines
hline(0, title='Zero Line', color=colorBlack, linestyle=hline.style_solid)
hline(200, title='+Sidewinder line', color=colorBlack, linestyle=hline.style_dotted)
hline(-200, title='-Sidewinder line', color=colorBlack, linestyle=hline.style_dotted)
hline(100, title='+chop zone line', color=colorBlack, linestyle=hline.style_dotted)
hline(-100, title='-chop zone line', color=colorBlack, linestyle=hline.style_dotted)





