Technical Ratings[TradingView] for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
0Pee9ij.png

Creator Message:
OVERVIEW

This indicator calculates TradingView's well-known "Strong Buy", "Buy", "Neutral", "Sell" or "Strong Sell" states using the aggregate biases of 26 different technical indicators.
█ FEATURES

Differences with the built-in version

 • The built-in version produces values matching the states displayed in the "Technicals" ratings gauge; this one does not always.
 • A strategy version is also available as a built-in; this script is an indicator—not a strategy.
 • This indicator will show a slightly different vertical scale, as it does not use a fixed scale like the built-in.
 • This version allows control over repainting of the signal when you do not use a higher timeframe. Higher timeframe (HTF) information from this version does not repaint.
 • You can adjust the weight of the Oscillators and MAs components of the rating here.
 • You can configure markers on signal breaches of configurable levels, or on advances declines of the signal.

The indicator's settings allow you to:
 • Choose the timeframe you want calculations to be made on.
 • When not using a HTF, you can select a repainting or non-repainting signal.
 • When using both MAs and Oscillators groups to calculate the rating, you can vary the weight of each group in the calculation. The default is 50/50.
  Because the MAs group uses longer periods for some of its components, its value is not as jumpy as the Oscillators value.
  Increasing the weight of the MAs group will thus have a calming effect on the signal.
 • Alerts can be created on the indicator using the conditions configured to control the display of markers.

Display
The calculated rating is displayed as columns, but you can change the style in the inputs. The color of the signal can be one of three colors: bull, bear, or neutral. You can choose from a few presets, or check one and edit its color. The color is determined from the rating's value. Between 0.1 and -0.1 it is in the neutral color. Above/below 0.1/-0.1 it will appear in the bull/bear color. The intensity of the bull/bear color is determined by cumulative advances/declines in the rating. It is capped to 5, so there are five intensities for each of the bull/bear colors.

The "Strong Buy", "Buy", "Neutral", "Sell" or "Strong Sell" state of the last calculated value is displayed to the right of the last bar for each of the three groups: All, MAs and Oscillators. The first value always reflects your selection in the "Rating uses" field and is the one used to display the signal. A "Strong Buy" or "Strong Sell" state appears when the signal is above/below the 0.5/-0.5 level. A "Buy" or "Sell" state appears when the signal is above/below the 0.1/-0.1 level. The "Neutral" state appears when the signal is between 0.1 and -0.1 inclusively.

Five levels are always displayed: 0.5 and 0.1 in the bull color, zero in the neutral color, and -0.1 and - 0.5 in the bull color.

The levels that can be used to determine the breaches displaying long/short markers will only be visible when their respective long/short markers are turned on in the "Direction" input. The levels appear as a bright dotted line in bull/bear colors. You can control both levels separately through the "Longs Level" and "Shorts Level" inputs.

If you specify a higher timeframe that is not greater than the chart's timeframe, an error message will appear and the indicator's background will turn red, as it doesn't make sense to use a lower timeframe than the chart's.

Markers
Markers are small triangles that appear at the bottom and top of the indicator's pane. The marker settings define the conditions that will trigger an alert when you configure an alert on the indicator. You can:
 • Choose if you want long, short or both long and short markers.
 • Determine the signal level and/or the number of cumulative advances/declines in the signal which must be reached for either a long or short marker to appear.
  Reminder: the number of advances/declines is also what controls the brightness of the plotted signal.
 • Decide if you want to restrict markers to ones that alternate between longs and shorts, if you are displaying both directions.
  This helps to minimize the number of markers, e.g., only the first long marker will be displayed, and then no more long markers will appear until a short comes in, then a long, etc.

Repainting
This indicator uses a two-pronged approach to control repainting. The repainting of the displayed signal is controlled through the "Repainting" field in the script's inputs. This only applies when you have "Same as chart" selected in the "Timeframe" field, as higher timeframe data never repaints. Regardless of that setting, markers and thus alerts never repaint.

