提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
魔法命令
提示:以下是本篇文章正文内容,下面案例可供参考
一、魔法是什么?
画图更专业、股票走势等
二、使用步骤
1.引入库
代码如下(示例):
代码运行需安装tushar pro 并获取TOKEN码, 这里获取token码
# coding=utf-8
import math
import tushare as ts
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import talib
import pandas as pd
from datetime import datetime, date
import seaborn as sns
sns.set(style="darkgrid", palette="muted", color_codes=True)
from scipy import stats,integrate
%matplotlib inline
sns.set(color_codes=True)
matplotlib.rcParams['axes.unicode_minus']=False
plt.rcParams['font.sans-serif']=['SimHei']
2.读入数据
ts.set_token('这里输入token码')
pro = ts.pro_api()
#读取数据
star='2010Q1'
end='2020Q1'
start_cpi='200601'
end_cpi='202003'
start_time='20060301'
end_time="20200531"
dcc= pro.cn_cpi(start_m=start_cpi, end_m=end_cpi)#,fields='nt_yoy')
df1 = pro.cn_gdp(start_q=star, end_q=end)
dsc1 = pro.index_daily(ts_code='000300.SH', start_date=start_time, end_date=end_time,fields='ts_code,trade_date,close')
dsp = pro.index_daily(ts_code='NHCI.NH', start_date=start_time, end_date=end_time,fields='ts_code,trade_date,close')
dsb = pro.index_daily(ts_code='000012.SH', start_date=start_time, end_date=end_time,fields='ts_code,trade_date,close')
定义一个处理函数,随意
def mon_fun(dsb):
dsb.index = pd.to_datetime(dsb.trade_date,format="%Y-%m")#转换成datetime类型的
dsbb=dsb.resample("M").mean()
return dsbb
dsbb=mon_fun(dsb)
dscc1=mon_fun(dsc1)
dspp=mon_fun(dsp)
dsbb.rename(columns={"close":"close_b"},inplace=True)
dscc1.rename(columns={"close":"close_c"},inplace=True)
dspp.rename(columns={'close':'close_p'},inplace=True)
dcc.index=pd.to_datetime(dsbb.index,format="%Y-%m")
画双轴图
################数据x坐标轴需要两个相同的共享的X轴###################
fig = plt.figure(figsize=(10,6))
ax1 = fig.add_subplot(111)
ax1.plot(dsbb.close_b,)
ax1.set_ylabel('上债综合指数闭盘价',fontdict={'weight': 'normal', 'size': 15})
ax1.set_title("上债综合指数与CPI同比变化对比图",fontdict={'weight': 'normal', 'size': 15})
ax2 = ax1.twinx() # this is the important function
ax2.plot( dcc.nt_yoy , 'r')
ax2.set_ylabel('CPI全国同比',fontdict={'weight': 'normal', 'size': 15})
ax2.set_xlabel('Same')
#参数rotation设置坐标旋转角度,参数fontdict设置字体和字体大小
#ax1.set_xticklabels(rotation=90,fontdict={'weight': 'normal', 'size': 15})
plt.show()
结果
1.python量化——alpha股票-指数期货对冲策略
2.多因子选股策略
3.海龟交易策略
4.移动平均策略——单/双均线策略
5.改进的美林时钟(介绍)
6.改进的美林时钟策略(一)
7.改进的美林时钟策略(二)
8.改进的美林时钟策略(三)
这里获取token码