finnski
New member
Hey,
I'm not going to have time to complete this, but here's code that shows the nearest previous gap. I'm pretty sure it shouldn't be incredibly hard to modify it to show the previous N gaps. I hope this helps.
I'm not going to have time to complete this, but here's code that shows the nearest previous gap. I'm pretty sure it shouldn't be incredibly hard to modify it to show the previous N gaps. I hope this helps.
Code:
# Finntech 8/26/23
# [email protected]
#
# Draws morning regular trading hours price gaps from yesterday's close to today's open.
# Gap will update as the gap fills. When the gap is completely filled it will show the
# nearest next unfilled gap.
#
# TODO: 1) add support for extending previous 3 gaps (or input n if possible)
# 2) improve computing the daily high / low until now
# helper function to compute gap
script gap {
input dailyOpen = open;
input yesterdayClose = close;
input dailyHigh = high;
input dailyLow = low;
def difference = yesterdayClose - dailyOpen;
def half = dailyOpen + difference / 2;
def gapUp = if yesterdayClose < dailyOpen then 1 else 0;
def gapDown = if yesterdayClose > dailyOpen then 1 else 0;
def remaining = if gapUp then Max(dailyLow - yesterdayClose, 0) else if gapDown then Max(yesterdayClose - dailyHigh, 0) else 0;
def percent = 100 * remaining / AbsValue(difference);
def gapFilled = percent == 0;
def halfGapFilled = percent <= 50;
plot isGapUp = if (gapFilled) then Double.Nan else gapUp;
plot day = if (gapFilled) then Double.NaN else getDay();
plot gapRemaining = if (gapFilled) then Double.NaN else percent;
plot gapHigh = if (gapFilled) then Double.NaN else if gapUp then dailyLow else yesterdayClose;
plot gapLow = if (gapFilled) then Double.NaN else if gapUp then yesterdayClose else dailyHigh;
plot gapHalf = if (halfGapFilled or gapFilled) then Double.NaN else half;
}
rec dailyOpen = open(period = "DAY")[0];
rec dailyClose = close(period = "DAY")[0];
rec dailyHigh = high(period = "DAY")[0];
rec dailyLow = low(period = "DAY")[0];
rec targetDay = gap(dailyOpen[0], dailyClose[1], dailyHigh[0], dailyLow[0]).day;
rec checkRemaining = gap(dailyOpen[0], dailyClose[1], dailyHigh[0], dailyLow[0]).gapRemaining;
rec targetDayC = CompoundValue(1, if isNaN(targetDay) then targetDayC[1] else targetDay, targetDay[1]);
# TODO: find a better way to do this with recursion or fold. there seems to be an issue with passing recursive values
# into functions. it might be passing by value or limited to dataholders for reference and not functions. would
# be nice to be able to use lambdas...
#rec highUntilNow = if (getDay() > targetDay) then min(highUntilNow[1], min(dailyHigh[-1], min(dailyHigh[1], dailyHigh[0]))) else min(dailyHigh[-1], dailyHigh[0]);
#def daysBack = getDay() - targetDay;
#rec highUntilNow = if daysBack == 0 then dailyHigh else if isNan(daysBack) then highUntilNow[1] else max(highUntilNow, highUntilNow[-1]);
#rec highUntilNow = if isNan(checkRemaining) then dailyHigh else max(dailyHigh, fold k = 0 to if isNan(daysBack) then 0 else daysBack+1 with tempHigh = dailyHigh while !isNan(daysback) and !isNan(getValue(dailyHigh, -k)) and tempHigh > dailyHigh do max(tempHigh, getValue(dailyHigh, -k)));
rec highUntilNow = max(dailyHigh[-10], max(dailyHigh[-9], max(dailyHigh[-8], max(dailyHigh[-7], max(dailyHigh[-6], max(dailyHigh[-5], max(dailyHigh[-4], max(dailyHigh[-3], max(dailyHigh[-2], max(dailyHigh, dailyHigh[-1]))))))))));
#rec lowUntilNow = if (getDay() > targetDay) then min(lowUntilNow[1], min(dailyLow[-1], min(dailyLow[1], dailyLow[0]))) else min(dailyLow[-1], min(dailyLow[-1], dailyLow[0]));
#rec lowUntilNow = if isNan(checkRemaining) then dailyLow else min(dailyLow, fold j = 0 to if isNan(daysBack) then 0 else daysBack+1 with tempLow = dailyLow while !isNan(daysBack) and !isNan(getValue(dailyLow, -j)) and tempLow < dailyLow do min(tempLow, getValue(dailyLow, -j)));
rec lowUntilNow = min(dailyLow[-10], min(dailyLow[-9], min(dailyLow[-8], min(dailyLow[-7], min(dailyLow[-6], min(dailyLow[-5], min(dailyLow[-4], min(dailyLow[-3], min(dailyLow[-2], min(dailyLow[-1], dailyLow[0]))))))))));
rec lastGapDay = gap(dailyOpen[0], dailyClose[1], highUntilNow, lowUntilNow).day;
rec lastGapRemaining = gap(dailyOpen[0], dailyClose[1], highUntilNow, lowUntilNow).gapRemaining;
rec lastGapHigh = gap(dailyOpen[0], dailyClose[1], HighUntilNow, lowUntilNow).gapHigh;
rec lastGapLow = gap(dailyOpen[0], dailyClose[1], HighUntilNow, lowUntilNow).gapLow;
rec lastGapHalf = gap(dailyOpen[0], dailyClose[1], HighUntilNow, lowUntilNow).gapHalf;
rec lastGapDayC = CompoundValue(1, if isNaN(lastGapRemaining) then lastGapDayC[1] else lastGapDay, gap(dailyOpen[1], dailyClose[2], highUntilNow[1], lowUntilNow[1]).day);
rec lastGapRemainingC = CompoundValue(1, if isNaN(lastGapRemaining) then lastGapRemainingC[1] else lastGapRemaining, gap(dailyOpen[1], dailyClose[2], highUntilNow[1], lowUntilNow[1]).gapRemaining);
rec lastGapHighC = CompoundValue(1, if isNaN(lastGapHigh) or isNan(checkRemaining) then lastGapHighC[1] else lastGapHigh, gap(dailyOpen[1], dailyClose[2], highUntilNow[1], lowUntilNow[1]).gapHigh);
rec lastGapLowC = CompoundValue(1, if isNaN(lastGapLow) or isNan(checkRemaining) then lastGapLowC[1] else lastGapLow,gap(dailyOpen[1], dailyClose[2], highUntilNow[1], lowUntilNow[1]).gapLow);
rec lastGapHalfC = CompoundValue(1, if isNaN(lastGapHalf) or isNan(checkRemaining) then lastGapHalfC[1] else lastGapHalf, gap(dailyOpen[1], dailyClose[2], highUntilNow[1], lowUntilNow[1]).gapHalf);
# draw
plot gapRemaining = lastGapRemainingC;
plot gapHighPlot = lastGapHighC;
plot gapLowPlot = lastGapLowC;
plot gapHalfPlot = lastGapHalfC;
gapHighPlot.SetPaintingStrategy(PaintingStrategy.DASHES);
gapHighPlot.AssignValueColor(Color.VIOLET);
gapLowPlot.SetPaintingStrategy(PaintingStrategy.DASHES);
gapLowPlot.AssignValueColor(Color.VIOLET);
gapHalfPlot.SetStyle(Curve.LONG_DASH);
gapHalfPlot.SetDefaultColor(Color.PINK);
gapHighPlot.HideBubble();
gapLowPlot.HideBubble();
gapHalfPlot.HideBubble();
AddCloud(gapHighPlot, gapLowPlot, Color.VIOLET, Color.VIOLET);
AddLabel(yes, "Gap 1: " + gapRemaining + " % remaining, Gap 2: " + "TODO", Color.VIOLET);
# debug
#AddChartBubble(
# SecondsFromTime(0930) == 0,
# "price location" = dailyOpen,
# text = "highUntilNow " + highUntilNow + " lowUntilNow " + lowUntilNow +
# "\nremainging " + lastGapRemaining[0] + " remaingingC " + lastGapRemainingC[0] +
## "\nlastGapHigh " + lastGapHigh + " lastGapHighC " + lastGapHighC +
# "\nlastGapLow " + lastGapLow + " lastGapLowC " + lastGapLowC +
# "\nlastGapHalf " + lastGapHalf + " lastGapHalfC " + lastGapHalfC +
# "\ntargetDay " + targetDay + " daysBack " + daysBack +
# "\ndailyHigh " + dailyHigh + " dailyLow " + dailyLow,
# color = Color.YELLOW,
# up = no
#);