掘金量化的一个代码,对本人写策略避免入坑有重要意义

  1. # coding=utf-8
  2. from __future__ import print_function, absolute_import, unicode_literals
  3. from gm.api import *
  4. import numpy as np
  5. def init(context):
  6. # 选择的两个合约
  7. context.symbol = ['DCE.j1901', 'DCE.jm1901']
  8. # 订阅历史数据
  9. subscribe(symbols=context.symbol,frequency='1d',count=11,wait_group=True)
  10. def on_bar(context, bars):
  11. # 数据提取
  12. j_close = context.data(symbol=context.symbol[0],frequency='1d',fields='close',count=31).values
  13. jm_close = context.data(symbol=context.symbol[1],frequency='1d',fields='close',count=31).values
  14. # 提取最新价差
  15. new_price = j_close[-1] - jm_close[-1]
  16. # 计算历史价差,上下限,止损点
  17. spread_history = j_close[:-2] - jm_close[:-2]
  18. context.spread_history_mean = np.mean(spread_history)
  19. context.spread_history_std = np.std(spread_history)
  20. context.up = context.spread_history_mean + 0.75 * context.spread_history_std
  21. context.down = context.spread_history_mean - 0.75 * context.spread_history_std
  22. context.up_stoppoint = context.spread_history_mean + 2 * context.spread_history_std
  23. context.down_stoppoint = context.spread_history_mean - 2 * context.spread_history_std
  24. # 查持仓
  25. position_jm_long = context.account().position(symbol=context.symbol[0],side=1)
  26. position_jm_short = context.account().position(symbol=context.symbol[0],side=2)
  27. # 设计买卖信号
  28. # 设计开仓信号
  29. if not position_jm_short and not position_jm_long:
  30. if new_price > context.up:
  31. print('做空价差组合')
  32. order_volume(symbol=context.symbol[0],side=OrderSide_Sell,volume=1,order_type=OrderType_Market,position_effect=1)
  33. order_volume(symbol=context.symbol[1], side=OrderSide_Buy, volume=1, order_type=OrderType_Market, position_effect=PositionEffect_Open)
  34. if new_price < context.down:
  35. print('做多价差组合')
  36. order_volume(symbol=context.symbol[0], side=OrderSide_Buy, volume=1, order_type=OrderType_Market, position_effect=PositionEffect_Open)
  37. order_volume(symbol=context.symbol[1], side=OrderSide_Sell, volume=1, order_type=OrderType_Market, position_effect=PositionEffect_Open)
  38. # 设计平仓信号
  39. # 持jm多仓时
  40. if position_jm_long:
  41. if new_price >= context.spread_history_mean:
  42. # 价差回归到均值水平时,平仓
  43. print('价差回归到均衡水平,平仓')
  44. order_volume(symbol=context.symbol[0], side=OrderSide_Sell, volume=1, order_type=OrderType_Market, position_effect=PositionEffect_Close)
  45. order_volume(symbol=context.symbol[1], side=OrderSide_Buy, volume=1, order_type=OrderType_Market, position_effect=PositionEffect_Close)
  46. if new_price < context.down_stoppoint:
  47. # 价差达到止损位,平仓止损
  48. print('价差超过止损点,平仓止损')
  49. order_volume(symbol=context.symbol[0], side=OrderSide_Sell, volume=1, order_type=OrderType_Market, position_effect=PositionEffect_Close)
  50. order_volume(symbol=context.symbol[1], side=OrderSide_Buy, volume=1, order_type=OrderType_Market, position_effect=PositionEffect_Close)
  51. # 持jm空仓时
  52. if position_jm_short:
  53. if new_price <= context.spread_history_mean:
  54. # 价差回归到均值水平时,平仓
  55. print('价差回归到均衡水平,平仓')
  56. order_volume(symbol=context.symbol[0], side=OrderSide_Buy, volume=1, order_type=OrderType_Market, position_effect=PositionEffect_Close)
  57. order_volume(symbol=context.symbol[1], side=OrderSide_Sell, volume=1, order_type=OrderType_Market, position_effect=PositionEffect_Close)
  58. if new_price > context.up_stoppoint:
  59. # 价差达到止损位,平仓止损
  60. print('价差超过止损点,平仓止损')
  61. order_volume(symbol=context.symbol[0], side=OrderSide_Buy, volume=1, order_type=OrderType_Market, position_effect=PositionEffect_Close)
  62. order_volume(symbol=context.symbol[1], side=OrderSide_Sell, volume=1, order_type=OrderType_Market, position_effect=PositionEffect_Close)
  63. if __name__ == '__main__':
  64. '''
  65. strategy_id策略ID,由系统生成
  66. filename文件名,请与本文件名保持一致
  67. mode实时模式:MODE_LIVE回测模式:MODE_BACKTEST
  68. token绑定计算机的ID,可在系统设置-密钥管理中生成
  69. backtest_start_time回测开始时间
  70. backtest_end_time回测结束时间
  71. backtest_adjust股票复权方式不复权:ADJUST_NONE前复权:ADJUST_PREV后复权:ADJUST_POST
  72. backtest_initial_cash回测初始资金
  73. backtest_commission_ratio回测佣金比例
  74. backtest_slippage_ratio回测滑点比例
  75. '''
  76. run(strategy_id='strategy_id',
  77. filename='main.py',
  78. mode=MODE_BACKTEST,
  79. token='token',
  80. backtest_start_time='2018-02-01 08:00:00',
  81. backtest_end_time='2018-12-31 16:00:00',
  82. backtest_adjust=ADJUST_PREV,
  83. backtest_initial_cash=2000000,
  84. backtest_commission_ratio=0.0001,
  85. backtest_slippage_ratio=0.0001)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/449666.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C++ STL学习笔记

