BT Leading Candle Indicator For ThinkOrSwim

jygoli

Member
The author states:
The oscillator display consists of 3 lines (K, D and J - hence the name of the display) and 2 levels. K and D are the same lines you see when using the stochastic oscillator. The J line in turn represents the deviation of the D value from the K value. The convergence of these lines indicates new trading opportunities. Just like the Stochastic Oscillator, oversold and overbought levels correspond to the times when the trend is likely to reverse.
FPafzYS.png


Does anyone has already converted this TV indicator ?
BT Leading Candle Indicator
https://www.tradingview.com/v/s80XW3Pk/[/B][/URL]
 
Last edited by a moderator:

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

Does anyone has already converted this TV indicator ?
BT Leading Candle Indicator
https://www.tradingview.com/v/s80XW3Pk/[/B][/URL]
find below the original code

CODE:
CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © blackcat1402
#study("[blackcat] L2 Leading KDJ Candle Indicator", overlay=false)
# Converted by Sam4Cok@Samer800    - 09/2023
declare lower;

#// inputs
input Length = 30;
input leadingKdjStyle = {"Line", "Candles",Default "Line & Candles"};


def na = Double.NaN;
def last = isNaN(close);
def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;
def candle = leadingKdjStyle==leadingKdjStyle."Candles";
def Line =  leadingKdjStyle==leadingKdjStyle."Line";

script nz {
    input data  = 1;
    input repl  = 0;
    def ret_val = if IsNaN(data) then repl else data;
    plot return = ret_val;
}
#//functions
#xrf(values, length) =>
script xrf {
    input values = close;
    input length = 0;
    def r_val;
    r_val = if length >= 1 then
            fold i = 0 to length with p do
            if IsNaN(r_val[1]) or !IsNaN(values[i]) then values[i] else r_val[1] else Double.NaN;
    plot return =  r_val;
}

#xsa(src,len,wei) =>
script xsa {
    input src = close;
    input len = 0;
    input wei = 0;
    def sumf;
    def ma;
    def out;
    sumf =  CompoundValue(1, sumf[1] - src[len] + src, src);
    ma   =  if IsNaN(src[len]) then Double.NaN else sumf / len;
    out  =  if IsNaN(out[1]) then ma else (src * wei + out[1] * (len - wei)) / len;
    plot return = out;
}

#//KDJ indicator
def ll = lowest(low,Length);
def hh = highest(high,Length);
def rsv = (close - ll)/(hh - ll) * 100;
def k_ = xsa(rsv, 3, 1);
def d_ = xsa(k_, 3, 1);
def k = if k_ then k_ else na;
def d = if d_ then d_ else na;
def crossUp = Crosses(k,d, CrossingDirection.ABOVE);
def crossDn = Crosses(d,k, CrossingDirection.BELOW);
def buysig =  if d < 25 and crossUp then 30 else 0;
def selsig =  if d > 75 and crossDn then 70 else 100;

#//plot buy and sell signal
plot ple = buysig;#,color=color.green, linewidth=1,style=plot.style_area, transp=0)
plot pse = selsig;#,color=color.red, linewidth=2,style=plot.style_line, transp=0)
ple.SetdefaultColor(Color.GREEN);
pse.SetDefaultColor(Color.RED);

AddCloud(pos, pse, Color.DARK_RED);
AddCloud(ple, neg, Color.DARK_GREEN);
#// KDJ leading line
def var1 = (close - Average(close,13))/Average(close,13)*100;
def var2 = (close - Average(close,26))/Average(close,21)*100;
def var3 = (close - Average(close,90))/Average(close,34)*100;
def var4 = (var1 + 3 * var2 + 9 * var3)/13;
def var5 = (100 - AbsValue(var4));
def var10 = lowest(low,10);
def var13 = highest(high,25);
def leadingline = ExpAverage((close - var10)/(var13 - var10) * 4, 4) * 25;
def up = k >= d;

plot pbias = if candle then na else leadingline;
pbias.AssignValueColor(if up then Color.YELLOW else Color.MAGENTA);
pbias.SetLineWeight(2);

# Plot the new Chart

AddChart(high = if up then d else na, low = if Line then na else k , open = k,  close = d,
         type = ChartType.CANDLE, growcolor =  CreateColor(38,166,154));

AddChart(high = if up then na else d , low = if Line then na else k , open = d,  close = k,
         type = ChartType.CANDLE, growcolor =  CreateColor(239,83,80));

#//threshold lines
def h1 = if last then na else 80;#,color=color.red, linestyle=hline.style_dotted)
def h2 = if last then na else 20;#, color=color.yellow, linestyle=hline.style_dotted)
def h3 = if last then na else 10;#,color=color.lime, linestyle=hline.style_dotted)
def h4 = if last then na else 90;#, color=color.fuchsia, linestyle=hline.style_dotted)

