Repaints SlingShot + MTF + Open Close Cross Strategy For ThinkOrSwim

Repaints

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

Hello,

In the trading view, I discovered the Slingshot indicator as follows:
https://www.tradingview.com/script/6RrnYult/

Code:
//Created by moneros
//Based on CM_Ultimate_MA_MTF_V2, SlingShot by ChrisMoody and Open Close Cross Strategy by JayRogers
strategy(title='SlingShot + MTF + Open Close Cross Strategy', shorttitle='SlingShot+MTF+OCC Strategy',commission_type=strategy.commission.percent,commission_value=0.02 ,overlay=true, pyramiding=0, initial_capital=1000, currency=currency.USD)


ae = input(true, title="Show Aggressive Entry?, Or Use as Alert To Potential Conservative Entry?")
sce = input(true, title="Show Conservative Entry?")
st = input(true, title="Show Trend Arrows at Top and Bottom of Screen?")
def = input(false, title="Only Choose 1 - Either Conservative Entry Arrows or 'B'-'S' Letters")
pa = input(true, title="Show Conservative Entry Arrows?")
sl = input(false, title="Show 'B'-'S' Letters?")

// === INPUTS ===
useRes      = input(defval = true, title = "Use Alternate Resolution? ( recommended )")
stratRes    = input(defval = "120", title = "Set Resolution ( should not be lower than chart )", type = resolution)
useMA       = input(defval = true, title = "Use MA? ( otherwise use simple Open/Close data )")
basisType   = input(defval = "DEMA", title = "MA Type: SMA, EMA, DEMA, TEMA, WMA, VWMA, SMMA, HullMA, LSMA, ALMA ( case sensitive )", type = string)
basisLen    = input(defval = 14, title = "MA Period", minval = 1)
offsetSigma = input(defval = 6, title = "Offset for LSMA / Sigma for ALMA", minval = 0)
offsetALMA  = input(defval = 0.85, title = "Offset for ALMA", minval = 0, step = 0.01)
useStop     = input(defval = true, title = "Use Trailing Stop?")
slPoints    = input(defval = 200, title = "Stop Loss Trail Points", minval = 1)
slOffset    = input(defval = 400, title = "Stop Loss Trail Offset", minval = 1)
// === /INPUTS ===


//EMA Definitions
emaSlow = ema(close, 62)
emaFast = ema(close, 38)
//Color definition for Moving Averages
col = emaFast > emaSlow ? lime : emaFast < emaSlow ? red : yellow
//Moving Average Plots and Fill
p1 = plot(emaSlow, title="Slow MA", style=linebr, linewidth=4, color=col, transp=70)
p2 = plot(emaFast, title="Slow MA", style=linebr, linewidth=2, color=col, transp=70)
fill(p1, p2, color=silver, transp=70)

bgcolor(crossover(emaFast, emaSlow) ? lime : na, transp=30)
bgcolor(crossunder(emaFast, emaSlow) ? red : na, transp=30)


bgcolor(emaFast < emaSlow? red : na, transp=85)
bgcolor(emaFast > emaSlow? lime : na, transp=97)

strategy.entry("enter long", true, 1, when = crossover(emaFast, emaSlow))
strategy.entry("enter short", false, 1, when = crossunder(emaFast, emaSlow))






src = close
useCurrentRes = input(true, title="Use Current Chart Resolution?")
resCustom = input(title="Use Different Timeframe? Uncheck Box Above", type=resolution, defval="D")
len = input(20, title="Moving Average Length - LookBack Period")
//periodT3 = input(defval=7, title="Tilson T3 Period", minval=1)
factorT3 = input(defval=7, title="Tilson T3 Factor - *.10 - so 7 = .7 etc.", minval=0)
atype = input(1,minval=1,maxval=8,title="1=SMA, 2=EMA, 3=WMA, 4=HullMA, 5=VWMA, 6=RMA, 7=TEMA, 8=Tilson T3")
spc=input(false, title="Show Price Crossing 1st Mov Avg - Highlight Bar?")
cc = input(true,title="Change Color Based On Direction?")
smoothe = input(2, minval=1, maxval=10, title="Color Smoothing - Setting 1 = No Smoothing")
doma2 = input(false, title="Optional 2nd Moving Average")
spc2=input(false, title="Show Price Crossing 2nd Mov Avg?")
len2 = input(50, title="Moving Average Length - Optional 2nd MA")
sfactorT3 = input(defval=7, title="Tilson T3 Factor - *.10 - so 7 = .7 etc.", minval=0)
atype2 = input(1,minval=1,maxval=8,title="1=SMA, 2=EMA, 3=WMA, 4=HullMA, 5=VWMA, 6=RMA, 7=TEMA, 8=Tilson T3")
cc2 = input(true,title="Change Color Based On Direction 2nd MA?")
warn = input(false, title="You Can Turn On The Show Dots Parameter Below Without Plotting 2nd MA to See Crosses")
warn2 = input(false, title="If Using Cross Feature W/O Plotting 2ndMA - Make Sure 2ndMA Parameters are Set Correctly")
sd = input(false, title="Show Dots on Cross of Both MA's")

