OoplesFinance.StockIndicators icon indicating copy to clipboard operation
OoplesFinance.StockIndicators copied to clipboard

New Indicator : TDI

Open kingchenc opened this issue 1 year ago • 8 comments

Hello,

Firstly, thanks for your big Indicator Collection :)

Is there a way you can add TDI ( Traders Dynamic Index ) to your lib?

TDI is a combination of the RSI, BA and MA.

Example From Tradingview > https://www.tradingview.com/script/9Ltwsixb-TDI-Traders-Dynamic-Index-Goldminds/

//
// @author LazyBear
// If you use this code in its orignal/modified form, do drop me a note. 
// 
// Modified by Bromley

study("TDI - Traders Dynamic Index [Goldminds]", shorttitle="TDIGM")

rsiPeriod = input(11, minval = 1, title = "RSI Period")
bandLength = input(31, minval = 1, title = "Band Length")
lengthrsipl = input(1, minval = 0, title = "Fast MA on RSI")
lengthtradesl = input(9, minval = 1, title = "Slow MA on RSI")

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 = blue)                              // Upper Band
dnl = plot(dn, "Lower Band", color = blue)                              // Lower Band
midl = plot(mid, "Middle of Bands", color = orange, linewidth = 2)      // Middle of Bands

plot(slowMA, "Slow MA", color=green, linewidth=2)                       // Plot Slow MA
plot(fastMA, "Fast MA", color=red, linewidth=2)                         // Plot Fast MA

fill(upl, midl, red, transp=90)                                         // Fill Upper Half Red
fill(midl, dnl, green, transp=90)                                       // Fill Lower Half Green

kingchenc avatar Jul 30 '22 16:07 kingchenc

@kingchenc I want to apologize for the long delay in a reply. My life has become super hectic as we recently found out that my wife is pregnant with twins. I actually have this indicator already in my collection and it is called the TradersDynamicIndex and you can calculate it by using this pseudo code below:

var stockData = new StockData(openPrices, highPrices, lowPrices, closePrices, volumes); var results = stockData.CalculateTradersDynamicIndex();

Let me know if you have any other questions or close this issue if this answers your question. You can also find it in the Volatility indicators section in my indicators list

ooples avatar Aug 02 '22 13:08 ooples

@ooples Oh I'm sorry, I overlooked that. thank you :)

And good luck with your twins <3

kingchenc avatar Aug 02 '22 16:08 kingchenc

@ooples Hey :) Was the birth good? :) I think you have now a stressfull time :D

Sorry that i push this closed thread again:

Are you sure your code is correct? If i verify it with correct DT with Tradingview, then all values are totally different...

test

Above your TDI Bottom 2 "different" TDI's from Tradingview

What is wrong here? with the TDI code, or my fault? I tested it with your "example code"

var stockData = new StockData(openPrices, highPrices, lowPrices, closePrices, volumes); var results = stockData.CalculateTradersDynamicIndex();

kingchenc avatar Sep 27 '22 16:09 kingchenc

@ooples Any update here?

What i can say for now:

Calculating my own RSI Values (other issue thread), with the RSI values i calculate SMA 2 + 7 and for the last i calculate Bollinger Bands, and booom, i have the TDI values. Own TDI Values too very different vs your direct way to calculate TDI

kingchenc avatar Oct 05 '22 11:10 kingchenc

@kingchenc My wife still hasn't given birth to the twins yet. We are expecting the birth within the next month. For the Traders Dynamic Index, I used the same formula that LazyBear from tradingview is using. Here is my full code below for calculating the Traders Dynamic Index so feel free to let me know if you spot any errors with it:

public static StockData CalculateTradersDynamicIndex(this StockData stockData, MovingAvgType maType = MovingAvgType.SimpleMovingAverage, 
    int length1 = 13, int length2 = 34, int length3 = 2, int length4 = 7)
{
    List<decimal> upList = new();
    List<decimal> dnList = new();
    List<decimal> midList = new();
    List<Signal> signalsList = new();

    var rsiList = CalculateRelativeStrengthIndex(stockData, maType, length1, length2);
    var rList = rsiList.CustomValuesList;
    var maList = rsiList.OutputValues["Signal"];
    stockData.CustomValuesList = rList;
    var stdDevList = CalculateStandardDeviationVolatility(stockData, maType, length2).CustomValuesList;
    var mabList = GetMovingAverageList(stockData, maType, length3, rList);
    var mbbList = GetMovingAverageList(stockData, maType, length4, rList);

    for (int i = 0; i < stockData.Count; i++)
    {
        decimal rsiSma = maList[i];
        decimal stdDev = stdDevList[i];
        decimal mab = mabList[i];
        decimal mbb = mbbList[i];
        decimal prevMab = i >= 1 ? mabList[i - 1] : 0;
        decimal prevMbb = i >= 1 ? mbbList[i - 1] : 0;
        decimal offs = 1.6185m * stdDev;

        decimal prevUp = upList.LastOrDefault();
        decimal up = rsiSma + offs;
        upList.AddRounded(up);

        decimal prevDn = dnList.LastOrDefault();
        decimal dn = rsiSma - offs;
        dnList.AddRounded(dn);

        decimal mid = (up + dn) / 2;
        midList.AddRounded(mid);

        var signal = GetBollingerBandsSignal(mab - mbb, prevMab - prevMbb, mab, prevMab, up, prevUp, dn, prevDn);
        signalsList.Add(signal);
    }

    stockData.OutputValues = new()
    {
        { "UpperBand", upList },
        { "MiddleBand", midList },
        { "LowerBand", dnList },
        { "Tdi", mabList },
        { "Signal", mbbList }
    };
    stockData.SignalsList = signalsList;
    stockData.CustomValuesList = mabList;
    stockData.IndicatorName = IndicatorName.TradersDynamicIndex;

    return stockData;
}

ooples avatar Oct 05 '22 14:10 ooples

Lets discuss first the Rsi, if your Rsi Math is wrong, then the TDI is too wrong, but for now i dont find any error in your code.

kingchenc avatar Oct 05 '22 16:10 kingchenc

@ooples Nope, with correct RSI now, TDI still incorrect, im pushing soon a update in my TEST lib, maybe we can figure it out where the fail is :)

kingchenc avatar Oct 17 '22 12:10 kingchenc

@kingchenc sounds great!

ooples avatar Oct 17 '22 19:10 ooples