Stoch + ATR For ThinkOrSwim

  1. Can anyone help to convert the following to Thinkscript.
    1. https://www.tradingview.com/script/OK4OIiJ2-stochastic-atr/
  2. What are the corresponding settings for ThinScripts

//@version=3
strategy("Stoch + ATR", overlay=false, pyramiding = 0, calc_on_order_fills = false, commission_type = strategy.commission.percent, commission_value = 0.0454, default_qty_type = strategy.percent_of_equity, default_qty_value = 100)

len = input(34, minval=1, title="Length for Main Stochastic & ATR")
smoothK = input(2, minval=1, title="SmoothK for Main Stochastic")
lowLine = input(10, minval=-50, maxval=50, title="Multiplier for up/low lines")

//Stoch formula
k = sma(stoch(close, high, low, len), smoothK)
plot(k, color=aqua, title = "Stoch")

//len=input
atr=atr(len)
plot(rsi(atr, len)+lowLine , color=red,linewidth=2, title = "low line")
plot(rsi(atr, len)*-1+100-lowLine, color=lime,linewidth=2, title = "up line")

aboveLine = crossunder(k,(rsi(atr, len)+lowLine))? 1 : 0
belowLine = crossover(k,(rsi(atr, len)*-1+100-lowLine))? 1 : 0

aboveLine2 = crossover(k,(rsi(atr, len)+lowLine))? 1 : 0
belowLine2 = crossunder(k,(rsi(atr, len)*-1+100-lowLine))? 1 : 0

skip=(aboveLine2==1 or belowLine2==1) and (aboveLine==1 or belowLine==1)? 1 : 0

//BackGroound Color Plots
plotchar(belowLine==1 and skip==0, title="Buy Signal", char='B', location=location.bottom, color=white, transp=0, offset=0)
plotchar(aboveLine==1 and skip==0, title="Sell Signal", char='S', location=location.top, color=white, transp=0, offset=0)
plotchar(belowLine2==1 and skip==0, title="Close Signal", char='C', location=location.bottom, color=white, transp=0, offset=0)
plotchar(aboveLine2==1 and skip==0, title="Close Signal", char='C', location=location.top, color=white, transp=0, offset=0)

bgcolor(aboveLine==1 ? red : na, transp=30, title = "sell signal")
bgcolor(belowLine==1 ? lime : na, transp=30, title = "buy signal")

bgcolor(aboveLine2==1 ? lime : na, transp=80, title = "close short")
bgcolor(belowLine2==1 ? red : na, transp=80, title = "close long")

bgcolor(skip==1 ? black : na, transp=0, title = "skip signal")

//strategy
longCondition = belowLine==1
shortCondition = aboveLine==1

strategy.entry("BUY", strategy.long, when = longCondition)
strategy.entry("SELL", strategy.short, when = shortCondition)
strategy.cancel_all(when = skip==1)
check out the below.
CSS:
#//@laptevmaxim92
#strategy("Stoch + ATR")
# Converted by Sam4Cok@Samer800 - 12/2022
declare lower;
input ShowBubbles = yes;
input Length = 34;    # "Length for Main Stochastic & ATR"
input smoothK = 2;    # "SmoothK for Main Stochastic"
input threshold = 10; # "Multiplier for up/low lines"

def na = Double.NaN;
def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;

# stoch(source, high, low, length) =>
script stoch {
input src = close;
input h = high;
input l = low;
input len = 34;
    def stoch = 100 * (src - lowest(l, len)) / (highest(h, len) - lowest(l, len));
    plot return = stoch;
}
#//Stoch formula
def k = SimpleMovingAvg(stoch(close, high, low, Length), smoothK);

#//len=input
def nATR = atr(Length=Length);
def nRSI = rsi(Price=nATR,Length=Length);
def LowLine =  nRSI + threshold;
def UpLine  = -nRSI + 100 - threshold;
plot LowPlot = LowLine;
LowPlot.SetDefaultColor(Color.RED);
LowPlot.SetLineWeight(2);
plot UpPlot = UpLine;
UpPlot.SetDefaultColor(Color.GREEN);
UpPlot.SetLineWeight(2);
plot Stoch = k;
Stoch.SetDefaultColor(Color.CYAN);

