TDI Goden Cross
Posted: Tue Feb 02, 2021 11:00 pm
I have been using a strategy profitably for the last year and I want to automate part of it. I want to mark on the indicator window where the mid band and the two sma's cross with a TDI indicator. I have the indicator drawing the TDI bands fine but just need to check for the cross. I can write the code in Python but just cant find a similar way in pinescript.
Here is the code. Any help appreciated :-)
Here is the code. Any help appreciated :-)
Code: Select all
//
// @author Keith Power
//
//@version=4
study("Fortuna TDI Golden Cross", shorttitle="F-GC")
rsiPeriod = input(11, minval = 1, title = "RSI Period")
bandLength = input(34, minval = 1, title = "Band Length")
lengthrsipl = input(1, minval = 0, title = "Fast MA on RSI")
lengthtradesl = input(7, minval = 1, title = "Slow MA on RSI")
tolerance = input(1.0, minval = 0, title = "Tolerance")
goldenCross = false
src = close // Source of Calculations (Close of Bar)
r = rsi(src, rsiPeriod) // RSI of Close
ma = sma(r, bandLength) // Moving Average of RSI [current]
offs = (1.6185 * stdev(r, bandLength)) // Offset
up = ma + offs // Upper Bands
dn = ma - offs // Lower Bands
mid = (up + dn) / 2 // Average of Upper and Lower Bands
fastMA = sma(r, lengthrsipl) // Moving Average of RSI 2 bars back
slowMA = sma(r, lengthtradesl) // Moving Average of RSI 7 bars back
hline(30) // Oversold
hline(50) // Midline
hline(70) // Overbought
upl = plot(up, "Upper Band", color = color.blue) // Upper Band
dnl = plot(dn, "Lower Band", color = color.blue) // Lower Band
midl = plot(mid, "Middle of Bands", color = color.orange, linewidth = 2) // Middle of Bands
plot(slowMA, "Slow MA", color=color.green, linewidth=2) // Plot Slow MA
plot(fastMA, "Fast MA", color=color.red, linewidth=2) // Plot Fast MA
// Find Middle Value from midl, slowMA, fastMA
x = [midl,slowMA,fastMA]
sorted(x)[len(x) // 2]
// Check if the middle value is within tolerance of the other two using the tolerance input
if x == midl:
if abs(x - slowMA) and abs(x - fastMA) == tolerance:
goldenCross = true
else if x == slowMA:
if abs(x - midl) and abs(x - fastMA) == tolerance:
goldenCross = true
else:
if abs(x - midl) and abs(x - slowMA) == tolerance:
goldenCross = true
//Mark on the chart an X where the 3 lines are within tolerance
if goldenCross: