This indicator is inspired by a Tradingview indicator that tracked the 40 biggest stocks in the SP500. This version uses the SP500 components.
This is my first crack at Thinkscript. Please let me know how it can be improved. My first thought is adding TICK/TRIN. I'm still struggling with best practice for resetting the start of day.
This is my first crack at Thinkscript. Please let me know how it can be improved. My first thought is adding TICK/TRIN. I'm still struggling with best practice for resetting the start of day.
Ruby:
# SP Hi/Lo plots the number of components of the SP500 making a new hi/lo in each period.
# The SPY is plotted on the background
# The start of the trading day will have components making new highs and new lows. A single component can do both in a single bar.
# Then double sided nature will often become one sided later in the day.
declare lower;
input DisplayLabel = yes;
#input averageType = AverageType.WILDERS;
#input aggregationPeriod = AggregationPeriod.DAY;
#input length = 5;
def regularSession = SecondsFromTime(0930) > 0 && SecondsTillTime(1600) > 0;
script MyHi {
input ticker = "SPY";
input weight = 1;
def regularSession = SecondsFromTime(0930) > 0 && SecondsTillTime(1600) > 0;
def p = high(symbol = ticker);
def pp = if regularSession then if (p > pp[1]) then p else pp[1] else p;
plot rv = p > pp[1];
}
script MyLo {
input ticker = "SPY";
input weight = 1;
def regularSession = SecondsFromTime(0930) > 0 && SecondsTillTime(1600) > 0;
def p = low(symbol = ticker);
def pp = if regularSession then if (p < pp[1]) then p else pp[1] else p;
plot rv = p < pp[1];
}
plot ZeroLine = 0;
ZeroLine.SetDefaultColor(GetColor(9));
# $ for c in xlk xlv xly xlf xlc xli xlp xle xlu xlb xlre; do echo "MyLo(\"$c\", 1) +"; done
def new_highs = (
MyHi("xlk", 1) + MyHi("xlv", 1) + MyHi("xly", 1) + MyHi("xlf", 1) +
MyHi("xlc", 1) + MyHi("xli", 1) + MyHi("xlp", 1) + MyHi("xle", 1) +
MyHi("xlu", 1) + MyHi("xlb", 1) + MyHi("xlre", 1)) / 11;
def new_lows = (
MyLo("xlk", 1) + MyLo("xlv", 1) + MyLo("xly", 1) + MyLo("xlf", 1) +
MyLo("xlc", 1) + MyLo("xli", 1) + MyLo("xlp", 1) + MyLo("xle", 1) +
MyLo("xlu", 1) + MyLo("xlb", 1) + MyLo("xlre", 1)) / 11;
def Spy = (MyHi("spy",1) - MyLo("spy", 1)) * 0.5;
AddCloud(Spy, ZeroLine, Color.LIGHT_GREEN, Color.LIGHT_RED);
plot pNet = new_highs - new_lows;
pNet.SetDefaultColor(Color.YELLOW);
def newDay = GetDay() != GetDay()[1];
AddVerticalLine(newDay, "New Day", Color.GRAY, Curve.SHORT_DASH);
plot Highs = new_highs;
Highs.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Highs.AssignValueColor(if Highs >= Highs[1] then Color.UPTICK else Color.DOWNTICK);
plot Lows = new_lows * -1;
Lows.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Lows.AssignValueColor(if Lows > Lows[1] then Color.UPTICK else Color.DOWNTICK);
AddLabel(DisplayLabel, "Hi/Lo Count: " + Round(Highs*11,0) + ":" + -Round(Lows*11,0), if pNet < 0 then Color.DOWNTICK else Color.UPTICK);