def aboveLine = if k crosses below LowLine then 1 else 0;
def belowLine = if k crosses above UpLine then 1 else 0;

def aboveLine2 = if k crosses above LowLine then 1 else 0;
def belowLine2 = if k crosses below UpLine then 1 else 0;

def skip = if (aboveLine2 or belowLine2) and (aboveLine or belowLine) then 1 else 0;

#//BackGroound Color Plots
plot BuySignal = if belowLine and !skip then 5 else na;
BuySignal.SetPaintingStrategy(PaintingStrategy.POINTS);
BuySignal.SetDefaultColor(Color.GREEN);
plot SellSignal = if aboveLine and !skip then 95 else na;
SellSignal.SetPaintingStrategy(PaintingStrategy.POINTS);
SellSignal.SetDefaultColor(Color.RED);

plot CloseBuy = if belowLine2 and !skip then 5 else na;
CloseBuy.SetPaintingStrategy(PaintingStrategy.POINTS);
CloseBuy.SetDefaultColor(Color.LIGHT_GREEN);
plot CloseSell = if aboveLine2 and !skip then 95 else na;
CloseSell.SetPaintingStrategy(PaintingStrategy.POINTS);
CloseSell.SetDefaultColor(Color.PINK);

plot midLine = if isNaN(close) then na else 50;
midLine.SetDefaultColor(Color.GRAY);
midLine.SetStyle(Curve.SHORT_DASH);
plot HiLine = if isNaN(close) then na else 100;
HiLine.SetDefaultColor(Color.WHITE);
HiLine.SetStyle(Curve.LONG_DASH);
plot LoLine = if isNaN(close) then na else 0;
LoLine.SetDefaultColor(Color.MAGENTA);
LoLine.SetStyle(Curve.LONG_DASH);

#----- Bubbles
AddChartBubble(ShowBubbles and belowLine and !skip, 5, "B", Color.GREEN, no);
AddChartBubble(ShowBubbles and aboveLine and !skip,95, "S", Color.RED, yes);
AddChartBubble(ShowBubbles and belowLine2 and !skip, 5, "C", Color.DARK_GREEN, no);
AddChartBubble(ShowBubbles and aboveLine2 and !skip,95, "C", Color.DARK_RED, yes);
# --- Signals
AddChart(high = if aboveLine then pos else na , low = neg , open = pos,  close = neg,
         type = ChartType.CANDLE, growcolor =  Color.DARK_RED);
AddChart(high = if belowLine then pos else na , low = neg , open = pos,  close = neg,
         type = ChartType.CANDLE, growcolor =  Color.DARK_GREEN);
AddChart(high = if aboveLine2 then pos else na , low = neg , open = pos,  close = neg,
         type = ChartType.CANDLE, growcolor =  CreateColor(57,95,57));
AddChart(high = if belowLine2 then pos else na , low = neg , open = pos,  close = neg,
         type = ChartType.CANDLE, growcolor =  CreateColor(127,96,101));

#--- End Code
 
check out the below.
CSS:
#//@laptevmaxim92
#strategy("Stoch + ATR")
# Converted by Sam4Cok@Samer800 - 12/2022
declare lower;
input ShowBubbles = yes;
input Length = 34;    # "Length for Main Stochastic & ATR"
input smoothK = 2;    # "SmoothK for Main Stochastic"
input threshold = 10; # "Multiplier for up/low lines"

def na = Double.NaN;
def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;

# stoch(source, high, low, length) =>
script stoch {
input src = close;
input h = high;
input l = low;
input len = 34;
    def stoch = 100 * (src - lowest(l, len)) / (highest(h, len) - lowest(l, len));
    plot return = stoch;
}
#//Stoch formula
def k = SimpleMovingAvg(stoch(close, high, low, Length), smoothK);

