Convert TradingView Indicator to ThinkorSwim

javin82

New member
I need help converting a pine script indicator into a think script study for Thinkorswim. The name of the indicator is called Range. It is a reversal indicator and plots long and short signals. If you can help me with this, please let me know. Thank you!

PineScript code below:
Code:
//////////////
//@version=3
study("Range fv2",overlay=true)
showTrendLine = input(false, title="Show Trend Line")
colorBars = input(true, title="Color Bars?")
filterTrend = input(true, title="Filter Trend?")

//Source
src = close

//Sampling Period
per = input(defval=25, minval=1, title="Sampling Period")

//Range Multiplier
mult = input(defval=2, minval=0, title="Range Multiplier")

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Definitions
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------

//Smooth Average Range
smoothrng(x, t, m)=>
    wper      = (t/3) - 1
    avrng     = sma(abs(x - x[1]), t)
    smoothrng = sma(avrng, wper)*m
    smoothrng
smrng = smoothrng(src, per, mult)

//Range Filter
rngfilt(x, r)=>
    rngfilt  = x
    rngfilt := x > nz(rngfilt[1]) ? ((x - r) < nz(rngfilt[1]) ? nz(rngfilt[1]) : (x - r)) : ((x + r) > nz(rngfilt[1]) ? nz(rngfilt[1]) : (x + r))
    rngfilt
filt = rngfilt(src, smrng)

//Filter Direction
upward   = 0.0
upward  := filt > filt[1] ? nz(upward[1]) + 1 : filt < filt[1] ? 0 : nz(upward[1])
downward = 0.0
downward := filt < filt[1] ? nz(downward[1]) + 1 : filt > filt[1] ? 0 : nz(downward[1])

//Target Bands
hband = filt + smrng
lband = filt - smrng

//Colors
filtcolor = upward > 0 ? lime : downward > 0 ? red : orange
barcolor  = (src > filt) and (src > src[1]) and (upward > 0) ? lime : (src > filt) and (src < src[1]) and (upward > 0) ? green : (src < filt) and (src < src[1]) and (downward > 0) ? red : (src < filt) and (src > src[1]) and (downward > 0) ? maroon : orange
barcolor(colorBars ? barcolor : na)

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Plots
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------

//Range Filter
if filterTrend == true
    showTrendLine := false
filtplot = plot(filterTrend ? filt : na, color=filtcolor, linewidth=4, title="Range Filter")
//
Factor=input(3, minval=1,maxval = 100)
Pd=input(3, minval=1,maxval = 100)

Up=hl2-(Factor*atr(Pd))
Dn=hl2+(Factor*atr(Pd))
TrendUp = 0.0
TrendDown = 0.0
linecolor = na
up = na
down = na
Tsl = 0.0
Trend = 0.0
TrendUp := close[1]>TrendUp[1]? max(Up,TrendUp[1]) : Up
TrendDown := close[1]<TrendDown[1]? min(Dn,TrendDown[1]) : Dn
Trend := close > TrendDown[1] ? 1: close< TrendUp[1]? -1: nz(Trend[1],1)
Tsl := Trend==1? TrendUp: TrendDown
linecolor := Trend == 1 ? green : red
plot(showTrendLine ? Tsl : na, color = linecolor , style = line , linewidth = 3,title = "SuperTrend")

// Strategy


longCond = na
shortCond = na
longCond :=crossunder(Tsl,filt)
shortCond :=crossover(Tsl,filt)

// Count your long short conditions for more control with Pyramiding

sectionLongs = 0
sectionLongs := nz(sectionLongs[1])
sectionShorts = 0
sectionShorts := nz(sectionShorts[1])

if longCond
    sectionLongs := sectionLongs + 1
    sectionShorts := 0

if shortCond
    sectionLongs := 0
    sectionShorts := sectionShorts + 1

// Pyramiding

pyrl = 1


// These check to see your signal and cross references it against the pyramiding settings above

longCondition = longCond and sectionLongs <= pyrl
shortCondition = shortCond and sectionShorts <= pyrl

// Get the price of the last opened long or short

