Esempio n. 1
0
def SLR(df, price='Close', predictions_int=0):
    """
    Simple Linear Regression
    Returns: list of floats = jhta.SLR(df, price='Close', predictions_int=0)
    Source: https://machinelearningmastery.com/implement-simple-linear-regression-scratch-python/
    """
    x_list = list(range(len(df[price]) - predictions_int))
    p_list = df[price][0:len(df[price]) - predictions_int]
    b1 = jhta.COV(x_list, p_list) / jhta.VARIANCE({'x': x_list}, len(x_list),
                                                  'x')[-1]
    b0 = jhta.MEAN({'y': p_list}, len(p_list), 'y')[-1] - b1 * jhta.MEAN(
        {'x': x_list}, len(x_list), 'x')[-1]
    return [b0 + b1 * i for i in range(len(df[price]))]
Esempio n. 2
0
def COV(x_list, y_list):
    """
    Covariance
    Returns: float = jhta.COV(x_list, y_list)
    Source: https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Covariance
    """
    x_mean = jhta.MEAN({'x_list': x_list}, len(x_list), 'x_list')[-1]
    y_mean = jhta.MEAN({'y_list': y_list}, len(y_list), 'y_list')[-1]
    covariance = .0
    for i in range(len(x_list)):
        a = x_list[i] - x_mean
        b = y_list[i] - y_mean
        covariance += a * b / len(x_list)
    return covariance
Esempio n. 3
0
def STANDARDIZE(df, price='Close'):
    """
    Standardize
    Returns: list of floats = jhta.STANDARDIZE(df, price='Close')
    Source: https://machinelearningmastery.com/normalize-standardize-time-series-data-python/
    """
    standardize_list = []
    start = None
    for i in range(len(df[price])):
        if df[price][i] != df[price][i] or i < 1:
            standardize = float('NaN')
        else:
            if start is None:
                start = i
                x = df[price][start:]
                mean = jhta.MEAN({'x': x}, len(x), 'x')[-1]
                standard_deviation = jhta.STDEV({'x': x}, len(x), 'x')[-1]
            standardize = (df[price][i] - mean) / standard_deviation
        standardize_list.append(standardize)
    return standardize_list
Esempio n. 4
0
def STANDARDIZE(df, price='Close'):
    """
    Standardize
    """
    standardize_list = []
    i = 0
    start = None
    while i < len(df[price]):
        if df[price][i] != df[price][i] or i < 1:
            standardize = float('NaN')
        else:
            if start is None:
                start = i
                x = df[price][start:]
                mean = jhta.MEAN({'x': x}, len(x), 'x')[-1]
                standard_deviation = jhta.STDEV({'x': x}, len(x), 'x')[-1]
            standardize = (df[price][i] - mean) / standard_deviation
        standardize_list.append(standardize)
        i += 1
    return standardize_list
Esempio n. 5
0
def INFO(df, price='Close'):
    """
    Print df Information
    """
    print('{:_<28}:{:_>22}'.format('DF PRICE COLUMN', price))
    print('{:_<28}:{:_>22d}'.format('LEN', len(df[price])))
    print('{:_<28}:{:_>28.5f}'.format('MIN',
                                      jhta.MIN(df, len(df[price]), price)[-1]))
    print('{:_<28}:{:_>28.5f}'.format('MAX',
                                      jhta.MAX(df, len(df[price]), price)[-1]))
    print('{:_<28}:{:_>28.5f}'.format('SUM',
                                      jhta.SUM(df, len(df[price]), price)[-1]))
    print('{:_<28}:{:_>28.5f}'.format('MEAN',
                                      jhta.MEAN(df, len(df[price]),
                                                price)[-1]))
    #    print ('{:_<28}:{:_>28.5f}'.format('HARMONIC_MEAN', jhta.HARMONIC_MEAN(df, len(df[price]), price)[-1]))
    print('{:_<28}:{:_>28.5f}'.format(
        'MEDIAN',
        jhta.MEDIAN(df, len(df[price]), price)[-1]))
    print('{:_<28}:{:_>28.5f}'.format(
        'MEDIAN_LOW',
        jhta.MEDIAN_LOW(df, len(df[price]), price)[-1]))
    print('{:_<28}:{:_>28.5f}'.format(
        'MEDIAN_HIGH',
        jhta.MEDIAN_HIGH(df, len(df[price]), price)[-1]))
    print('{:_<28}:{:_>28.5f}'.format(
        'MEDIAN_GROUPED',
        jhta.MEDIAN_GROUPED(df, len(df[price]), price)[-1]))
    #    print ('{:_<28}:{:_>28.5f}'.format('MODE', jhta.MODE(df, len(df[price]), price)[-1]))
    print('{:_<28}:{:_>28.5f}'.format(
        'PSTDEV',
        jhta.PSTDEV(df, len(df[price]), price)[-1]))
    print('{:_<28}:{:_>28.5f}'.format(
        'PVARIANCE',
        jhta.PVARIANCE(df, len(df[price]), price)[-1]))
    print('{:_<28}:{:_>28.5f}'.format(
        'STDEV',
        jhta.STDEV(df, len(df[price]), price)[-1]))
    print('{:_<28}:{:_>28.5f}'.format(
        'VARIANCE',
        jhta.VARIANCE(df, len(df[price]), price)[-1]))
