决策树模型(固定模型)
最后更新于:2022-04-01 21:54:49
# 决策树模型(固定模型)
> 来源:https://uqer.io/community/share/568dce2d228e5b18e2ba296e
楼主上学时学的是机器学习,现在在BAT做数据挖掘,一直对将机器学习的知识应用到金融领域比较感兴趣。
最近发现了优矿这个平台之后,有点着迷了,通过看大家的策略,也学到些知识。
因为楼主对金融投资认识不多,所以写的策略比较简单粗暴,希望向大家多多学习~
策略: 1、不预测具体股价,只预测次日收盘价相比今日是涨是跌; 2、如果预测为涨,则全部买入或持有;如果预测为跌,则全部卖出。
方法: 基于某只股票的历史数据,采用机器学习的方法,挖掘其中规律,预测该只股票次日收盘价是涨还是跌
```py
import numpy as np
from CAL.PyCAL import *
from sklearn.cross_validation import train_test_split
from sklearn.externals import joblib
import pandas as pd
cal = Calendar('China.SSE')
# 第一步:设置基本参数
start = '2015-01-01'
end = '2015-11-01'
capital_base = 1000000
refresh_rate = 1
benchmark = 'HS300'
##HS300
freq = 'd'
#601872.XSHG HS300
# 第二步:选择主题,设置股票池
universe = ['601872.XSHG', ]
##训练模型
def model_train(begin_date,end_date):
data1=DataAPI.MktEqudGet(secID=u"601872.XSHG",beginDate=begin_date,endDate=end_date,field=['tradeDate','highestPrice','lowestPrice','openPrice','closePrice','turnoverVol','turnoverRate'],pandas="1")
data2=DataAPI.MktStockFactorsDateRangeGet(secID=u"601872.XSHG",beginDate=begin_date,endDate=end_date,field=['tradeDate','DAVOL5','EMA5','EMA10','MA5','MA20','RSI','VOL5','VOL10','MACD'],pandas="1")
df_data=pd.merge(data1,data2,on='tradeDate')
tmp=[]
for i in range(len(df_data.values)):
mark_1=0
for j in range(len(df_data.values[i])):
if str(df_data.values[i][j])=='nan':
mark_1=1
if mark_1==0:
a=list(df_data.values[i])
a.append(df_data.values[i][4]-df_data.values[i][10])
a.append(df_data.values[i][4]-df_data.values[i][11])
tmp.append(a)
data=tmp
print len(data)
x=[]
y=[]
for i in range(len(data)-1):
if data[i][4]0 and stock not in account.valid_secpos:
p = account.referencePrice[stock]
order(stock,int(c / p))
if y_predict==0 and stock in account.valid_secpos:
order_to(stock,0)
#print today,x_predict[3],y_predict
```
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-07-30_579cbdac2fb5a.jpg)
```
713
0.580056179775 0.334384858044 0.445224719101
```
This is an empty markdown cell
';