When using the chart's timeframe, choosing a non-repainting signal makes the signal one  bar late, so that it only displays a value once the bar it was calculated has elapsed. When using a higher timeframe, new values are only displayed once the higher timeframe completes.

Because the markers never repaint, their logic adapts to the repainting setting used for the signal. When the signal repaints, markers will only appear at the close of a realtime bar. When the signal does not repaint (or if you use a higher timeframe), alerts will appear at the beginning of the realtime bar, since they are calculated on values that already do not repaint.

█ CALCULATIONS
The indicator calculates the aggregate value of two groups of indicators: moving averages and oscillators.

The "MAs" group is comprised of 15 different components:
 • Six Simple Moving Averages of periods 10, 20, 30, 50, 100 and 200
 • Six Exponential Moving Averages of the same periods
 • A Hull Moving Average of period 9
 • A Volume-weighed Moving Average of period 20
 • Ichimoku‎

The "Oscillators" group includes 11 components:
 • RSI
 • Stochastic
 • CCI
 • ADX
 • Awesome Oscillator
 • Momentum
 • MACD
 • Stochastic RSI
 • Wiliams %R
 • Bull Bear Power
 • Ultimate Oscillator

The state of each group's components is evaluated to a +1/0/-1 value corresponding to its bull/neutral/bear bias. The resulting value for each of the two groups are then averaged to produce the overall value for the indicator, which oscillates between +1 and -1.

CODE:
CSS:
#// © TradingView
#indicator("Technical Ratings", "Ratings", precision = 2)
# Converted by Sam4Cok@Samer800 - 01/2023 - Not typlical
declare lower;
#// ————— Inputs
input BarColor = yes;
input useChartTimeframe = yes;
input HigherTimeframe = AggregationPeriod.FIFTEEN_MIN;
input ShowLabel    = Yes;
input ShowSignalLine = yes;
input repaintInput = {default ON, OFF};    # "Repainting"
input calcsInput  = {default "MAs and Oscillators", "MAs", "Oscillators"};# "Rating uses"
input WeightOfMasPer = 50;                 # "Weight of MAs (%)"
input SignalDirection = {default "None", "Longs", "Shorts", "Longs and Shorts"};#  "Direction"
input Alternate = {default ON, OFF};       # "Alternate Longs & Shorts"
input levelUpInput = 0.5;                  # "Longs Level"
input levelDnInput = -0.5;                 # "Shorts Level"
input gradInput    = 0;                    # "Cumulative adv./decl."

def na = Double.NaN;
def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;
script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if isNaN(data) then repl else data;
    plot return = ret_val;
}
script fixnan{
    input source = close;
    def fix = if !isNaN(source) then source else fix[1];
    plot result = fix;
}
def ctf; def hltf; def htf; def ltf; def vtf;
if useChartTimeframe {
    ctf = close;
    hltf = hl2;
    htf = high;
    ltf = low;
    vtf = volume;
    } else if !useChartTimeframe {
    ctf = close(Period=HigherTimeframe);
    hltf = hl2(Period=HigherTimeframe);
    htf = high(Period=HigherTimeframe);
    ltf = low(Period=HigherTimeframe);
    vtf = volume(Period=HigherTimeframe);
    } else {
    ctf  = nz(ctf[1],close);
    hltf = nz(hltf[1],hl2);
    htf  = nz(htf[1], high);
    ltf  = nz(ltf[1], low);
    vtf  = nz(vtf[1], volume);
}
def maInput = if WeightOfMasPer>100 then 1 else
              if WeightOfMasPer<0 then 0 else WeightOfMasPer/100;
def htfUsed = !useChartTimeframe;
def isrealtime = !IsNaN(ctf) and !IsNaN(ctf[1]);
def idx1 = if (htfUsed and isrealtime) then 1 else 0;
def idx2 = if (repaintInput and !htfUsed) or (htfUsed and isrealtime) then 0 else 1;
#def dir = if SignalDirection == SignalDirection."Longs" then 1 else
#          if SignalDirection == SignalDirection."Shorts" then -1 else
#          if SignalDirection == SignalDirection."Longs and Shorts" then 0 else na;
def doLongs  = SignalDirection == SignalDirection."Longs" or SignalDirection == SignalDirection."Longs and Shorts";
def doShorts = SignalDirection == SignalDirection."Shorts" or SignalDirection == SignalDirection."Longs and Shorts";

