## Edit to the gap study code from thinkscript.com
## Originally made by yungtraderfrommontana
## Edited by Chemmy
input conso = .02;
input outo = .01;
input coc = close;
input linRegLength = 21;
input smLength = 3;
input displace2 = 0;
input length = 10;
def bn = BarNumber();
def lastBar = HighestAll(if IsNaN(close) then 0 else bn);
def offset = Min(length - 1, lastBar - bn);
def swingLow = low < Lowest(low[1], length - 1) and low == GetValue(Lowest(low, length), -offset);
# identify the very last swing low point
def lowPointOneBarNumber = HighestAll(if swingLow then bn else 0);
def lowPointOneValue = if bn == lowPointOneBarNumber then low else lowPointOneValue[1];
plot low1 = if bn < lowPointOneBarNumber then Double.NaN else lowPointOneValue;
low1.SetDefaultColor(Color.LIGHT_RED);
# SWING HIGH
# define swing high points
def swingHigh = high > Highest(high[1], length - 1) and high == GetValue(Highest(high, length), -offset);
# identify the very last swing high point
def highPointOneBarNumber = HighestAll(if swingHigh then bn else 0);
def highPointOneValue = if bn == highPointOneBarNumber then high else highPointOneValue[1];
plot high1 = if bn < highPointOneBarNumber then Double.NaN else highPointOneValue;
high1.SetDefaultColor(Color.CYAN);
# ADJUST CANDLE COLORS
# change candle colors just to make it easier to see what we are working with
#AssignPriceColor(if swingLow then Color.cyan else if swingHigh then Color.mageNTA else Color.current);
# End Swing High and Swing Low
def value = Average(Inertia(coc[-displace2], linRegLength), smLength);
def error = Average(sterr(coc[-displace2], linRegLength), smLength);
def MiddleLine = value;
def total = if close < open then (high + 2 * low + close) / 2 else if close > open then (2 * high + low + close) / 2 else (high + low + 2 * close) / 2;
def UpperPr = total[1] - low[1];
def LowerPr = total[1] - high[1];
def price = close;
def length4 = 10;
def displace = 0;
def AvgExp = ExpAverage(price[-displace], length4);
def length6 = 48.5;
def AvgExp2 = ExpAverage(price[-displace], length6);
def c = close;
def length2 = 30;
def calclength2 = 5;
def smoothlength2 = 3;
input agg = AggregationPeriod.DAY;
def o10 = open(period = agg);
def c10 = close(period = agg);
def data = fold i = 0 to length2
with s
do s + (if c10 > GetValue(o10, i)
then 1
else if c10 < GetValue(o10, i)
then - 1
else 0);
input averagetype = AverageType.EXPONENTIAL;
def EMA5 = MovingAverage(averagetype, data, calclength2);
def Main = MovingAverage(averagetype, EMA5, smoothLength2);
def Signal = MovingAverage(averagetype, Main, smoothLength2);
def ob3 = if IsNaN(c10) then Double.NaN else Round(length2 * .7);
input BuyEntry = 3;
input SellEntry = 3;
plot QB = Highest(high, BuyEntry);
plot QS = Lowest(low, SellEntry);
plot midline = (qs[1]+qb[1])/2;
plot differencechange = ((main-signal) < 1) and ((main - signal) > -1) and (main < ob3);
plot comboupstrongest = (close > qb[1]) and (c[1] < qb[2]) and (((open - qb[1])/100) < outo) and (((qb[1] - Avgexp)/100) < conso) and (close < midline[1]);
#AddVerticalLine(comboupstrongest, "", Color.Green, curve.SHORT_DASH);
plot comboupstrong = (close > qb[1]) and (c[1] < qb[2]) and (((open - qb[1])/100) < outo) and (((qb[1] - Avgexp)/100) < conso) and (open[1] or open[2] or open[3] < midline) and comboupstrongest is false and (open < qb[1]) and (open[1] > Qs[2]) and (close[1] < midline[2]);
plot comboupweak = (close > qb[1]) and (close[1] < qb[2]) and (((open - qb[1])/100) < outo) and (((qb[1] - Avgexp)/100) < conso) and comboupstrongest is false and comboupstrong is false and (close > open) and (low < middleline) and (close > open);
plot midcrossgapup = (((open < midline) and ((close > midline)) or ((close[1] < midline[2])) and (open > midline[1]))) and (close > open) and (differencechange) and (close[1] < qb[2]) and (close > upperpr);
plot combodownstrongest = (qs[1] > qs[2]) and (close < qs[1]) and (c[1] > qs[2]) and (((qs[1] - open)/100) < outo) and (((Avgexp - qs[1])/100) < conso);
plot combodownstrong = (close < qs[1]) and (c[1] > qs[2]) and (((qs[1] - open)/100) < outo) and (((Avgexp - qs[1])/100) < conso) and (open[1] or open[2] or open[3] > midline) and combodownstrongest is false and (close < qs[1]) and (close[1] < QB[2]) and (close[1] > midline[2]);
plot combodownweak = (close < qs[1]) and (c[1] > qs[2]) and (((qs[1] - open)/100) < outo) and (((Avgexp - qs[1])/100) < conso) and combodownstrong is false and combodownstrongest is false and (close < open) and (low > middleline);
plot midcrossgapdown = (((open > midline) and ((close < midline)) or ((close[1] > midline[2])) and (open < midline[1]))) and (close < open) and (close[1] > qs[2]) and (close < lowerpr);
plot goodsetup =((swinglow or swinglow[1] or swinglow[2])) and ((comboupstrong or comboupstrongest or midcrossgapup));
plot sellputs = (lowerpr < QS);