Search code examples
pythonpandasmathcandlesticks

Trend line continuation


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


Solution

  •     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()