聚宽源码29

原文策略源码如下:
#基于LSTM模型预测股票价格走势

导入聚宽函数库

import jqdata

初始化函数,设定要操作的股票、基准等等

def initialize(context):
# 通过模型预测2017-05-13 到 2018-03-24的收盘价格
context.labels = [3484.4731,3419.8232,3405.0974,3417.4797,3433.4858,3396.5896,3391.5007,3423.003,3421.6697,3465.112,3471.467,3465.7864,3487.3418,3491.7178,3546.959,3436.56,3514.53,3529.1199,3557.9495,3601.758,3541.025,3491.9634,3536.0886,3536.858,3547.974,3528.3457,3542.721,3578.8118,3650.272,3683.9583,3663.6384,3694.049,3699.703,3662.5566,3607.9272,3622.7446,3611.0764,3639.1382,3616.0864,3664.207,3621.2336,3639.712,3707.037,3601.1592,3597.3801,3699.4297,3723.4106,3726.5364,3743.6357,3741.419,3706.606,3683.4072,3748.0212,3733.877,3769.4783,3748.6301,3744.3965,3713.9695,3733.689,3742.2969,3683.7659,3691.0076,3593.5571,3709.6367,3742.3257,3708.5461,3717.597,3745.0974,3761.5957,3754.7583,3776.6123,3761.9675,3735.6716,3799.6272,3764.0454,3800.448,3830.6057,3815.3823,3863.0005,3908.6187,3810.4922,3817.296,3846.0645,3825.2102,3788.061,3874.5283,3854.4138,3820.1802,3850.1833,3858.4153,3857.99,3847.9673,3850.671,3825.7795,3836.8047,3834.4863,3835.9675,3838.4946,3866.0347,3879.392,3895.466,3906.8342,3914.512,3939.9197,3906.9272,3936.1904,3934.7993,3902.4858,3918.806,3951.7837,3966.8457,4007.3047,4015.1685,4024.8362,4025.7075,4017.6519,4003.1348,3986.678,4004.0417,4041.1533,4079.7434,4088.711,4160.403,4160.6655,4145.7754,4104.3496,4112.224,4167.823,4125.745,4262.363,4285.2695,4177.9688,4153.7603,4104.226,4033.8774,4081.047,4040.2146,4026.65,4024.3823,4071.1711,4007.6072,3994.592,4024.8647,4094.205,4038.3335,4040.5596,4036.214,4008.3818,3997.3276,4016.4563,4036.231,4076.2664,4043.742,4079.1006,4068.2737,4041.2944,4060.8088,4032.3176,4079.996,4142.461,4081.8547,4164.163,4211.9136,4179.7275,4208.5596,4199.5635,4216.003,4306.757,4294.753,4307.5894,4276.661,4303.402,4321.5454,4375.543,4401.335,4366.192,4352.632,4368.7324,4271.167,4283.705,4230.268,4257.1523,4299.1123,4228.6143,4157.212,4082.6318,3922.8433,3931.488,3972.2476,3888.1587,4036.5857,4044.2336,4151.31,4116.5684,4052.486,4041.2666,4024.7532,4014.3164,4079.2344,4044.7148,4038.3608,4129.4355,4178.1733,4108.99,4090.9297,4088.3188,4071.6282,4079.1306,4063.0908,4103.768,4068.3186,3979.124,3865.6921,3936.0256,3869.5344,3906.2402,3898.3296,3917.3855,3856.9749,3877.4792,3846.8347,3943.2932,3960.0173,3897.677,3877.7727,3852.6196,3791.1196,3766.4897,3795.0635,3783.1785,3768.4807,3812.6948,3816.6575,3771.9333,3743.5732,3753.6853,3776.6038,3763.3572,3820.007,3878.4482,3826.642,3859.9102,3865.516,3908.1729,3892.5042,3886.5645,3840.99,3892.7214,3937.22,3888.6008,3871.1763,3814.2075,3804.7695,3816.1355,3815.977,3740.9553,3784.1594,3760.4597,3787.7817,3813.8044,3811.6921,3827.662,3775.4116,3760.547,3809.9937,3765.7212,3763.5486,3764.579,3660.5212,3564.6982,3605.7383,3592.9866,3589.3928,3494.2688,3489.3257,3437.5164,3493.3125,3437.318,3355.9753,3408.8022,3466.5505,3404.5852,3445.302,3472.906,3414.7698,3429.539,3479.1172,3484.4563,3474.8198,3472.6768,3462.7024,3408.558,3450.1145,3544.4707,3579.5774,3568.074,3595.3762,3544.1062,3587.7869,3509.163,3462.2117,3410.8735,3377.351,3367.894,3388.9998,3370.033,3433.4487,3389.0122,3431.4001,3385.0483,3356.17,3354.9207,3356.1194,3361.0208,3404.1577,3381.7974,3389.3867,3368.8357,3396.3364,3416.04]
# 定义一个全局变量, 保存要操作的股票
# 沪深300
g.security = ‘399300.XSHE’
# 设定沪深300作为基准
set_benchmark(‘000300.XSHG’)
# 开启动态复权模式(真实价格)
set_option(‘use_real_price’, True)
# 运行函数
run_daily(market_open, time=‘every_bar’)
# context.labels 数据的读取位置记录
context.index =0
# 存储上一周期预测值
context.pr_predict_value = 0

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

def market_open(context):
security = g.security
# 获取股票的收盘价
close_data = attribute_history(security, 5, ‘1d’, [‘close’])
# 取得上一时间点价格
current_price = close_data[‘close’][-1]
# 取得当前的现金
cash = context.portfolio.cash
# 获取当天预测的收盘价
predict_value = context.labels[context.index]
# 获取当天的误差率
error = abs(predict_value-current_price)/current_price
# 上一周期预测值初始值
if context.pr_predict_value == 0:
context.pr_predict_value = current_price
# 当前预测的值 大于 上一周期预测的收盘价 则买入
if predict_value>context.pr_predict_value:
order_value(security, cash)
log.info(“Buying %s” % (security))
else:
#负责进行卖出
order_target(security, 0)
log.info(“Selling %s” % (security))
# 对预测的收盘价进行缓存
context.pr_predict_value = predict_value
# 指向下一个预测收盘价的数据位置
context.index = context.index+1

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值