Bridge Bands For ThinkOrSwim

hdmvJ6W.png


CODE:

CSS:
#/ This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © calebsandfort
#study("Bridge Bands", precision = 3, overlay = true)
# Converted by Sam4Cok@Samer800 - 02/2023 request from UseThinkScript.com member
input UseChartTimeframe = yes;
input resolution = AggregationPeriod.FIFTEEN_MIN;    # "Resolution"
input BarColor  = yes;
input ShowLabel = yes;
input ShowCloud = yes;
input ShowTrendLine = yes;
input ShowBridgeBand = yes;
input showPivotMarkers = yes;         # "Show Pivot Markers"
input showTradePlot = no;             # "Show Trade Plot"
input showBridgeRange = no;           # "Show Bridge Range"
input showBollingerBands = no;        # "Show Bollinger Bands"
input pivotsLookback = 21;            # "Pivots Lookback"
input length = 15;                    # "Length"
input tradeSignalLength = 15;         # "Trade Signal Length"
input trendSignalMethod = {default "Donchian", "Momentum", "RSI", "VixFix"};   # "Trend Signal Method"
input trendLength = 63;               # "Trend Length"
input trendRsiThreshold = 50;         # "Trend RSI Threshold"
input trendConfirmationLength = 1;    # "Trend Confirmation Length"
input vixFixPeriod = 22;              # "VixFix LookBack Period"
input maxPositionSize = 6.0;                      # "Max Position Size %"
input topRiskRangePositionSizePtg = 25;           # "Top RR Position Size %"
input midRiskRangePositionSizePtg = 75;           # "Mid RR Position Size %"
input bottomRiskRangePositionSizePtg = 125;       # "Bottom RR Position Size %"


def na = Double.NaN;
def Donch = trendSignalMethod==trendSignalMethod."Donchian";
def Mom   =  trendSignalMethod==trendSignalMethod."Momentum";
def rsInd = trendSignalMethod==trendSignalMethod."RSI";
def Vix = trendSignalMethod==trendSignalMethod."VixFix";
def lengthMinus1 = length - 1;
def security_close;
def security_high;
def security_low;
def security_Vol;
#-- Color
DefineGlobalColor("darkGr" , CreateColor(0,75,0));
DefineGlobalColor("darkRe" , CreateColor(103,0,0));
DefineGlobalColor("blue"   , CreateColor(33,150,243));
DefineGlobalColor("red"    , CreateColor(255,82,82));
DefineGlobalColor("test"  , Color.DARK_RED);

if UseChartTimeframe {
    security_close = close;
    security_high = high;
    security_low = low;
    security_Vol = Volume;
} else
if !UseChartTimeframe {
    security_close = close(Period = resolution);
    security_high = high(Period = resolution);
    security_low = low(Period = resolution);
    security_Vol = Volume(Period = resolution);
} else {
    security_close = close;
    security_high = high;
    security_low = low;
    security_Vol = Volume;
}
#min_diff_func(src, s, n) =>
script min_diff_func {
    input src = close;
    input s   = 1;
    input n   = 20;
    def m;# = 100000000.0
    m = fold i = 0 to n with p=100000000.0 do
            Min(p, GetValue(src, n - i) - (src[n] + (s * i)));
    plot out = m;
}
#max_diff_func(src, s, n) =>
script max_diff_func {
    input src = close;
    input s   = 1;
    input n   = 20;
    def m;# = -100000000.0
    m = fold i = 0 to n with p=-100000000.0 do
            Max(p, GetValue(src, n - i) - (src[n] + (s * i)));
    plot out = m;
}
#stdev_sample(src, length) =>
script stdev_sample {
    input src = close;
    input length = 20;
    def EPS = 0.00000000001;
    def EPS1 = 0.00004;
    def avg = Average(src, length);
    def sumOfSquareDeviations;# = 0.0
    sumOfSquareDeviations = fold i = 0 to length - 1 with p do
         p + If(src[i] <= EPS, 0, If(src[i] > EPS1, src[1], 15) - avg) * If(src[i] <= EPS, 0, If(src[i] > EPS1, src[1], 15) - avg);
    def stdev = Sqrt(sumOfSquareDeviations / (length - 1));
    plot out = stdev;
}
def slope = (security_close[0] - security_close[lengthMinus1]) / lengthMinus1;
def min_diff = min_diff_func(security_close, slope[0], lengthMinus1);
def max_diff = max_diff_func(security_close, slope[0], lengthMinus1);
def bridge_range_bottom = security_close + min_diff;
def bridge_range_top = security_close + max_diff;

