掘金量化的一個代碼,對本人寫策略避免入坑有重要意義

  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.pswp.cn/news/449666.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/449666.shtml
英文地址,請注明出處:http://en.pswp.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…