Page 1 of 1

TDI Goden Cross

Posted: Tue Feb 02, 2021 11:00 pm
by Keith
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 :-)

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:

Re: TDI Goden Cross

Posted: Thu Feb 04, 2021 5:07 pm
by kmarryat
I'm not completely clear on what your calculations are doing so I can't validate that final values are correct but, a basic formula to find the middle value of 3 values would be something like

middle = (a + b + c) - min(a,b,c) - max(a,b,c)

Applying that logic, the below code should get you where you need to go. The changes I made are as follows:
Added the MidValue calculation
Changed references from x to MidValue
Removed several unneeded colons : in your if statements
Updated your tolerance comparisons. You have to compare each value to your tolerance separately
Changed your goldenCross = statements to goldenCross :=
Since you already defined the goldenCross variable you need to use := to change its value
Added a background color statement to identify when goldenCross is true.
You can change that to a plot character plotchar or plot shape plotshape function if you prefer.

Hope this helps. Good Luck!

Code: Select all

// Find Middle Value from midl, slowMA, fastMA
MidValue = (mid + slowMA + fastMA) - min(mid,slowMA,fastMA) - max(mid,slowMA,fastMA)

// Check if the middle value is within tolerance of the other two using the tolerance input
if MidValue == mid
    // if the difference between MidValue and slowMA is less than or equal to tolerance
    // and the difference between Midvalue and fasMA is less than or equal to tolerance
	if abs(MidValue - slowMA) <= tolerance and abs(MidValue - fastMA) <= tolerance
        goldenCross := true	//Change goldenCross to true
else if MidValue == slowMA
    if abs(MidValue - mid) <= tolerance and abs(MidValue - fastMA) <= tolerance
        goldenCross := true    
else
    if abs(MidValue - mid) <= tolerance and abs(MidValue - slowMA) <= tolerance
        goldenCross := true	
    
//Change Background Color to Green where the 3 lines are within tolerance
bgcolor(goldenCross ? color.green : na)

Re: TDI Goden Cross

Posted: Sat Feb 06, 2021 10:04 pm
by Keith
Thanks very much. Works perfectly :up: