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 realize the intra-day High and low Breakthrough Strategy of python

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how python achieves the strategy of breaking through the high and low points of the day. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

What is intra-day trading?

The purpose of intraday trading is to gain profits from the small price fluctuations in the market of the day with smaller losses. It refers to the trading mode of opening and closing positions in the same day or within the same trading period. Opening and closing positions can be single or multiple, as long as the opening and closing ends before the same trading day.

In theory, intra-day trading does not bear the risk of overnight jump, which is a perfect low-risk trading strategy, but in fact this is not the case, although intra-day trading avoids the risk brought by jump, but also misses the profit brought by jump. But if you trade in the right way, intra-day trading can often yield handsome returns by cooperating with different trading rules.

Policy logic

We know that the easiest way to judge an upward trend is that the current low is higher than the previous low and the current high is higher than the previous high; in the same way, the easiest way to fall is that the current low is lower than the previous low. the current high is also lower than the previous high. However, it is too crude to judge the rise and fall of the trend only by the comparison of high and low points, because prices may jump back and forth dozens or even hundreds of times at one point, resulting in trading too frequently.

So we need to set a price range to filter these daily clutter to improve the simple high and low point breakthrough strategy. We can form a channel including upper and lower tracks according to the highest and lowest prices in the historical market. According to the principle of homeopathic trading, long positions are opened when prices break through the upper tracks, and short positions are opened when prices break through the downtracks.

Long opening: there is currently no position, the time is between the opening and closing 10 minutes, and the price is higher than the track.

Short opening: there is currently no position, the time is between opening and closing 10 minutes before closing, and the price is less than the lower track.

Long closing: the current long order, the price is less than the lower track, or the time is greater than 14:50

Short closing: currently holding a short order, the price is greater than the above track, or the time is greater than 14:50

Some people have calculated that most of the narrow stops are ineffective, and the stops in small spaces will hit the face frequently, so what we need to do is to design a wide stop: if the price falls instead of rising after the long position is opened, what we need to do is not to stop the loss immediately, but to wait and see until the price falls below the derailment. The same is true after the short position is opened, when the price rises instead of falling, continue to wait for whether the price will correct itself, and then stop the loss until it falls below the track.

Strategy writing

Step 1: write a strategy framework

Writing a strategy is like building a house, building the foundation and frame first, and then filling it. We use two functions here, one is the main main function, the other is the onTick function, the program will first execute the code from the main function, in the main function, we use an infinite loop mode to repeat the onTick 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: import the time library

# Import library import time

Because when the intra-day strategy is written, it is necessary to judge the current time to control the opening logic, this strategy is designed as follows: only between 9: 30 and 14: 50, all positions are closed after 14:50, and the rest of the time is filtered. So you need to introduce the time time library.

Step 3: set global variables

Mp = 0 # is used to control virtual position on _ line = 0 # upper rail under_line = 0 # lower rail

In the global variables, mp is mainly used to control virtual positions, which are generally divided into two types, one is the real account position, the other is the 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. On_line and under_line record the upper and lower tracks, respectively.

Step 4: processing time

# processing time function def can_time (hour Minute): hour = str (hour) minute = str (minute) if len (minute) = = 1: minute = "0" + minute return int (hour + minute) exchange.SetContractType ("MA888") # subscription futures variety bar_arr = exchange.GetRecords () # get the K-line array time_new = bar_ Arlen (bar_arr)-1] ['Time'] # get the timestamp time of the root K-line _ local_new = time.localtime (time_new / 1000) # processing timestamp hour_new = int (time.strftime ("% H") Time_local_new)) # formatting timestamp And get the hourly minute_new = int (time.strftime ("% M", time_local_new)) # formatted timestamp, and get the minute day_new = int (time.strftime ("% d", time_local_new)) # formatted timestamp And get the date time_previous = bar_ arr [len (bar_arr)-2] ['Time'] # get the timestamp time_local_previous = time.localtime (time_previous / 1000) # processing timestamp day_previous = int ("% d", time_local_previous)) # format timestamp and get the date

The processing time is used in two places: one is to judge whether the current time is within the trading time specified by us, and open the position if it is within this time and the opening condition is met. if not within this time and there is a position at present, all positions will be closed. The other is to determine whether the current K line is the K line of the latest trading day, and if the current K line is the K line of the latest trading day, reset the values of on_line and under_line.

Step 4: calculate the high and low points on and off the orbit

Global mp, on_line, under_line # introduce the global variable high = bar_ arr [len (bar_arr)-2] ['High'] # to get the highest price of the upper K line low = bar_ Arr [len (bar_arr)-2] [' Low'] # get the lowest price if day_new! = day_previous or len (bar_arr) = = 1: # if it is currently the first K line Or the latest K line on_line = high * up # reset upper rail under_line = low * down # reset lower rail if can_time (hour_new, minute_new)

< 930: # 如果不是在规定交易的时间内 if high >

On_line: # if the highest price of the upper K line is greater than the upper rail on_line = high * up # reset the rail elif low

