# -*- coding: utf-8 -*-
"""
Created on Thu May 25 08:55:12 2017
@author: yunjinqi
E-mail:yunjinqi@qq.com
Differentiate yourself in the world from anyone else.
"""
import pandas as pd
import numpy as np
import datetime
import time
import random
import matplotlib.pyplot as plt
import seaborn as sns
#获取数据
df=pd.read_csv('C:/Users/HXWD/Desktop/rb000.csv',encoding='gbk')
df.head()
df.columns=['date','open','high','low','close','volume','amt']
df.head()
value=[]
for i in range(5,6):
for j in range(20,21):
df['ma5']=df['close'].rolling(i).mean()
df['ma20']=df['close'].rolling(j).mean()
df.ix[df['ma5']>df['ma20'],'cross']=1
df.ix[df['ma5']<=df['ma20'],'cross']=-1
#df[['close','ma5','ma20']][-200:].plot()
df['ret']=(df['close']-df['close'].shift(1))/df['close'].shift(1)
df['profit']=df['ret']*df['cross']
#df['profit'].plot()
target=df['profit'].sum()
s=[i,j,target]
ts=time.strftime('%Y-%m-%d %X', time.localtime() )
value.append(s)
print('当前时间:{}短期参数:{},长期参数:{}优化完毕,净利润{}'.format(ts,i,j,s))
#基于蒙特卡洛模拟评估双均线的预测能力
data=df[['ret','cross']].fillna(0)
target_value=[]
for i in range(5000):
li=list(data['ret'])
random.shuffle(li)
profit=(li*data['cross']).sum()
print(profit)
target_value.append(profit)
target_value
plt.hist(target_value)
jianyan_profit=(data['ret']*data['cross']).sum()
jianyan_profit
#基于随机的收益与策略的收益差距非常大,我们有理由相信,策略是具有预测能力的。
#量化投资交流群:226224941,欢迎大家加入。