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 use Pandas Library to implement a Commodity Futures Grid Strategy in python

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article is about how to use Pandas library to achieve a commodity futures grid strategy in python. I think it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.

Data structure of Pandas

Series: one-dimensional array, similar to one-dimensional array in Numpy. They are also very similar to List, the basic data structure of Python. Series can now save different data types, strings, Boolean values, numbers, and so on can be stored in Series.

Time-Series: Series indexed by time.

DataFrame: two-dimensional tabular data structure. Many features are similar to data.frame in R. You can think of DataFrame as a container for Series.

Panel: a three-dimensional array that can be understood as a container for DataFrame.

Panel4D: a 4-dimensional data container like Panel.

PanelND: with the factory collection, you can create modules for N-dimensional naming containers like Panel4D.

Use the powerful features of Pandas to encode the grid strategy

The principle of grid strategy is that "position strategy is more important than timing strategy". Its basic mode of operation is to take a certain point as the basic point, hang a certain number of empty orders with a certain number of points up and down, set profit targets, but do not set stop losses, and close the position when the price moves in the desired direction. and hang the same buying order and selling order at the original position. In this way, these trading orders form an array like a fishnet, profiting back and forth in a volatile market.

This strategy first calculates the mean and standard deviation of the past 300 price data, and obtains the interval dividing line of the grid according to the mean addition and subtraction of 1 and 2 standard deviations, with position weights of 0.3 and 0.5, respectively. Then configure the position according to the interval of the price (+ /-40 is the upper and lower bound, which has no practical significance):

(- 40], (- 3)], (- 3)], (- 2], (2), (3) (40) (specific price equals mean + digital multiple standard deviation)

-0.5.-0.3, 0.0, 0.3, 0.5.

As a first step, we need to introduce libraries that need to be used in the policy code

Import typesimport numpy as npimport pandas as pd

In the second step, we need to initialize the contract data in the inventor quantification platform. In this case, we still use rebar futures as an example.

Def init (): # subscribe to the 2005 contract for rebar And get all the closing prices of the current cycle of the inventor quantification platform exchange.SetContractType ("rb2005") records = exchange.GetRecords () close_01 = records.Close # obtain the grid interval dividing line context.band = np.mean (close_01) + np.array ([- 40,-3,-2, 2, 3, 40]) * np.std (close_01) # set the position of the grid context.weight = [0.5 0.3, 0.0, 0.3, 0.5]

Third, and most important, we began to write policy logic and automate transactions

It should be noted here that we need to use the domestic commodity futures template of the inventor quantification platform. The address of the template is: https://www.fmz.com/strategy/24288. When you code the inventor quantification strategy page, you need to copy this template to your own policy base first, and then check it during the back test. Here, please pay attention.

For information on how to deploy custodians and robots, please refer to my previous article: https://www.fmz.com/bbs-topic/4140

Readers who want to buy their own cloud computing server deployment trustees can refer to this article: https://www.fmz.com/bbs-topic/2848

Policy logic and the realization of its automated transactions:

Def onTick (context, bars): obj = ext.NewPositionManager () # use the inventor quantitative transaction class library # here to obtain position information positions = exchange.GetPosition () # get the position array if len (positions) = = 0: # if the length of the position array is 0 return 0 # it is proved to be short Return 0 for i in range (len (positions)): # iterate the position array if (positions [I] ['Type'] = = PD_LONG) or (positions [I] [' Type'] = = PD_LONG_YD): position_long = 1 # Mark position_long as 1 elif (positions [I] ['Type'] = PD_SHORT) or (positions [I] [' Type'] = PD_SHORT_YD ): position_short =-1 # Mark position_short as-1 bar = bars [0] # according to the price (- 40) -3], (- 3), (- 2), (- 2), (2), (3), (3) to get the price range of the latest closing price grid = pd.cut ([close_01], context.band, labels= [0, 1, 2). 4]) [0] # if there is no position and the price breaks through, open the position according to the set interval if not position_long and not position_short and grid! = 2: # greater than 3 is above the middle grid, long if grid > = 3: obj.OpenLong ("rb2005", 1) # open multiple positions at the market price if grid = 3: obj.OpenLong ("rb2005") 1) # take the market price monotonous long position # equals 2 as the middle grid, close the position elif grid = = 2: obj.closebuy ("rb2005", 1) # take the market price order fully closed long position # less than 1 as the bottom of the intermediate grid, short elif grid = 3: obj.OpenLong ("rb2005") 1) # opening multiple positions at market price if grid = 3: obj.OpenLong ("rb2005", 1) # monotonous multiple positions at market price # equals 2 in the middle grid, closing elif grid = 2: obj.closebuy ("rb2005", 1) # closing multiple positions at market price # less than 1 is below the middle grid Short elif grid

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