Code below

Please use the code only for yourself without posting online or distributing. This indicator and code is to be used for educational purposes only. copy and paste everything below starting with “#”:

(for help importing a study into think or swim, I recommend this: https://www.youtube.com/watch?v=UHG6x_ZZaNc

#The output of this indicator is for informational and educational use only, is not an investment recommendation or advice, and should not be relied upon in making the decision to buy or sell a security or pursue a particular investment strategy.

input length1 = 45;

input avgType1 = AverageType.EXPONENTIAL;

input length2 = 105;

input avgType2 = AverageType.SIMPLE;

input avgToUse = {default "avg1", "avg2"};

input pivotLength = 10;

input majorPivotLength = 60;

input minBarsSinceLastTouch = 5;

input maxPullbacks = 2;

input entryOffsetTicks = 1;

input profitTicks = 0;

input stopTicks = 10;

input useOvernightOpen = no;

input overnightOpenTime = 1800;

input barsToPlotEntry = 2;

input barsToPlotTargetAndStop = 30;

def na = Double.NaN;

def ts = tickSize();

plot avg1 = MovingAverage(avgType1, close, length1);

plot avg2 = MovingAverage(avgType2, close, length2);

input highPrice = high;

input lowPrice = low;

#def bottom =

#CompoundValue(1,

# !bottom[1] and

# lowPrice[pivotLength] == Lowest(lowPrice, pivotLength + pivotLength + 1)

#, 0)

#;

#def top =

#CompoundValue(1,

# !top[1] and

# highPrice[pivotLength] == Highest(highPrice, pivotLength + pivotLength + 1)

#, 0)

#;

#def ph = top[-pivotLength];

#def pl = bottom[-pivotLength];

#def pivotHigh = if IsNaN(ph) then 0 else ph;

#def pivotLow = if IsNaN(pl) then 0 else pl;

def H1 = Highest(Highprice, pivotLength);

def H2 = H1[-(pivotLength)];

def ph = !ph[1] and Highprice > H1[1] and Highprice >= H2[0];

def pivothigh = if IsNaN(ph) then 0 else ph;

def L1 = Lowest(Lowprice, pivotLength);

def L2 = L1[-(pivotLength)];

def pl = !pl[1] and Lowprice < L1[1] and Lowprice <= L2[0];

def pivotlow = if IsNaN(pl) then 0 else pl;

def highBars1;

#def highBars2;

if pivotHigh

then {

highBars1 = 0;

# highBars2 = highBars1[1] + 1;

}

else {

highBars1 = highBars1[1] + 1;

# highBars2 = highBars2[1] + 1;

}

def lowBars1;

#def lowBars2;

if pivotLow

then {

lowBars1 = 0;

# lowBars2 = lowBars1[1] + 1;

}

else {

lowBars1 = lowBars1[1] + 1;

# lowBars2 = lowBars2[1] + 1;

}

def low1 = GetValue(lowPrice, lowBars1);

#def low2 = GetValue(lowPrice, lowBars2);

def high1 = GetValue(highPrice, highBars1);

#def high2 = GetValue(highPrice, highBars2);

def php = if pivotHigh then highPrice else if close crosses above high1 then 0 else php[1];

def plp = if pivotLow then lowPrice else if close crosses below low1 then 0 else plp[1];

plot pivotHighPrice = if php > 0 then php else na;

plot pivotLowPrice = if plp > 0 then plp else na;

def avg;

switch (avgToUse){

case "avg1":

avg = avg1;

case "avg2":

avg = avg2;

}

def secs = GetAggregationPeriod() / 1000;

def firstBar = SecondsFromTime(overnightOpenTime) between 0 and (secs - 1);

def oo = if firstBar then open else oo[1];

plot overnightOpen = if oo > 0 then oo else na;

# BUY_________________________________________________________________________________

def firstLow = pivotLow and lowPrice < avg2;

def firstLowBars = if firstLow then 0 else firstLowBars[1] + 1;

def firstLowPrice = getValue(lowPrice, firstLowBars);

def majorHigh =

highPrice > Highest(highPrice[1], majorPivotLength-1)

and pivotHigh

# and firstLowBars == lowBars1

and highPrice > avg

;

def majorHighBars = if majorHigh then 0 else majorHighBars[1] + 1;

def lowTouchBars = if lowPrice[1] <= avg[1] then 1 else lowTouchBars[1] + 1;

def resetLongBars = if open < avg and close < avg then 0 else resetLongBars[1] + 1;

def resetLongCount = if avgToUse == avgToUse.avg1 then avg1 crosses above avg2 else resetLongBars == 0;

def countPullbacksDown = if resetLongCount then 0 else if lowPrice crosses below avg and lowTouchBars >= minBarsSinceLastTouch then countPullbacksDown[1] + 1 else countPullbacksDown[1];

plot buyAlert =

majorHigh

# and majorHighBars < firstLowBars

and majorHighBars < resetLongBars

and majorHighBars == highBars1

and lowTouchBars >= minBarsSinceLastTouch-1

and countPullbacksDown < maxPullbacks

and lowPrice > avg2

and if(avgToUse == avgToUse."avg1", avg1 >= avg2, 1)

;

alert(buyAlert, "Possible BUY Signal Coming", Alert.BAR, Sound.Ding);

plot buySignal =

lowPrice <= avg

and if(avgToUse == avgToUse."avg1", lowPrice > avg2, 1)

and lowPrice >= firstLowPrice

# and majorHighBars < firstLowBars

and majorHighBars < resetLongBars

and majorHighBars == highBars1

and lowTouchBars >= minBarsSinceLastTouch

and countPullbacksDown <= maxPullbacks

and if(useOvernightOpen, overnightOpen > (high + entryOffsetTicks * ts + profitTicks * ts) or overnightOpen < low, 1)

;

alert(buySignal, "BUY Signal", alert.BAR, sound.Ding);

def bp = if buySignal or (buySignal[1] and lowPrice >= avg and high < high[1]) then high + entryOffsetTicks * ts else bp[1];

plot buyPrice = if buySignal within barsToPlotEntry bars then bp else na;

def b = compoundValue(1, if isNaN(buyPrice) then 0 else !b[1] and high >= buyPrice, 0);

plot buy = b;

def buyBars = if buySignal then 0 else buyBars[1] + 1;

def ls = if buy then getValue(low, buyBars) - stopTicks * ts else ls[1];

def lt = if buy then if profitTicks > 0 then bp + profitTicks * ts else high1 else lt[1];

plot longStopLoss = if buy within barsToPlotTargetAndStop bars then ls else na;

plot longTarget = if buy within barsToPlotTargetAndStop bars then lt else na;

# SELL________________________________________________________________________________

def firstHigh = pivotHigh and highPrice > avg2;

def firstHighBars = if firstHigh then 0 else firstHighBars[1] + 1;

def firstHighPrice = getValue(highPrice, firstHighBars);

def majorLow =

lowPrice < lowest(lowPrice[1], majorPivotLength-1)

and pivotLow

# and firstHighBars == highBars1

and lowPrice < avg

;

#addchartBubble(majorlow, high, "X", color.cyan);

def majorLowBars = if majorLow then 0 else majorLowBars[1] + 1;

def highTouchBars = if highPrice[1] >= avg[1] then 1 else highTouchBars[1] + 1;

def resetShortBars = if open > avg and close > avg then 0 else resetShortBars[1] + 1;

def resetShortCount = if avgToUse == avgToUse.avg1 then avg1 crosses below avg2 else resetShortBars == 0;

def countPullbacksUp = if resetShortCount then 0 else if highPrice crosses above avg and highTouchBars >= minBarsSinceLastTouch then countPullbacksUp[1] + 1 else countPullbacksUp[1];

plot sellAlert =

majorLow

# and majorLowBars < firstHighBars

and majorLowBars < resetShortBars

and majorLowBars == lowBars1

and highTouchBars >= minBarsSinceLastTouch-1

and countPullbacksUp < maxPullbacks

and highPrice < avg2

and if(avgToUse == avgToUse."avg1", avg1 <= avg2, 1)

;

alert(sellAlert, "Possible SELL Signal Coming", Alert.BAR, Sound.Ding);

plot sellSignal =

highPrice >= avg

and if(avgToUse == avgToUse."avg1", highPrice < avg2, 1)

and highPrice <= firstHighPrice

# and majorLowBars < firstHighBars

and majorLowBars < resetShortBars

and majorLowBars == lowBars1

and highTouchBars >= minBarsSinceLastTouch

and countPullbacksUp <= maxPullbacks

and if(useOvernightOpen, overnightOpen < (low - entryOffsetTicks * ts - profitTicks * ts) or overnightOpen > high, 1)

;

alert(sellSignal, "SELL Signal", alert.BAR, sound.Ding);

def sp = if sellSignal or (sellSignal[1] and highPrice <= avg and low > low[1]) then low - entryOffsetTicks * ts else sp[1];

plot sellPrice = if sellSignal within barsToPlotEntry bars then sp else na;

def s = compoundValue(1, if isNaN(sellPrice) then 0 else !s[1] and low <= sellPrice, 0);

plot sell = s;

def sellBars = if sellSignal then 0 else sellBars[1] + 1;

def ss = if sell then getValue(high, sellBars) + stopTicks * ts else ss[1];

def st = if sell then if profitTicks > 0 then sp - profitTicks * ts else low1 else st[1];

plot shortStopLoss = if sell within barsToPlotTargetAndStop bars then ss else na;

plot shortTarget = if sell within barsToPlotTargetAndStop bars then st else na;

# Appearance__________________________________________________________________________

#pivotHigh.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);

#pivotLow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);

#pivotHigh.SetDefaultColor(Color.GRAY);

#pivotLow.SetDefaultColor(Color.GRAY);

pivotHighPrice.SetDefaultColor(Color.DARK_RED);

pivotLowPrice.SetDefaultColor(Color.DARK_GREEN);

pivotHighPrice.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

pivotLowPrice.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

avg1.SetDefaultColor(GetColor(4));

avg2.SetDefaultColor(Color.RED);

avg1.SetStyle(Curve.MEDIUM_DASH);

avg2.SetStyle(Curve.MEDIUM_DASH);

overnightOpen.setDefaultColor(color.gray);

overnightOpen.setPaintingStrategy(paintingStrategy.DASHES);

overnightOpen.setStyle(curve.LONG_DASH);

def signal = if(isNaN(buySignal), 0, buySignal) + if(isNaN(sellSignal), 0, sellSignal);

assignPriceColor(if signal then color.white else color.current);

buyAlert.setpaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);