res = useCurrentRes ? period : resCustom
//hull ma definition
hullma = wma(2*wma(src, len/2)-wma(src, len), round(sqrt(len)))
//TEMA definition
ema1 = ema(src, len)
ema2 = ema(ema1, len)
ema3 = ema(ema2, len)
tema = 3 * (ema1 - ema2) + ema3

//Tilson T3
factor = factorT3 *.10
gd(src, len, factor) => ema(src, len) * (1 + factor) - ema(ema(src, len), len) * factor
t3(src, len, factor) => gd(gd(gd(src, len, factor), len, factor), len, factor)
tilT3 = t3(src, len, factor)
 

avg = atype == 1 ? sma(src,len) : atype == 2 ? ema(src,len) : atype == 3 ? wma(src,len) : atype == 4 ? hullma : atype == 5 ? vwma(src, len) : atype == 6 ? rma(src,len) : atype == 7 ? 3 * (ema1 - ema2) + ema3 : tilT3
//2nd Ma - hull ma definition
hullma2 = wma(2*wma(src, len2/2)-wma(src, len2), round(sqrt(len2)))
//2nd MA TEMA definition
sema1 = ema(src, len2)
sema2 = ema(sema1, len2)
sema3 = ema(sema2, len2)
stema = 3 * (sema1 - sema2) + sema3

//2nd MA Tilson T3
sfactor = sfactorT3 *.10
sgd(src, len2, sfactor) => ema(src, len2) * (1 + sfactor) - ema(ema(src, len2), len2) * sfactor
st3(src, len2, sfactor) => sgd(sgd(gd(src, len2, sfactor), len2, sfactor), len2, sfactor)
stilT3 = st3(src, len2, sfactor)

avg2 = atype2 == 1 ? sma(src,len2) : atype2 == 2 ? ema(src,len2) : atype2 == 3 ? wma(src,len2) : atype2 == 4 ? hullma2 : atype2 == 5 ? vwma(src, len2) : atype2 == 6 ? rma(src,len2) : atype2 == 7 ? 3 * (ema1 - ema2) + ema3 : stilT3

out = avg
out_two = avg2

out1 = security(tickerid, res, out)
out2 = security(tickerid, res, out_two)

//Formula for Price Crossing Moving Average #1
cr_up = open < out1 and close > out1
cr_Down = open > out1 and close < out1
//Formula for Price Crossing Moving Average #2
cr_up2 = open < out2 and close > out2
cr_Down2 = open > out2 and close < out2
//barcolor Criteria for Price Crossing Moving Average #1
iscrossUp() => cr_up
iscrossDown() => cr_Down
//barcolor Criteria for Price Crossing Moving Average #2
iscrossUp2() => cr_up2
iscrossDown2() => cr_Down2

ma_up = out1 >= out1[smoothe]
ma_down = out1 < out1[smoothe]

colour = cc ? ma_up ? lime : ma_down ? red : aqua : aqua
col2 = cc2 ? ma_up ? lime : ma_down ? red : aqua : white

circleYPosition = out2


// === BASE FUNCTIONS ===
// Returns MA input selection variant, default to SMA if blank or typo.
variant(type, src, len, offSig, offALMA) =>
    v1 = sma(src, len)                                                  // Simple
    v2 = ema(src, len)                                                  // Exponential
    v3 = 2 * v2 - ema(v2, len)                                          // Double Exponential
    v4 = 3 * (v2 - ema(v2, len)) + ema(ema(v2, len), len)               // Triple Exponential
    v5 = wma(src, len)                                                  // Weighted
    v6 = vwma(src, len)                                                 // Volume Weighted
    v7 = na(v5[1]) ? sma(src, len) : (v5[1] * (len - 1) + src) / len    // Smoothed
    v8 = wma(2 * wma(src, len / 2) - wma(src, len), round(sqrt(len)))   // Hull
    v9 = linreg(src, len, offSig)                                       // Least Squares
    v10 = alma(src, len, offALMA, offSig)                               // Arnaud Legoux
    type=="EMA"?v2 : type=="DEMA"?v3 : type=="TEMA"?v4 : type=="WMA"?v5 : type=="VWMA"?v6 : type=="SMMA"?v7 : type=="HullMA"?v8 : type=="LSMA"?v9 : type=="ALMA"?v10 : v1