plot BridgeRangeBot = if showBridgeRange then bridge_range_bottom else na;    # 'Bridge Range Bottom'
plot BridgeRangeTop = if showBridgeRange then bridge_range_top else na;       # 'Bridge Range Top'
BridgeRangeBot.SetDefaultColor(Color.MAGENTA);
BridgeRangeTop.SetDefaultColor(Color.MAGENTA);

def tr1 = TrueRange(security_high, security_close, security_low);
def tr = if isNaN(tr1) then security_high - security_low else tr1;
def nATR = WildersAverage(tr, length);
def logLength = log(length);
def hurst = (log(highest(security_high, length) - lowest(security_low, length)) - log(nATR)) / logLength;
def sd = stdev(security_close, length);
def nWMA = wma(security_close, length);
def bb_bottom = nWMA - (sd * 2);
def bb_top = nWMA + (sd * 2);

plot BollingerBandsBot = if showBollingerBands then bb_bottom else na;    # 'Bollinger Bands Bottom'
plot BollingerBandsTop = if showBollingerBands then bb_top else na;       # 'Bollinger Bands Top'
BollingerBandsBot.SetDefaultColor(GlobalColor("blue"));
BollingerBandsTop.SetDefaultColor(GlobalColor("blue"));

def bridge_band_bottom = bb_bottom + ((bridge_range_bottom - bb_bottom) * AbsValue((hurst * 2) - 1));
def bridge_band_top = bb_top - ((bb_top - bridge_range_top) * AbsValue((hurst * 2) - 1));
def bridge_band_mid = bridge_band_bottom + ((bridge_band_top - bridge_band_bottom) / 2);
def bridge_band_bottom_mid = bridge_band_bottom + ((bridge_band_mid - bridge_band_bottom) / 2);
def bridge_band_top_mid = bridge_band_mid + ((bridge_band_top - bridge_band_mid) / 2);
def trade = lowest(security_low, tradeSignalLength) + ((highest(security_high, tradeSignalLength) - lowest(security_low, tradeSignalLength)) / 2);
def bullishTrade = security_close > trade;

plot trade_plot = if showTradePlot then trade else na;    # 'Trade'
trade_plot.SetPaintingStrategy(PaintingStrategy.POINTS);
trade_plot.AssignValueColor(if bullishTrade then GlobalColor("blue") else GlobalColor("red"));

def changeClose = security_close - security_close[1];
def up   = WildersAverage(max(changeClose, 0), 14);
def down = WildersAverage(-min(changeClose, 0), 14);
def nRSI = if down == 0 then 100 else if up == 0 then 0 else 100 - (100 / (1 + up / down));

def wvf = ((highest(security_close, vixFixPeriod) - security_low) / (highest(security_close, vixFixPeriod))) * 100;
def wvfTrend = lowest(wvf, trendLength) + ((highest(wvf, trendLength) - lowest(wvf, trendLength)) / 2);

def trend1 = Donch or rsInd or Vix;
def trend = if trend1 then lowest(security_low, trendLength) + ((highest(security_high, trendLength) - lowest(security_low, trendLength)) / 2) else
        if Mom then security_close[trendLength] else na;

def bearish;# = true

if trendConfirmationLength == 1 {
    bearish = if rsInd then nRSI < trendRsiThreshold else
              if Vix then wvf > wvfTrend else security_close < trend;
    } else {
    bearish = fold i = 0 to trendConfirmationLength - 1 with p=yes do
            p and (if rsInd then nRSI[i] < trendRsiThreshold else if Vix then wvf[i] > wvfTrend[i] else security_close[i] < trend[i]);
}
def bullish = !bearish;

plot trend_plot = if ShowTrendLine then trend else na;    # 'Trend'
trend_plot.SetLineWeight(2);
trend_plot.AssignValueColor(if bullish then CreateColor(33,150,243) else CreateColor(255,82,82));

plot bridge_band_bottom_plot = if ShowBridgeBand then bridge_band_bottom else na;    # 'Bottom'
plot bridge_band_top_plot = if ShowBridgeBand then bridge_band_top else na;          # 'Top'
bridge_band_bottom_plot.AssignValueColor(if bullish then Color.GREEN else Color.RED);
bridge_band_top_plot.AssignValueColor(if bullish then Color.GREEN else Color.RED);