last_open_longCondition = na
last_open_shortCondition = na
last_open_longCondition := longCondition ? open : nz(last_open_longCondition[1])
last_open_shortCondition := shortCondition ? open : nz(last_open_shortCondition[1])

// Check if your last postion was a long or a short

last_longCondition = na
last_shortCondition = na
last_longCondition := longCondition ? time : nz(last_longCondition[1])
last_shortCondition := shortCondition ? time : nz(last_shortCondition[1])

in_longCondition = last_longCondition > last_shortCondition
in_shortCondition = last_shortCondition > last_longCondition

// Take profit

isTPl = input(true, "Take Profit Long")
isTPs = input(true, "Take Profit Short")
tp = input(5, "Take Profit ", type=float)
long_tp = isTPl and crossover(high, (1+(tp/100))*last_open_longCondition) and longCondition == 0 and in_longCondition  == 1
short_tp = isTPs and crossunder(low, (1-(tp/100))*last_open_shortCondition) and shortCondition == 0 and in_shortCondition == 1


// Create a single close for all the different closing conditions.

long_close = long_tp  ? 1 : 0
short_close = short_tp ? 1 : 0

// Get the time of the last close

last_long_close = na
last_short_close = na
last_long_close := long_close ? time : nz(last_long_close[1])
last_short_close := short_close ? time : nz(last_short_close[1])


// Alerts & Signals

bton(b) => b ? 1 : 0
plotshape(longCondition , title="Long", color=green, textcolor=green, transp=0,
          style=shape.triangleup, location=location.belowbar, size=size.small,text="LONG",offset=0)


plotshape(shortCondition, title="Short", color=red, textcolor=red, transp=0,
          style=shape.triangledown, location=location.abovebar, size=size.small,text="SHORT",offset=0)


//plotshape(longCondition, title = "BUY Signal", text = "Buy", style=shape.triangleup, location=location.belowbar, color = blue, editable = false, transp = 0)
//plotshape(shortCondition, title = "SELL Signal", text = "Sell", style=shape.triangledown, location=location.abovebar, color = black, editable = false, transp = 0)

plotshape(long_tp and last_longCondition > nz(last_long_close[1]), text ="TP", title="Take Profit Long", style=shape.triangledown,
   location=location.abovebar, color = red, editable = false, transp = 0)
plotshape(short_tp and last_shortCondition > nz(last_short_close[1]) , text ="TP", title="Take Profit Short", style=shape.triangleup,
   location=location.belowbar, color = lime, editable = false, transp = 0)

alertcondition(bton(longCondition), title="Long Alert")
alertcondition(bton(shortCondition), title="Short Alert")
alertcondition(bton(long_tp and last_longCondition > nz(last_long_close[1])), title="Take Profit Long")
alertcondition(bton(short_tp and last_shortCondition > nz(last_short_close[1])), title="Take Profit Short")

Any help I can get with this will be greatly appreciated! Thank you!

Admin's Notes:

You can now use this thread to make your own request. If there is a TradingView script that you would like to see in ThinkorSwim, please post the source code and link to that script below. We'll try our best to help you with the conversion.

Below are the TradingView scripts that have been converted to TOS.
 
Last edited by a moderator:

oneill

New member
@oneill Can you edit your post to include what it is you expect, an image of the original indicator, and a link to where the original code is located...??? Just posting a block of code doesn't tell us much at all... In addition, I consider it somewhat rude to think that in your first post, after joining just 4 minutes ago, that you might expect us to convert an indicator from another platform to Thinkscript... Have you even searched to see whether a comparable indicator might already exist...???

Oh, and lest I appear rude, welcome to the usethinkscript forums...
Hey sorry to come off rude, I had an old account and i forgot the password and it was easier to just connect to my discord account. Ill edit the post now. Ive been on the forum for some time finding indicators and never seen one similar though
 

Volatility Trading Range

VTR is a momentum indicator that shows if a stock is overbought or oversold based on its Weekly and Monthly average volatility trading range.

Download the indicator

oneill

New member
Hi all, I am not very good at coding. I will insert the code here from tradeview. Its linked here: https://www.tradingview.com/script/vkyFo1sQ/ Called Range Filter buy and sell 5min. I use it on the 15min and its pretty good. It creates red sell labels and green buy labels. Thats what i was hoping to have recreated in tos. Along with alerts when they populate. Appreciate any potential help. I've tried creating my own codes, but this ones too complex for me.
Code:
//@version=3


