[ Beginner -2 ] Best Algo Trading Tutorial – KDJ Indicator

Yaonology Algorithmic Trading
Share on facebook
Share on twitter
Share on linkedin
Share on email
Share on reddit

Algo trading is not as difficult as you think. In this tutorial, there are four simple steps to lead you to how to code your own algorithmic trading strategy with KDJ indicator. All the coding is using pine script and based on the Tradingview platform.

The article will first display the full version of the KDJ indicator strategy content at each step and will separate it into steps to explain. 

The black part is a default code part, and the yellow part can be changed according to personal needs!

Next, the article will introduce the meaning of each step.

// KDJ Indicators
//@version = 4
strategy("Yaonology KDJ Indicators Tutorial", overlay=false, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, currency = currency.USD, initial_capital = 10000, commission_type =
strategy.commission.percent, commission_value = 0)

prd = input(9, title="period")
sgnl = input(3, title="signal")

v(s,l,m) =>
   v = 0.0
    v := (m*s+(l-m)*nz(v[1]))/l

c = close
h = highest(high, prd)
l = lowest(low,prd)
RSV = 100*((c-l)/(h-l)

K = v(RSV, sgnl, 1)

D = v(K, sgnl, 1)
J = 3 * K-2 * D

plot(K, color = color.green)
plot(D, color = color.red)
plot(J, color = color.black)

if (K < 20 and D < 20 and J < 20 and K > D and K[1] < D[1]) or (J > 0 and J[1] < 0)
strategy.entry(id = "kd", long = true)  
if (K > 80 and D > 80 and J > 80 and K < D and K[1] > D[1]) or (J < 100 and J[1] > 100)
strategy.close(id = "kd")

Four Steps Tutorial

Step One: Strategy Setting

strategy(“Yaonology KDJ Indicators Tutorial“, overlay=false, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, currency = currency.USD, initial_capital = 10000, commission_type = strategy.commission.percent, commission_value = 0)

strategy: Name of strategy (Yaonology sets the name as Yaonology KDJ Indicators Tutorial)

overlay: No – False

default_qty_type : Type of quantity – percent of equity

default_qty_value : Value of quantity – 100

currency:  USD

initial_capital: Initial trading price – 10000

commission_type: Type of commission – percent

commission_value: Value of commission ( there is no commission, so we set 0)

Step Two: Input

prd = input(9, title=”period”)

sgnl = input(3, title=”signal”)

In general, the period will be 9 (days) and signal will be 3 (as the picture below)

[ Beginner -2 ] Best Algo Trading Tutorial – KDJ Indicator
Source: Yaonology

Formula Setting: 

v(s,l,m) => This section is to build functions to calculates K and J values 

    v = 0.0 => Indicates that the value of V is 0

    v := (m*s+(l-m)*nz(v[1]))/l => This formula is for calculating K and D values

(: = Indicates forced writing, v [1] indicates the previous day’s value of V value, and nz       indicates that v is replaced by 0 when there is no previous day value)

c = close – Closing price 

h = highest(high, prd) – The highest price in this period

l = lowest(low,prd) – The lowest price in this period

RSV = 100*((c-l)/(h-l)) – Formula to calculate RSV

K = v(RSV, sgnl, 1) – Formula to calculate K

Ex: //v = (1*RSV + (3 – 1)*v[1])/3
          = ⅓*RSV + ⅔*v[1] 

D = v(K, sgnl, 1) – Formula to calculate D

J = 3 * K-2 * D – Formula to calculate J

Step Three: Plot

plot(K, color = color.green) – Yaonology sets K curve as green

plot(D, color = color.red) – Yaonology sets K curve as red

plot(J, color = color.black) – Yaonology sets K curve as black

Step Four: Strategy.entry & Strategy.close

  •         Method 1

if (K < 20 and D < 20 and J < 20 and K > D and K[1] < D[1]) 

strategy.entry(id = “kd“, long = true

       =>  [1] means one day before

        If this condition applies, this is the point to consider to buy it.

        Strategy.entry: The command to enter market position

        id: The order name. Yaonology sets as kd

         long: This means long position, and here is true

if (K > 80 and D > 80 and J > 80 and K < D and K[1] > D[1]) 

strategy.close(id = “kd“)

      =>  If this condition applies, this is the point to consider to sell it.

        strategy.close: The command to close the market

        The id should set the same one as the entry one since it is the same order

  • Method 2 (The result will be more reliable with J)

*Because the number of samples is insufficient, the reference is not accurate enough. Combining J value can obtain enough samples and calculate a more accurate result. Besides, the reference value exceeds 30 would be more reliable. As shown in the red box below (The first picture is the result without J and the second picture is the result with J)

[ Beginner -2 ] Best Algo Trading Tutorial – KDJ Indicator
Result from sample without J
[ Beginner -2 ] Best Algo Trading Tutorial – KDJ Indicator
Result from sample with J

if (K < 20 and D < 20 and J < 20 and K > D and K[1] < D[1]) or (J > 0 and J[1] < 0)

strategy.entry(id = “kd“, long = true)   

          =>  If this condition applies, this is the point to consider to buy it.

if (K > 80 and D > 80 and J > 80 and K < D and K[1] > D[1]) or (J < 100 and J[1] > 100) 

strategy.close(id = “kd“)

=> If this condition applies, this is the point to consider to sell it.

Complete Version

[ Beginner -2 ] Best Algo Trading Tutorial – KDJ Indicator
Source: Yaonology

Yaonology

#Yaonology #algorithmictrading #coding #SP500 #KDJ

My Rewards

This website uses cookies to ensure you get the best experience on our website.

Read Privacy Policy