C STL学习笔记一 为何要学习STL&#xff1a; 数据结构与算法是编程的核心&#xff0c;STL中包含各种数据结构和优秀的算法&#xff0c;确实值得深入学习&#xff0c;本文中虽然着重使用&#xff0c;但希望有心的朋友能多看看相关数据结构的实现&#xff0c;对于C语言确实会有较…

ItelliJ IDEA开发工具使用—创建一个web项目

转自&#xff1a;https://blog.csdn.net/wangyang1354/article/details/50452806概念需要明确一下IDEA中的项目&#xff08;project&#xff09;与eclipse中的项目&#xff08;project&#xff09;是不同的概念&#xff0c;IDEA的project 相当于之前eclipse的workspace,IDEA的M…

AKOJ-2037-出行方案

链接&#xff1a;https://oj.ahstu.cc/JudgeOnline/problem.php?id2037 题意&#xff1a; 安科的夏天真是不一般的热&#xff0c;避免炎热&#xff0c;伍学长因此想为自己规划一个校园出行方案&#xff0c;使得从宿舍出发到校园的各个地方距离花费时间最短。我们已知校园一共有…

akshare 布林通道策略

import datetime import pandas as pd import backtrader as bt import matplotlib.pyplot as plt from datetime import datetime import matplotlib import akshare as ak %matplotlib inline class Boll_strategy(bt.Strategy):#自定义参数&#xff0c;每次买入1800手param…

一些资源网站..

github上各种免费编程书籍~~~ : https://github.com/EbookFoundation/free-programming-books/blob/master/free-programming-books-zh.md正则表达式学习 :https://web.archive.org/web/20161119141236/http://deerchao.net:80/tutorials/regex/regex.htmtorch&#xff1a;http…

极客无极限 一行HTML5代码引发的创意大爆炸

摘要&#xff1a;一行HTML5代码能做什么&#xff1f;国外开发者Jose Jesus Perez Aguinaga写了一行HTML5代码的文本编辑器。这件事在分享到Code Wall、Hacker News之后&#xff0c;引起了众多开发者的注意&#xff0c;纷纷发表了自己的创意。 这是最初的HTML5代码&#xff0c;它…

c# 写文件注意问题及用例展示

以txt写string举例&#xff0c;正确代码如下&#xff1a; private void xie(){FileStream fs new FileStream("1.txt", FileMode.Create);StreamWriter sw new StreamWriter(fs, Encoding.Default);sw.Write("123");sw.Flush();sw.Close();//fs.Flush();…

akshare sma策略