#-------Color
DefineGlobalColor("LIME1" , CreateColor(0,255,0));
DefineGlobalColor("LIME2" , CreateColor(0,153,0));
DefineGlobalColor("LIME3" , CreateColor(0,51,0));
DefineGlobalColor("RED1" , CreateColor(255,0,128));
DefineGlobalColor("RED2" , CreateColor(153,0,76));
DefineGlobalColor("RED3" , CreateColor(51,0,25));
DefineGlobalColor("GRAY"  , Color.GRAY);

#// Levels determining "Strong Buy/Sell" and "Buy/Sell" ratings.
def LEVEL_STRONG = 0.5;
def LEVEL_WEAK   = 0.1;

#// Determine base bull/bear colors as per user selection.
#def paint = nz(if repaintInput==repaintInput.OFF then 1 else 0);
def alt = nz(if Alternate==Alternate.ON then 1 else 0);
def calcInput = if calcsInput==calcsInput."MAs and Oscillators" then 0 else
                if calcsInput==calcsInput."MAs" then 1 else -1;
#                if calcsInput==calcsInput."Oscillators" then -1 else calcInput[1];

#          if dirInput == dirInput."Longs and Shorts" then 0 else

#textFromRating(series float rating) =>
script textFromRating {
input rating = 0;
    def textFromRating =
        if rating >  0.5 then 2 else
        if rating >  0.1 then 1 else
        if rating < -0.5 then -2 else
        if rating < -0.1 then -1 else 0;
       plot result = textFromRating;
}
#// ————— Helper functions.
#// @function        Test if the trend is now up by examining the close price in relation to the 50 EMA.  
#// @returns         (bool) True if the `close` is above its 50 EMA, false otherwise.
def trendUp = ctf > ExpAverage(ctf, 50);
def trendDn = ctf < ExpAverage(ctf, 50);
#vwma(source, length)
script VWMA {
    input x = close;
    input y = 15;
    input vtf = volume;
def VWMA = SimpleMovingAvg(x * vtf, y) / SimpleMovingAvg(vtf, y);
    Plot result = VWMA;
}
#notNa(series float src) =>
script notNa {
    input src = close;
    def notNa = !IsNaN(src) and !IsNaN(src[1]);
    plot result = notNa;
}
#ratingMa(series float ma, series float src) =>
script ratingMa {
    input ma = close;
    input src = close;
    def ratingMa = Sign(src - ma);
    plot result = ratingMa;
}
#ratingBullBear(series bool bullCond, series bool bearCond) =>
script ratingBullBear {
    input bullCond = yes;
    input bearCond = no;
    def ratingBullBear = if IsNaN(bullCond) or IsNaN(bearCond) then Double.NaN else
                         if bullCond then 1 else if bearCond then -1 else 0;
    plot result = ratingBullBear;
}
# stoch(source, high, low, length) =>
script stoch {
    input src = close;
    input h = high;
    input l = low;
    input len = 0;
    def stoch = 100 * (src - Lowest(l, len)) / (Highest(h, len) - Lowest(l, len));
    plot return = stoch;
}
#// ————— Ichimoku rating.
#donchian(series int length) =>
script donchian {
    input length = 10;
    def hh = Highest(high, length);
    def ll = Lowest(low, length);
    def  donchian = (hh + ll) / 2;
    plot result = donchian;
}
def conversion = donchian(9);
def base       = donchian(26);
def lead1      = (conversion + base) / 2;
def lead2      = donchian(52);
def bullIchim = lead1 > lead2 and ctf > lead1 and ctf < base and ctf[1] < conversion and ctf > conversion;
def bearIchim = lead2 > lead1 and ctf < lead2 and ctf > base and ctf[1] > conversion and ctf < conversion;
def resultDon = ratingBullBear(bullIchim, bearIchim);
def Ichimoku = if !(IsNaN(lead1) or IsNaN(lead2) or IsNaN(ctf) or IsNaN(ctf[1]) or IsNaN(base) or IsNaN(conversion))
               then resultDon  else na;