#//len=input
def nATR = atr(Length=Length);
def nRSI = rsi(Price=nATR,Length=Length);
def LowLine =  nRSI + threshold;
def UpLine  = -nRSI + 100 - threshold;
plot LowPlot = LowLine;
LowPlot.SetDefaultColor(Color.RED);
LowPlot.SetLineWeight(2);
plot UpPlot = UpLine;
UpPlot.SetDefaultColor(Color.GREEN);
UpPlot.SetLineWeight(2);
plot Stoch = k;
Stoch.SetDefaultColor(Color.CYAN);

def aboveLine = if k crosses below LowLine then 1 else 0;
def belowLine = if k crosses above UpLine then 1 else 0;

def aboveLine2 = if k crosses above LowLine then 1 else 0;
def belowLine2 = if k crosses below UpLine then 1 else 0;

def skip = if (aboveLine2 or belowLine2) and (aboveLine or belowLine) then 1 else 0;

#//BackGroound Color Plots
plot BuySignal = if belowLine and !skip then 5 else na;
BuySignal.SetPaintingStrategy(PaintingStrategy.POINTS);
BuySignal.SetDefaultColor(Color.GREEN);
plot SellSignal = if aboveLine and !skip then 95 else na;
SellSignal.SetPaintingStrategy(PaintingStrategy.POINTS);
SellSignal.SetDefaultColor(Color.RED);

plot CloseBuy = if belowLine2 and !skip then 5 else na;
CloseBuy.SetPaintingStrategy(PaintingStrategy.POINTS);
CloseBuy.SetDefaultColor(Color.LIGHT_GREEN);
plot CloseSell = if aboveLine2 and !skip then 95 else na;
CloseSell.SetPaintingStrategy(PaintingStrategy.POINTS);
CloseSell.SetDefaultColor(Color.PINK);

plot midLine = if isNaN(close) then na else 50;
midLine.SetDefaultColor(Color.GRAY);
midLine.SetStyle(Curve.SHORT_DASH);
plot HiLine = if isNaN(close) then na else 100;
HiLine.SetDefaultColor(Color.WHITE);
HiLine.SetStyle(Curve.LONG_DASH);
plot LoLine = if isNaN(close) then na else 0;
LoLine.SetDefaultColor(Color.MAGENTA);
LoLine.SetStyle(Curve.LONG_DASH);

#----- Bubbles
AddChartBubble(ShowBubbles and belowLine and !skip, 5, "B", Color.GREEN, no);
AddChartBubble(ShowBubbles and aboveLine and !skip,95, "S", Color.RED, yes);
AddChartBubble(ShowBubbles and belowLine2 and !skip, 5, "C", Color.DARK_GREEN, no);
AddChartBubble(ShowBubbles and aboveLine2 and !skip,95, "C", Color.DARK_RED, yes);
# --- Signals
AddChart(high = if aboveLine then pos else na , low = neg , open = pos,  close = neg,
         type = ChartType.CANDLE, growcolor =  Color.DARK_RED);
AddChart(high = if belowLine then pos else na , low = neg , open = pos,  close = neg,
         type = ChartType.CANDLE, growcolor =  Color.DARK_GREEN);
AddChart(high = if aboveLine2 then pos else na , low = neg , open = pos,  close = neg,
         type = ChartType.CANDLE, growcolor =  CreateColor(57,95,57));
AddChart(high = if belowLine2 then pos else na , low = neg , open = pos,  close = neg,
         type = ChartType.CANDLE, growcolor =  CreateColor(127,96,101));

#--- End Code
Great Job Samer800 - Can you adjust the Bubbles so more visible without expanding the lower study. Thanks Again !!!
 
Great Job Samer800 - Can you adjust the Bubbles so more visible without expanding the lower study. Thanks Again !!!
I don't see option to adjust the bubbles in TOS. But I can see if replace bubbles with Arrows may can be more visible. try the code below.

CSS:
#//@laptevmaxim92
#strategy("Stoch + ATR")
# Converted by Sam4Cok@Samer800 - 12/2022
declare lower;
input ShowBubbles = no;
input Length = 34;    # "Length for Main Stochastic & ATR"
input smoothK = 2;    # "SmoothK for Main Stochastic"
input threshold = 10; # "Multiplier for up/low lines"

