# TOS TechnicalStockRating
input vfiLength = 130;
input averageLength = 100;
input trendQualityLength = 63;
input maxStiffness = 7;
input marketIndex = "SPY";
input weightForMoneyFlow = 1.0;
input weightForTradingAboveAverage = 1.0;
input weightForUptrend = 1.0;
input weightForTrendQuality = 1.0;
input weightForMarketDirection = 2.0;
def vfi = reference VolumeFlowIndicator(length = vfiLength, "max volume cut off" = 2.5);
def sma = Average(close, averageLength);
def ema = ExpAverage(close(marketIndex), averageLength);
def stiffness = Sum(close < sma, trendQualityLength);
plot Score;
score.hide();
def MoneyFlow = vfi > 0;
def TradingAboveAverage = close > sma;
def Uptrend = sma > sma[4];
def TrendQuality = stiffness <= maxStiffness;
def MarketDirection = ema >= ema[2];
Score = (!isnan(weightForMoneyFlow) * !isnan(MoneyFlow)) + (!isnan(weightForTradingAboveAverage) * !isnan(TradingAboveAverage)) + (!isnan(weightForUptrend * Uptrend)) + (!isnan(weightForTrendQuality * TrendQuality)) + (!isnan(weightForMarketDirection) * !isnan(MarketDirection));
DefineGlobalColor("Pre_Cyan", CreateColor(50, 200, 255)) ;
DefineGlobalColor("LabelGreen", CreateColor(0, 165, 0)) ;
DefineGlobalColor("LabelRed", CreateColor(225, 0, 0)) ;
AddLabel(yes, "Score=" +score,
if Score> 5 then GlobalColor("Pre_Cyan") else
if Score==5 then GlobalColor("LabelGreen") else GlobalColor("LabelRed"));
AddLabel(!isnan(MoneyFlow), "Money flow",
if MoneyFlow then GlobalColor("LabelGreen") else GlobalColor("LabelRed"));
AddLabel(!isnan(TradingAboveAverage), "Trading above average",
if TradingAboveAverage then GlobalColor("LabelGreen") else GlobalColor("LabelRed") );
AddLabel(!isnan(Uptrend), "Uptrend",
if Uptrend then GlobalColor("LabelGreen") else GlobalColor("LabelRed"));
AddLabel(!isnan(TrendQuality), "Trend quality",
if TrendQuality then GlobalColor("LabelGreen") else GlobalColor("LabelRed"));
AddLabel(!isnan(MarketDirection), "Market direction",
if MarketDirection then GlobalColor("LabelGreen") else GlobalColor("LabelRed"));