def ratingIchimoku = Ichimoku;

#// ————— Oscillator ratings.
def nRSI    = RSI(Price = ctf, Length = 14);
def bullRsi = nRSI < 30 and (nRSI < nRSI[1]);
def bearRsi = nRSI > 70 and (nRSI > nRSI[1]);
def resultrsi = ratingBullBear(bullRsi, bearRsi);
def notRsi = if notNa(nRSI) then resultrsi else na;
def ratingRsi = notRsi;

def k = Average(stoch(ctf, htf, ltf, 14), 3);
def d = Average(k, 3);
def BullStoch = k < 20 and d < 20 and k > d and k[1] < d[1];
def BearStoch = k > 80 and d > 80 and k < d and k[1] > d[1];
def resultstoch = ratingBullBear(BullStoch, BearStoch);
def Stoch = if notNa(k) and notNa(d) then resultstoch else na;
def ratingStoch = Stoch;

def nnCci = CCI(length = 20);
def BullCCI = nnCci < -100 and (nnCci > nnCci[1]);
def BearCCI = nnCci > 100 and (nnCci < nnCci[1]);
def resultcci = ratingBullBear(BullCCI, BearCCI);
def nCci = if notNa(nnCci) then resultcci else na;
def ratingCci = nCci;

def hiDiff = htf - htf[1];
def loDiff = ltf[1] - ltf;
def plusDM = if hiDiff > loDiff and hiDiff > 0 then hiDiff else 0;
def minusDM =  if loDiff > hiDiff and loDiff > 0 then loDiff else 0;
def tr = TrueRange(htf, ctf, ltf);
def ATR = WildersAverage(tr, 14);
def diPlus = 100 * WildersAverage(plusDM, 14) / ATR;
def diMinus = 100 * WildersAverage(minusDM, 14) / ATR;
def DX = if (diPlus + diMinus > 0) then 100 * AbsValue(diPlus - diMinus) / (diPlus + diMinus) else 0;
def ADX = WildersAverage(DX, 14);
def BullADX = ADX > 20 and diPlus[1] < diMinus[1] and diPlus > diMinus;
def BearADX = ADX > 20 and diPlus[1] > diMinus[1] and diPlus < diMinus;
def resultAdx = ratingBullBear(BullADX, BearADX);
def nAdx = if notNa(diPlus) and notNa(diMinus) and !IsNaN(ADX) then resultAdx else na;
def ratingAdx = nAdx;

def ao = Average(hltf, 5) - Average(hltf, 34);
def aoXUp  = if (ao > 0) then aoXUp[1] + 1 else 0;
def aoXDn  = if (ao < 0) then aoXDn[1] + 1 else 0;
def BullAO = aoXUp==1 or (ao > 0 and ao[1] > 0 and (ao > ao[1]));
def BearAO = aoXDn==1 or (ao < 0 and ao[1] < 0 and (ao < ao[1]));
def resultAo = ratingBullBear(BullAO, BearAO);
def nAo = if notNa(ao) then resultAo else na;
def ratingAo = nAo;

def mom = ctf - nz(ctf[10],ctf[1]);
def resultMom = ratingBullBear(mom > mom[1], mom < mom[1]);
def nMom = if notNa(mom) then resultMom else na;
def ratingMom = nMom;

def macd = ExpAverage(ctf, 12) - ExpAverage(ctf, 26);
def signal = ExpAverage(macd, 9);
def resultmacd = ratingBullBear(macd > signal, macd < signal);
def nMacd = if !IsNaN(macd) and !IsNaN(signal) then resultmacd else na;
def ratingMacd = nMacd;

