In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces you how to build an Aron strategy in Python, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Brief introduction of Alon Index
By calculating the number of K-lines between the highest price and the lowest price before the current K-line distance, the Aron index helps traders predict the relative position relationship between the price trend and the trend area. It consists of two parts, namely: AroonUp and AroonDown. These two lines move up and down between 0100. Although they are named as online and offline, they are not really online and offline like BOLL indicators on the chart. As shown in the following figure, the Aaron indicator:
The calculation method of Alon Index
The Aaron index requires that you first set a time period parameter, just like setting the moving average cycle parameter. In traditional market software, this cycle number is 14, of course, this cycle parameter is not fixed, you can also set it to 10 or 50, and so on. For ease of understanding, let's define this time period parameter as: N. After determining N, we can calculate Alon online (AroonUp) and Aron offline (AroonDown). The specific calculation formula is as follows:
Aaron online = [(cycle parameter set-number of cycles after the highest price) / number of cycles calculated] * 100
Aaron offline = [(cycle parameter set-number of cycles after the lowest price) / number of cycles calculated] * 100
From this formula, we can roughly see the idea of Aron index. That is: how many cycles, prices below recent highs / lows, to help predict whether the current trend will continue, while measuring the strength of the current trend. If we classify this indicator, it is clear that it belongs to the trend tracking type. But unlike other trend-following indicators, it pays more attention to time than price.
How to use the Alon indicator
AroonUp online and AroonDown offline reflect the distance between the current time and the previous highest or lowest price. The nearer the time is, the larger the value is, and the farther the time is, the smaller the value is. And when the two lines cross, it indicates that the price direction may change, if AroonUp indicates that the price is on an upward trend above AroonDown, the price may rise further in the future; if AroonDown indicates that the price is on a downward trend above AroonUp, the price may fall further in the future.
At the same time, we can also set several fixed values to accurately enter the venue. We know that the Aaron index has been running up and down between 0100 and 100, so when the market is in an upward trend, that is, when AroonUp is above AroonDown, when AroonUp is greater than 50, it means that the upward trend of the market has been formed, and prices may continue to rise in the future; when the AroonUp is below 50, it shows that the driving force of price rise is weakening, and prices may fluctuate and fall in the future.
On the contrary, when the market is in a downward trend, that is, when AroonDown is above AroonUp, when AroonDown is greater than 50, it means that the market downward trend has been formed, and prices may continue to fall in the future; when AroonDown is below 50, it shows that the driving force of price decline is weakening, and prices may fluctuate and rise in the future. So according to the above two paragraphs of theory, we can list the terms of sale as:
When AroonUp is greater than AroonDown and AroonUp is greater than 50, long positions are opened.
When AroonUp is less than AroonDown, or AroonUp is less than 50, long positions are closed.
When AroonDown is greater than AroonUp and AroonDown is greater than 50, short positions are opened.
When AroonDown is less than AroonUp, or AroonDown is less than 50, short positions are closed.
Construction of transaction Strategy based on Aron Index
After sorting out the transaction logic, we can use the code to implement it, and open it in turn: fmz.com > Log in > Control Center > Policy Library > New Policy > Click the drop-down menu in the upper right corner to select the Python language, start writing the policy, and pay attention to the comments in the code below.
Step 1: write a strategy framework
We know that in quantitative transactions, the program is a cyclic process of constantly getting data, processing data, and issuing order transactions, so we continue to use the main function and onTick function mentioned earlier, where the onTick function is executed in an infinite loop in the main function. It is 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
Step 2: import the library
In addition, when calculating AROON, we need to use the talib library, and we import it directly with one line of import code. Because when using talib calculation, the data must be processed into numpy.array type first, so it is also imported into numpy library.
Import talibimport numpy as np
Step 3: define virtual position variables
There are two kinds of judgment positions in quantitative trading, one is real account position, the other is virtual position, and the other is the joint judgment of real position and virtual position. It is sufficient for us to use only real positions when making a firm offer, but here we use virtual positions as a demonstration to simplify the strategy.
Mp = 0 # used to control virtual positions
The principle of using virtual position is very simple. At the beginning of the strategy, the default is short position mp=0. When multiple orders are opened, virtual positions are reset to mp=1, when short orders are opened, virtual positions are reset to mp=-1, and when multiple orders or short orders are leveled, virtual positions are reset to mp=0. In this way, we only need to judge the value of mp when judging the construction logic to get the position.
Step 4: calculate the Alon index
To calculate the Aaron index, we must first obtain the basic data, but the premise is to subscribe to the data, that is, to subscribe to the specific contract code, you can subscribe to the index or main force continuous, or even subscribe to the contract code for the specific delivery month. Then the K-line array is obtained. The K-line array is a series of data including opening and closing, trading volume and time, and it is also the basic data for calculating most of the indicators.
After getting the K-line array, you need to determine the length of the K-line array, because an exception occurs if the K-line array is too short to calculate the metric. So we use the if statement here to determine that if the K-line array is less than the indicator parameter, it will be returned directly.
When using talib to calculate metrics, the parameters it passes in are numpy.array type data, so the necessary data in the K-line array is extracted and converted into numpy.array type data. Here we customize a get_data function to extract the necessary data first.
# convert the K-line array to the highest and lowest price array Used to convert to numpy.array type data def get_data (bars): arr = [], [] for i in bars: arr [0] .append (I ['High']) arr [1] .append (I [' Low']) return arr exchange.SetContractType ("ZC000") # subscription futures variety bars = exchange.GetRecords () # get K-line array if len (bars)
< cycle_length + 1: # 如果K线数组的长度太小,所以直接返回 returnnp_arr = np.array(get_data(bars)) # 把列表转换为numpy.array类型数据,用于计算AROON的值aroon = talib.AROON(np_arr[0], np_arr[1], 20); # 计算阿隆指标aroon_up = aroon[1][len(aroon[1]) - 2]; # 阿隆指标上线倒数第2根数据aroon_down = aroon[0][len(aroon[0]) - 2]; # 阿隆指标下线倒数第2根数据 talib在计算阿隆指标时,需要三个参数,依次是:最高价numpy.array类型数据、最低价numpy.array类型数据、时间周期。所以我们在自定义get_data函数中只需要把K线数组中的最高价和最低价提取出来就可以了,并把它们都转换成numpy.array类型数据。 紧接着,就可以计算阿隆指标了,直接调用talib.AROON方法并传入参数。计算后的阿隆指标是一个二维数组,所以我们分别把阿隆指标上线和下线分别提取出来,以便于判断开平仓逻辑。 第五步:下单交易 在下单交易之前,我们要先获取当前最新价格,因为在下单时需要在函数中传入下单价格。还需要引入全局变量mp,主要用于控制虚拟仓位。 close0 = bars[len(bars) - 1].Close; # 获取当根K线收盘价global mp # 全局变量,用于控制虚拟仓位if mp == 0 and aroon_up >Aroon_down and aroon_up > 50: # if the current short position, and the Aaron online is greater than the offline, and the Aaron online is greater than 50 exchange.SetDirection ("buy") # set the trading direction and type exchange.Buy (close0, 1) # multiple orders mp = 1 # set the virtual position value, that is, there are multiple if mp = = 0 and aroon_down > aroon_up and aroon_down > 50: # if the current short position And Aron offline is larger than online, and Aron offline is less than 50 exchange.SetDirection ("sell") # set trading direction and type exchange.Sell (close0-1,1) # open order mp =-1 # set the value of virtual position, that is, there are empty orders if mp > 0 and (aroon_up)
< aroon_down or aroon_up < 50): # 如果当前持有多单,并且阿隆上线小于下线或者阿隆上线小于50 exchange.SetDirection("closebuy") # 设置交易方向和类型 exchange.Sell(close0 - 1, 1) # 平多单 mp = 0 # 设置虚拟持仓的值,即空仓 if mp < 0 and (aroon_down < aroon_up or aroon_down < 50): # 如果当前持有空单,并且阿隆下线小于上线或者阿隆下线小于50 exchange.SetDirection("closesell") # 设置交易方向和类型 exchange.Buy(close0, 1) # 平空单 mp = 0 # 设置虚拟持仓的值,即空仓 万事俱备之后,就可以判断策略逻辑并开平仓下单交易了。在判断策略逻辑时肯定是使用if语句,先判断mp的持仓状态,然后再判断阿隆上线和下线的相互位置关系。需要注意的是在期货交易下单之前,先指定交易的方向类型,即:开多、开空、平多、平空。调用exchange.SetDirection()函数,分别传入:"buy"、"sell"、"closebuy"、"closesell"。最后下单之后重置持仓状态mp的值。 完整策略'''backteststart: 2015-02-22 00:00:00end: 2019-10-29 00:00:00period: 1dexchanges: [{"eid":"Futures_CTP","currency":"FUTURES"}]'''import talibimport numpy as np# 外部参数# cycle_length = 100# 定义全局变量mp = 0 # 用于控制虚拟持仓# 把K线数组转换成最高价和最低价数组,用于转换为numpy.array类型数据def get_data(bars): arr = [[], []] for i in bars: arr[0].append(i['High']) arr[1].append(i['Low']) return arr# 策略主函数def onTick(): exchange.SetContractType("ZC000") # 订阅期货品种 bars = exchange.GetRecords() # 获取K线数组 if len(bars) < cycle_length + 1: # 如果K线数组的长度太小,所以直接返回 return np_arr = np.array(get_data(bars)) # 把列表转换为numpy.array类型数据,用于计算AROON的值 aroon = talib.AROON(np_arr[0], np_arr[1], 20); # 计算阿隆指标 aroon_up = aroon[1][len(aroon[1]) - 2]; # 阿隆指标上线倒数第2根数据 aroon_down = aroon[0][len(aroon[0]) - 2]; # 阿隆指标下线倒数第2根数据 close0 = bars[len(bars) - 1].Close; # 获取当根K线收盘价 global mp # 全局变量,用于控制虚拟仓位 if mp == 0 and aroon_up >Aroon_down and aroon_up > 50: # if the current short position and Aaron online is greater than offline, and Aaron online is greater than 50 exchange.SetDirection ("buy") # set the trading direction and type exchange.Buy (close0, 1) # open multiple orders mp = 1 # set the value of virtual position That is, there are multiple single if mp = = 0 and aroon_down > aroon_up and aroon_down > 50: # if the current short position, and Aron offline is larger than online, and Aron offline is less than 50 exchange.SetDirection ("sell") # set transaction direction and type exchange.Sell (close0-1,1) # Open order mp =-1 # set the value of virtual position That is, there is an empty order if mp > 0 and (aroon_up < aroon_down or aroon_up < 50): # if you currently hold multiple orders, and Aron online is less than 50 exchange.SetDirection ("closebuy") # set the trading direction and type exchange.Sell (close0-1,1) # flat multiple single mp = 0 # set the value of virtual position That is, short if mp < 0 and (aroon_down < aroon_up or aroon_down < 50): # if you currently hold a short order and Aaron's offline is less than 50 exchange.SetDirection ("closesell") # set the trading direction and type exchange.Buy (close0, 1) # flat single mp = 0 # set the value of virtual position That is, short position # Program entry def main (): while True: # enter infinite loop mode onTick () # execute policy main function Sleep (1000) # hibernate for 1 second
Advantages and disadvantages of Alon Index
Advantages: the Aaron index can judge the state of the trend market, take into account the ability to find the market trend and judge the price shift, and help traders improve the utilization rate of funds, which is particularly important in the volatile market.
Disadvantages: Aron index is only one of a series of trend tracking indicators, but also has the shortcomings of trend tracking indicators. And it only judges the number of cycles of the highest or lowest price at a given time, but sometimes the highest or lowest price will be accidental in the whole market trend, which will interfere with the Alon index itself and cause false signals.
On how to build an Aron strategy in Python to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.