python回归模型_python量化就交易模型:线性回归的趋势跟踪系统

本篇通过各个收盘的波峰波谷做简单线性回归拟合出类似的趋势线,然后结合股价以及预测值来跟踪趋势。思路:上升趋势线用波谷拟合,用波峰的值和拟合预测值做比较;下降趋势线用波峰拟合,用波谷的值和拟合预测值做比较。 注:这种拟合的方式也许信号发出的不

本篇通过各个收盘的波峰波谷做简单线性回归拟合出类似的趋势线,然后结合股价以及预测值来跟踪趋势。思路:上升趋势线用波谷拟合,用波峰的值和拟合预测值做比较;下降趋势线用波峰拟合,用波谷的值和拟合预测值做比较。

注:这种拟合的方式也许信号发出的不一定准确,后续可以加上拟合度置信区间来提高信号的准确性。

收益与风险

一星期

44_160908120656_1.png

一个月

44_160908120725_1.png

六个月

44_160908120745_1.png

一年

44_160908120914_1.png

全部

44_160908120937_1.png

风险

源码:

from datetime import datetime, timedelta

import numpy as np

import pandas as pd

from sklearn import datasets, linear_model

# 定义一个全局变量, 保存要操作的证券

security = ‘510180.XSHG’

# 初始化此策略

# 设置我们要操作的股票池, 这里我们只操作一支股票

set_universe([security])

arraySplitNum = 5

def initialize(context):

g.fixedStartDate = datetime(2000,1,1)

g.peakPrice = pd.DataFrame(columns=[‘xDays’, ‘yPrice’, ‘tDate’])

g.throughPrice = pd.DataFrame(columns=[‘xDays’, ‘yPrice’, ‘tDate’])

g.priceBuy = 0.0

# 每个单位时间(如果按天回测,则每天调用一次,如果按分钟,则每分钟调用一次)调用一次

def handle_data(context, data):

hData = history(3, ‘1d’, ‘close’, [security])

hData.columns = [‘close’]

pLen = len(g.peakPrice)

tLen = len(g.throughPrice)

# 计算相对时间变量

xDays = (context.current_dt - g.fixedStartDate).days - 1

if hData.iloc[-2].close >= hData.iloc[-3].close \

and hData.iloc[-1].close <= hData.iloc[-2].close:

# 这是波峰

g.peakPrice.loc[pLen] = [xDays, hData.iloc[-2].close, context.current_dt + timedelta(days=-1)]

elif hData.iloc[-2].close <= hData.iloc[-3].close \

and hData.iloc[-1].close >= hData.iloc[-2].close:

# 这是波谷

g.throughPrice.loc[tLen] = [xDays, hData.iloc[-2].close, context.current_dt + timedelta(days=-1)]

pricePredictP = data[security].pre_close

pricePredictT = data[security].pre_close

lastPePrice = pricePredictP

lastThPrice = pricePredictT

pLen = len(g.peakPrice)

tLen = len(g.throughPrice)

if pLen > 1 :

# Create linear regression object

regrP = linear_model.LinearRegression()

# Train the model using the training sets

regrP.fit(np.reshape(g.peakPrice.xDays.values, pLen).reshape(pLen,1), g.peakPrice.yPrice.values)

pricePredictP = regrP.predict(xDays)

lastPePrice = g.peakPrice.ix[pLen - 1].yPrice

if tLen > 1 :

# Create linear regression object

regrT = linear_model.LinearRegression()

# Train the model using the training sets

regrT.fit(np.reshape(g.throughPrice.xDays.values, tLen).reshape(tLen,1), g.throughPrice.yPrice.values)

pricePredictT = regrT.predict(xDays)

lastThPrice = g.throughPrice.ix[tLen - 1].yPrice

bsFlag = ‘’

lastClosePrice = data[security].pre_close

if lastClosePrice > pricePredictP or lastThPrice > pricePredictP:

bsFlag = ‘+’

elif lastClosePrice < pricePredictT or lastPePrice < pricePredictT:

bsFlag = ‘-’

# 取得当前价格

current_price = data[security].open

# 取得当前的现金

cash = context.portfolio.cash

if bsFlag == ‘+’ and g.priceBuy == 0 :

# 全仓买入

# 计算可以买多少只股票

number_of_shares = int(cash/current_price)

# 购买量大于0时,下单

if number_of_shares > 0:

# 买入股票

order(security, +number_of_shares)

g.priceBuy = current_price

# 记录这次买入

log.info(str(context.current_dt) + " Buying %s" % (security))

#g.peakPrice = pd.DataFrame(columns=[‘xDays’, ‘yPrice’, ‘tDate’])

elif bsFlag == ‘-’ and g.priceBuy > 0 :

# 清仓卖出

# 卖出所有股票,使这只股票的最终持有量为0

order_target(security, 0)

g.priceBuy = 0.0

g.throughPrice = pd.DataFrame(columns=[‘xDays’, ‘yPrice’, ‘tDate’])

# 记录这次卖出

log.info(str(context.current_dt) + " Selling %s" % (security))

#record(closeP=data[security].close)

record(peakP=pricePredictP)

record(throughP=pricePredictT)

#record(lastThP=lastThPrice)

python量化交易模型

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值