SAR指标解释:baike.baidu.com/item/SAR指标/6329095?fr=aladdin 

SAR指标画图 talib_sar2.py 

# -*- coding: utf-8 -*-
import os, sys
import tushare as ts
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import talib

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

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

if code < '600000':
    ts_code = code +'.SZ'
else:
    ts_code = code +'.SH'

# 初始化pro接口
pro = ts.pro_api('your token')

dh = pro.daily(ts_code=ts_code, start_date='20200101')
if dh.empty ==True:
    print(" df is empty ")
    sys.exit(2)

df = dh.sort_values(by='trade_date')
#df = df.reset_index(drop=True)
# 为了与原来的 tushare 保持一致性,修改列名
df.rename(columns={'trade_date':'date','vol':'volume'}, inplace=True)
if len(df) <10:
    print(" len(df) <10 ")
    sys.exit(2)

#df = df.resample('B').ffill()
print(df.head())
# SAR,Stop and Reverse,是 Welles Wilder发明的,SAR是一个基于价格/时间的指标.
sar = talib.SAR(df.high, df.low, acceleration=0.02, maximum=0.2)
#print(sar[-5:])

df['ma10'] = df['close'].rolling(window=10).mean()
df.index = pd.to_datetime(df.date)
print(df.tail(7))
# 画股票收盘价图 , SAR 散点图
df[['close','ma10']].plot(grid=True, title=code)
plt.plot(df.index, sar, '.',c='black', label='sar')
plt.legend(loc='best', shadow=True)
plt.show()

请尽快使用Pro版接口:https://waditu.com/document/2

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