赞
踩
模型使用 10、20 两个周期。
链接:https://pan.baidu.com/s/16LhFbQ2gPqO9O5hMsuVsKQ?pwd=1111
提取码:1111
另外,整个教程的完整版视频教程请阅读前言部分内容说明。
下面是详细的代码及针对代码的每一部分详细介绍。
代码部分:
import pandas as pd import matplotlib.pyplot as plt import numpy as np file_name='data.xls' dataframe = pd.read_excel(file_name, header=0) close_data=dataframe['close'] close_list=list(close_data) #计算双均线 arg1=10 arg2=20 data_list=[] ma1_list=[] ma2_list=[] open_price=None profit_list=[0] #将数据模拟成逐个回放 for close in close_list: data_list.append(close) if len(data_list)>=arg2: #用numpy来计算均值,mean() av1=np.mean(data_list[-arg1:]) av2 = np.mean(data_list[-arg2:]) ma1_list.append(av1) ma2_list.append(av2) #计算收益 if open_price==None: profit_list.append(profit_list[-1]) else: profit=profit_list[-1]+close-data_list[-2] profit_list.append(profit) if len(ma1_list) > 1 and len(ma2_list) > 1: #当金叉,开仓 if ma1_list[-1]>ma2_list[-1] and ma1_list[-2]<ma2_list[-2]: open_price=close #当死叉,平仓 if ma1_list[-1] < ma2_list[-1] and ma1_list[-2] > ma2_list[-2]: open_price=None fig, ax1 = plt.subplots() ax2 = ax1.twinx() ax1.plot(close_list) ax1.plot(ma1_list) ax1.plot(ma2_list) ax2.plot(profit_list,color='red') plt.show()
代码解析:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。