AddCloud(if !ShowCloud then na else
         if bullish then bridge_band_top_plot else bridge_band_bottom_plot,
         if bullish then bridge_band_bottom_plot else bridge_band_top_plot, GlobalColor("darkGr"), Color.DARK_RED);

def reward = (bridge_band_top - security_close) / (security_close - bridge_band_bottom);
def upside_ptg = (bridge_band_top - security_close) / security_close * 100;
def downside_ptg = (security_close - bridge_band_bottom) / security_close * 100;

def minSize = maxPositionSize * topRiskRangePositionSizePtg/100;
def maxSize = maxPositionSize * midRiskRangePositionSizePtg/100;
def minSize1 = maxPositionSize * midRiskRangePositionSizePtg/100;
def maxSize1 = maxPositionSize * bottomRiskRangePositionSizePtg/100;
def targetPosSize;# = 0.0

if security_close > bridge_band_top {
    targetPosSize = if security_close < bridge_band_bottom then maxPositionSize * bottomRiskRangePositionSizePtg/100 else
                       maxPositionSize * topRiskRangePositionSizePtg/100;
    } else
if security_close > bridge_band_mid {
    targetPosSize = minSize + ((maxSize - minSize) * ((bridge_band_top - security_close) / (bridge_band_top - bridge_band_mid)));
    } else {
    targetPosSize = minSize1 + ((maxSize1 - minSize1) * ((bridge_band_mid - security_close) / (bridge_band_mid - bridge_band_bottom)));
}
def vol = security_Vol;
def oneDayVolComp = (vol[0] - vol[1]) / vol[1] * 100;
def twoDayVolComp = (vol[0] - vol[2]) / vol[2] * 100;

def volumeWeekAvg = Average(vol, 5);
def weekVolComp = (vol - volumeWeekAvg) / volumeWeekAvg * 100;

def volume1MAvg = Average(vol, 21);
def oneMVolComp = (vol - volume1MAvg) / volume1MAvg * 100;

def volume3MAvg = Average(vol, 63);
def threeMVolComp = (vol - volume3MAvg) / volume3MAvg * 100;

def momo_1m = security_close[0] > security_close[21];
def momo_3m = security_close[0] > security_close[63];

def bridge_band_pos = (bridge_band_top - close) / (bridge_band_top - bridge_band_bottom);
#bridge_band_pos_color = bridge_band_pos > .5 ? color.from_gradient(bridge_band_pos, 0.5, .85, color.blue, bullishColor) : color.from_gradient(bridge_band_pos, 0.15, .5, bearishColor, color.blue )

def bridge_band_top_minus_1 = bridge_band_top[1];
def bridge_band_bottom_minus_1 = bridge_band_bottom[1];

def bridge_band_top_highest = highest(bridge_band_top_minus_1, pivotsLookback);
def bridge_band_top_lowest = lowest(bridge_band_top_minus_1, pivotsLookback);
def bridge_band_bottom_highest = highest(bridge_band_bottom_minus_1, pivotsLookback);
def bridge_band_bottom_lowest = lowest(bridge_band_bottom_minus_1, pivotsLookback);

def  pivot_marker_offset = .01;


plot BullSigUp = if showPivotMarkers and bridge_band_top > bridge_band_top_highest then (bridge_band_top + bridge_band_top * pivot_marker_offset) else na;
BullSigUp.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
BullSigUp.SetDefaultColor(Color.GREEN);
plot BullSigDn = if showPivotMarkers and bridge_band_bottom > bridge_band_bottom_highest then (bridge_band_bottom - bridge_band_bottom * pivot_marker_offset) else na;
BullSigDn.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
BullSigDn.SetDefaultColor(Color.GREEN);

plot BearUp = if showPivotMarkers and bridge_band_top < bridge_band_top_lowest then (bridge_band_top + bridge_band_top * pivot_marker_offset) else na;
BearUp.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
BearUp.SetDefaultColor(Color.RED);
plot BearDn = if showPivotMarkers and bridge_band_bottom < bridge_band_bottom_lowest then (bridge_band_bottom - bridge_band_bottom * pivot_marker_offset) else na;
BearDn.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
BearDn.SetDefaultColor(Color.RED);

#--- Label
AddLabel(ShowLabel, if Bullish then "BULLISH" else "BEARISH", if Bullish then Color.GREEN else Color.RED);

#-- Bar Color
def ExtUp = bullishTrade and bullish;
def Upt   = !bullishTrade and bullish;
def ExtDn = !bullishTrade and !bullish;
def Dnt   = bullishTrade and !bullish;

