提起唐奇安通道,很多人都会联想到海龟交易法则,这也许是有史以来最成功的交易员培训课程。海龟们用神奇的交易系统赚了成百上千万美元,直到1983年海龟交易法则解密,人们才发现这个神奇的交易系统用的是修正版的唐奇安通道。但时过境迁,现在的市场环境已经发生了很大的变化,这导致唐奇安通道策略变得低效,那么今天我们试着改进,看看增强版的唐奇安通道策略效果如何。 点击阅读更多内容
# 回测配置 '''backtest start: 2015-02-22 00:00:00 end: 2019-11-27 00:00:00 period: 1h exchanges: [{"eid":"Futures_CTP","currency":"FUTURES"}] ''' # 定义全局变量 mp = 0 # 用于控制虚拟持仓 # 外部参数 long_coefficient = 0.999 short_coefficient = 1.001 cycle_length = 55 def onTick(): exchange.SetContractType("rb000") # 订阅期货品种 bar_arr = exchange.GetRecords() # 获取K线数组 if len(bar_arr) < cycle_length + 1: return close_new = bar_arr[len(bar_arr) - 1]['Close'] # 获取最新价格(卖价),用于开平仓 close_last = bar_arr[len(bar_arr) - 2]['Close'] # 上根K线收盘价 bar_arr.pop() on_line = TA.Highest(bar_arr, cycle_length, 'High') * long_coefficient under_line = TA.Lowest(bar_arr, cycle_length, 'Low') * short_coefficient middle_line = (on_line + under_line) / 2 global mp # 引入全局变量 if mp == 0: # 如果当前无持仓 if close_last > on_line: # 如果价格大于上轨 exchange.SetDirection("buy") # 设置交易方向和类型 exchange.Buy(close_new, 1) # 开多单 mp = 1 # 设置虚拟持仓的值,即有多单 elif close_last < under_line: # 如果价格小于下轨 exchange.SetDirection("sell") # 设置交易方向和类型 exchange.Sell(close_new - 1, 1) # 开空单 mp = -1 # 设置虚拟持仓的值,即有空单 # 如果持多单,并且价格小于下轨 if mp > 0 and close_last < middle_line: exchange.SetDirection("closebuy") # 设置交易方向和类型 exchange.Sell(close_new - 1, 1) # 平多单 mp = 0 # 设置虚拟持仓的值,即空仓 # 如果持空单,并且价格大于上轨 if mp < 0 and close_last > middle_line: exchange.SetDirection("closesell") # 设置交易方向和类型 exchange.Buy(close_new, 1) # 平空单 mp = 0 # 设置虚拟持仓的值,即空仓 LogStatus(mp) # 程序入口 def main(): while True: onTick() Sleep(1000) #休眠1秒