#chat261_fix_diverg
#https://usethinkscript.com/threads/chatgpt-bard-other-ai-scripts-which-cant-be-used-in-thinkorswim.13822/page-14#post-143210
#James Chen
#261
#Please fix these scripts for divergence detection. Thanks.
#declare lower;
input prd = 5;
input source = close;
input searchdiv = "Regular";
input showindis = "Full";
input showlimit = 1;
input maxpp = 10;
input maxbars = 100;
input shownum = yes;
input showlast = no;
input dontconfirm = no;
input showlines = yes;
input showpivot = no;
input calcmacd = yes;
input calcmacda = yes;
input calcrsi = yes;
input calcstoc = yes;
input calccci = yes;
input calcmom = yes;
input calcobv = yes;
input calcvwmacd = yes;
input calccmf = yes;
input calcmfi = yes;
input calcext = no;
input externalindi = close;
# Define color constants
DefineGlobalColor("PositiveRegDiv", CreateColor(255, 255, 0)); # Yellow
DefineGlobalColor("NegativeRegDiv", CreateColor(0, 0, 255)); # Blue
DefineGlobalColor("PositiveHidDiv", CreateColor(0, 255, 0)); # Green
DefineGlobalColor("NegativeHidDiv", CreateColor(255, 0, 0)); # Red
DefineGlobalColor("PosDivText", CreateColor(0, 0, 0)); # Black
DefineGlobalColor("NegDivText", CreateColor(255, 255, 255)); # White
input reg_div_l_style_ = Curve.MEDIUM_DASH;
input hid_div_l_style_ = Curve.SHORT_DASH;
input reg_div_l_width = 2;
input hid_div_l_width = 1;
input showmas = no;
# Moving Averages
#def cma1col = Color.GREEN;
#def cma2col = Color.RED;
# Moving Averages
plot ma50 = if showmas then Average(close, 50) else Double.NaN;
#ma50.SetDefaultColor(cma1col);
ma50.SetDefaultColor(Color.GREEN);
plot ma200 = if showmas then Average(close, 200) else Double.NaN;
#ma200.SetDefaultColor(cma2col);
ma200.SetDefaultColor(Color.RED);
# RSI
#def rsiValue = if calcrsi then RSI(close, 14) else Double.NaN;
def rsiValue = if calcrsi then RSI(length = 14, price = close) else Double.NaN;
# MACD
#def macd = if calcmacd then MACD(close, 12, 26, 9) else Double.NaN;
def macd = if calcmacd then MACD(12, 26, 9) else Double.NaN;
def macdValue = if calcmacd then macd[1] - macd[2] else Double.NaN;
# Momentum
#def moment = if calcmom then Momentum(close, 10) else Double.NaN;
def moment = if calcmom then Momentum(10,close) else Double.NaN;
# CCI
#def cciValue = if calccci then CCI(close, 10) else Double.NaN;
def cciValue = if calccci then CCI(10,close) else Double.NaN;
# OBV
def obvValue = if calcobv then TotalSum(if close > close[1] then volume else if close < close[1] then -volume else 0) else Double.NaN;
# Stochastic
def stkValue = if calcstoc then StochasticSlow(14, 3) else Double.NaN;
# VW MACD
def vwmacd = if calcvwmacd then MovingAverage(AverageType.WILDERS, close, 12) - MovingAverage(AverageType.WILDERS, close, 26) else Double.NaN;
# Chaikin Money Flow
def cmf = if calccmf then ChaikinMoneyFlow(21) else Double.NaN;
# Money Flow Index
def mfi = if calcmfi then MoneyFlowIndex(close, 14) else Double.NaN;
# Pivot Points
def ph = if source == close then high else low;
def pl = if source == close then low else high;
def phValue = if high == Highest(high, prd) then high else Double.NaN;
def plValue = if low == Lowest(low, prd) then low else Double.NaN;
def phPosition = if !IsNaN(phValue) then BarNumber() else Double.NaN;
def plPosition = if !IsNaN(plValue) then BarNumber() else Double.NaN;
# Divergence Detection
def positiveRegular = if !IsNaN(phPosition) then
(if macdValue > Highest(macdValue[prd], prd) and low < Lowest(low[prd], prd) then 1 else 0)
else 0;
def negativeRegular = if !IsNaN(plPosition) then
(if macdValue < Lowest(macdValue[prd], prd) and high > Highest(high[prd], prd) then 1 else 0)
else 0;
def positiveHidden = if !IsNaN(phPosition) then
(if macdValue < Lowest(macdValue[prd], prd) and low > Lowest(low[prd], prd) then 1 else 0)
else 0;
def negativeHidden = if !IsNaN(plPosition) then
(if macdValue > Highest(macdValue[prd], prd) and high < Highest(high[prd], prd) then 1 else 0)
else 0;
plot PositiveRegularDivergence = positiveRegular;
PositiveRegularDivergence.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
PositiveRegularDivergence.SetDefaultColor(GlobalColor("PositiveRegDiv"));
plot NegativeRegularDivergence = negativeRegular;
NegativeRegularDivergence.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
NegativeRegularDivergence.SetDefaultColor(GlobalColor("NegativeRegDiv"));
plot PositiveHiddenDivergence = positiveHidden;
PositiveHiddenDivergence.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
PositiveHiddenDivergence.SetDefaultColor(GlobalColor("PositiveHidDiv"));
plot NegativeHiddenDivergence = negativeHidden;
NegativeHiddenDivergence.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
NegativeHiddenDivergence.SetDefaultColor(GlobalColor("NegativeHidDiv"));
#