def na = Double.NaN;
def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;

# stoch(source, high, low, length) =>
script stoch {
input src = close;
input h = high;
input l = low;
input len = 34;
    def stoch = 100 * (src - lowest(l, len)) / (highest(h, len) - lowest(l, len));
    plot return = stoch;
}
#//Stoch formula
def k = SimpleMovingAvg(stoch(close, high, low, Length), smoothK);

#//len=input
def nATR = atr(Length=Length);
def nRSI = rsi(Price=nATR,Length=Length);
def LowLine =  nRSI + threshold;
def UpLine  = -nRSI + 100 - threshold;
plot LowPlot = LowLine;
LowPlot.SetDefaultColor(Color.RED);
LowPlot.SetLineWeight(2);
plot UpPlot = UpLine;
UpPlot.SetDefaultColor(Color.GREEN);
UpPlot.SetLineWeight(2);
plot Stoch = k;
Stoch.SetDefaultColor(Color.CYAN);

def aboveLine = if k crosses below LowLine then 1 else 0;
def belowLine = if k crosses above UpLine then 1 else 0;

def aboveLine2 = if k crosses above LowLine then 1 else 0;
def belowLine2 = if k crosses below UpLine then 1 else 0;

def skip = if (aboveLine2 or belowLine2) and (aboveLine or belowLine) then 1 else 0;

#//BackGroound Color Plots
plot BuySignal = if belowLine and !skip then 10 else na;
BuySignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
BuySignal.SetDefaultColor(Color.GREEN);
plot SellSignal = if aboveLine and !skip then 90 else na;
SellSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
SellSignal.SetDefaultColor(Color.RED);

plot CloseBuy = if belowLine2 and !skip then 10 else na;
CloseBuy.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
CloseBuy.SetDefaultColor(Color.LIGHT_GREEN);
plot CloseSell = if aboveLine2 and !skip then 90 else na;
CloseSell.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
CloseSell.SetDefaultColor(Color.PINK);

plot midLine = if isNaN(close) then na else 50;
midLine.SetDefaultColor(Color.GRAY);
midLine.SetStyle(Curve.SHORT_DASH);
plot HiLine = if isNaN(close) then na else 100;
HiLine.SetDefaultColor(Color.WHITE);
HiLine.SetStyle(Curve.LONG_DASH);
plot LoLine = if isNaN(close) then na else 0;
LoLine.SetDefaultColor(Color.MAGENTA);
LoLine.SetStyle(Curve.LONG_DASH);

#----- Bubbles
AddChartBubble(ShowBubbles and belowLine and !skip,10, "B", Color.GREEN, no);
AddChartBubble(ShowBubbles and aboveLine and !skip,90, "S", Color.RED, yes);
AddChartBubble(ShowBubbles and belowLine2 and !skip,10, "C", Color.DARK_GREEN, no);
AddChartBubble(ShowBubbles and aboveLine2 and !skip,90, "C", Color.DARK_RED, yes);
# --- Signals
AddChart(high = if aboveLine then pos else na , low = neg , open = pos,  close = neg,
         type = ChartType.CANDLE, growcolor =  Color.DARK_RED);
AddChart(high = if belowLine then pos else na , low = neg , open = pos,  close = neg,
         type = ChartType.CANDLE, growcolor =  Color.DARK_GREEN);
AddChart(high = if aboveLine2 then pos else na , low = neg , open = pos,  close = neg,
         type = ChartType.CANDLE, growcolor =  CreateColor(57,95,57));
AddChart(high = if belowLine2 then pos else na , low = neg , open = pos,  close = neg,
         type = ChartType.CANDLE, growcolor =  CreateColor(127,96,101));


#--- End Code
 
I don't see option to adjust the bubbles in TOS. But I can see if replace bubbles with Arrows may can be more visible. try the code below.