AssignPriceColor( if !BarColor then Color.CURRENT else
                  if ExtUp then Color.GREEN else
                  if Upt   then Color.DARK_GREEN else
                  if ExtDn then Color.RED else
                  if Dnt   then Color.DARK_RED else Color.GRAY);

#--- END CODE
 
https://www.tradingview.com/script/...e Range". (Pg. 179 of Misbehavior of Markets)


Any way someone can make some watchlist columns for this indicator?

1) Need a column that says the price for the thresholld of the bands.

2) Need another column. Need the current price of the upper band minus yesterdays close of the bridge band threshold divided by the current price of the upper band.

3) Last column. Current price of the lower band minus yesterdays close of the bridge band threshold divided by the current price of the lower band.

Code:
input trendLength = 63;
input threshold = .5;

def length = 15;
def lengthMinus1 = length - 1;

def slope = CompoundValue(length, (close[0] - close[lengthMinus1]) / lengthMinus1, 0);

def min_diff_0 = close[lengthMinus1 - 0] - (close[lengthMinus1] + (slope[0] * 0));
def min_diff_1 = close[lengthMinus1 - 1] - (close[lengthMinus1] + (slope[0] * 1));
def min_diff_2 = close[lengthMinus1 - 2] - (close[lengthMinus1] + (slope[0] * 2));
def min_diff_3 = close[lengthMinus1 - 3] - (close[lengthMinus1] + (slope[0] * 3));
def min_diff_4 = close[lengthMinus1 - 4] - (close[lengthMinus1] + (slope[0] * 4));
def min_diff_5 = close[lengthMinus1 - 5] - (close[lengthMinus1] + (slope[0] * 5));
def min_diff_6 = close[lengthMinus1 - 6] - (close[lengthMinus1] + (slope[0] * 6));
def min_diff_7 = close[lengthMinus1 - 7] - (close[lengthMinus1] + (slope[0] * 7));
def min_diff_8 = close[lengthMinus1 - 8] - (close[lengthMinus1] + (slope[0] * 8));
def min_diff_9 = close[lengthMinus1 - 9] - (close[lengthMinus1] + (slope[0] * 9));
def min_diff_10 = close[lengthMinus1 - 10] - (close[lengthMinus1] + (slope[0] * 10));
def min_diff_11 = close[lengthMinus1 - 11] - (close[lengthMinus1] + (slope[0] * 11));
def min_diff_12 = close[lengthMinus1 - 12] - (close[lengthMinus1] + (slope[0] * 12));
def min_diff_13 = close[lengthMinus1 - 13] - (close[lengthMinus1] + (slope[0] * 13));
def min_diff_14 = close[lengthMinus1 - 14] - (close[lengthMinus1] + (slope[0] * 14));

def min_diff = CompoundValue(length,
Min(min_diff_14,
Min(min_diff_13,
Min(min_diff_12,
Min(min_diff_11,
Min(min_diff_10,
Min(min_diff_9,
Min(min_diff_8,
Min(min_diff_7,
Min(min_diff_6,
Min(min_diff_5,
Min(min_diff_4,
Min(min_diff_3,
Min(min_diff_2,
Min(min_diff_1,
Min(min_diff_0, 10000000))))))))))))))), 0);

def max_diff_0 = close[lengthMinus1 - 0] - (close[lengthMinus1] + (slope[0] * 0));
def max_diff_1 = close[lengthMinus1 - 1] - (close[lengthMinus1] + (slope[0] * 1));
def max_diff_2 = close[lengthMinus1 - 2] - (close[lengthMinus1] + (slope[0] * 2));
def max_diff_3 = close[lengthMinus1 - 3] - (close[lengthMinus1] + (slope[0] * 3));
def max_diff_4 = close[lengthMinus1 - 4] - (close[lengthMinus1] + (slope[0] * 4));
def max_diff_5 = close[lengthMinus1 - 5] - (close[lengthMinus1] + (slope[0] * 5));
def max_diff_6 = close[lengthMinus1 - 6] - (close[lengthMinus1] + (slope[0] * 6));
def max_diff_7 = close[lengthMinus1 - 7] - (close[lengthMinus1] + (slope[0] * 7));
def max_diff_8 = close[lengthMinus1 - 8] - (close[lengthMinus1] + (slope[0] * 8));
def max_diff_9 = close[lengthMinus1 - 9] - (close[lengthMinus1] + (slope[0] * 9));
def max_diff_10 = close[lengthMinus1 - 10] - (close[lengthMinus1] + (slope[0] * 10));
def max_diff_11 = close[lengthMinus1 - 11] - (close[lengthMinus1] + (slope[0] * 11));
def max_diff_12 = close[lengthMinus1 - 12] - (close[lengthMinus1] + (slope[0] * 12));
def max_diff_13 = close[lengthMinus1 - 13] - (close[lengthMinus1] + (slope[0] * 13));
def max_diff_14 = close[lengthMinus1 - 14] - (close[lengthMinus1] + (slope[0] * 14));