AddCloud(h2,h3, Color.VIOLET, Color.VIOLET, yes);
AddCloud(h1,h4, Color.PLUM, Color.PLUM, yes);

# End of CODE
 
find below the original code

CODE:
CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © blackcat1402
#study("[blackcat] L2 Leading KDJ Candle Indicator", overlay=false)
# Converted by Sam4Cok@Samer800    - 09/2023
declare lower;

#// inputs
input Length = 30;
input leadingKdjStyle = {"Line", "Candles",Default "Line & Candles"};


def na = Double.NaN;
def last = isNaN(close);
def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;
def candle = leadingKdjStyle==leadingKdjStyle."Candles";
def Line =  leadingKdjStyle==leadingKdjStyle."Line";

script nz {
    input data  = 1;
    input repl  = 0;
    def ret_val = if IsNaN(data) then repl else data;
    plot return = ret_val;
}
#//functions
#xrf(values, length) =>
script xrf {
    input values = close;
    input length = 0;
    def r_val;
    r_val = if length >= 1 then
            fold i = 0 to length with p do
            if IsNaN(r_val[1]) or !IsNaN(values[i]) then values[i] else r_val[1] else Double.NaN;
    plot return =  r_val;
}

#xsa(src,len,wei) =>
script xsa {
    input src = close;
    input len = 0;
    input wei = 0;
    def sumf;
    def ma;
    def out;
    sumf =  CompoundValue(1, sumf[1] - src[len] + src, src);
    ma   =  if IsNaN(src[len]) then Double.NaN else sumf / len;
    out  =  if IsNaN(out[1]) then ma else (src * wei + out[1] * (len - wei)) / len;
    plot return = out;
}

#//KDJ indicator
def ll = lowest(low,Length);
def hh = highest(high,Length);
def rsv = (close - ll)/(hh - ll) * 100;
def k_ = xsa(rsv, 3, 1);
def d_ = xsa(k_, 3, 1);
def k = if k_ then k_ else na;
def d = if d_ then d_ else na;
def crossUp = Crosses(k,d, CrossingDirection.ABOVE);
def crossDn = Crosses(d,k, CrossingDirection.BELOW);
def buysig =  if d < 25 and crossUp then 30 else 0;
def selsig =  if d > 75 and crossDn then 70 else 100;

#//plot buy and sell signal
plot ple = buysig;#,color=color.green, linewidth=1,style=plot.style_area, transp=0)
plot pse = selsig;#,color=color.red, linewidth=2,style=plot.style_line, transp=0)
ple.SetdefaultColor(Color.GREEN);
pse.SetDefaultColor(Color.RED);

AddCloud(pos, pse, Color.DARK_RED);
AddCloud(ple, neg, Color.DARK_GREEN);
#// KDJ leading line
def var1 = (close - Average(close,13))/Average(close,13)*100;
def var2 = (close - Average(close,26))/Average(close,21)*100;
def var3 = (close - Average(close,90))/Average(close,34)*100;
def var4 = (var1 + 3 * var2 + 9 * var3)/13;
def var5 = (100 - AbsValue(var4));
def var10 = lowest(low,10);
def var13 = highest(high,25);
def leadingline = ExpAverage((close - var10)/(var13 - var10) * 4, 4) * 25;
def up = k >= d;

plot pbias = if candle then na else leadingline;
pbias.AssignValueColor(if up then Color.YELLOW else Color.MAGENTA);
pbias.SetLineWeight(2);

# Plot the new Chart

AddChart(high = if up then d else na, low = if Line then na else k , open = k,  close = d,
         type = ChartType.CANDLE, growcolor =  CreateColor(38,166,154));

AddChart(high = if up then na else d , low = if Line then na else k , open = d,  close = k,
         type = ChartType.CANDLE, growcolor =  CreateColor(239,83,80));

#//threshold lines
def h1 = if last then na else 80;#,color=color.red, linestyle=hline.style_dotted)
def h2 = if last then na else 20;#, color=color.yellow, linestyle=hline.style_dotted)
def h3 = if last then na else 10;#,color=color.lime, linestyle=hline.style_dotted)
def h4 = if last then na else 90;#, color=color.fuchsia, linestyle=hline.style_dotted)

AddCloud(h2,h3, Color.VIOLET, Color.VIOLET, yes);
AddCloud(h1,h4, Color.PLUM, Color.PLUM, yes);

# End of CODE
Thank you so much Samer
 
This is actually a very good indicator for my 1min scalping. However, has anyone been able to figure out an adjustment to the calculations of the "LeadingLine" in order to balance it? It's extremely leading that it often jumps straight to extreme OB/OS areas too immaturely.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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