Home > Net >  Trend line continuation
Trend line continuation

Time:04-01

I have 2 trends lines, that is generate automatically with candlestick date (high, low, close, open). When this is generated I store the trend line values. When I load this data I need that the lines continues on future. For exemple: I had a Dataframe:

At 10:00 -> df.iloc[0]['TrendLine'] = 100 At 11:00 -> df.iloc[1]['TrendLine'] = 130 At 12:00 -> df.iloc[2]['TrendLine'] = ? (I need a continuation plot value of trend.)

Follow the image that make its more clear: enter image description here

CodePudding user response:

What you're looking for is to find the slope of these lines and and continue the trend. An easy way to do this is with the NumPy polyval method to evaluate a polynomial at a given value, where you will pass a poly1d object. Will look something like:

    import numpy as np
    np.polyval(np.polyfit(x, y, 1), x)

You will most likely have to pull those columns out of the dataframe with the OHLC data and put them in their own if you are projecting these trend lines out to future dates where you don't have data yet.

More on numpy.polyval(): https://numpy.org/doc/stable/reference/generated/numpy.polyval.html

CodePudding user response:

    import matplotlib.pyplot as plt
    import numpy as np
    
    up = []
    dn = []
    up.append(1.00)
    dn.append(1.25)
    for i in range(1, 25):
        dn.append(dn[i-1] / 1.0015)
        up.append(up[i-1] * 1.003)
    
    #We absolutely do not know what the trend lines should be in the future.
    koef_up = up[len(up)-1] / up[len(up)-2]#get coeficents up
    koef_dn = dn[len(up)-2] / dn[len(up)-1]#get coeficents dn
    print('koef_up', koef_up, 'koef_dn', koef_dn)
    
    for i in range(25, 30):
        up.append(up[i-1] * koef_up)#calculate line to the future up
        dn.append(dn[i-1] / koef_dn)#calculate line to the future dn
    
    ind = np.arange(30)
    fig, ax = plt.subplots()
    ax.plot(ind, up, color="green")
    ax.plot(ind, dn, color="red")
    ax.plot(ind[25:], up[25:], color="yellow")
    ax.plot(ind[25:], dn[25:], color="yellow")
    fig.autofmt_xdate()
    plt.show()
  • Related