Statistic Functions statistic functions

  TA-Lib provides a common basis for statistical function, is calculated based on the time-series moving window. Note TA-Lib of beta, the example is to strive for the highest price mobile beta value of a stock with the lowest sequence, the default time period is 5 days, and the capital asset pricing in general is to analyze a stock relative to the market (market index ) the fluctuations.

BETA: Beta Coefficient Capita Asset Pricing Model (CAPM) Capital Asset Pricing Model in the beta coefficient:

ta.BETA (high, low, timeperiod = 5), the sum of two sequences of values ​​of movement beta (regression analysis)

CORREL: Pearson's Correlation Coefficient (r) Pearson correlation coefficient:

ta.CORREL(high, low, timeperiod=30)

LINEARREG: Linear Regression Linear Regression:

ta.LINEARREG (close, timeperiod = 14), the closing price of the time sequence t linear regression, and outputs the predicted value

LINEARREG_ANGLE: Linear Regression Angle linear regression slope of tangent angle:

ta.LINEEARREG_ANGLE (close, timeperiod = 14) closing price of a linear regression of the time t of the sequence, and outputs the predicted value

LINEARREG_INTERCEPT: Linear Regression Angle linear regression intercept:

ta.LINEARREG_INTERCEPR(close, timeperiod=14)

LINEARREG_SLOPE: Linear Regression Slope slope of the linear regression:

ta.LINEARREG_SLOPE(close, timeperiod=14)

STDDEV: Standard Deviation standard deviation:

ta.STDDEV (close, timeperiod = 5, nbdev = 1), by default every five closing price standard deviation

TSF: Time Series Forecast Time Series Prediction:

ta.TSF(close, timeperiod=14)

VAR: Variance Variance:

ta.VAR(close, timeperiod=5, nbdev=1)

 

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import talib as ta
import tushare as ts


plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

def get_data(code, start='2015-01-01'):
    df = ts.get_k_data(code, start)
    df.index = pd.to_datetime(df.date)
    df = df.sort_index()
    return df

df = get_data('sh')[['open','close','high','low']]
df['linearreg'] = ta.LINEARREG(df.close, timeperiod=14)
df['tsf'] = ta.TSF(df.close, timeperiod=14)
df.loc['2018-08-01':, ['close','linearreg','tsf']
      ].plot(figsize=(12,6))

df['beta'] = ta.BETA(df.high, df.low, timeperiod=5)
df['correl'] = ta.CORREL(df.high, df.low, timeperiod=30)
df['stdev'] = ta.STDDEV(df.close, timeperiod=5, nbdev=1)

df[['close', 'beta', 'correl','stdev']
  ].plot(figsize=(18,8), subplots=True, layout=(2,2))
plt.subplots_adjust(wspace=0, hspace=0.2)

 

Guess you like

Origin www.cnblogs.com/wintalau/p/11618587.html