def k1 = Average(stoch(nRSI, nRSI, nRSI, 14), 3);
def d1 = Average(k1, 3);
def BullStochRsi = trendDn and k1 < 20 and d1 < 20 and k1 > d1 and k1[1] < d1[1];
def BearStochRsi = trendUp and k1 > 80 and d1 > 80 and k1 < d1 and k1[1] > d1[1];
def resultStochRsi = ratingBullBear(BullStochRsi, BearStochRsi);
def StochRsi = if notNa(k1) and notNa(d1) and !IsNaN(trendDn) and !IsNaN(trendUp) then resultStochRsi else na;
def ratingStochRsi = StochRsi;

def wpr = WilliamsPercentR(Length = 14);
def BullWPR = wpr < -80 and (wpr > wpr[1]);
def BearWPR = wpr > -20 and (wpr < wpr[1]);
def resultWpr = ratingBullBear(BullWPR, BearWPR);
def nWpr = if notNa(wpr) then resultWpr else na;
def ratingWpr = nWpr;

def powerBull = htf - ExpAverage(ctf, 13);
def powerBear = ltf  - ExpAverage(ctf, 13);
def BullBbp   = trendUp and powerBear < 0 and (powerBear > powerBear[1]);
def BearBbp   = trendDn and powerBull > 0 and (powerBull < powerBear[1]);
def resultBbp = ratingBullBear(BullBbp, BearBbp);
def Bbp = if notNa(powerBull) and notNa(powerBear) and !IsNaN(trendDn) and !IsNaN(trendUp) then resultBbp else na;
def ratingBbp = Bbp;

def pFast =  7;
def pMid  = 14;
def pLong = 28;
def tl  = if ctf[1] < ltf then ctf[1] else ltf;
def uo; def p0; def p1;def v7;def v8; def v9;
def v1  = Sum(tr, pFast);
def v2  = Sum(tr, pMid);
def v3  = Sum(tr, pLong);
def v4  = Sum(ctf - tl, pFast);
def v5  = Sum(ctf - tl, pMid);
def v6  = Sum(ctf - tl, pLong);
if v1 != 0 and v2 != 0 and v3 != 0 {
    p0 = pLong / pFast;
    p1 = pLong / pMid;
    v7 = (v4 / v1) * p0;
    v8 = (v5 / v2) * p1;
    v9 = (v6 / v3);
    uo = 100 * (v7 + v8 + v9) / (p0 + p1 + 1);
    } else {
    p0 = p0[1];
    p1 = p1[1];
    v7 = v7[1];
    v8 = v8[1];
    v9 = v9[1];
    uo = na;#uo[1];
}
def resultUo = ratingBullBear(uo > 70, uo < 30);
def nUo = if !IsNaN(uo) then resultUo else na;
def ratingUo = nUo;

def sma10 = ratingMa(Average(ctf,10), ctf);
def sma20 = ratingMa(Average(ctf,20), ctf);
def sma30 = ratingMa(Average(ctf,30), ctf);
def sma50 = ratingMa(Average(ctf,50), ctf);
def sma100 = ratingMa(Average(ctf,100), ctf);
def sma200 = ratingMa(Average(ctf,200), ctf);
def ema10 = ratingMa(ExpAverage(ctf,10), ctf);
def ema20 = ratingMa(ExpAverage(ctf,20), ctf);
def ema30 = ratingMa(ExpAverage(ctf,30), ctf);
def ema50 = ratingMa(ExpAverage(ctf,50), ctf);
def ema100 = ratingMa(ExpAverage(ctf,100), ctf);
def ema200 = ratingMa(ExpAverage(ctf,200), ctf);
def hma9 = ratingMa(HullMovingAvg(ctf,9), ctf);
def vma20 = ratingMa(VWMA(ctf,20,vtf), ctf);
def ich = ratingIchimoku;

def MasAvg = (!isNaN(sma10) + !isNaN(sma20) + !isNaN(sma30) + !isNaN(sma50) + !isNaN(sma100) + !isNaN(sma200) + !isNaN(ich) +
 !isNaN(ema10) + !isNaN(ema20) + !isNaN(ema30) + !isNaN(ema50) + !isNaN(ema100) + !isNaN(ema200) + !isNaN(hma9) + !isNaN(vma20));