def max_diff = CompoundValue(length,
Max(max_diff_14,
Max(max_diff_13,
Max(max_diff_12,
Max(max_diff_11,
Max(max_diff_10,
Max(max_diff_9,
Max(max_diff_8,
Max(max_diff_7,
Max(max_diff_6,
Max(max_diff_5,
Max(max_diff_4,
Max(max_diff_3,
Max(max_diff_2,
Max(max_diff_1,
Max(max_diff_0, -10000000))))))))))))))), 0);

def bridge_range_bottom = close + min_diff;

def bridge_range_top = close + max_diff;

def tr = CompoundValue(length, TrueRange(high, close, low), 0);
def atr = if (IsNaN(atr[1]) or atr[1] == 0) then SimpleMovingAvg(tr, length) else (atr[1] * (length - 1) + tr) / length;

def logLength = Log(length);

plot hurst = (Log(Highest(high, length) - Lowest(low, length)) - Log(atr)) / logLength;
hurst.SetHiding(1 > 0);

script StDev_Sample {
input data = close;
input length = 12;

def avgData = Average(data, length);

plot StDevTS1 = Sqrt( (fold i = 0 to length with SD do SD + Sqr(GetValue(data, i) - avgData) ) / (length - 1));
StDevTS1.SetHiding(1 > 0);
}

def sd = StDev_Sample(close, length);

def WMA = WMA(close, length);

def bb_bottom = WMA - (sd * 2);

def bb_top = WMA + (sd * 2);

plot trend = Lowest(low, trendLength) + ((Highest(high, trendLength) - Lowest(low, trendLength)) / 2);
trend.SetLineWeight(1);
trend.AssignValueColor(if close > trend then Color.GREEN else Color.RED);
trend.SetPaintingStrategy(PaintingStrategy.DASHES);

plot bridge_band_bottom = bb_bottom + ((bridge_range_bottom - bb_bottom) * AbsValue((hurst * 2) - 1));
bridge_band_bottom.AssignValueColor(if close > trend then Color.GREEN else Color.RED);
bridge_band_bottom.SetLineWeight(3);

plot bridge_band_top = bb_top - ((bb_top - bridge_range_top) * AbsValue((hurst * 2) - 1));
bridge_band_top.AssignValueColor(if close > trend then Color.GREEN else Color.RED);
bridge_band_top.SetLineWeight(3);

plot bridge_band_threshold = bridge_band_bottom + ((bridge_band_top - bridge_band_bottom) * threshold);
bridge_band_threshold.SetDefaultColor(Color.LIGHT_GRAY);
 
Last edited by a moderator:
I know many have asked this question before, and it was suggested to others that they post their code in order to help figure out the problem. So here is my question and my code. I am trying to run a scan on this study in thinkscript ( I am not the one who wrote it, I copied it from somewhere else and made some modifications). The reason for the question I have an active scan running on a related study which is far more complex, and somehow thinkorswim accepts the can without this exception, but with this code I get the exception. In fact, the reason I want to use this code is because it looks to me like it would require less computational power than the code that I am already running an active scan on. So here is the code:

Code:
input trendLength = 63;
input threshold = .5;
input nearTrigger = 0.9;
input showNearTriggerLine = no;
input useRSI = yes;
input useStoch = yes;
input price = close;
input RSIlength = 11;
input over_Bought_rsi = 75;
input over_Sold_rsi = 25;
input averageType = AverageType.EXPONENTIAL;
input over_Bought_stoch = 80;
input over_Sold_stoch = 20;
input KPeriod = 10;
input DPeriod = 10;
input priceH = high;
input priceL = low;
input expSlopeLength = 9;
input showOpenSignals = yes;

def sdlength = 15;
def lengthMinus1 = sdlength - 1;
def na = Double.NaN;

def slope = CompoundValue(sdlength, (close[0] - close[lengthMinus1]) / lengthMinus1, 0);

