Volatility Indicator Functions volatility index function

  The current difference between the highest price and the lowest difference between the trading day, the previous day's closing price and the current highest price of the trading day, the previous day's closing price and the lowest difference between the current trading day, the three of the biggest true Range value.

  I.e. True Range = max (maximum, yesterday's closing price) - min (the minimum value, yesterday's closing price),

  Average True Range index equal to N Day True Range moving average. Fluctuations can show traders' expectations and enthusiasm . A sharp increase in volatility indicates traders in the day may be ready to continue to buy or sell a stock, reducing the volatility of said traders do not have much interest in the stock market. Volatility indicators used to measure fluctuations in prices, the possibility of assisting in determining the change in trend, the market trading atmosphere, but also can take advantage of the volatility index to help stop loss and profit .

ATR: Average True Range Average True Range: ta.ATR (high, low, close, timeperiod = 14)

NATR: Normalized Average True Range normalized average True Range: ta.NATR (high, low, close, timeperiod = 14)

TRANGE: True Range Average True Range: ta.TRANGE (high, low, close)

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['atr'] = ta.ATR(df.high, df.low, df.close, timeperiod=14)
df['natr'] = ta.NATR(df.high, df.low, df.close, timeperiod=14)
df['trange'] = ta.TRANGE(df.high, df.low, df.close)
df.tail()

df[['close', 'atr', 'natr', 'trange']
].plot(figsize=(16,12), subplots=True,
layout=(2,2))
plt.subplots_adjust(wspace=0, hspace=0.2)

The Shanghai index trend and volatility index

 

Guess you like

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