def ratingMas = (nz(sma10) + nz(sma20) + nz(sma30) + nz(sma50) + nz(sma100) + nz(sma200) + nz(ich) +
                nz(ema10) + nz(ema20) + nz(ema30) + nz(ema50) + nz(ema100) + nz(ema200) + nz(hma9) + nz(vma20)) / MasAvg;

#    // Calculate Oscillators rating.
def oratingRsi = ratingRsi;
def oratingStoch = ratingStoch;
def oratingCci = ratingCci;
def oratingAdx = ratingAdx;
def oratingAo = ratingAo;
def oratingMom = ratingMom;
def oratingMacd = ratingMacd;
def oratingStochRsi = ratingStochRsi;
def oratingWpr = ratingWpr;
def oratingBbp = ratingBbp;
def oratingUo = ratingUo;

def AvgOsc = !isNaN(oratingRsi)+!isNaN(oratingStoch)+!isNaN(oratingCci)+!isNaN(oratingAdx)+!isNaN(oratingAo) +
!isNaN(oratingMom)+!isNaN(oratingMacd)+!isNaN(oratingStochRsi)+!isNaN(oratingWpr)+!isNaN(oratingBbp)+!isNaN(oratingUo);
def ratingOsc = (nz(oratingRsi) + nz(oratingStoch) + nz(oratingCci) + nz(oratingAdx) + nz(oratingAo) + nz(oratingMom) +
                nz(oratingMacd) + nz(oratingStochRsi) + nz(oratingWpr) + nz(oratingBbp) + nz(oratingUo)) / AvgOsc;

#    // Calculate weighted average of the two groups: MAs and Oscillators.
def ratingTot = (ratingMas * maInput) + (ratingOsc * (1 - maInput));

#    [ratingTot[offset], ratingMas[offset], ratingOsc[offset]]
#// ————— Build signal color.
script countRising {
input src = close;
    def cnt;
    def chg = AbsValue(src-src[1]);
    cnt =
        if src == 0 then 0 else
        if chg  > 0 then min(5, nz(cnt[1]) + 1) else
        if chg  < 0 then max(1, nz(cnt[1]) - 1) else 0;
    def countRising = if src > 0 then cnt else -cnt;
    plot result = countRising;
}
script signalColor {
input gradient = 0;
input ratingTot = close;
    def col  = if gradient > 0 then if ratingTot>ratingTot[1] then 3 else 2 else
               if gradient < 0 then if ratingTot<ratingTot[1] then -3 else -2 else
               if gradient==0 then if ratingTot>0 then 1 else -1 else 0;
plot result = col;
}
def condBuy      = ratingTot >  LEVEL_WEAK;
def condSell     = ratingTot < -LEVEL_WEAK;
def valsBuy      = if condBuy  then ratingTot else 0;
def valsSell     = if condSell then ratingTot else 0;
def risingBuys   = countRising(valsBuy);
def fallingSells = countRising(valsSell);
def gradientLvl  = if condBuy then risingBuys else if condSell then fallingSells else 0;
def signalColor  = signalColor(gradientLvl,ratingTot);

def mcondBuy      = ratingMas >  LEVEL_WEAK;
def mcondSell     = ratingMas < -LEVEL_WEAK;
def mvalsBuy      = if mcondBuy  then ratingMas else 0;
def mvalsSell     = if mcondSell then ratingMas else 0;
def mrisingBuys   = countRising(mvalsBuy);
def mfallingSells = countRising(mvalsSell);
def mgradientLvl  = if mcondBuy then mrisingBuys else if mcondSell then mfallingSells else 0;
def masColor  = signalColor(mgradientLvl,ratingMas);

def ocondBuy      = ratingOsc >  LEVEL_WEAK;
def ocondSell     = ratingOsc < -LEVEL_WEAK;
def ovalsBuy      = if ocondBuy  then ratingOsc else 0;
def ovalsSell     = if ocondSell then ratingOsc else 0;
def orisingBuys   = countRising(ovalsBuy);
def ofallingSells = countRising(ovalsSell);
def ogradientLvl  = if ocondBuy then orisingBuys else if ocondSell then ofallingSells else 0;
def oscColor  = signalColor(ogradientLvl,ratingOsc);