def min_diff_0 = close[lengthMinus1 - 0] - (close[lengthMinus1] + (slope[0] * 0));
def min_diff_1 = close[lengthMinus1 - 1] - (close[lengthMinus1] + (slope[0] * 1));
def min_diff_2 = close[lengthMinus1 - 2] - (close[lengthMinus1] + (slope[0] * 2));
def min_diff_3 = close[lengthMinus1 - 3] - (close[lengthMinus1] + (slope[0] * 3));
def min_diff_4 = close[lengthMinus1 - 4] - (close[lengthMinus1] + (slope[0] * 4));
def min_diff_5 = close[lengthMinus1 - 5] - (close[lengthMinus1] + (slope[0] * 5));
def min_diff_6 = close[lengthMinus1 - 6] - (close[lengthMinus1] + (slope[0] * 6));
def min_diff_7 = close[lengthMinus1 - 7] - (close[lengthMinus1] + (slope[0] * 7));
def min_diff_8 = close[lengthMinus1 - 8] - (close[lengthMinus1] + (slope[0] * 8));
def min_diff_9 = close[lengthMinus1 - 9] - (close[lengthMinus1] + (slope[0] * 9));
def min_diff_10 = close[lengthMinus1 - 10] - (close[lengthMinus1] + (slope[0] * 10));
def min_diff_11 = close[lengthMinus1 - 11] - (close[lengthMinus1] + (slope[0] * 11));
def min_diff_12 = close[lengthMinus1 - 12] - (close[lengthMinus1] + (slope[0] * 12));
def min_diff_13 = close[lengthMinus1 - 13] - (close[lengthMinus1] + (slope[0] * 13));
def min_diff_14 = close[lengthMinus1 - 14] - (close[lengthMinus1] + (slope[0] * 14));

def min_diff = CompoundValue(sdlength,
Min(min_diff_14,
Min(min_diff_13,
Min(min_diff_12,
Min(min_diff_11,
Min(min_diff_10,
Min(min_diff_9,
Min(min_diff_8,
Min(min_diff_7,
Min(min_diff_6,
Min(min_diff_5,
Min(min_diff_4,
Min(min_diff_3,
Min(min_diff_2,
Min(min_diff_1,
Min(min_diff_0, 10000000))))))))))))))), 0);

def max_diff_0 = close[lengthMinus1 - 0] - (close[lengthMinus1] + (slope[0] * 0));
def max_diff_1 = close[lengthMinus1 - 1] - (close[lengthMinus1] + (slope[0] * 1));
def max_diff_2 = close[lengthMinus1 - 2] - (close[lengthMinus1] + (slope[0] * 2));
def max_diff_3 = close[lengthMinus1 - 3] - (close[lengthMinus1] + (slope[0] * 3));
def max_diff_4 = close[lengthMinus1 - 4] - (close[lengthMinus1] + (slope[0] * 4));
def max_diff_5 = close[lengthMinus1 - 5] - (close[lengthMinus1] + (slope[0] * 5));
def max_diff_6 = close[lengthMinus1 - 6] - (close[lengthMinus1] + (slope[0] * 6));
def max_diff_7 = close[lengthMinus1 - 7] - (close[lengthMinus1] + (slope[0] * 7));
def max_diff_8 = close[lengthMinus1 - 8] - (close[lengthMinus1] + (slope[0] * 8));
def max_diff_9 = close[lengthMinus1 - 9] - (close[lengthMinus1] + (slope[0] * 9));
def max_diff_10 = close[lengthMinus1 - 10] - (close[lengthMinus1] + (slope[0] * 10));
def max_diff_11 = close[lengthMinus1 - 11] - (close[lengthMinus1] + (slope[0] * 11));
def max_diff_12 = close[lengthMinus1 - 12] - (close[lengthMinus1] + (slope[0] * 12));
def max_diff_13 = close[lengthMinus1 - 13] - (close[lengthMinus1] + (slope[0] * 13));
def max_diff_14 = close[lengthMinus1 - 14] - (close[lengthMinus1] + (slope[0] * 14));

def max_diff = CompoundValue(sdlength,
Max(max_diff_14,
Max(max_diff_13,
Max(max_diff_12,
Max(max_diff_11,
Max(max_diff_10,
Max(max_diff_9,
Max(max_diff_8,
Max(max_diff_7,
Max(max_diff_6,
Max(max_diff_5,
Max(max_diff_4,
Max(max_diff_3,
Max(max_diff_2,
Max(max_diff_1,
Max(max_diff_0, -10000000))))))))))))))), 0);

