IDEAL BB with MA For ThinkOrSwim

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

Need to convert this pinescript
https://in.tradingview.com/script/GL9WwKMO-IDEAL-BB-with-MA-With-Alerts/
to thinkscript, some alerts come at the end of the 15min candle before market close, is it possible to get the alert before the end of the last 15mins candle, the sooner the better.

Code:
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Adarsh

//@version=4
// Moving Average 3.0 (3rd Generation) script may be freely distributed under the MIT license. Copyright (c) 2018-present, Alex Orekhov (everget)
study("IDEAL BB with MA (With Alerts) by Adarsh", overlay=true)

length1 = input(title="1st Length", type=input.integer, minval=1, defval=120)
length2 = input(title="2nd Length", type=input.integer, minval=1, defval=12)
maInput = input(title="MA Type", defval="EMA", options=["EMA", "SMA", "VWMA", "WMA"])
src = input(title="Source", type=input.source, defval=hl2)

getMA(src, length) =>
    ma = 0.0

    if maInput == "EMA"
        ma := ema(src, length)

    if maInput == "SMA"
        ma := sma(src, length)

    if maInput == "VWMA"
        ma := vwma(src, length)

    if maInput == "WMA"
        ma := wma(src, length)
    ma

getNMA(src, length1, length2) =>
    lambda = length1 / length2
    alpha = lambda * (length1 - 1) / (length1 - lambda)
  
    ma1 = getMA(src, length1)
    ma2 = getMA(ma1, length2)

    nma = (1 + alpha) * ma1 - alpha * ma2

nma = getNMA(src, length1, length2)
//emaLength = input(90, minval=1, title="EMA Length")
//emaSource = input(close, title="EMA Source")
//ema = ema(emaSource, emaLength)

plot(nma, title="NMA Black Line", linewidth=2, style=plot.style_stepline, color=color.black, transp=0)
//plot(ema, title="EMA", linewidth=1, color=color.red, transp=0)

