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

效果图:

Python打造五线图谱(乐活五线谱)简单版_第1张图片

环境

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()

 

 

你可能感兴趣的:(数据分析)