def bridge_range_bottom = close + min_diff;

def bridge_range_top = close + max_diff;

def tr = CompoundValue(sdlength, TrueRange(high, close, low), 0);
def atr = if (IsNaN(atr[1]) or atr[1] == 0) then SimpleMovingAvg(tr, sdlength) else (atr[1] * (sdlength - 1) + tr) / sdlength;

def logLength = Log(sdlength);

plot hurst = (Log(Highest(high, sdlength) - Lowest(low, sdlength)) - Log(atr)) / logLength;
hurst.SetHiding(1 > 0);

script StDev_Sample {
    input data = close;
    input length = 12;

    def avgData = Average(data, length);

    plot StDevTS1 = Sqrt( (fold i = 0 to length with SD do SD + Sqr(GetValue(data, i) - avgData) ) / (length - 1));
    StDevTS1.SetHiding(1 > 0);
}

def sd = StDev_Sample(close, sdlength);
def WMA = WMA(close, sdlength);

def bb_bottom = WMA - (sd * 2);
def bb_top = WMA + (sd * 2);

plot trend = Lowest(low, trendLength) + ((Highest(high, trendLength) - Lowest(low, trendLength)) / 2);
trend.SetLineWeight(1);
def bullishTrend = close > trend;
def bearishTrend = !bullishTrend;
trend.AssignValueColor(if bullishTrend then Color.GREEN else Color.RED);
trend.SetPaintingStrategy(PaintingStrategy.DASHES);

plot bridge_band_bottom = bb_bottom + ((bridge_range_bottom - bb_bottom) * AbsValue((hurst * 2) - 1));
bridge_band_bottom.AssignValueColor(if close > trend then Color.GREEN else Color.RED);
bridge_band_bottom.SetLineWeight(3);

plot bridge_band_top = bb_top - ((bb_top - bridge_range_top) * AbsValue((hurst * 2) - 1));
bridge_band_top.AssignValueColor(if close > trend then Color.GREEN else Color.RED);
bridge_band_top.SetLineWeight(3);

plot bridge_band_threshold = bridge_band_bottom + ((bridge_band_top - bridge_band_bottom) * threshold);
bridge_band_threshold.SetDefaultColor(Color.LIGHT_GRAY);


# ***********************************
# RSI section
# ***********************************
def NetChgAvg = MovingAverage(averageType, price - price[1], RSIlength);
def TotChgAvg = MovingAverage(averageType, AbsValue(price - price[1]), RSIlength);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def RSI = 50 * (ChgRatio + 1);
def overBot_rsiSig = (RSI >= over_Bought_rsi) or (RSI[1] >= over_Bought_rsi) or (RSI[2] >= over_Bought_rsi);
def overSold_rsiSig = (RSI <= over_Sold_rsi) or (RSI[1] <= over_Sold_rsi) or (RSI[2] <= over_Sold_rsi);

# ***********************************
# slow stochastic section
# ***********************************
def SlowK = reference StochasticFull(over_Bought_stoch, over_Sold_stoch, KPeriod, DPeriod, priceH, priceL, price, 3, averageType).FullK;
def SlowD = reference StochasticFull(over_Bought_stoch, over_Sold_stoch, KPeriod, DPeriod, priceH, priceL, price, 3, averageType).FullD;
def stochBuy = SlowK <= over_Sold_stoch;
def stochSell = SlowK >= over_Bought_stoch;
# ***********************************

# ***********************************
# EMA slope section
# ***********************************

def AvgExp = ExpAverage(price, expSlopeLength);
def EMAslope = AvgExp - AvgExp[1];

# *****************************************
# added signals for setting entry values
# *****************************************
def bbNL = bridge_band_bottom + ((bridge_band_top - bridge_band_bottom) * (1 - nearTrigger));
plot bridge_band_nearLow = if showNearTriggerLine then bbNL else na;
bridge_band_nearLow.SetDefaultColor(Color.LIGHT_GRAY);
def bbNL_rnd = Round(bridge_band_nearLow, 2);

def bbNH = bridge_band_bottom + ((bridge_band_top - bridge_band_bottom) * nearTrigger);
plot bridge_band_nearHigh = if showNearTriggerLine then bbNH else na;
bridge_band_nearHigh.SetDefaultColor(Color.LIGHT_GRAY);
def bbNH_rnd = Round(bridge_band_nearHigh, 2);