//VWAP BANDS
lenvwap = input(1, minval=1, title="VWAP Length")
src1a = input(close, title="VWAP Source")
offsetvwap = input(title="VWAP Offset", type=input.integer, defval=0, minval=-500, maxval=500)
srcvwap = hlc3
vvwap = vwap(srcvwap)
line1 = sma(src1a, lenvwap)
plot(vvwap, color=#e91e63, linewidth=2, style=plot.style_line, title="VWAP MIDDLE")


// Boll Bands
emaSource = close
emaPeriod = 20
devMultiple = 2
baseline = sma(emaSource, emaPeriod)
plot(baseline, title = "BB Red Line", color = color.red)
stdDeviation = devMultiple * (stdev(emaSource, emaPeriod))
upperBand = (baseline + stdDeviation)
lowerBand = (baseline - stdDeviation)
p1 = plot(upperBand, title = "BB Top", color = color.blue)
p2 = plot(lowerBand, title = "BB Bottom", color = #311b92)
fill(p1, p2, color = color.blue)

//HULL TREND WITH KAHLMAN
srchull       = input(hl2,   "Price Data")
lengthhull    = input(24,    "Lookback")
showcross = input(true,  "Show cross over/under")
gain      = input(10000, "Gain")
k         = input(true,  "Use Kahlman")

hma(_srchull, _lengthhull) =>
    wma((2 * wma(_srchull, _lengthhull / 2)) - wma(_srchull, _lengthhull), round(sqrt(_lengthhull)))
  
hma3(_srchull, _lengthhull) =>
    p = lengthhull/2
    wma(wma(close,p/3)*3 - wma(close,p/2) - wma(close,p),p)

kahlman(x, g) =>
    kf = 0.0
    dk = x - nz(kf[1], x)
    smooth = nz(kf[1],x)+dk*sqrt((g/10000)*2)
    velo = 0.0
    velo := nz(velo[1],0) + ((g/10000)*dk)
    kf := smooth+velo
 
a = k ? kahlman(hma(srchull, lengthhull), gain) : hma(srchull, lengthhull)
b = k ? kahlman(hma3(srchull, lengthhull), gain) : hma3(srchull, lengthhull)
c = b > a ? color.lime : color.red
crossdn = a > b and a[1] < b[1]
crossup = b > a and b[1] < a[1]

p1hma = plot(a,color=c,linewidth=1,transp=75, title="Long Plot")
p2hma = plot(b,color=c,linewidth=1,transp=75, title="Short Plot")
fill(p1hma,p2hma,color=c,transp=55,title="Fill")
plotshape(showcross and crossdn ? a : na, location=location.abovebar, style=shape.labeldown, color=color.red, size=size.tiny, text="Sell", textcolor=color.white, transp=0, offset=-1)
plotshape(showcross and crossup ? a : na, location=location.belowbar, style=shape.labelup, color=color.green, size=size.tiny, text="Buy", textcolor=color.white, transp=0, offset=-1)

//ALERTS
alertcondition(crossup, title='Buy', message='Go Long')
alertcondition(crossdn, title='Sell', message='Go Short')
check the below.
CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © Adarsh
#// Moving Average 3.0 (3rd Generation) script may be freely distributed under the MIT license. Copyright (c) 2018-present, Alex Orekhov (everget)
#study("IDEAL BB with MA (With Alerts) by Adarsh", overlay=true)
# Converted by Sam4Cok@Samer800     - 03/2023

input length1 = 120;#(title="1st Length", type=input.integer, minval=1, defval=120)
input length2 = 12;#(title="2nd Length", type=input.integer, minval=1, defval=12)
input maInput = AverageType.EXPONENTIAL;#(title="MA Type", defval="EMA", options=["EMA", "SMA", "VWMA", "WMA"])
input src = hl2;#(title="Source", type=input.source, defval=hl2)
input lenVwap = 1;#, minval=1, title="VWAP Length")
input src1a = close;#, title="VWAP Source")
input offsetvwap = 0;#(title="VWAP Offset", type=input.integer, defval=0, minval=-500, maxval=500)
input srcHull   = hl2;#,   "Price Data")
input lengthHull = 24;#,    "Lookback")
input showcross = yes;#(true,  "Show cross over/under")
input gain      = 10000;#, "Gain")
input UseKahlman = yes;#(true,  "Use Kahlman")

def na = Double.NaN;
def k = UseKahlman;
##color
DefineGlobalColor("vwap"    , CreateColor(233,30,99));
DefineGlobalColor("base"   , CreateColor(255,82,82));
DefineGlobalColor("bbup"   , CreateColor(33,150,243));
DefineGlobalColor("bbdn" , CreateColor(49,27,146));
script f_Vwap {
    input src = close;
    def src_v = Volume;
    def tf = GetDay();
    def start0 = tf - tf[1];
    def sumSrc0 = src * src_v;
    def sumVol0 = src_v;
    def sumSrc2 = src_v * Sqr(src);
    def sumSrc1 = CompoundValue(1, if start0 then sumSrc0 else sumSrc0 + sumSrc1[1], sumSrc0);
    def sumVol1 = CompoundValue(1, if start0 then sumVol0 else sumVol0 + sumVol1[1], sumVol0);
    def sumVol2 = CompoundValue(1, if start0 then sumSrc2 else sumSrc2 + sumVol2[1], sumSrc2);
    def Vwap = sumSrc1 / sumVol1;
    def deviation = Sqrt(Max(sumVol2 / sumVol1 - Sqr(Vwap), 0));
    plot wap = Vwap;
    plot dev = deviation;
}

def lambda = length1 / length2;
def alpha = lambda * (length1 - 1) / (length1 - lambda);
def ma1 = MovingAverage(maInput, src, length1);
def ma2 = MovingAverage(maInput, ma1, length2);
def nma_ = (1 + alpha) * ma1 - alpha * ma2;
def nma = nma_;


plot NMABlackLine = nma;#, title="NMA Black Line", linewidth=2, style=plot.style_stepline, color=color.black, transp=0)
NMABlackLine.SetDefaultColor(Color.DARK_GRAY);

#//VWAP BANDS
def srcvwap = hlc3;
def vvwap = f_Vwap(srcvwap);
def line1 = Average(src1a, lenvwap);
plot vwapLine = vvwap;    # "VWAP MIDDLE"
vwapLine.SetDefaultColor(GlobalColor("vwap"));

#// Boll Bands
def emaSource = close;
def emaPeriod = 20;
def devMultiple = 2;
def baseline = average(emaSource, emaPeriod);
def stdDeviation = devMultiple * (stdev(emaSource, emaPeriod));
def upperBand = (baseline + stdDeviation);
def lowerBand = (baseline - stdDeviation);
plot bbCenter = baseline;#, title = "BB Red Line", color = color.red)
plot bbUp = upperBand;#, title = "BB Top", color = color.blue)
plot bbDn = lowerBand;#, title = "BB Bottom", color = #311b92)
bbCenter.SetDefaultColor(GlobalColor("base"));
bbup.SetDefaultColor(GlobalColor("bbup"));
bbdn.SetDefaultColor(GlobalColor("bbdn"));
AddCloud(bbUp, bbDn, Color.DARK_GRAY);

#//HULL TREND WITH KAHLMAN


#hma3(_srchull, _lengthhull) =>
Script hma3 {
input _srchull = close;
input _lengthhull = 24;
    def p = floor(_lengthhull/2);
    def hma = wma(wma(close,p/3)*3 - wma(close,p/2) - wma(close,p),p);
    plot out = hma;
}

Script kahlman {
input x = hl2;
input g = 10000;
    def kf;# = 0.0
    def dk = x - if(kf[1]==0 or isNaN(kf[1]), x, kf[1]);
    def smooth = if(kf[1]==0 or isNaN(kf[1]), x, kf[1]) + dk * sqrt((g/10000)*2);
    def velo;# = 0.0
    velo = velo[1] + ((g/10000)*dk);
    kf = smooth+velo;
plot out = kf;
}
 
def a = if k then kahlman(HullMovingAvg(srchull, lengthhull), gain) else HullMovingAvg(srchull, lengthhull);
def b = if k then kahlman(hma3(srchull, lengthhull), gain) else hma3(srchull, lengthhull);
def c = b > a;# ? color.lime : color.red
def crossdn = a > b and a[1] < b[1];
def crossup = b > a and b[1] < a[1];

def p1hma = a;#,color=c,linewidth=1,transp=75, title="Long Plot")
def p2hma = b;#,color=c,linewidth=1,transp=75, title="Short Plot")
AddCloud(p1hma,p2hma, Color.DARK_GREEN, Color.DARK_RED, yes);

AddChartBubble(showcross and crossdn[-1], high, "Sell", Color.RED, yes);#textcolor=color.white, transp=0, offset=-1)
AddChartBubble(showcross and crossup[-1], low, "Buy", Color.GREEN, no);#=color.white, transp=0, offset=-1)


#--- END of CODE
 
Thank you very much for the conversion, it was exactly what i wanted. However, there are typically buy or sell alerts on certain stocks after market closes between 16:00 and 16:15 EST. for example LRN on 01/24/23 at past 16:00, around 16:18 while usiing the on demaand feature, there's a buy alert on there. My question now is that, is it possible to get those alerts before market closes, and how fast can it be done, so if i need to enter a trade i can make it before market closes. The timeframe i use for this is typically 15mins.
 
Thank you very much for the conversion, it was exactly what i wanted. However, there are typically buy or sell alerts on certain stocks after market closes between 16:00 and 16:15 EST. for example LRN on 01/24/23 at past 16:00, around 16:18 while usiing the on demaand feature, there's a buy alert on there. My question now is that, is it possible to get those alerts before market closes, and how fast can it be done, so if i need to enter a trade i can make it before market closes. I have attached a screenshot using the on demand feature with LRN, the timeframe i use for this is typically 15mins.
No, it is not possible to get an alert before the alert occurs.
You could try moving from a 10min chart. The bars will signal slightly earlier but some of the 15min signals might not occur on the 10min chart at all.
 
No, it is not possible to get an alert before the alert occurs.
You could try moving from a 10min chart. The bars will signal slightly earlier but some of the 15min signals might not occur on the 10min chart at all.
Thank you
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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