plot "0"   = if isNaN(close) then na else 0;
"0".SetDefaultColor(GlobalColor("GRAY"));

plot userRating = if isNaN(close) then na else if calcInput==0 then (ratingTot) else na;
userRating.SetLineWeight(4);
userRating.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
userRating.AssignValueColor(if signalColor==3 then GlobalColor("LIME1") else
                            if signalColor==2 then GlobalColor("LIME2") else
                            if signalColor==1 then GlobalColor("LIME3") else
                            if signalColor==-3 then GlobalColor("RED1") else
                            if signalColor==-2 then GlobalColor("RED2") else
                            if signalColor==-1 then GlobalColor("RED3") else  GlobalColor("GRAY"));
plot masRating = if isNaN(close) then na else if calcInput==1 then (ratingMas) else na;
masRating.SetLineWeight(4);
masRating.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
masRating.AssignValueColor(if masColor==3 then GlobalColor("LIME1") else
                            if masColor==2 then GlobalColor("LIME2") else
                            if masColor==1 then GlobalColor("LIME3") else
                            if masColor==-3 then GlobalColor("RED1") else
                            if masColor==-2 then GlobalColor("RED2") else
                            if masColor==-1 then GlobalColor("RED3") else  GlobalColor("GRAY"));
plot oscRating = if isNaN(close) then na else if calcInput==-1 then (ratingOsc) else na;
oscRating.SetLineWeight(4);
oscRating.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
oscRating.AssignValueColor(if oscColor==3 then GlobalColor("LIME1") else
                            if oscColor==2 then GlobalColor("LIME2") else
                            if oscColor==1 then GlobalColor("LIME3") else
                            if oscColor==-3 then GlobalColor("RED1") else
                            if oscColor==-2 then GlobalColor("RED2") else
                            if oscColor==-1 then GlobalColor("RED3") else  GlobalColor("GRAY"));

#def "50"  = if isNaN(close) then na else LEVEL_STRONG;
#"50".SetDefaultColor(GlobalColor("RED1"));
plot "10"  = if isNaN(close) then na else LEVEL_WEAK;
"10".SetDefaultColor(GlobalColor("LIME2"));
"10".SetStyle(Curve.MEDIUM_DASH);
#def "-50" = if isNaN(close) then na else -LEVEL_STRONG;
#"-50".SetDefaultColor(GlobalColor("LIME1"));
plot "-10" = if isNaN(close) then na else -LEVEL_WEAK;
"-10".SetDefaultColor(GlobalColor("RED2"));
"-10".SetStyle(Curve.MEDIUM_DASH);
#/ Marker breach levels.
plot LongLevel = if isNaN(close) then na else if doLongs then levelUpInput else na;#, "Long Level"
LongLevel.SetDefaultColor(GlobalColor("LIME2"));
LongLevel.SetStyle(Curve.SHORT_DASH);
plot ShortLevel =if isNaN(close) then na else if doShorts then levelDnInput else na;#, "Short Level"
ShortLevel.SetDefaultColor(GlobalColor("RED2"));
ShortLevel.SetStyle(Curve.SHORT_DASH);


def all = if ShowLabel then textFromRating(ratingTot) else na;
def osc = if ShowLabel then textFromRating(ratingOsc) else na;
def ma = if ShowLabel then textFromRating(ratingMas) else na;

AddLabel(if all==2 then 1 else 0, "All: Strong Buy" , Color.GREEN);
AddLabel(if all==1 then 1 else 0, "All: Buy" , Color.DARK_GREEN);
AddLabel(if all==-2 then 1 else 0, "All: Strong Sell" , Color.RED);
AddLabel(if all==-1 then 1 else 0, "All: Sell" , Color.DARK_RED);
AddLabel(if all==0 then 1 else 0, "All: Neutral" , Color.GRAY);