//Original Script > @DonovanWall

// Actual Version > @guikroth


//////////////////////////////////////////////////////////////////////////
// Settings for 5min chart, BTCUSDC. For Other coin, change the paremeters
//////////////////////////////////////////////////////////////////////////


study(title="Range Filter Buy and Sell 5min", overlay=true)

// Source

src = input(defval=close, title="Source")

// Sampling Period
// Settings for 5min chart, BTCUSDC. For Other coin, change the paremeters

per = input(defval=100, minval=1, title="Sampling Period")

// Range Multiplier

mult = input(defval=3.0, minval=0.1, title="Range Multiplier")

// Smooth Average Range

smoothrng(x, t, m)=>
    wper      = (t*2) - 1
    avrng     = ema(abs(x - x[1]), t)
    smoothrng = ema(avrng, wper)*m
    smoothrng
smrng = smoothrng(src, per, mult)

// Range Filter

rngfilt(x, r)=>
    rngfilt  = x
    rngfilt := x > nz(rngfilt[1]) ? ((x - r) < nz(rngfilt[1]) ? nz(rngfilt[1]) : (x - r)) : ((x + r) > nz(rngfilt[1]) ? nz(rngfilt[1]) : (x + r))
    rngfilt
filt = rngfilt(src, smrng)

// Filter Direction

upward   = 0.0
upward  := filt > filt[1] ? nz(upward[1]) + 1 : filt < filt[1] ? 0 : nz(upward[1])
downward = 0.0
downward := filt < filt[1] ? nz(downward[1]) + 1 : filt > filt[1] ? 0 : nz(downward[1])

// Target Bands

hband = filt + smrng
lband = filt - smrng

// Colors

filtcolor = upward > 0 ? lime : downward > 0 ? red : orange
barcolor  = (src > filt) and (src > src[1]) and (upward > 0) ? lime : (src > filt) and (src < src[1]) and (upward > 0) ? green :
   (src < filt) and (src < src[1]) and (downward > 0) ? red : (src < filt) and (src > src[1]) and (downward > 0) ? maroon : orange

filtplot = plot(filt, color=filtcolor, linewidth=3, title="Range Filter")

// Target

hbandplot = plot(hband, color=aqua, transp=100, title="High Target")
lbandplot = plot(lband, color=fuchsia, transp=100, title="Low Target")

// Fills

fill(hbandplot, filtplot, color=aqua, title="High Target Range")
fill(lbandplot, filtplot, color=fuchsia, title="Low Target Range")

// Bar Color

barcolor(barcolor)

// Break Outs

longCond = na
shortCond = na
longCond := ((src > filt) and (src > src[1]) and (upward > 0)) or ((src > filt) and (src < src[1]) and (upward > 0))
shortCond := ((src < filt) and (src < src[1]) and (downward > 0)) or ((src < filt) and (src > src[1]) and (downward > 0))

CondIni = 0
CondIni := longCond ? 1 : shortCond ? -1 : CondIni[1]
longCondition = longCond and CondIni[1] == -1
shortCondition = shortCond and CondIni[1] == 1

//Alerts

plotshape(longCondition, title = "Buy Signal", text ="BUY", textcolor = white, style=shape.labelup, size = size.normal, location=location.belowbar, color = green, transp = 0)
plotshape(shortCondition, title = "Sell Signal", text ="SELL", textcolor = white, style=shape.labeldown, size = size.normal, location=location.abovebar, color = red, transp = 0)

alertcondition(longCondition, title="Buy Alert", message = "BUY")
alertcondition(longCondition, title="Buy Alert", message = "BUY")
alertcondition(longCondition, title="Buy Alert", message = "BUY")
alertcondition(shortCondition, title="Sell Alert", message = "SELL")
 
Last edited:

rick888

New member
VIP
They are very similar but I was hoping to somehow convert my two favorite indicators to have them available on TOS. Here is my setup on on Tradingview:

403DUgW.png


On top I have the super trend. The lower ares are the OBVX and OBV delta.