Esempio n. 6
0
def main():
#    quandl_data = quandl.get('BCHARTS/BITSTAMPUSD', order='asc', collapse='daily', returns='numpy', authtoken='YOUR_AUTH_TOKEN')
    quandl_data = quandl.get('BCHARTS/BITSTAMPUSD', order='asc', collapse='daily', returns='numpy')

    df = {'datetime': [], 'Open': [], 'High': [], 'Low': [], 'Close': [], 'Volume': []}
    i = 0
    while i < len(quandl_data['Close']):
#        df['datetime'].append(i)
        df['datetime'].append(quandl_data['Date'][i])
        df['Open'].append(float(quandl_data['Open'][i]))
        df['High'].append(float(quandl_data['High'][i]))
        df['Low'].append(float(quandl_data['Low'][i]))
        df['Close'].append(float(quandl_data['Close'][i]))
        df['Volume'].append(int(quandl_data['Volume (BTC)'][i]))
        i += 1

    x = df['datetime']

    sma_list = jhta.SMA(df, 200)
    mmr_list = jhta.MMR(df)
    mmr_mean_list = jhta.MEAN({'mmr': mmr_list}, len(mmr_list), 'mmr')
    mom_list = jhta.MOM(df, 365)
    mom_mean_list = jhta.MEAN({'mom': mom_list}, len(mom_list), 'mom')

    print ('Calculated from %i data points:' % len(x))
    print ('Last Close: %f' % df['Close'][-1])
    print ('Last SMA 200: %f' % sma_list[-1])
    print ('Last MMR: %f' % mmr_list[-1])
    print ('Last MEAN MMR: %f' % mmr_mean_list[-1])
    print ('Last MOM 365: %f' % mom_list[-1])
    print ('Last MEAN MOM 365: %f' % mom_mean_list[-1])

#    left = 365
#    right = len(x)

#    print ('Plot starts from %i until %i in Log scale:' % (left, right))

    plt.figure(1, (30, 10))

    plt.subplot(311)
    plt.title('Time / Price / Ratio')
    plt.xlabel('Time')
    plt.ylabel('Price')
    plt.grid(True)
    plt.plot(x, df['Close'], color='blue')
    plt.plot(x, sma_list, color='red')
    plt.legend(['Close', 'SMA 200'], loc='upper left')
    plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
    plt.gca().xaxis.set_major_locator(mdates.YearLocator())
    plt.gcf().autofmt_xdate()
#    plt.xlim(left=left, right=right)
    plt.yscale('log')

    plt.subplot(312)
    plt.xlabel('Time')
    plt.ylabel('Ratio')
    plt.grid(True)
    plt.plot(x, [1] * len(x), color='red')
    plt.plot(x, mmr_list)
    plt.plot(x, mmr_mean_list)
    plt.plot(x, [2.4] * len(x))
    plt.legend(['SMA 200', 'MMR', 'MEAN MMR', 'THRESHOLD 2.4'], loc='upper left')
    plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
    plt.gca().xaxis.set_major_locator(mdates.YearLocator())
    plt.gcf().autofmt_xdate()
#    plt.xlim(left=left, right=right)
    plt.yscale('log')

    plt.subplot(313)
    plt.xlabel('Time')
    plt.ylabel('Ratio')
    plt.grid(True)
    plt.plot(x, [0] * len(x), color='blue')
    plt.plot(x, mom_list)
    plt.plot(x, mom_mean_list)
    plt.legend(['Price', 'MOM 365', 'MEAN MOM 365'], loc='upper left')
    plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
    plt.gca().xaxis.set_major_locator(mdates.YearLocator())
    plt.gcf().autofmt_xdate()
#    plt.xlim(left=left, right=right)
    plt.yscale('symlog')

    plt.show()