buyalert.setDefaultColor(color.BLUE);

buyalert.hide();

buySignal.setPaintingStrategy(paintingStrategy.BOOLEAN_WEDGE_UP);

buySignal.setDefaultColor(color.greeN);

buysignal.setlineWeight(3);

buyPrice.setPaintingStrategy(paintingStrategy.HORIZONTAL);

buyprice.setDefaultColor(color.maGENTA);

buy.setPaintingStrategy(paintingStrategy.BOOLEAN_ARROW_UP);

buy.setDefaultColor(color.green);

buy.setlineWeight(5);

longStoploss.setDefaultColor(color.dark_orange);

longTarget.setDefaultColor(color.light_green);

longtarget.setlineWeight(2);

longstoploss.setlineWeight(2);

sellAlert.setPaintingStrategy(paintingStrategy.BOOLEAN_POINTS);

sellAlert.setDefaultColor(color.blue);

sellalert.hide();

sellSignal.setPaintingStrategy(paintingStrategy.BOOLEAN_WEDGE_DOWN);

sellSignal.setDefaultColor(color.red);

sellsignal.setlineWeight(3);

sellPrice.setPaintingStrategy(paintingStrategy.HORIZONTAL);

sellprice.setDefaultColor(color.maGENTA);

sell.setPaintingStrategy(paintingStrategy.BOOLEAN_ARROW_DOWN);

sell.setDefaultColor(color.red);

sell.setlineWeight(5);

shortStoploss.setDefaultColor(color.dark_orange);

shortTarget.setDefaultColor(color.light_green);

shorttarget.setlineWeight(2);

shortStopLoss.setLineWeight(2);

robert benson