Python打造五线图谱(乐活五线谱)简单版

附件别乱下载ztfb.csv

这个是代码股票数据 别乱下载,别乱下载,别乱下载,别乱下载,别乱下载,别乱下载,别乱下载,别乱下载,别乱下载,别乱下载,别乱下载,别乱下载,别乱下载,别乱下载

立即下载

效果图:

环境

python 3.7
scipy==1.1.0   
plotly==4.7.1 
cufflinks==0.17.3  

计算代码

import pandas as pd
import talib
import os
import cufflinks as cf
import chart_studio.plotly  as py
import plotly.graph_objs as go
import plotly.offline as pyoff
import plotly.graph_objs as go
import plotly.io as pio
import pandas as pd
import numpy as np
import plotly
plotly.io.orca.config.executable = '/usr/local/bin/orca'
plotly.io.orca.config.save()
%reload_ext autoreload
%autoreload 2   
df = pd.read_csv('ztfb.csv')
# 线性回归
from sklearn import linear_model
import numpy as np
reg = linear_model.LinearRegression()
#对应序号是 range(len(data))
df['itx'] =[i  for i in range(1,len(list(df['last_close']))+1)]
# x , y
reg.fit (df['itx'].values.reshape(-1, 1),df['last_close'])
#斜率为
print(reg.coef_)
#截距为
print(reg.intercept_)
df['coef'] = reg.coef_[0]
df['intercept'] = reg.intercept_
# y = c+x*b = 截距+x*斜率
#趋势线
df['priceTL'] = df['intercept']+df['itx']*df['coef']
#误差
df['y-TL'] = df['close']-df['priceTL']
# 标准差
df['SD'] = df['y-TL'].std()
# 分别计算上下 1个和2个标准差
df['TL-2SD'] = df['priceTL']-2*df['SD']
df['TL-SD'] = df['priceTL']-df['SD']
df['TL+2SD'] = df['priceTL']+2*df['SD']
df['TL+SD'] = df['priceTL']+df['SD']

绘图

import plotly.graph_objects as go

fig = go.Figure()
for i in ['last_close','priceTL','TL-2SD', 'TL-SD',
       'TL+SD', 'TL+2SD']:
    fig.add_trace(
    go.Scatter(
        x=df['time_key'],
        y=df[i],
        name=i
    ))

fig.update_layout(
   xaxis_title="x Axis Title",
    yaxis_title="y Axis Title",
    font=dict(
        family="Courier New, monospace",
        size=18,
        color="#7f7f7f"
    ),
    title={'text': "Plot Title",'xanchor': 'center','y':0.995,
        'x':0.5,
        'yanchor': 'top'},
)

fig.show()

 

 

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读