AddLabel(if osc==2 then 1 else 0, "Osc: Strong Buy" , Color.GREEN);
AddLabel(if osc==1 then 1 else 0, "Osc: Buy" , Color.DARK_GREEN);
AddLabel(if osc==-2 then 1 else 0, "Osc: Strong Sell" , Color.RED);
AddLabel(if osc==-1 then 1 else 0, "Osc: Sell" , Color.DARK_RED);
AddLabel(if osc==0 then 1 else 0, "Osc: Neutral" , Color.GRAY);

AddLabel(if ma==2 then 1 else 0, "MAs: Strong Buy" , Color.GREEN);
AddLabel(if ma==1 then 1 else 0, "MAs: Buy" , Color.DARK_GREEN);
AddLabel(if ma==-2 then 1 else 0, "MAs: Strong Sell" , Color.RED);
AddLabel(if ma==-1 then 1 else 0, "MAs: Sell" , Color.DARK_RED);
AddLabel(if ma==0 then 1 else 0, "MAs: Neutral" , Color.GRAY);

#// ————— Alerts.
#// The triggering conditions can only be met in such a way that they never repaint.
#// If no HTF is used and no-repainting is required, we wait for the bar to close. Otherwise we can trigger at the bar's open because the signal is non-repainting signal.
def ensureNoRepaintIdx = if !htfUsed and repaintInput then 1 else 0;
def crossUp = if (userRating > levelUpInput) then crossUp[1] + 1 else 0;
def crossDn = if (userRating < levelDnInput) then crossDn[1] + 1 else 0;

def xUp = levelUpInput != 0 and crossUp==1;
def xDn = levelDnInput != 0 and crossDn==1;
def gUp = gradInput != 0 and gradientLvl ==  gradInput and gradientLvl[1] <  gradInput;
def gDn = gradInput != 0 and gradientLvl == -gradInput and gradientLvl[1] > -gradInput;
def lastDirectionUp;
#// Trigger alerts on user-selected conditions.
def triggerLong  = (xUp or gUp) and (!alt or isNaN(lastDirectionUp[1]) or !lastDirectionUp[1]) and doLongs;
def trigLong = triggerLong[ensureNoRepaintIdx];
def triggerShort = (xDn or gDn) and (!alt or isNaN(lastDirectionUp[1]) or lastDirectionUp[1]) and doShorts;
def trigShort = triggerShort[ensureNoRepaintIdx];

if triggerLong {
    lastDirectionUp = 1;
    } else
if triggerShort {
    lastDirectionUp = 0;
    } else {
    lastDirectionUp = if lastDirectionUp[1]==1 then 0 else if lastDirectionUp[1]==0 then 1 else nz(lastDirectionUp[1]);
}

plot long = if trigLong then 0.65 else na;
long.SetLineWeight(3);
long.SetPaintingStrategy(PaintingStrategy.SQUARES);
long.SetDefaultColor(Color.GREEN);

plot short = if trigShort then 0.65 else na;
short.SetLineWeight(3);
short.SetPaintingStrategy(PaintingStrategy.SQUARES);
short.SetDefaultColor(Color.RED);

plot Sig = if isNaN(close) or !ShowSignalLine then na else 0.65;
Sig.AssignValueColor( if condBuy then Color.DARK_GREEN else if condSell then Color.DARK_RED else Color.GRAY);
Sig.SetPaintingStrategy(PaintingStrategy.POINTS);

#--- BarColor

AssignPriceColor(if !BarColor then Color.CURRENT else
                 if trigLong and oscColor==3 then Color.CYAN else
                 if oscColor==3 then GlobalColor("LIME1") else
                 if oscColor==2 then GlobalColor("LIME2") else
                 if oscColor==1 then GlobalColor("LIME3") else
                 if trigShort and oscColor==-3 then Color.MAGENTA else
                 if oscColor==-3 then GlobalColor("RED1") else
                 if oscColor==-2 then GlobalColor("RED2") else
                 if oscColor==-1 then GlobalColor("RED3") else  GlobalColor("GRAY"));

#--- END CODE
 
Last edited by a moderator:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
227 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top