{"id":19966673,"url":"https://github.com/truthhun/turtle-trading","last_synced_at":"2025-07-21T08:33:58.301Z","repository":{"id":88501867,"uuid":"119640992","full_name":"TruthHun/Turtle-Trading","owner":"TruthHun","description":"海龟交易法(期货)，基于海龟交易法则的交易策略","archived":false,"fork":false,"pushed_at":"2018-01-31T05:50:54.000Z","size":55,"stargazers_count":27,"open_issues_count":0,"forks_count":8,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-04T00:32:08.317Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://www.myquant.cn/docs/python_strategyies/110","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TruthHun.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2018-01-31T05:47:47.000Z","updated_at":"2025-03-19T21:07:24.000Z","dependencies_parsed_at":null,"dependency_job_id":"6f6bd935-18e6-4548-aa01-2d04897ea56f","html_url":"https://github.com/TruthHun/Turtle-Trading","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/TruthHun/Turtle-Trading","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TruthHun%2FTurtle-Trading","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TruthHun%2FTurtle-Trading/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TruthHun%2FTurtle-Trading/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TruthHun%2FTurtle-Trading/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TruthHun","download_url":"https://codeload.github.com/TruthHun/Turtle-Trading/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TruthHun%2FTurtle-Trading/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266267315,"owners_count":23902341,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-13T02:37:32.689Z","updated_at":"2025-07-21T08:33:58.295Z","avatar_url":"https://github.com/TruthHun.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 海龟交易法(期货)\n基于海龟交易法则的交易策略\n\n## 源码\n```python\n# coding=utf-8\nfrom __future__ import print_function, absolute_import, unicode_literals\n\nimport sys\n\nimport numpy as np\nimport pandas as pd\n\ntry:\n    import talib\nexcept:\n    print('请安装TA-Lib库')\n    sys.exit(-1)\nfrom gm.api import *\n\n'''\n本策略通过计算CZCE.FG801和SHFE.rb1801的ATR.唐奇安通道和MA线,\n当价格上穿唐奇安通道且短MA在长MA上方时开多仓;当价格下穿唐奇安通道且短MA在长MA下方时开空仓(8手)\n若有多仓则在价格跌破唐奇安平仓通道下轨的时候全平仓位,否则根据跌破\n持仓均价 - x(x=0.5,1,1.5,2)倍ATR把仓位平至6/4/2/0手\n若有空仓则在价格涨破唐奇安平仓通道上轨的时候全平仓位,否则根据涨破\n持仓均价 + x(x=0.5,1,1.5,2)倍ATR把仓位平至6/4/2/0手\n回测数据为:CZCE.FG801和SHFE.rb1801的1min数据\n回测时间为:2017-09-15 09:15:00到2017-10-01 15:00:00\n'''\n\n\ndef init(context):\n    # context.parameter分别为唐奇安开仓通道.唐奇安平仓通道.短ma.长ma.ATR的参数\n    context.parameter = [55, 20, 10, 60, 20]\n    context.tar = context.parameter[4]\n    # context.goods交易的品种\n    context.goods = ['CZCE.FG801', 'SHFE.rb1801']\n    # 订阅context.goods里面的品种, bar频率为1min\n    subscribe(symbols=context.goods, frequency='60s', count=101)\n    # 止损的比例区间\n\n\ndef on_bar(context, bars):\n    bar = bars[0]\n    symbol = bar['symbol']\n    recent_data = context.data(symbol=symbol, frequency='60s', count=101, fields='close,high,low')\n    close = recent_data['close'].values[-1]\n    # 计算ATR\n    atr = talib.ATR(recent_data['high'].values, recent_data['low'].values, recent_data['close'].values,\n                    timeperiod=context.tar)[-1]\n    # 计算唐奇安开仓和平仓通道\n    context.don_open = context.parameter[0] + 1\n    upper_band = talib.MAX(recent_data['close'].values[:-1], timeperiod=context.don_open)[-1]\n    context.don_close = context.parameter[1] + 1\n    lower_band = talib.MIN(recent_data['close'].values[:-1], timeperiod=context.don_close)[-1]\n    # 若没有仓位则开仓\n    position_long = context.account().position(symbol=symbol, side=PositionSide_Long)\n\n    position_short = context.account().position(symbol=symbol, side=PositionSide_Short)\n    if not position_long and not position_short:\n        # 计算长短ma线.DIF\n        ma_short = talib.MA(recent_data['close'].values, timeperiod=(context.parameter[2] + 1))[-1]\n        ma_long = talib.MA(recent_data['close'].values, timeperiod=(context.parameter[3] + 1))[-1]\n        dif = ma_short - ma_long\n        # 获取当前价格\n        # 上穿唐奇安通道且短ma在长ma上方则开多仓\n        if close \u003e upper_band and (dif \u003e 0):\n            order_target_volume(symbol=symbol, volume=8, position_side=PositionSide_Long, order_type=OrderType_Market)\n            print(symbol, '市价单开多仓8手')\n        # 下穿唐奇安通道且短ma在长ma下方则开空仓\n        if close \u003c lower_band and (dif \u003c 0):\n            order_target_volume(symbol=symbol, volume=8, position_side=PositionSide_Short, order_type=OrderType_Market)\n            print(symbol, '市价单开空仓8手')\n    elif position_long:\n        # 价格跌破唐奇安平仓通道全平仓位止损\n        if close \u003c lower_band:\n            order_close_all()\n            print(symbol, '市价单全平仓位')\n        else:\n            # 获取持仓均价\n            vwap = position_long['vwap']\n            # 获取持仓的资金\n            band = vwap - np.array([200, 2, 1.5, 1, 0.5, -100]) * atr\n            # 计算最新应持仓位\n            grid_volume = int(pd.cut([close], band, labels=[0, 1, 2, 3, 4])[0]) * 2\n            order_target_volume(symbol=symbol, volume=grid_volume, position_side=PositionSide_Long,\n                                order_type=OrderType_Market)\n            print(symbol, '市价单平多仓到', grid_volume, '手')\n    elif position_short:\n        # 价格涨破唐奇安平仓通道或价格涨破持仓均价加两倍ATR平空仓\n        if close \u003e upper_band:\n            order_close_all()\n            print(symbol, '市价单全平仓位')\n        else:\n            # 获取持仓均价\n            vwap = position_short['vwap']\n            # 获取平仓的区间\n            band = vwap + np.array([-100, 0.5, 1, 1.5, 2, 200]) * atr\n            # 计算最新应持仓位\n            grid_volume = int(pd.cut([close], band, labels=[0, 1, 2, 3, 4])[0]) * 2\n            order_target_volume(symbol=symbol, volume=grid_volume, position_side=PositionSide_Short,\n                                order_type=OrderType_Market)\n            print(symbol, '市价单平空仓到', grid_volume, '手')\n\n\nif __name__ == '__main__':\n    '''\n    strategy_id策略ID,由系统生成\n    filename文件名,请与本文件名保持一致\n    mode实时模式:MODE_LIVE回测模式:MODE_BACKTEST\n    token绑定计算机的ID,可在系统设置-密钥管理中生成\n    backtest_start_time回测开始时间\n    backtest_end_time回测结束时间\n    backtest_adjust股票复权方式不复权:ADJUST_NONE前复权:ADJUST_PREV后复权:ADJUST_POST\n    backtest_initial_cash回测初始资金\n    backtest_commission_ratio回测佣金比例\n    backtest_slippage_ratio回测滑点比例\n    '''\n    run(strategy_id='strategy_id',\n        filename='main.py',\n        mode=MODE_BACKTEST,\n        token='token_id',\n        backtest_start_time='2017-09-15 09:15:00',\n        backtest_end_time='2017-10-01 15:00:00',\n        backtest_adjust=ADJUST_PREV,\n        backtest_initial_cash=10000000,\n        backtest_commission_ratio=0.0001,\n        backtest_slippage_ratio=0.0001)\n```\n\n## 绩效\n![绩效](attach.png)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftruthhun%2Fturtle-trading","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftruthhun%2Fturtle-trading","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftruthhun%2Fturtle-trading/lists"}