CSS:
#//@laptevmaxim92
#strategy("Stoch + ATR")
# Converted by Sam4Cok@Samer800 - 12/2022
declare lower;
input ShowBubbles = no;
input Length = 34;    # "Length for Main Stochastic & ATR"
input smoothK = 2;    # "SmoothK for Main Stochastic"
input threshold = 10; # "Multiplier for up/low lines"

def na = Double.NaN;
def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;

# stoch(source, high, low, length) =>
script stoch {
input src = close;
input h = high;
input l = low;
input len = 34;
    def stoch = 100 * (src - lowest(l, len)) / (highest(h, len) - lowest(l, len));
    plot return = stoch;
}
#//Stoch formula
def k = SimpleMovingAvg(stoch(close, high, low, Length), smoothK);

#//len=input
def nATR = atr(Length=Length);
def nRSI = rsi(Price=nATR,Length=Length);
def LowLine =  nRSI + threshold;
def UpLine  = -nRSI + 100 - threshold;
plot LowPlot = LowLine;
LowPlot.SetDefaultColor(Color.RED);
LowPlot.SetLineWeight(2);
plot UpPlot = UpLine;
UpPlot.SetDefaultColor(Color.GREEN);
UpPlot.SetLineWeight(2);
plot Stoch = k;
Stoch.SetDefaultColor(Color.CYAN);

def aboveLine = if k crosses below LowLine then 1 else 0;
def belowLine = if k crosses above UpLine then 1 else 0;

def aboveLine2 = if k crosses above LowLine then 1 else 0;
def belowLine2 = if k crosses below UpLine then 1 else 0;

def skip = if (aboveLine2 or belowLine2) and (aboveLine or belowLine) then 1 else 0;

#//BackGroound Color Plots
plot BuySignal = if belowLine and !skip then 10 else na;
BuySignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
BuySignal.SetDefaultColor(Color.GREEN);
plot SellSignal = if aboveLine and !skip then 90 else na;
SellSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
SellSignal.SetDefaultColor(Color.RED);

plot CloseBuy = if belowLine2 and !skip then 10 else na;
CloseBuy.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
CloseBuy.SetDefaultColor(Color.LIGHT_GREEN);
plot CloseSell = if aboveLine2 and !skip then 90 else na;
CloseSell.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
CloseSell.SetDefaultColor(Color.PINK);

plot midLine = if isNaN(close) then na else 50;
midLine.SetDefaultColor(Color.GRAY);
midLine.SetStyle(Curve.SHORT_DASH);
plot HiLine = if isNaN(close) then na else 100;
HiLine.SetDefaultColor(Color.WHITE);
HiLine.SetStyle(Curve.LONG_DASH);
plot LoLine = if isNaN(close) then na else 0;
LoLine.SetDefaultColor(Color.MAGENTA);
LoLine.SetStyle(Curve.LONG_DASH);

#----- Bubbles
AddChartBubble(ShowBubbles and belowLine and !skip,10, "B", Color.GREEN, no);
AddChartBubble(ShowBubbles and aboveLine and !skip,90, "S", Color.RED, yes);
AddChartBubble(ShowBubbles and belowLine2 and !skip,10, "C", Color.DARK_GREEN, no);
AddChartBubble(ShowBubbles and aboveLine2 and !skip,90, "C", Color.DARK_RED, yes);
# --- Signals
AddChart(high = if aboveLine then pos else na , low = neg , open = pos,  close = neg,
         type = ChartType.CANDLE, growcolor =  Color.DARK_RED);
AddChart(high = if belowLine then pos else na , low = neg , open = pos,  close = neg,
         type = ChartType.CANDLE, growcolor =  Color.DARK_GREEN);
AddChart(high = if aboveLine2 then pos else na , low = neg , open = pos,  close = neg,
         type = ChartType.CANDLE, growcolor =  CreateColor(57,95,57));
AddChart(high = if belowLine2 then pos else na , low = neg , open = pos,  close = neg,
         type = ChartType.CANDLE, growcolor =  CreateColor(127,96,101));


#--- End Code
It worked!!! thank you Samer800
 

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
394 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