安装 Ta-lib:
pip install Ta-lib
安装pyecharts:
pip install pyecharts
npm install -g phantomjs-prebuilt
安装图片保存插件:
pip install pyecharts-snapshot
import pandas as pd
import numpy as np
import talib as ta
from decimal import Decimal
from pyecharts.charts import Kline, Line, Bar, Grid
from pyecharts.commons.utils import JsCode
from pyecharts import options as opts
from pyecharts.globals import CurrentConfig, NotebookType
CurrentConfig.NOTEBOOK_TYPE = NotebookType.JUPYTER_NOTEBOOK
# 精度计算
def digital_utils(temps):
temps = str(temps)
if temps.find('E'):
temps = '{:.8f}'.format(Decimal(temps))
nums = temps.split('.')
if int(nums[1]) == 0:
return nums[0]
else:
num = str(int(nums[1][::-1]))
result = '{}.{}'.format(nums[0], num[::-1])
return result
def sum_resampler(df):
if df.shape[0] < 1:
return float("nan")
return np.sum(df)
def low_resampler(df):
return np.min(df)
def high_resampler(df):
return np.max(df)
def avg_resampler(df):
return digital_utils(np.average(df))
def open_resampler(df):
if df.shape[0] < 1:
return float("nan")
return np.asarray(df)[0]
def close_resampler(df):
if df.shape[0] < 1:
return float("nan")
return np.asarray(df)[-1]
def volume_resampler(df):
if df.shape[0] < 1:
return float("nan")
volume = np.asarray(df)[-1] - np.asarray(df)[0]
if volume < 1:
return float("nan")
return volume
# 根据tikt数据合成K线数据
def getKline(price, volume):
data_close = price.resample('T', label='right').apply(close_resampler) # 1分钟聚合,使用最右边的index作为新的index
data_open = price.resample('T', label='right').apply(open_resampler) # 1分钟聚合,使用最右边的index作为新的index
data_high = price.resample('T', label='right').apply(high_resampler) # 1分钟聚合,使用最右边的index作为新的index
data_low = price.resample('T', label='right').apply(low_resampler) # 1分钟聚合,使用最右边的index作为新的index
data_volume = volume.resample('T', label='right').apply(volume_resampler) # 1分钟聚合,使用最右边的index作为新的index
data = pd.concat([data_open, data_close, data_low, data_high, data_volume],axis=1)
data.columns = ['open', 'close&#