Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Tang Qi'an Channel Strategy by python

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to achieve Tang Qi'an channel strategy in python". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to achieve Tang Qi'an channel strategy in python" can help you solve the problem.

Writing strategy

By now, you should have a good understanding of the original Donquian channel rules and the way we are going to improve it. Now let's code this trading strategy.

Step 1: write a strategy framework

The policy framework is actually two functions, of which the main function is the entry function of the entire program, that is to say, when the policy starts to execute, the main function will be executed first; the other is the onTick function, onTick is just the name of a function, of course, you can also name it freely, and the onTick function mainly writes policy logic. The whole framework is actually repeating the onTick function in the main function.

# Policy main function def onTick (): pass# program entry def main (): while True: # enter infinite loop mode onTick () # execute policy main function Sleep (1000) # hibernate for 1 second

Step 2: define global variables and external parameters

Our strategy only needs a global variable to control the virtual position. The so-called virtual position refers to the theoretical position rather than the real position. Whether the position is opened or closed, we all assume that the order has been fully closed.

# define the global variable mp = 0 # to control the virtual position # external parameter long_coefficient = 0.999short_coefficient = 1.001cycle_length = 55

Step 3: deal with K-line data

As we have defined earlier, the upper track is the maximum value of the highest price of the past N days, and the lower track is the minimum value of the lowest price of the past N days. To calculate these two values, you must first get the basic K-line data. But after using the GetRecords method to obtain the basic K-line data, don't panic to calculate the upper and lower tracks, but deal with the data first.

Because we need N K lines when calculating the upper and lower tracks, it cannot be calculated if the number of K lines is too small, so we need to add an if condition to determine whether the current K line meets the number we need, and if not, go back directly and wait for the next cycle. In addition, we also need to extract the current latest price and the closing price of the last K-line from the K-line array, which is mainly used to open and close the position, and the closing price of the last K-line is mainly used to judge the opening and closing signal.

Some friends may ask, why not directly use the latest price to judge the opening signal? This is because if we use the latest price to judge, there may be the problem of repeated signals, and at the same time, in order to avoid the common quantitative trading problems such as future functions and stolen prices, our strategy in design is: the current K-line sends out the signal, and the next K-line issues the order.

Exchange.SetContractType ("rb000") # subscription futures variety bar_arr = exchange.GetRecords () # get K-line array if len (bar_arr)

< cycle_length + 1: # 判断K线数组的长度 return # 如果K线长度过小,就直接返回close_new = bar_arr[len(bar_arr) - 1]['Close'] # 获取最新价格(卖价),用于开平仓close_last = bar_arr[len(bar_arr) - 2]['Close'] # 上根K线收盘价,用于 第4步:计算上轨、下轨、中轨 在发明者量化交易软件中,已经内置了talib库中的Highest函数和Lowest函数,所以我们直接调用这两个函数就可以计算上轨和下轨的值。但因为我们是使用上根K线收盘价为基准,来判断它与上轨、下轨、中轨的位置关系来开平仓,所以在计算上轨和下轨之前需要先删除K线数组中的最后一个元素。 bar_arr.pop() # 去掉K线数组最后一个元素,策略是当前K线出信号,下根K线发单,这样可以避免未来函数和偷价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 # 计算中轨 第5步:下单交易 根据Python的语法规则,要想在函数内使用外部的全局变量,需要在使用这个变量之前,先用global关键字把变量引入。注意下面代码中的注释,真个代码流程是使用if语句,然后根据我们之前定义的策略逻辑来编写。有两个地方需要注意,一个是在下单之前需要先设置下单的类型方向,也就是先调用SetDirection函数。另一个是在下单之后,要把虚拟持仓变量mp重新赋值。 global mp # 引入全局变量if mp == 0: # 如果当前无持仓 if close_last >

On_line: # if the price is greater than the above track exchange.SetDirection ("buy") # set the trading direction and type exchange.Buy (close_new, 1) # open multiple orders mp = 1 # set the value of virtual positions, that is, there are multiple 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") # set trading direction and type exchange.Buy (close_new, 1) # flat single mp = 0 # set the value of virtual position, that is, short position

Complete policy code

# backtest configuration''backteststart: 2015-02-22 00:00:00end: 2019-11-27 00:00:00period: 1hexchanges: [{"eid": "Futures_CTP" "currency": "FUTURES"}]''# define the global variable mp = 0 # to control the virtual position # external parameter long_coefficient = 0.999short_coefficient = 1.001cycle_length = 55def onTick (): exchange.SetContractType ("rb000") # subscription futures variety bar_arr = exchange.GetRecords () # get K-line array 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: # if the price is greater than the above track exchange.SetDirection ("buy") # set the trading direction and type exchange.Buy (close_new, 1) # open multiple orders mp = 1 # set the value of virtual positions, that is, there are multiple 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") # sets the trading direction and type exchange.Buy (close_new, 1) # flat single mp = 0 # sets the value of the virtual position That is, short LogStatus (mp) # program entry def main (): while True: onTick () Sleep (1000) # dormant for 1 second, this is the end of the content about "how python implements Tang Qi'an channel strategy". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report