In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article is to share with you about how to use Python to achieve Dual Thrust interval breakthrough strategy. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Introduction to Dual Thrust
The Dual Thrust strategy belongs to the opening range breakthrough strategy, which determines an upper and lower track by adding or subtracting a certain range of the opening price of the day, long when the price breaks through the upper track and shorting when the price breaks through the lower track. However, compared with other breakthrough strategies, there are two differences: the first is that when the Dual Thrust strategy sets the range, it introduces the four prices of the first N trading days, which makes the range relatively stable in a certain period of time, which is more reasonable for the trend tracking strategy.
The second is that Dual Thrust strategy considers asymmetry on the trigger conditions of bulls and shorts. Through external parameters Ks and Kx, we can choose different periods for bulls and bears, which is more in line with the characteristics of slow rise and rapid decline in the futures market. When Ks is less than Kx, bulls are relatively easy to trigger, and when Ks is greater than Kx, short positions are relatively easy to trigger. The advantage is that you can adjust the values of Ks and Kx dynamically according to your trading experience. The strategy can also be used based on the optimal parameters tested by historical data.
Dual Thrust upper and lower rail
In the Dual Thrust strategy, we first need to define the concussion interval of the first N K lines, then multiply the concussion interval by the long and short coefficients to calculate the range, then add or subtract this range with the opening price to form the upper and lower rails, and finally open the closing position according to the relationship between the price and the upper and lower rails.
Calculate the shock interval
To calculate the volatility range, we first need to obtain four prices, which are: the highest price in the first N K lines (hh), the highest closing price (hc), the lowest closing price (ll) and the lowest closing price (lc). Then get the difference between hh and lc and the difference between hc and ll, and finally get the maximum value of the two differences. The formula is:
Range = Max (hh-lc,hc-ll)
Calculation range
When calculating the range, we need to use two external parameters, namely the long coefficient Ks and the short coefficient Kx, whose values can be set according to the experience of the trader. So the range of so many heads is Rang times Ks;, and the range of short positions is Rang times Kx. The formula is:
Long_range = Range * Ks
Short_range = Range * Kx
Calculate the upper and lower rails
With the long range and the short range, the values of the upper and lower tracks can be calculated according to the opening price, in which the value of the upper track is the opening price plus the long range, and the value of the lower rail is the opening price minus the short range. The formula is:
Up_line = open + long_rang
Down_line = open-short_range
Policy logic
Be long: the price breaks through the track upward.
Short selling: the price breaks through the downward track.
Like other breakthrough strategies, the Dual Thrust strategy is also based on the relative position relationship between the price and the upper and lower tracks to open the position, when the price upward breaks through the upper rail, and when the price breaks down the lower rail, open the short order. In addition, the Dual Thrust strategy has no stop-loss-stop-profit mechanism and no active closing mechanism. That is to say, when holding a multi-order, if the price breaks down the downtrack, the direct anti-short is more; when holding a short order, if the price breaks through the upper track, it is more empty.
Strategy writing
Step 1: write a policy architecture
It is also the familiar policy framework, including a main program entry function and an onTick policy main function, as follows:
# 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
Define global variables
The global variable is defined because, in the repeated execution of the onTick function, if the variable is defined in the onTick function, the value of the variable will change with the execution of the onTick. But sometimes we need to change this variable when a certain condition is reached, so we need to write the variable outside the onTick function.
Mp = 0 # to control virtual position last _ bar_time = 0 # to judge K line time up_line = 0 # upper rail down_line = 0 # lower rail
Calculate the upper and lower rails
Take a closer look at the comments in the following code, first introduce all the global variables at once, then subscribe to the futures variety and get the K-line array, and then judge whether the status of the K-line array meets our conditions. if there is no problem, get the latest K-line data and the latest closing price from the K-line array.
With the above basic data, the values of the upper and lower tracks can be calculated. The first is to get four prices: the highest price, the highest closing price, the lowest closing price, and then the range can be calculated. finally, the upper and lower tracks can be calculated according to the range. You can be familiar with the following code according to the above calculation process.
Global mp, last_bar_time, up_line, down_line # introduce the global variable exchange.SetContractType (FuturesCode) # subscription futures variety bar_arr = exchange.GetRecords () # to get K-line array if not bar_arr or len (bar_arr)
< Cycle: return # 如果没有获取到K线数据或者K线数据太短就返回last_bar = bar_arr[len(bar_arr) - 1] # 最新的K线last_bar_close = last_bar['Close'] # 最新K线的收盘价if last_bar_time != last_bar['Time']: # 如果产生了新的K线 hh = TA.Highest(bar_arr, Cycle, 'High') # 最高价 hc = TA.Highest(bar_arr, Cycle, 'Close') # 最高的收盘价 ll = TA.Lowest(bar_arr, Cycle, 'Low') # 最低价 lc = TA.Lowest(bar_arr, Cycle, 'Close') # 最低的收盘价 Range = max(hh - lc, hc - ll) # 计算范围 up_line = _N(last_bar['Open'] + Ks * Range) # 计算上轨 down_line = _N(last_bar['Open'] - Kx * Range) # 计算下轨 last_bar_time = last_bar['Time'] # 更新最后时间戳 下单交易 下单交易很简单,使用if语句判断当前的持仓状态和价格与上下轨的相互位置关系来开平仓。同样的在下单交易之前也需要设置交易方向和类型,即:开多、开空、平多、平空。最后下单之后重置虚拟持仓的状态。 if mp == 0 and last_bar_close >= up_line: exchange.SetDirection ("buy") # set the direction and type of transaction exchange.Buy (last_bar_close, 1) # open multiple orders mp = 1 # set the value of virtual position, that is, multiple orders if mp = = 0 and last_bar_close
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.