Maybe I just need to live with it.
hi,do you have this obv script for Td?
 

Nick

Active member
2019 Donor
VIP
I would like to convert the following Trading View Indicator into ThinkScript. Can anyone out there lend a helping hand? Thank you so much 🙏 :)

Code:
New highs/lows
study(title = "New Highs-Lows NASDAQ", shorttitle="New Highs-Lows NASDAQ", precision=0)

newhigh = security ("HIGQ", period, close)
newlow = security ("LOWQ", period, close)

net = newhigh - newlow

avgH = sma(newhigh, 10)
avgL= -1* sma(newlow, 10)


threeDown = net < 0 and net[1] < 0 and net[2] < 0
threeUp = net > 0 and net[1] > 0 and net[2] > 0

uVol= security ("usi:uvolq","D", close)
dVol= security ("usi:dvolq", "D", close)

upThrust = (uVol / (uVol+ dVol)) > .85
dnThrust = (dVol / (uVol+ dVol)) > .85

plotshape(upThrust, style=shape.triangleup,
                 location=location.abovebar, color=#4CAF50)

plotshape(dnThrust, style=shape.triangledown,
                 location=location.abovebar, color=#880E4F)


bgcolor(threeDown ? #FF5252 : na, transp = 70)
bgcolor(threeUp ? #4CAF50 : na, transp = 70)

plot(newhigh, title="New Highs NASDAQ", color = green, style=columns, linewidth=2)
plot(newlow*(-1), title="New Lows NASDAQ", color = red, style=columns, linewidth=2)
plot(net, title="Net", color = black, style=columns, linewidth=2)
plot(avgH, title="New Highs 10day Avg", color = green, style=line, linewidth=2)
plot(avgL*(-1), title="New Lows 10day Avg", color = red, style=line, linewidth=2)
 

rad14733

Well-known member
VIP
@Nick You would need to find TOS alternatives to the $HIGQ, $LOWQ, usi:uvolq, and usi:dvolq symbols because they aren't supported... Once you find those the script is fairly basic... You should be able to sort it out once you have the proper symbols... Otherwise, it's not possible...
 

rad14733

Well-known member
VIP
Anyone heard of/used this?

wonder if it could be replicated for TOS.. seems really powerful

Go for it...!!! We don't just convert indicators based on a single request and video... I know this topic keeps getting more and more requests but those who do know how to code would need to have a personal interest in order to put for any effort... Not to mention that this is not the intent of these forums... Members share what they have developed with others, willingly... And the forums are also a place to learn to code in Thinkscript, and we're more than willing to help those willing to help themselves... I could load these forums full of more ideas than there are people willing to code them, single-handedly, but I don't... I write my own code just as I always have from the day I purchased my first computer... I don't ask for help often, but when I do you can rest assured that I've exhausted every possibility of solving the problem myself - and that goes for every facet of my life...
 

moola

New member
I have an indicator I like on tradingview and would need help to convert it for use on TOS. Code is below. Thank you

Code:
//@version=3
study(shorttitle="CCI & BB", title="CCI and Bollinger Bands")

//input ------
cci_len = input(14, "CCI length")
cci_src = input(close, "CCI source")

length = input(14, minval=1, title="BB length")
mult = input(2.0, minval=0.001, maxval=50, title="Deviations σ")


//CCI -----
cci = cci(cci_src, cci_len)


//BB -----
bb_src = cci
basis = sma(bb_src, length)
dev = mult * stdev(bb_src, length)
upper = basis + dev
lower = basis - dev


//bgcolor -----
c1 = cci > upper
c2 = cci < upper and cci > basis
c3 = cci > lower and cci < basis
c4 = cci < lower

cci_color = c1 ? #F8B856 : c2 ? #F6AD3C : c3 ? #F5A21B : #F39800
fill_bb_color = c1 ? lime : c2 ? green : c3 ? maroon : red

cross_color_1 = crossover(cci, 100) ? green : crossunder(cci, 100) ? red : na
cross_color_2 = crossover(cci, -100) ? green : crossunder(cci, -100) ? red : na


//plot -----
hline(0, title="zero line")
h1 = hline(100, title="upper line")
h2 = hline(-100, title="lower line")
fill(h1, h2, color=gray, transp=80, title="CCI fill")
p1 = plot(upper, color=#0099cc, title="uppper band")
p2 = plot(lower, color=#0099cc, title="lower band")
fill(p1, p2, color=fill_bb_color, transp=80, title="BB fill")

plot(basis, color=#0099cc, title="center line")
plot(cci, color=cci_color, transp=0, linewidth=2, title="CCI")
plot(100, color=cross_color_1, transp=0, style=circles, linewidth=3, title="CCI cross 100")
plot(-100, color=cross_color_2, transp=0, style=circles, linewidth=3, title="CCI cross -100")

lines = input(false, "add 8 lines")
line1 = input(100, "line 1 & -1")
line2 = input(200, "line 2 & -2")
line3 = input(300, "line 3 & -3")
line4 = input(400, "line 4 & -4")
plot(lines ? line1 : na, color=gray, title="line 1")
plot(lines ? line2 : na, color=gray, title="line 2")
plot(lines ? line3 : na, color=gray, title="line 3")
plot(lines ? line4 : na, color=gray, title="line 4")
plot(lines ? line1 * -1 : na, color=gray, title="line -1")
plot(lines ? line2 * -1 : na, color=gray, title="line -2")
plot(lines ? line3 * -1 : na, color=gray, title="line -3")
plot(lines ? line4 * -1 : na, color=gray, title="line -4")
 

rad14733

Well-known member
VIP
@moola We get a ton of requests to convert indicators from other platforms... What's in it for us...??? Have you done ample research to be sure a comparable indicator doesn't exist...??? We're trying instill in members that we aren't here to do other peoples coding for them... We'll help members learn Thinkscript but it's somewhat bold to think we have nothing better to do than write code for a single member so they can potentially profit from it in the markets... I could never ask someone else to do that, but maybe I have more manners than that... It's just poor forum etiquette that has become rampant as of late... From a new member in their first post no less... I hope you get my point...
 

moola

New member
@moola We get a ton of requests to convert indicators from other platforms... What's in it for us...??? Have you done ample research to be sure a comparable indicator doesn't exist...??? We're trying instill in members that we aren't here to do other peoples coding for them... We'll help members learn Thinkscript but it's somewhat bold to think we have nothing better to do than write code for a single member so they can potentially profit from it in the markets... I could never ask someone else to do that, but maybe I have more manners than that... It's just poor forum etiquette that has become rampant as of late... From a new member in their first post no less... I hope you get my point...
Its not being bold or less manners asking for help even as a new member. I am new didn't know the platform rules. I didn't wrong you and am sorry if you took it personally, just don't be salty to newbies, more like bullying
 

rad14733

Well-known member
VIP
Its not being bold or less manners asking for help even as a new member. I am new didn't know the platform rules. I didn't wrong you and am sorry if you took it personally, just don't be salty to newbies, more like bullying
Not bullying, merely point out the obvious... No foul, no harm...
 

xcmsx

New member
2019 Donor
please convert this code to thinkscript scan, seems to give good signals on 1h-2h timeframes

Code:
study(title="Carl's BOTTOM DETECTOR: Williams %R + normalized ATR", shorttitle="Carls Bottom + SPX volatility Detector", overlay=true)
//@version=4
// Williams %R
length_williams  = input(52, minval=1, title = "Williams Length")
upper    = highest(length_williams)
lower    = lowest(length_williams)
output   = 100* (close - upper) / (upper - lower) + 100

// ATR_normalized
source = close
atrlen = 13
lookback = input(52, minval=1, title ="ATR Lookback")
atrp = percentrank(atr(atrlen),lookback)

atr_treshold = input(90, minval=1, title = "ATR Threshold")
williams_treshold = input(10, minval=1, title = "Williams Threshold")

atr_condition = atrp > atr_treshold
williams_condition = output < williams_treshold

bottom_condition = atr_condition and williams_condition

plotshape(bottom_condition, style=shape.triangleup, location=location.belowbar, text="", color=    #FF0000, size=size.small)
 

sbryanvaughn

New member
Hello everyone,
I was wondering if there is a coding genius hanging around these forums that I could pay to convert a script for me? i am new to TOS coming from TradingView and I have one indicator that I live and die by....that I do not have in TOS and this is the main thing that is holding me back. Right now I am trying to use TOS with Tradingview open on another window simply for this indicator...Its a dynamic RSI that shows you the moving range of the stock. It helps me to stay out of trades that whipsaw and have no range and I have buy setups when the oscillator line crosses into the dynamic zone.

If anyone wants to take a crack at it here is the code:

Code:
study(title = "Dynamic Zones Oscillators [Alerts]", shorttitle = "DZO [Alerts]", overlay = false, precision = 8, max_bars_back = 200)

oscSrc    = input(defval = close, title = "Oscillator Source",        type = input.source)
dataSmple = input(defval = 70,    title = "Sample Length",            type = input.integer, minval = 1)
pcntAbove = input(defval = 88,    title = "Hi is Above X% of Sample", type = input.float, minval = 0, maxval = 100, step = 1.0)
pcntBelow = input(defval = 88,    title = "Lo is Below X% of Sample", type = input.float, minval = 0, maxval = 100, step = 1.0)

smplAbove = percentile_nearest_rank(oscSrc, dataSmple, pcntAbove)
smplBelow = percentile_nearest_rank(oscSrc, dataSmple, 100 - pcntBelow)

above     = plot(oscSrc > smplAbove ? oscSrc : smplAbove, title = "", color = na)
probOB    = plot(smplAbove, title = "OB", color = color.gray)
probOS    = plot(smplBelow, title = "OS", color = color.gray)
below     = plot(oscSrc < smplBelow ? oscSrc : smplBelow, title = "", color = na)
fill(above,  probOB, color = #00FF00, transp = 80)

Again, I will happily pay something for someones time to do this....although I am ignorant to what is fair?

Thanks in advance....
 

Knightime

New member
Hi everyone, I've been using an indicator in TradingView and has been working great! Below is the Pinescript but would love for it to be in thinkscript so I can use it on thinkorswim as well as try and make a scanner out of it to scan for stocks with the "Long" trigger. Thank you!

Code:
// Inputs
obv_length       = input(20,    title = "OBV Length")
obv_source       = input(close, title = "OBV Source")
obv_show_ema     = input(true,  title = "Show OBV EMA")
obv_ema1_length  = input(12,     title = "EMA1 Length")
obv_ema2_length  = input(26,     title = "EMA2 Length")

// Calculatios
obv      = sum(volume * sign(change(obv_source)), obv_length)
obv_osc  = obv / sum(volume, obv_length)
obv_ema1 = ema(obv_osc, obv_ema1_length)
obv_ema2 = ema(obv_osc, obv_ema2_length)

plot(0,                            color = color.gray,   title = "0 line")
plot(obv_osc,                      color = color.blue,   title = "OBV Osc", linewidth = 2)
plot(obv_show_ema ? obv_ema1 : na, color = color.orange, title = "OBV EMA1", linewidth = 2)
plot(obv_show_ema ? obv_ema2 : na, color = color.green , title = "OBV EMA2", linewidth = 2)


strategy.entry('long',  true,  when = crossover( obv_ema1, obv_ema2))
strategy.entry('short', false, when = crossunder(obv_ema1, obv_ema2))
 

MerryDay

Well-known member
VIP
@Knightime Did you look at the TOS study OnBalanceVolumeModified? It also uses OBV in 2 ema lengths.
Click on scan.
Click on +Add filter
Click on the pencil icon next to the filter you just added
Click edit
1. In the left column, click on the pull-down window, click add study
Type in OnBalanceVolumeModified
2. Under Plot, click on the pull-down window, choose OBVM
3. Change Length to 12
4. In the middle column, choose Crosses Above
5. In the right column, click on the pull-down window, click add study
Type in OnBalanceVolumeModified
6. Under Plot, click on the pull-down window, choose Signal
7. Change Signal Length to 26
Save
UQE4Axh.png


HTH
 

New Indicator: Buy the Dip

Check out our Buy the Dip indicator and see how it can help you find profitable swing trading ideas. Scanner, watchlist columns, and add-ons are included.

Download the indicator

Similar threads

Top