// security wrapper for repeat calls
reso(exp, use, res) => use ? security(tickerid, res, exp) : exp
// === /BASE FUNCTIONS ===

// === SERIES SETUP ===
// open/close
closeSeries = useMA ? reso(variant(basisType, close, basisLen, offsetSigma, offsetALMA), useRes, stratRes) : reso(close, useRes, stratRes)
openSeries  = useMA ? reso(variant(basisType, open, basisLen, offsetSigma, offsetALMA), useRes, stratRes) : reso(open, useRes, stratRes)
trendState  = closeSeries > openSeries ? true : closeSeries < openSeries ? false : trendState[1]
// === /SERIES ===

// === PLOTTING ===
barcolor(color = closeSeries > openSeries ? #006600 : #990000, title = "Bar Colours")
// channel outline
closePlot   = plot(closeSeries, title = "Close Line", color = #009900, linewidth = 2, style = line, transp = 90)
openPlot    = plot(openSeries, title = "Open Line", color = #CC0000, linewidth = 2, style = line, transp = 90)
// channel fill
closePlotU  = plot(trendState ? closeSeries : na, transp = 100, editable = false)
openPlotU   = plot(trendState ? openSeries : na, transp = 100, editable = false)
closePlotD  = plot(trendState ? na : closeSeries, transp = 100, editable = false)
openPlotD   = plot(trendState ? na : openSeries, transp = 100, editable = false)
fill(openPlotU, closePlotU, title = "Up Trend Fill", color = #009900, transp = 40)
fill(openPlotD, closePlotD, title = "Down Trend Fill", color = #CC0000, transp = 40)
// === /PLOTTING ===



plot(out1, title="Multi-Timeframe Moving Avg", style=line, linewidth=4, color = colour)


bgcolor(emaFast > emaSlow and ma_down and not trendState ? yellow : na, transp=80)


//

//plotchar(crossover(emaFast, emaSlow) ? close:na, char='❄', size=size.normal, color=lime, textcolor=lime)

I find it very helpful in pinpointing entries. Can anyone please help convert this code into TOS.

Thanks
test the below
CSS:
#//Created by moneros
#//Based on CM_Ultimate_MA_MTF_V2, SlingShot by ChrisMoody and Open Close Cross Strategy by JayRogers
#strategy(title='SlingShot + MTF + Open Close Cross Strategy',
# Converted by Sam4Cok@Samer800 - 01/2023

#// === INPUTS ===
input BarColor = yes;
input BackgroundColor = yes;
input useRes      = yes;    #"Use Alternate Resolution? ( recommended )")
input stratRes    = AggregationPeriod.TWO_HOURS;# "Set Resolution ( should not be lower than chart )"
input useMA       = yes;    # "Use MA? ( otherwise use simple Open/Close data )")
input basisType   = {SMA, EMA, default DEMA, TEMA, WMA, VWMA, SMMA, HullMA, LSMA, ALMA};# "MA Type
input basisLen    = 14;    # "MA Period"
input offsetSigma = 6;     # "Offset for LSMA / Sigma for ALMA",
input offsetALMA  = 0.85;  # "Offset for ALMA"
input useCurrentRes = yes; # "Use Current Chart Resolution?"
input resCustom = AggregationPeriod.DAY;   # "Use Different Timeframe? Uncheck Box Above"
input len = 20;                            # "Moving Average Length - LookBack Period"
input factorT3 = 7;        # "Tilson T3 Factor
input atype = {default SMA, EMA, WMA, HullMA, VWMA, RMA, TEMA, "Tilson T3"};
input cc = yes;#,title="Change Color Based On Direction?")
input smoothe = 2;#, minval=1, maxval=10, title="Color Smoothing - Setting 1 = No Smoothing")
input len2 = 50;#, title="Moving Average Length - Optional 2nd MA")
input sfactorT3 = 7;#, title="Tilson T3 Factor - *.10 - so 7 = .7 etc.", minval=0)
input atype2 = {default SMA, EMA, WMA, HullMA, VWMA, RMA, TEMA, "Tilson T3"};
input cc2 = yes;#(true,title="Change Color Based On Direction 2nd MA?")


def na = Double.NaN;
def src = close;
def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;
#//EMA Definitions
def emaSlow = ExpAverage(src, 62);
def emaFast = ExpAverage(src, 38);
#//Color definition for Moving Averages
def col = if emaFast > emaSlow then 1 else if emaFast < emaSlow then -1 else 0;
#//Moving Average Plots and Fill
plot p1 = emaSlow;# "Slow MA"
p1.SetLineWeight(2);
p1.AssignValueColor(if col == 1 then Color.GREEN else if col == -1 then Color.RED else Color.YELLOW);
plot p2 = emaFast;#, title="Slow MA",
p2.AssignValueColor(if col == 1 then Color.GREEN else if col == -1 then Color.RED else Color.YELLOW);

AddCloud(p1, p2, Color.DARK_GRAY, Color.DARK_GRAY);

AddVerticalLine(emaFast crosses above emaSlow, "" , Color.GREEN, Curve.FIRM);
AddVerticalLine(emaFast crosses below emaSlow, "" , Color.RED, Curve.FIRM);

AddCloud(if BackgroundColor and emaFast<emaSlow then pos else na, neg, CreateColor(80,0,0));#? red : na, transp=85)
AddCloud(if BackgroundColor and emaFast>emaSlow then pos else na, neg, CreateColor(1, 31, 20));
def current = GetAggregationPeriod();
def res = if useCurrentRes then current else resCustom;
def reso = if useRes then stratRes else current;
def srcHT   = close(Period = res);
def CloseHT = close(Period = reso);
def OpenHT  = open(Period = reso);

#// === BASE FUNCTIONS ===
#// Returns MA input selection variant, default to SMA if blank or typo.
#pine_alma(series, windowsize, offset, sigma) =>
script ALMA {
    input series = close;
    input windowsize = 9;
    input Offset = 0.85;
    input Sigma = 6;
    def m = Offset * (windowsize - 1);
    def s = windowsize / Sigma;
    def norm  = fold z = 0 to windowsize with CW do
         CW + Exp(-(Sqr(z - m)) / (2 * Sqr(s)));
    def sum  = fold y = 0 to windowsize with WS do
         WS + Exp(-(Sqr(y - m)) / (2 * Sqr(s))) * GetValue(series, windowsize - 1 - y);
    plot ALMA = sum  / norm ;
}
#vwma(source, length)
script VWMA {
    input src = close;
    input len = 15;
    def v = volume;
    def VWMA = SimpleMovingAvg(src * v, len) / SimpleMovingAvg(v, len);
    plot result = VWMA;
}
#linreg(src, len, offset) =>
script linreg {
    input y = close;
    input n = 20;
    input offset = 0;
    def x = x[1] + 1;
    def a = (n * Sum(x * y, n) - Sum(x, n) * Sum(y, n) ) / ( n * Sum(Sqr(x), n) - Sqr(Sum(x, n)));
    def b = (Sum(Sqr(x), n) * Sum(y, n) - Sum(x, n) * Sum(x * y, n) ) / ( n * Sum(Sqr(x), n) - Sqr(Sum(x, n)));
    plot InertiaTS = a * x  + b;
}
script variant {
    input type = "SMA";
    input src = close;
    input len = 10;
    input offSig = 0;
    input offALMA = 0;
    def v1 = SimpleMovingAvg(src, len);#                                              // Simple
    def v2 = ExpAverage(src, len);#                                                   // Exponential
    def v3 = 2 * v2 - ExpAverage(v2, len);#                                          // Double Exponential
    def v4 = 3 * (v2 - ExpAverage(v2, len)) + ExpAverage(ExpAverage(v2, len), len);# // Triple Exponential
    def v5 = WMA(src, len);#                                                  // Weighted
    def v6 = vwma(src, len);#                                                 // Volume Weighted
    def v7 = if IsNaN(v5[1]) then Average(src, len) else (v5[1] * (len - 1) + src) / len;#    // Smoothed
    def v8 = WMA(2 * WMA(src, len / 2) - WMA(src, len), Round(Sqrt(len), 0));#   // Hull
    def v9 = inertia(src, len);#                                       // Least Squares
    def v10 = alma(src, len, offALMA, offSig);#                               // Arnaud Legoux
    def variant = if type == "EMA" then v2 else if type == "DEMA" then v3 else if type == "TEMA" then v4 else
                if type == "WMA" then v5 else if type == "VWMA" then v6 else if type == "SMMA" then v7 else
                if type == "HullMA" then v8 else if type == "LSMA" then v9[-offSig] else if type == "ALMA" then v10 else v1;
    plot return = variant;
}
#gd(src, len, factor) =>
script gd {
    input src = close;
    input len = 10;
    input factor = 3;
    def gd = ExpAverage(src, len) * (1 + factor) - ExpAverage(ExpAverage(src, len), len) * factor;
    plot return = gd;
}
#t3(src, len, factor) =>
script t3 {
    input src = close;
    input len = 10;
    input factor = 3;
    def t3 =  gd(gd(gd(src, len, factor), len, factor), len, factor);
    plot return = t3;
}
#//Tilson T3
def factor = factorT3 * 0.10;
def tilT3 = t3(src, len, factor);
def HTtilT3 = t3(srcHT, len, factor);

#//2nd MA Tilson T3
def sfactor = sfactorT3 * 0.10;
def stilT3 = t3(src, len2, sfactor);
def HTstilT3 = t3(srcHT, len2, sfactor);

def avg;
def avg2;
def out1;
def out2;
switch (atype) {
case SMA:
    avg = SimpleMovingAvg(src, len);
    out1 = SimpleMovingAvg(srcHT, len);
case EMA:
    avg = ExpAverage(src, len);
    out1 = ExpAverage(srcHT, len);
case WMA:
    avg = WMA(src, len);
    out1 = WMA(srcHT, len);
case HullMA:
    avg = HullMovingAvg(src, len);
    out1 = HullMovingAvg(srcHT, len);
case RMA:
    avg = WildersAverage(src, len);
    out1 = WildersAverage(srcHT, len);
case VWMA:
    avg = VWMA(src, len);
    out1 = VWMA(srcHT, len);
case TEMA:
    avg = TEMA(src, len);
    out1 = TEMA(srcHT, len);
case "Tilson T3":
    avg = tilT3;
    out1 = HTtilT3;
}
switch (atype2) {
case SMA:
    avg2 = SimpleMovingAvg(src, len2);
    out2 = SimpleMovingAvg(srcHT, len2);
case EMA:
    avg2 = ExpAverage(src, len2);
    out2 = ExpAverage(srcHT, len2);
case WMA:
    avg2 = WMA(src, len2);
    out2 = WMA(srcHT, len2);
case HullMA:
    avg2 = HullMovingAvg(src, len2);
    out2 = HullMovingAvg(srcHT, len2);
case RMA:
    avg2 = WildersAverage(src, len2);
    out2 = WildersAverage(srcHT, len2);
case VWMA:
    avg2 = VWMA(src, len2);
    out2 = VWMA(srcHT, len2);
case TEMA:
    avg2 = TEMA(src, len2);
    out2 = TEMA(srcHT, len2);
case "Tilson T3":
    avg2 = stilT3;
    out2 = HTstilT3;
}

def out = avg;
def out_two = avg2;

#/Formula for Price Crossing Moving Average #1
def cr_up = open < out1 and close > out1;
def cr_Down = open > out1 and close < out1;
#//Formula for Price Crossing Moving Average #2
def cr_up2 = open < out2 and close > out2;
def cr_Down2 = open > out2 and close < out2;
#//barcolor Criteria for Price Crossing Moving Average #1

def ma_up = out1 >= out1[smoothe];
def ma_down = out1 < out1[smoothe];

def colour = if cc then if ma_up then 1 else if ma_down then -1 else 0 else 0;
def circleYPosition = out1;

#// security wrapper for repeat calls

#/ === SERIES SETUP ===
def MAclose = variant(basisType, closeHT, basisLen, offsetSigma, offsetALMA);
def MAopen = variant(basisType, openHT, basisLen, offsetSigma, offsetALMA);
#// open/close
def closeSeries = if useMA then MAclose else closeHT;
def openSeries  = if useMA then MAopen else openHT;
def trendState  = if closeSeries > openSeries then yes else
                  if closeSeries < openSeries then no else trendState[1];
def trend = trendState;

#// === PLOTTING ===

#// channel outline
def closePlot = closeSeries;
def openPlot  = openSeries;
#// channel fill
def closePlotU = if trendState then closeSeries else na;
def openPlotU  = if trendState then openSeries else na;
def closePlotD = if trendState then na else closeSeries;
def openPlotD  = if trendState then na else openSeries;

AddCloud(openPlotU, closePlotU, Color.DARK_GREEN,Color.DARK_GREEN, yes);
AddCloud(openPlotD, closePlotD, Color.DARK_RED,Color.DARK_RED, yes);
#// === /PLOTTING ===

plot MTFMovAvg = circleYPosition;#, title="Multi-Timeframe Moving Avg"
MTFMovAvg.SetLineWeight(2);
MTFMovAvg.AssignValueColor(if colour>0 then Color.GREEN else
                           if colour<0 then color.RED else Color.GRAY);

AddCloud(if emaFast > emaSlow and ma_down and !trend then pos else na, neg, CreateColor(230,230,0));
AssignPriceColor(if !BarColor then Color.CURRENT else
                 if closeSeries > openSeries then CreateColor(0, 153, 0) else Color.RED);


#--- END CODE
 
test the below
CSS:
#//Created by moneros
#//Based on CM_Ultimate_MA_MTF_V2, SlingShot by ChrisMoody and Open Close Cross Strategy by JayRogers
#strategy(title='SlingShot + MTF + Open Close Cross Strategy',
# Converted by Sam4Cok@Samer800 - 01/2023

#// === INPUTS ===
input BarColor = yes;
input BackgroundColor = yes;
input useRes      = yes;    #"Use Alternate Resolution? ( recommended )")
input stratRes    = AggregationPeriod.TWO_HOURS;# "Set Resolution ( should not be lower than chart )"
input useMA       = yes;    # "Use MA? ( otherwise use simple Open/Close data )")
input basisType   = {SMA, EMA, default DEMA, TEMA, WMA, VWMA, SMMA, HullMA, LSMA, ALMA};# "MA Type
input basisLen    = 14;    # "MA Period"
input offsetSigma = 6;     # "Offset for LSMA / Sigma for ALMA",
input offsetALMA  = 0.85;  # "Offset for ALMA"
input useCurrentRes = yes; # "Use Current Chart Resolution?"
input resCustom = AggregationPeriod.DAY;   # "Use Different Timeframe? Uncheck Box Above"
input len = 20;                            # "Moving Average Length - LookBack Period"
input factorT3 = 7;        # "Tilson T3 Factor
input atype = {default SMA, EMA, WMA, HullMA, VWMA, RMA, TEMA, "Tilson T3"};
input cc = yes;#,title="Change Color Based On Direction?")
input smoothe = 2;#, minval=1, maxval=10, title="Color Smoothing - Setting 1 = No Smoothing")
input len2 = 50;#, title="Moving Average Length - Optional 2nd MA")
input sfactorT3 = 7;#, title="Tilson T3 Factor - *.10 - so 7 = .7 etc.", minval=0)
input atype2 = {default SMA, EMA, WMA, HullMA, VWMA, RMA, TEMA, "Tilson T3"};
input cc2 = yes;#(true,title="Change Color Based On Direction 2nd MA?")


def na = Double.NaN;
def src = close;
def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;
#//EMA Definitions
def emaSlow = ExpAverage(src, 62);
def emaFast = ExpAverage(src, 38);
#//Color definition for Moving Averages
def col = if emaFast > emaSlow then 1 else if emaFast < emaSlow then -1 else 0;
#//Moving Average Plots and Fill
plot p1 = emaSlow;# "Slow MA"
p1.SetLineWeight(2);
p1.AssignValueColor(if col == 1 then Color.GREEN else if col == -1 then Color.RED else Color.YELLOW);
plot p2 = emaFast;#, title="Slow MA",
p2.AssignValueColor(if col == 1 then Color.GREEN else if col == -1 then Color.RED else Color.YELLOW);

AddCloud(p1, p2, Color.DARK_GRAY, Color.DARK_GRAY);

AddVerticalLine(emaFast crosses above emaSlow, "" , Color.GREEN, Curve.FIRM);
AddVerticalLine(emaFast crosses below emaSlow, "" , Color.RED, Curve.FIRM);

AddCloud(if BackgroundColor and emaFast<emaSlow then pos else na, neg, CreateColor(80,0,0));#? red : na, transp=85)
AddCloud(if BackgroundColor and emaFast>emaSlow then pos else na, neg, CreateColor(1, 31, 20));
def current = GetAggregationPeriod();
def res = if useCurrentRes then current else resCustom;
def reso = if useRes then stratRes else current;
def srcHT   = close(Period = res);
def CloseHT = close(Period = reso);
def OpenHT  = open(Period = reso);

#// === BASE FUNCTIONS ===
#// Returns MA input selection variant, default to SMA if blank or typo.
#pine_alma(series, windowsize, offset, sigma) =>
script ALMA {
    input series = close;
    input windowsize = 9;
    input Offset = 0.85;
    input Sigma = 6;
    def m = Offset * (windowsize - 1);
    def s = windowsize / Sigma;
    def norm  = fold z = 0 to windowsize with CW do
         CW + Exp(-(Sqr(z - m)) / (2 * Sqr(s)));
    def sum  = fold y = 0 to windowsize with WS do
         WS + Exp(-(Sqr(y - m)) / (2 * Sqr(s))) * GetValue(series, windowsize - 1 - y);
    plot ALMA = sum  / norm ;
}
#vwma(source, length)
script VWMA {
    input src = close;
    input len = 15;
    def v = volume;
    def VWMA = SimpleMovingAvg(src * v, len) / SimpleMovingAvg(v, len);
    plot result = VWMA;
}
#linreg(src, len, offset) =>
script linreg {
    input y = close;
    input n = 20;
    input offset = 0;
    def x = x[1] + 1;
    def a = (n * Sum(x * y, n) - Sum(x, n) * Sum(y, n) ) / ( n * Sum(Sqr(x), n) - Sqr(Sum(x, n)));
    def b = (Sum(Sqr(x), n) * Sum(y, n) - Sum(x, n) * Sum(x * y, n) ) / ( n * Sum(Sqr(x), n) - Sqr(Sum(x, n)));
    plot InertiaTS = a * x  + b;
}
script variant {
    input type = "SMA";
    input src = close;
    input len = 10;
    input offSig = 0;
    input offALMA = 0;
    def v1 = SimpleMovingAvg(src, len);#                                              // Simple
    def v2 = ExpAverage(src, len);#                                                   // Exponential
    def v3 = 2 * v2 - ExpAverage(v2, len);#                                          // Double Exponential
    def v4 = 3 * (v2 - ExpAverage(v2, len)) + ExpAverage(ExpAverage(v2, len), len);# // Triple Exponential
    def v5 = WMA(src, len);#                                                  // Weighted
    def v6 = vwma(src, len);#                                                 // Volume Weighted
    def v7 = if IsNaN(v5[1]) then Average(src, len) else (v5[1] * (len - 1) + src) / len;#    // Smoothed
    def v8 = WMA(2 * WMA(src, len / 2) - WMA(src, len), Round(Sqrt(len), 0));#   // Hull
    def v9 = inertia(src, len);#                                       // Least Squares
    def v10 = alma(src, len, offALMA, offSig);#                               // Arnaud Legoux
    def variant = if type == "EMA" then v2 else if type == "DEMA" then v3 else if type == "TEMA" then v4 else
                if type == "WMA" then v5 else if type == "VWMA" then v6 else if type == "SMMA" then v7 else
                if type == "HullMA" then v8 else if type == "LSMA" then v9[-offSig] else if type == "ALMA" then v10 else v1;
    plot return = variant;
}
#gd(src, len, factor) =>
script gd {
    input src = close;
    input len = 10;
    input factor = 3;
    def gd = ExpAverage(src, len) * (1 + factor) - ExpAverage(ExpAverage(src, len), len) * factor;
    plot return = gd;
}
#t3(src, len, factor) =>
script t3 {
    input src = close;
    input len = 10;
    input factor = 3;
    def t3 =  gd(gd(gd(src, len, factor), len, factor), len, factor);
    plot return = t3;
}
#//Tilson T3
def factor = factorT3 * 0.10;
def tilT3 = t3(src, len, factor);
def HTtilT3 = t3(srcHT, len, factor);

#//2nd MA Tilson T3
def sfactor = sfactorT3 * 0.10;
def stilT3 = t3(src, len2, sfactor);
def HTstilT3 = t3(srcHT, len2, sfactor);

def avg;
def avg2;
def out1;
def out2;
switch (atype) {
case SMA:
    avg = SimpleMovingAvg(src, len);
    out1 = SimpleMovingAvg(srcHT, len);
case EMA:
    avg = ExpAverage(src, len);
    out1 = ExpAverage(srcHT, len);
case WMA:
    avg = WMA(src, len);
    out1 = WMA(srcHT, len);
case HullMA:
    avg = HullMovingAvg(src, len);
    out1 = HullMovingAvg(srcHT, len);
case RMA:
    avg = WildersAverage(src, len);
    out1 = WildersAverage(srcHT, len);
case VWMA:
    avg = VWMA(src, len);
    out1 = VWMA(srcHT, len);
case TEMA:
    avg = TEMA(src, len);
    out1 = TEMA(srcHT, len);
case "Tilson T3":
    avg = tilT3;
    out1 = HTtilT3;
}
switch (atype2) {
case SMA:
    avg2 = SimpleMovingAvg(src, len2);
    out2 = SimpleMovingAvg(srcHT, len2);
case EMA:
    avg2 = ExpAverage(src, len2);
    out2 = ExpAverage(srcHT, len2);
case WMA:
    avg2 = WMA(src, len2);
    out2 = WMA(srcHT, len2);
case HullMA:
    avg2 = HullMovingAvg(src, len2);
    out2 = HullMovingAvg(srcHT, len2);
case RMA:
    avg2 = WildersAverage(src, len2);
    out2 = WildersAverage(srcHT, len2);
case VWMA:
    avg2 = VWMA(src, len2);
    out2 = VWMA(srcHT, len2);
case TEMA:
    avg2 = TEMA(src, len2);
    out2 = TEMA(srcHT, len2);
case "Tilson T3":
    avg2 = stilT3;
    out2 = HTstilT3;
}

def out = avg;
def out_two = avg2;

#/Formula for Price Crossing Moving Average #1
def cr_up = open < out1 and close > out1;
def cr_Down = open > out1 and close < out1;
#//Formula for Price Crossing Moving Average #2
def cr_up2 = open < out2 and close > out2;
def cr_Down2 = open > out2 and close < out2;
#//barcolor Criteria for Price Crossing Moving Average #1

def ma_up = out1 >= out1[smoothe];
def ma_down = out1 < out1[smoothe];

def colour = if cc then if ma_up then 1 else if ma_down then -1 else 0 else 0;
def circleYPosition = out1;

#// security wrapper for repeat calls

#/ === SERIES SETUP ===
def MAclose = variant(basisType, closeHT, basisLen, offsetSigma, offsetALMA);
def MAopen = variant(basisType, openHT, basisLen, offsetSigma, offsetALMA);
#// open/close
def closeSeries = if useMA then MAclose else closeHT;
def openSeries  = if useMA then MAopen else openHT;
def trendState  = if closeSeries > openSeries then yes else
                  if closeSeries < openSeries then no else trendState[1];
def trend = trendState;

#// === PLOTTING ===

#// channel outline
def closePlot = closeSeries;
def openPlot  = openSeries;
#// channel fill
def closePlotU = if trendState then closeSeries else na;
def openPlotU  = if trendState then openSeries else na;
def closePlotD = if trendState then na else closeSeries;
def openPlotD  = if trendState then na else openSeries;

AddCloud(openPlotU, closePlotU, Color.DARK_GREEN,Color.DARK_GREEN, yes);
AddCloud(openPlotD, closePlotD, Color.DARK_RED,Color.DARK_RED, yes);
#// === /PLOTTING ===

plot MTFMovAvg = circleYPosition;#, title="Multi-Timeframe Moving Avg"
MTFMovAvg.SetLineWeight(2);
MTFMovAvg.AssignValueColor(if colour>0 then Color.GREEN else
                           if colour<0 then color.RED else Color.GRAY);

AddCloud(if emaFast > emaSlow and ma_down and !trend then pos else na, neg, CreateColor(230,230,0));
AssignPriceColor(if !BarColor then Color.CURRENT else
                 if closeSeries > openSeries then CreateColor(0, 153, 0) else Color.RED);


#--- END CODE
Thank you @samer800 for this gorgeous code. I have a question..FOr switching between multiple timeframes do I have to change anything. Also..if I just want to see the green and red clouds only on any time then, what should I turn off.

Much appreciated,

Fais
 
Thank you @samer800 for this gorgeous code. I have a question..FOr switching between multiple timeframes do I have to change anything. Also..if I just want to see the green and red clouds only on any time then, what should I turn off.

Much appreciated,

Fais
no idea on the indicator since I converted bases on your request. As a general guidelines, don't select time frame less than your chart time frame. if you have the indicator on 5 min charts, so select aggregation more than 5 min
 
no idea on the indicator since I converted bases on your request. As a general guidelines, don't select time frame less than your chart time frame. if you have the indicator on 5 min charts, so select aggregation more than 5 min
Hey @samer800, can you please help me how to remove the background color and the vertical lines on the chart without affecting the functionality of the code. Thanks
 
no idea on the indicator since I converted bases on your request. As a general guidelines, don't select time frame less than your chart time frame. if you have the indicator on 5 min charts, so select aggregation more than 5 min
I have tried to remove the background color from the code and used it to compare with TradingView charts. I see that in Trading View, the 2 hour and 4 hour charts have different start times than those in Thinkorswim. Will this significantly influence the indicator behavior if the aggregation is based on 2 hours or 4 hours?

( P.S : I do not know how to upload picture without an URL link) so I was unable to post an image)
 
no idea on the indicator since I converted bases on your request. As a general guidelines, don't select time frame less than your chart time frame. if you have the indicator on 5 min charts, so select aggregation more than 5 min
If i wanted to shorten the aggregation time of the study, would i then go line by line and change the time periods that are defined in the code?
 
If i wanted to shorten the aggregation time of the study, would i then go line by line and change the time periods that are defined in the code?

To change timeframes on MTF studies on this forum

Go to chart settings and look for an input similar to:
input stratRes = AggregationPeriod.TWO_HOURS;
 
Last edited by a moderator:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
383 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