< under_line: # 如果上根K线最低价小于下轨 under_line = low * down # 重置上轨 计算高低点上下轨的逻辑其实非常简单:如果当前是第一根K线,那么on_line和under_line的值分别是最高价和最低价,如果当前K线是最新交易日的K线,就重置on_line和under_line的值为最高价和最低价;一旦在规定的交易时间内,on_line和under_line的值就固定不变了,除非在这个时间之外并且如果上根K线最高价大于on_line就重置为最新的最高价;如果上根K线最低价小于under_line就重置为最新的最低价。 第5步:下单交易 close_new = bar_arr[len(bar_arr) - 1]['Close'] # 获取最新价格(卖价),用于开平仓if mp == 0 and 930 < can_time(hour_new, minute_new) < 1450: # 如果当前无持仓,并且在规定的交易时间内 if close_new >

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_new

< under_line: # 如果价格小于下轨 exchange.SetDirection("sell") # 设置交易方向和类型 exchange.Sell(close_new - 1, 1) # 开空单 mp = -1 # 设置虚拟持仓的值,即有空单# 如果持多单,并且价格小于下轨或者非规定的交易时间if mp >

0 and (close_new

< under_line or can_time(hour_new, minute_new) >

1450): exchange.SetDirection ("closebuy") # set trading direction and type exchange.Sell (close_new-1,1) # flat multiple single mp = 0 # set the value of virtual position, that is, if you hold a short order, and the price is higher than the above track or non-specified trading time if mp

< 0 and (high >

On_line or can_time (hour_new, minute_new) > 1450): exchange.SetDirection ("closesell") # sets the trading direction and type exchange.Buy (close_new, 1) # flat single mp = 0 # sets the value of virtual position, that is, short position

Before placing an order transaction, we need to get the current latest price, because the order price needs to be passed in the function when placing the order. Then use the if statement, according to the previously designed transaction logic, first judge the current position state, then judge the current time state, and the relationship between the latest price and the upper and lower tracks, and finally issue the order transaction and reset the virtual position status. It should be noted that before issuing orders in futures trading, specify the direction type of trading, that is, open more, open short, flat more, flat empty. Call the exchange.SetDirection () function, passing in: "buy", "sell", "closebuy", "closesell", respectively.

Complete strategy

# backtest configuration''backteststart: 2015-02-22 00:00:00end: 2019-10-17 00:00:00period: 5mexchanges: [{"eid": "Futures_CTP" "currency": "FUTURES"}]''# Import library import time# defines a global variable mp = 0 # used to control virtual positions on _ line = 0 # upper rail under_line = 0 # lower rail def onTick (): # processing time function def can_time (hour Minute): hour = str (hour) minute = str (minute) if len (minute) = = 1: minute = "0" + minute return int (hour + minute) exchange.SetContractType ("MA888") # subscription futures variety bar_arr = exchange.GetRecords () # get the array time_new = bar_ arr [len (bar_arr)-1] [ 'Time'] # get the timestamp of the root K line time_local_new = time.localtime (time_new / 1000) # processing timestamp hour_new = int (time.strftime ("% H") Time_local_new)) # formatting timestamp And get the hourly minute_new = int (time.strftime ("% M", time_local_new)) # formatted timestamp, and get the minute day_new = int (time.strftime ("% d", time_local_new)) # formatted timestamp And get the date time_previous = bar_ arr [len (bar_arr)-2] ['Time'] # get the timestamp of the upper K line time_local_previous = time.localtime (time_previous / 1000) # processing timestamp day_previous = int ("% d", time_local_previous)) # format timestamp And get the date global mp, on_line Under_line # introduce the global variable high = bar_ arr [len (bar_arr)-2] ['High'] # to get the highest price of the upper K line low = bar_ arr [len (bar_arr)-2] [' Low'] # get the lowest price of the upper K line if day_new! = day_previous or len (bar_arr) = = 1: # if it is currently the first K line Or the latest K line on_line = high * up # reset upper rail under_line = low * down # reset lower rail if can_time (hour_new, minute_new)

< 930: # 如果不是在规定交易的时间内 if high >

On_line: # if the highest price of the upper K line is greater than the upper rail on_line = high * up # reset the rail elif low

< under_line: # 如果上根K线最低价小于下轨 under_line = low * down # 重置上轨 close_new = bar_arr[len(bar_arr) - 1]['Close'] # 获取最新价格(卖价),用于开平仓 if mp == 0 and 930 < can_time(hour_new, minute_new) < 1450: # 如果当前无持仓,并且在规定的交易时间内 if close_new >

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_new

< under_line: # 如果价格小于下轨 exchange.SetDirection("sell") # 设置交易方向和类型 exchange.Sell(close_new - 1, 1) # 开空单 mp = -1 # 设置虚拟持仓的值,即有空单 # 如果持多单,并且价格小于下轨或者非规定的交易时间 if mp >

0 and (close_new

< under_line or can_time(hour_new, minute_new) >

1450): exchange.SetDirection ("closebuy") # set trading direction and type exchange.Sell (close_new-1,1) # flat multiple single mp = 0 # set the value of virtual position, that is, if you hold a short order, and the price is higher than the above track or non-specified trading time if mp

< 0 and (high >

On_line or can_time (hour_new, minute_new) > 1450): 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 position # program entry def main (): while True: onTick () Sleep (1000) # dormant for 1 second on "how to achieve intraday high and low point breakthrough strategy" this article is shared here. I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please 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.

Share To

Internet Technology

Wechat

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

12
Report