In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to use Python to achieve a simple commodity futures Bollinger index breakthrough strategy, the article is very detailed, has a certain reference value, interested friends must read it!
Brin index breakthrough strategy, the train of thought is very simple. Writing this strategy in Python language is also very easy to implement, plus the configuration information of the back test, there are 70 lines of code, which can actually be more concise. In view of the teaching strategy, it does not use the difficult Python syntax, but uses relatively basic sentences and structures. It is convenient for learners to use Python language for programmed trading of commodity futures for reference. In view of the length of the article, strategy structure, explanatory comments, written directly in the code comments.
Strategy:
Use the BOLL index up and down rails as a breakthrough boundary, break through the upper rails flat and long. Break through the lower track, go short. It is used to capture the trend, so there will be a pullback in the volatile market.
Policy implementation:
'' backteststart: 2019-07-01 00:00:00end: 2019-11-26 00:00:00period: 1hexchanges: [{"eid": "Futures_CTP", "currency": "FUTURES"}]''IDLE = 0 # defines a tag quantity, indicating idle state LONG = 1 # defines a tag quantity, indicates holding multi-position status SHORT = 2 # defines a tag quantity Indicates the short position status def CancelAll (): # implement a function while True: # cancel all pending orders while True: # loop execute orders = _ C (exchange.GetOrders) # read all pending orders. Orders is an array if len (orders) = = 0: # to determine whether the array length is 0. If 0 means there is no hanging order in this array, break # jumps out of the while loop, and no hanging order means there is no need to cancel for order in orders: # through the above if detection, it is executed here, indicating that the length of the orders array is not 0, and there are hanging orders. Traverse the orders array exchange.CancelOrder (order.Id) # cancel the order Sleep (1000) of the Id according to the Id in the traversal order information # control the cancellation frequency, pause def main (): # policy main function for 1 second each time you cancel After the policy starts, execute state = IDLE # define a state variable for the policy, initialize it to idle state direction = "" # define an order direction variable for the policy Initialize to empty string while True: # execute policy logic loop if exchange.IO ("status"): # check whether you are connected to the futures company server, log in to LogStatus (_ D (), "connected") exchange.SetContractType ("rb2001") # set the contract to operate Set it to rb2001 here, or make it as a parameter. Set records = _ C (exchange.GetRecords) # on the policy parameters to obtain K-line data. On the policy parameters interface, set the K-line period for 1 hour. Here, you get 1-hour K-line data if len (records).
< 21: # 使用BOLL指标的默认参数,所以K线数据的Bar数量要足够21才能计算出有效的BOLL指标 continue # 不满足条件的情况下,continue跳过后面的代码,重复循环 boll = TA.BOLL(records) # 当符合 len(records) >= 21:00, execute here, use TA.BOLL to calculate Bollinger index data, boll is a two-dimensional list, boll [0] is the upper track, boll [1] is the middle line, boll [2] is the lower track ext.PlotRecords (records, "K") # use the drawing class library interface, draw the K line Ext.PlotLine ("up", boll [0] [- 2], records [- 2] .time) # can be found in the policy plaza using the line drawing class library interface Draw the track ext.PlotLine ("mid", boll [1] [- 2], records [- 2] .time) # draw the center line ext.PlotLine ("down", boll [2] [- 2]) Records [- 2] .time) # pos = _ C (exchange.GetPosition) # read the position information of the current account if len (pos) = = 1: # if the current account has a position When there is no position, len (pos) is equal to 0 if pos [0] .Type = = PD_LONG or pos [0] .Type = = PD_LONG_YD: # based on the position direction in the position data Set the policy status variable state state = LONG # to hold multiple positions elif pos [0] .Type = = PD_SHORT or pos [0] .Type = = PD_SHORT_YD: state = SHORT # set to hold short status elif len (pos) = 0: # when there is no position state = IDLE # position status set to idle else: raise "error len (pos) > 1 "# if multiple positions are detected If you report an error and run this strategy alone, you will not have multiple positions. If an exception occurs, if records [- 2] .close > boll [0] [- 2] and (state = = IDLE or state = = SHORT): # if you are currently idle or holding a short position, determine that the price breaks through the upper track when the K-line BAR is completed and determine the next step to close the position (if you hold a short position) Open multiple positions (if idle) Set the direction trading direction variable if state = = IDLE: direction = "buy" # set the trading direction variable to open multiple positions else: direction = "closesell" # set the trading direction variable to short position exchange.SetDirection (direction) # call the trading direction setting function Set the direction exchange.Buy (records [- 1] .close + 2,1) # according to the current price, add two hops (for rb) to eat the order, place the order for 1 hand, and exchange.Buy issues the order function. The first parameter is price. The second parameter is the order quantity CancelAll () # after placing an order, try to cancel all pending orders (unclosed) elif records [- 2] .close < boll [2] [- 2] and (state = = IDLE or state = = LONG): # judge the breakthrough # level long position (if it is the status of holding long position) Open a short position (if idle) Set the direction transaction direction variable if state = = IDLE: direction = "sell" else: direction = "closebuy" exchange.SetDirection (direction) exchange.Sell (records [- 1] .close-2,1) CancelAll () else: LogStatus (_ D () "not connected") # if the futures company server is not connected Display time on the robot status bar, and unconnected information Sleep (500) # pause 500ms per cycle to control the program logic rotation frequency
Back test
Use rb2001 contract backtest
Be careful
It should be noted that the strategy is designed according to the simplest principles for teaching purposes. The order quantity is 1 hand, if the order quantity is multi-hand, you can think about what problems it may bring, how to transform and upgrade, and how to deal with such problems.
The strategy refers to the "drawing class library", which needs to be checked in the "template bar". The drawing class library can be found in the strategy square.
These are all the contents of this article entitled "how to use Python to achieve a simple Brin Index Breakthrough Strategy for Commodity Futures". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.