import datetimeimport pandas as pdimport backtrader as bt from datetime import datetime import matplotlib import akshare as ak %matplotlib inlineclass SmaCross(bt.Strategy):# 全局设定交易策略的参数params ((pfast, 5), (pslow, 20),)def __init__(self):sma1 …

DOCKER windows 7 详细安装教程

前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到教程。 DOCKER windows安装 DOCKER windows安装 1.下载程序包2. 设置环境变量3. 启动DOCKERT4. 分析start.sh5. 利用SSH工具管理6. 下载镜像 6.1…

c#UDP协议

UDP协议是不可靠的协议&#xff0c;传输速率快 服务器端&#xff1a; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;using System.Net.Sockets; using System.Net; using System.Threading;namespace…

芝麻信用免押金成趋势 报告称租赁经济有望突破10万亿元

中新网1月16日电 “很多物品都是租来的&#xff0c;但生活不是。”如今&#xff0c;越来越多的年轻人选择了“租”生活&#xff0c;从房子到车子&#xff0c;从服饰到电脑&#xff0c;甚至玩具、婴儿车&#xff0c;全都可以租用&#xff0c;租赁已成为当下年轻人追求品质生活的…

开发者成功学:扔掉你那些很sexy的想法

摘要&#xff1a;在开发者的世界里&#xff0c;开发iPhone应用并不像表面那么光鲜&#xff0c;收支不成正比是常有之事&#xff0c;劳心劳力开发的应用无人问津更是屡见不鲜。走出了开发的一小步却难以迈出销售推广上的一大步&#xff0c;究竟如何才能将应用卖出去并获取利润&a…

html-body相关标签

一 字体标签 字体标签包含&#xff1a;h1~h6、<font>、<u>、<b>、<strong><em>、<sup>、<sub> 标题 标题使用<h1>至<h6>标签进行定义。<h1>定义最大的标题&#xff0c;<h6>定义最小的标题。具有align属性&a…

rz、sz 命令 安装(Xshell 安装)

在linux下使用rz,就可以从本机上传到Linux服务器 在linux中rz 和 sz 命令允许开发者与主机通过串口进行传递文件了&#xff0c;下面我们就来简单的介绍一下rz 和 sz 命令的例子。 sz&#xff1a;将选定的文件发送&#xff08;send&#xff09;到本地机器 rz&#xff1a;运行该命…

Kotlin 学习笔记08

Lambda作为形参和返回值 声明高阶函数 任何以lambda或者函数引用作为参数的函数&#xff0c;或者返回值&#xff0c;或者两者都有&#xff0c;就是高阶函数。比如list.filter(4,"abc")-> {} 如下&#xff1a; { x, y -> x y} 这里省略了参数x&#xff0c;y类型…

一个开源工作者对开源与赚钱的一些想法

摘要&#xff1a;本文作者长期以来一直定期为开源世界贡献代码&#xff0c;最近重新思索了一下开源软件的意义&#xff0c;在开发者中引起了强烈共鸣。 15年来&#xff0c;我一直定期地贡献开源代码&#xff0c;但是现在我停下来思考这对我自己究竟意味着什么&#xff0c;也许仅…

Chapter 5 Blood Type——33

We were near the parking lot now. 我们现在离停车场不远。 I veered left, toward my truck. Something caught my jacket, yanking me back. 我转向左边&#xff0c;面对我的车。有人抓住了我的夹克让我回过神来。 "Where do you think youre going?" he asked,…

CentOS上安装Docker (图解)

更简单的办法&#xff1a;三分钟装好 Docker ( 图解&#xff09; 前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到教程。 // 用上面那个办法吧&#xff0c;简单多了&#xff0c;下面这个方法看看…

Uber提出有创造力的POET:自行开发更困难环境和解决方案

近日&#xff0c;Uber 发文介绍了一种开放式方法 POET&#xff08;Paired Open-Ended Trailblazer&#xff09;&#xff0c;可自行开发难度递增的环境及其解决方案&#xff0c;还可以实现不同环境中的智能体迁移&#xff0c;促进进化。Uber AI 实验室注重开放性&#xff08;ope…

spring boot 报错:Your ApplicationContext is unlikely to start due to a @ComponentScan of the default p

前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到教程。 ** WARNING ** : Your ApplicationContext is unlikely to start due to a ComponentScan of the default package. Your ApplicationCo…