def atFullLongEntry = (low <= bridge_band_bottom and high >= bridge_band_bottom)
    or (low <= bbNL and high >= bbNL);

def atHalfLongEntry = (low <= bridge_band_threshold and low >= bbNL);

def atFullShortEntry = (high >= bridge_band_top and low <= bridge_band_top)
    or (high >= bbNH and low <= bbNH);

def atHalfShortEntry = (high >= bridge_band_threshold and high <= bbNH);

def longOpenValue;
if atHalfLongEntry {
    longOpenValue = if price < bridge_band_threshold then price else bridge_band_threshold;
} else {
    if atFullLongEntry {
        if (low <= bridge_band_bottom and high >= bridge_band_bottom) {
            longOpenValue = bridge_band_bottom;
        } else {
            longOpenValue = if (low <= bbNL and high >= bbNL) then bbNL else open;
            }
    } else {
        longOpenValue = open;
    }
}

def shortOpenValue;
if atHalfShortEntry {
    shortOpenValue = if price > bridge_band_threshold then price else bridge_band_threshold;
} else {
    if atFullShortEntry {
        if (high >= bridge_band_top and low <= bridge_band_top) {
            shortOpenValue = bridge_band_top;
        } else {
            shortOpenValue = if (high >= bbNH and low <= bbNH) then bbNH else open;
            }
    } else {
        shortOpenValue = open;
    }
}


#-- conditions for screening if the ticker is in a buy to open or sell to open zone
# since I am only using this strat for the scan function, no need to have the close points calculated
def buyOpenCondition = ((atFullLongEntry and bullishTrend)
        and (overSold_rsiSig or !useRSI)
        and (stochBuy or !useStoch))
        and (!overBot_rsiSig or !useRSI)
        and (!stochSell or !useStoch)
    or
        ((atHalfLongEntry and bullishTrend)
        and EMAslope >= 0)
        and (!overBot_rsiSig or !useRSI)
        and (!stochSell or !useStoch);

def sellOpenCondition = (price >= 10) and (((atFullShortEntry and bearishTrend)
        and (overBot_rsiSig or !useRSI)
        and (stochSell or !useStoch))
        and (!overSold_rsiSig or !useRSI)
        and (!stochBuy or !useStoch)
    or
        ((atHalfShortEntry and bearishTrend)
        and EMAslope <= 0)
        and (!overSold_rsiSig or !useRSI)
        and (!stochBuy or !useStoch));

plot buyOpenSig = if (buyOpenCondition and showOpenSignals) then longOpenValue else Double.NaN;
plot sellOpenSig = if (sellOpenCondition and showOpenSignals) then shortOpenValue else Double.NaN;


buyOpenSig.SetDefaultColor(Color.BLUE);
buyOpenSig.SetPaintingStrategy(PaintingStrategy.ARROW_UP);

sellOpenSig.SetDefaultColor(Color.BLUE);
sellOpenSig.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

When I set up the scan, I want it to trigger on a list of about 100 tickers from this trigger:

Code:
MandelbrotBB_lessComp()."buyOpenSig" is true within 3 bars

Can anyone help me figure out why this is too complex for TOS to scan?

Thanks in advance.

PS - if this is your code that I modified, please call it out so that I can give credit where credit is due.
 
I watch Figuring out money on youtube. He's got a great sheet of the main markets and it shows the % away 10 and 20% from the bridgebands. I'd love to have a watchlist column that would be red at %20 and Dark Red at %10 of the upper band, and green at 20 and dark green at 10 of the lower band , maybe light green and light red for in between to mimic his report... here's an example of his report
1705328876311.png
 
@maston21 @schinaseatribe @Ronathan Edwards
This script is too complex for watchlists, the scan hacker, conditional orders, and all other ToS widgets.

As plots on the chart, no script is "too complex". For the most part, they are only updated on the current bar.

BUT TDA widgets, like watchlists and scanners, have to throttle complex scripts to prevent high load runs on the TDA servers.
This is because the widgets are not looking at just one bar.
They look at much more:
https://usethinkscript.com/threads/...can-hacker-for-thinkorswim.13267/#post-111810

@schinaseatribe
Your question is what makes this script complex?
Having over 50 variables to define and store and then running them through a script{} function recursively;
makes this study one of the more complex on the forum.
There isn't any way to even begin to attempt to make this script less complex enough for use in ToS widgets.
 
Last edited:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
510 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top