Coding Double Triangular MA Cross with Extrapolation (Pinescript)

Female Software Engineer Coding on Computer
// This source code is subject to the terms of the Mozilla Public License 2.0
// © Yaseen Khalil
// @version=5
// Define as indicator, overlay on chart
Indicator (" Double Triangular MA Cross", shorttitle="TMA Cross", overlay=true) 

// Inputs for adjusting and defining the Length of the 1st TMA 
length = input(title="Length1", type=integer, minval=1, defval=15)
shift     =  -15
cl          = green

// Formula for calculating the triangular moving average
tma      = wma(wma(close, ceil(length / 2)), floor(length / 2) + 1)

// Plotting the the first TMA on the chart
plot(tma, title="TMA", linewidth=2, color=cl, transp=0, offset=shift)

// Formula to extrapolate the first TMA a set amount into the future, based on past actions
func (i) =>
    x = wma(wma(close, ceil((length-i) / 2)), floor((length-i) / 2) + 1)[0]
    out = (x * (length-i) + close[0]*i)/length
ld=2
trs=0

// Plotting the extrapolation values as circles to differentiate from the TMA line
plot(func(1),color=cl,linewidth=ld, style=circles ,transp=trs, show_last=1)
plot(func(2),color=cl,linewidth=ld, style=circles ,transp=trs, show_last=1)
plot(func(3),color=cl,linewidth=ld, style=circles,transp=trs, show_last=1)
plot(func(4),color=cl,linewidth=ld, style=circles,transp=trs, show_last=1)
plot(func(5),color=cl,linewidth=ld, style=circles,transp=trs, show_last=1)
plot(func(6),color=cl,linewidth=ld, style=circles,transp=trs, show_last=1)
plot(func(7),color=cl,linewidth=ld, style=circles,transp=trs, show_last=1)
plot(func(8),color=cl,linewidth=ld, style=circles,transp=trs, show_last=1)
plot(func(9),color=cl,linewidth=ld, style=circles,transp=trs, show_last=1)
plot(func(10),color=cl,linewidth=ld, style=circles,transp=trs, show_last=1)
plot(func(11),color=cl,linewidth=ld, style=circles,transp=trs, show_last=1)
plot(func(12),color=cl,linewidth=ld, style=circles,transp=trs, show_last=1)
plot(func(13),color=cl,linewidth=ld, style=circles,transp=trs, show_last=1)
plot(func(14),color=cl,linewidth=ld, style=circles,transp=trs, show_last=1)
plot(func(15),color=cl,linewidth=ld, style=circles,transp=trs, show_last=1)
plot(func(16),color=cl,linewidth=ld, style=circles,transp=trs, show_last=1)
plot(func(17),color=cl,linewidth=ld, style=circles,transp=trs, show_last=1)
// Inputs for adjusting and defining the Length of the 2nd TMA 
length2 = input(title="Length1", type=integer, minval=1, defval=30)
shift2     =  -10
cl2          = red

// Formula for calculating the second triangular moving average
tma2      = wma(wma(close, ceil(length2 / 2)), floor(length2 / 2) + 1)

// Plotting the second TMA on the chart
plot(tma2, title="TMA", linewidth=2, color=cl2, transp=0, offset=shift2)

// Formula to extrapolate the second TMA a set amount into the future, based on past actions
func2 (i2) =>
    x2 = wma(wma(close, ceil((length2-i2) / 2)), floor((length2-i2) / 2) + 1)[1]
    out2 = (x2 * (length2-i2) + close[0]*i2)/length2
plot(func2(1),color=cl2,linewidth=ld, style=circles ,transp=trs, show_last=1)
plot(func2(2),color=cl2,linewidth=ld, style=circles ,transp=trs, show_last=1)
plot(func2(3),color=cl2,linewidth=ld, style=circles,transp=trs, show_last=1)
plot(func2(4),color=cl2,linewidth=ld, style=circles,transp=trs, show_last=1)
plot(func2(5),color=cl2,linewidth=ld, style=circles,transp=trs, show_last=1)
plot(func2(6),color=cl2,linewidth=ld, style=circles,transp=trs, show_last=1)
plot(func2(7),color=cl2,linewidth=ld, style=circles,transp=trs, show_last=1)
plot(func2(8),color=cl2,linewidth=ld, style=circles,transp=trs, show_last=1)
plot(func2(9),color=cl2,linewidth=ld, style=circles,transp=trs, show_last=1)
plot(func2(10),color=cl2,linewidth=ld, style=circles,transp=trs, show_last=1)
plot(func2(11),color=cl2,linewidth=ld, style=circles,transp=trs, show_last=1)
plot(func2(12),color=cl2,linewidth=ld, style=circles,transp=trs, show_last=1)
plot(func2(13),color=cl2,linewidth=ld, style=circles,transp=trs, show_last=1)
plot(func2(14),color=cl2,linewidth=ld, style=circles,transp=trs, show_last=1)
plot(func2(15),color=cl2,linewidth=ld, style=circles,transp=trs, show_last=1) 

Download and see more projects at my GitHub page.

Now, let’s check out how well it back tests!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top