python :talib 计算 ATR

ATR指标:Average True Range 真实波动幅度的移动平均值

python安装使用 talib 
安装主要在 http://www.lfd.uci.edu/~gohlke/pythonlibs/ 
这个网站找到
TA_Lib‑0.4.17‑cp37‑cp37m‑win32.whl
TA_Lib‑0.4.17‑cp37‑cp37m‑win_amd64.whl

cd \Anaconda3\Scripts
pip install \Talib\TA_Lib-0.4.17-cp37-cp37m-win_amd64.whl

get_atr.py

# coding: utf-8
import os, sys
import numpy as np
import pandas as pd
import tushare as ts
import talib
import math

if len(sys.argv) ==2:
    code = sys.argv[1]
else:
    print('usage: python get_atr.py stockcode ')
    sys.exit(1)

if len(code) !=6:
    print('stock code length: 6')
    sys.exit(2)

df = ts.get_k_data(code)
if df.empty ==True:
    print(" df is empty ")
    sys.exit(2)
df = df[ df['date'] > '2019-01-01']
if len(df) <15:
    print(" len(df) <15 ")
    sys.exit(2)

# 收盘价
close = np.array(df['close'])
# 最高价
high = np.array(df['high'])
# 最低价
low = np.array(df['low'])
# 获取最新的ATR值
atr = talib.ATR(high, low, close, timeperiod=14)
print('len(atr)=',len(atr))
print(atr[-5:])
per = 1000
price = close[-1]
print("price={0} , rate: {1:.4f}%".format(price, atr[-1]*100/price))
share = math.floor(per /atr[-1]/100)*100
print('share=',share)
print(code,':',price,'*',share,'=',price*share)

运行 python get_atr.py 600030

参考:https://www.programcreek.com/python/example/92324/talib.ATR

你可能感兴趣的:(python,python,talib,ATR)