In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to achieve a Philippine Ali four-price strategy in Python. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
A brief introduction to Fei Ali
Fei Ali is a Japanese trader who mainly prefers intra-day subjective trading in commodity futures. Its fame was won by 1098% in the ROBBINS-TAICOM Futures Championship Competition in 2001. And won the championship with 709% and 1131% in the next two years. From the results, we know that Fei Ali is a very good trader.
Fortunately, Fei Ali describes his trading method in detail in the book 1000% Men, and the four-price strategy is the summary of his trading method by later generations. Although it only imitates part of the external form plus our own understanding, it does not represent the whole essence of the transaction, but it can at least help us to expand our thinking when building the strategy.
Policy logic
Philippine Ali four-price strategy is a relatively simple trend intra-day trading strategy, four prices respectively refer to: yesterday's high, yesterday's low, yesterday's closing price, today's opening price. Judging from the transaction notes in the book, Faye Ali does not use any analytical tools, but uses the concept of overflow line, which is commonly referred to as resistance line and support line.
Resistance line = the highest price yesterday
Support line = yesterday's lowest price
For the definition of resistance line and support line, he uses yesterday's highest price and yesterday's lowest price, which can be regarded as the fluctuation range of yesterday's price, which also means that bulls or bears will effectively break through the resistance line and support line only if they have sufficient strength. And once there is a breakthrough in this fluctuation range, it means that the momentum behind the price is larger, and the follow-up trend may move along the minimum resistance line.
Long opening: price breaks through the resistance line
Short opening: price breaks through the support line
If the opening price is between the resistance line and the support line, bulls are established when the price breaks through the resistance line upward and bears are established when the price breaks down through the support line. If all goes well, the position will be held until the close. The advantage of this is that it meets the sufficient and non-necessary conditions, that is, a breakthrough may not necessarily rise / fall, but it will certainly break through, that is, it will always stay on the road where the market occurs and wait for an opportunity. because once the rise and fall of a larger market occurs, it is bound to break through the resistance line and support line.
Of course, this is also the method with the highest error rate, because in many cases, the price only temporarily breaks through the key position, and if you open the position hastily, you may face the risk of price reverse movement at any time. At this time, it is necessary to set some filtering conditions to limit the back and forth opening and closing problems caused by false breakthroughs. In addition, in the trading cycle, try to avoid volatility too chaotic 5-minute cycle below the K line.
But after opening the position, it is good to make a profit, if you encounter a loss, you can not accumulate from a small loss to a big loss, and then close the position at the close, which is obviously unreasonable. So we have two ways to close the position: close the position and stop the loss. If you break the high point or the low point on the K line and then return to the original range, you should consider the stop loss.
Long closing position: reach the long stop line 5 minutes before the close
Short closing: reach the short stop line 5 minutes before the close.
In fact, there are many trading methods in Fei Ali's subjective trading, including: first rising and then falling after opening, shorting below the opening price, stop loss setting at the highest point of previous rise; falling first and then rising after opening, breaking through the opening price to do long, stop loss is set at the lowest point of the previous decline. Friends with strong hands-on ability can add and change in their own strategies.
Here you will find that for a day's price trend, the probability of the closing price rising or falling relative to the opening price is close to 50%. Fei Ali's trading method is in an invincible position in terms of winning rate, coupled with the fact that the market has been held until the close when the market is smooth, and stop losses in time when the market does not meet their expectations. It forms a positive trading way to cut off losses and let profits run, which is also the reason for accumulating profits after long-term trading.
Strategy writing
Step 1: write a strategy framework
# 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 a main function and an onTick function, then write a while loop in the main function, and repeat the onTick function.
Step 2: import the library
Import time # used to convert time format
Because this is an intra-day strategy, we need to judge the K-line timestamp, if there is a position, and close out at the close. Then we can just import time.
Step 3: get the basic data
Bar_arr = _ C (exchange.GetRecords, PERIOD_D1) # get daily line array if len (bar_arr)
< 2: # 如果小于2根K线 return # 返回继续等待数据yesterday_high = bar_arr[-2]['High'] # 昨日最高价yesterday_low = bar_arr[-2]['Low'] # 昨日最低价yesterday_close = bar_arr[-2]['Close'] # 昨日收盘价today_open = bar_arr[-1]['Open'] # 当日开盘价 菲阿里四价需要用到四个数据:昨日最高价、昨日最低价、昨日收盘价、当日开盘价。因为这些都是日线级别的数据,所以我们在使用GetRecords的时候,可以直接传入PERIOD_D1参数,表明我们要获取的是日K,这样无论你的策略加载的是哪个周期的数据,它始终获取的都是日线级别的数据。 另外,细心的朋友可能已经发现,为什么这一次在调用GetRecords的时候,代码的写法跟以前不一样?这次我们使用的是发明者量化软件内置的重试函数_C()。使用这个函数的好处是,该函数会一直调用指定函数到成功返回,这样可以避免直接使用GetRecords函数时,没有获取到数据导致报错的情况。 第4步:处理时间和获取最新价格 bar_arr = _C(exchange.GetRecords) # 获取当前设置周期K线数组current_time = bar_arr[-1]['Time'] # 获取当前K线时间戳time_local = time.localtime(current_time / 1000) # 处理时间戳hour = int(time.strftime("%H", time_local)) # 格式化时间戳,并获取小时minute = int(time.strftime("%M", time_local)) # 格式化时间戳,并获取分钟current_close = bar_arr[-1]['Close'] # 获取最新价格 既然是获取当前的时间,那么肯定是使用获取当前设置周期的K线数据更为合适,所以需要重新使用一次GetRecords,这次我们同样也是使用_C()重试函数,在不传入参数的情况下,就是默认获取当前设置周期的K线数组。另外,获取最新价格的目的是,计算交易逻辑和下单。 第5步:处理时间 def trade_time(hour, minute): hour = str(hour) minute = str(minute) if len(minute) == 1: minute = "0">The reason for creating this function is that before opening, we need to determine whether the current time is within our specified trading time, and whether the current time is nearing the close when there is a position. In step 4, we have obtained the current K-line hours and minutes, in order to facilitate the comparison, we use the method of hours plus minutes, for example: if the K-line time is 9:05, then the result returned by trade_time is 905; if the K-line time is 14:30, then the result returned by trade_time is 1430, and so on.
Step 6: take a position
Real_position = _ C (exchange.GetPosition) # get position array if len (real_position) > 0: # if the length of the position array is greater than 0 real_position = real_position [0] if real_position ['ContractType'] = =' rb888': # if the position variety is equal to the subscription variety if real_position ['Type'] = 0 or real_position [' Type'] = 2: # if it is more Single mp = real_position ['Amount'] # assign a positive position elif real_position [' Type'] = = 1 or real_position ['Type'] = = 3: mp =-real_position [' Amount'] # assign a negative position else: mp = 0 # assign a position of 0
In the previous chapter, we have been using virtual positions, which are simple to write, but can only be used in ideal environments. In order to be closer to the actual combat, we will use real position data in later chapters. In fact, it is not that difficult to obtain the real position data. By directly using the GetPosition method of the inventor's quantitative software, you can get all the positions of the current account.
GetPosition returns an one-dimensional array containing dictionaries:
# examples [{'MarginLevel': 16,' Amount': 1, 'FrozenAmount': 0,' Price': 3540.0, 'Profit':-30,' Margin': 2124.0, 'Type': 0,' ContractType': 'rb888'}]
Where Amount is the number of positions, Price is the opening price, Profit is the current profit, Type is the long direction (according to the definition of the inventor quantified API, the value of Type is 0 or 2 is long, 1 or 3 is short) and ContractType is the contract code. On the other hand, we normalize the long positions to positive numbers and the short positions to negative numbers.
Step 7: set stop loss
# set bullish stop if today_open / yesterday_high > 1.005: # if the opening price of the day is greater than yesterday's highest price long_stop_loss = yesterday_high # set bullish stop price to yesterday's highest price elif today_open / yesterday_high
< 0.995: # 如果当天开盘价小于昨天最高价 long_stop_loss = today_open # 设置多头止损价为当天开盘价else: # 如果当天开盘价接近于昨天最高价 long_stop_loss = (yesterday_high + yesterday_low) / 2 # 设置多头止损为昨天中间价# 设置空头止损if today_open / yesterday_low < 0.995: # 如果当天开盘价小于昨天最低价 short_stop_loss = yesterday_low # 设置空头止损价为昨天最低价elif today_open / yesterday_low >1.005: # if the opening price of the day is greater than yesterday's lowest price short_stop_loss = today_open # set the short stop price to the day's opening price else: # if the opening price of the day is close to yesterday's lowest price short_stop_loss = (yesterday_high + yesterday_low) / 2 # set the bullish stop to yesterday's midpoint
In most cases, when the price breaks through the resistance line and support line, the stop loss is set to the current opening price. But there is a problem: if the opening price is greater than the resistance line and the price goes down, or if the opening price is less than the support line and the price goes up, it will cause logical errors and lead to frequent closing positions.
In order to solve this problem, we need to set different stop-loss prices according to the position relationship between the opening of the day and the resistance line and support line. If the opening price of the day is 0.5% higher than yesterday's high, then set the stop loss of the bulls at the highest price of yesterday; if the opening price of the day is between the resistance line and the support line, then the stop price of the bulls is still the opening price of the day; if the opening price of the day is close to yesterday's high, then set the stop loss of the bulls at yesterday's middle price. The stop loss of short positions is also based on this principle.
Step 8: place an order transaction
If mp > 0: # if you currently hold multiple if current_close
< long_stop_loss or trade_time(hour, minute) >1450: # if the current price is less than the long stop line, or if it exceeds the specified trading time exchange.SetDirection ("closebuy") # set the trading direction and type exchange.Sell (current_close-1,1) # flat single if mp
< 0: # 如果当前持有空单 if current_close >Short_stop_loss or trade_time (hour, minute) > 1450: # if the current price is greater than the short stop line, or if it exceeds the specified trading time exchange.SetDirection ("closesell") # sets the trading direction and type exchange.Buy (current_close + 1,1) # flat single if mp = = 0 and 930
< trade_time(hour, minute) < 1450: # 如果当前无持仓,并且在规定的交易时间内 if current_close >Yesterday_high: # if the current price is greater than yesterday's highest price exchange.SetDirection ("buy") # set transaction direction and type exchange.Buy (current_close + 1,1) # open multiple orders elif current_close < yesterday_low: # if the price is less than yesterday's lowest price exchange.SetDirection ("sell") # set transaction direction and type exchange.Sell (current_close-1) 1) # opening a short order on how to implement a Philippine Ali four-price strategy in Python, that's all. I hope the above content can be of some help to you and 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.