The first few articles to learn so many coin circle concepts, programming, quantitative trading basic concepts. Now it’s time to get down to the strategy itself, and let’s learn how to implement a simple strategy. For the [grid strategy], the students who do trading should have heard of it, and it doesn’t matter if they haven’t heard of it. Now the major exchanges have launched their own programming and quantitative trading functions, and the most simple and easy-to-use strategy is the grid strategy. However, the functions and details of the grid strategy provided by each exchange are different, since the plan is to enter the coin circle quantification. Why don’t we go ahead and implement a grid strategy ourselves?

At this time, some students may say: “can’t write code!” “, “see the generation dock big!

That’s true. It is very difficult for students who are not majoring in computer software and have not been engaged in programming to develop a complete trading strategy by themselves. This is because you have to do a series of pre-processing work starting with the exchange interface (maybe your trading logic program is only 100 lines, but the other coding work is quite a bit more difficult than writing the trading logic).

At this point, if you have a handy tool, it’s pretty easy, at least 70% less difficult. You can imagine how much easier it would be if you just wrote the trading logic itself and the rest of the exchange interface docking, signature verification, configuration files, runtime setup, UI writing, interaction writing, etc.

Don’t believe it? Let’s try it out!

Implement a simple off-the-shelf grid strategy

The tool we use: Inventor Quant Trading platform (FMZ.COM). The design core of grid strategy is actually the logic of buying and selling grid, so this is the thing that must be made clear before designing strategy. Our goal is to make the policy simple to understand, so as few parameters and as simple logic as possible. Here is the basic flow of the design strategy:

1. Summary of policy requirements

Basically, what your policy does, how it does, what it does, etc., can be written in a document (notepad or something) before you actually write the policy code. Developing a strategy on FMZ is very simple, the platform provides you with solutions to these requirements, and I don’t have to write them down in a notepad (which is not very easy to manage). I write the strategic requirements directly in the strategy notes.

After writing the policy, remember to save the policy, and then we will write the policy requirements (policy requirements are not fixed, as well as as development can be documented).

  • The strategy is designed for spot trading strategy, and the trading pair isXXX_USDT, such as:BTC_USDT.
  • The grid is designed to be evenly spaced, which means that the distance between adjacent points in the grid is a fixed price difference.
  • The grid is designed to be an infinite grid that can be expanded indefinitely.
  • Order using market price list.

2. Construct grid data structure:

For unclear ideas, we can start by drawing and analyzing.

You can construct a grid in both directions from the starting price as a base point. The so-called grid is a layer of buying line, selling line. Through the chart, we can find that each line has two possibilities: 1. The above price indicates that the price is going up, you need to sell, and then wait for the price to fall to buy a profit. 2, the price below wear. A lower price indicates that the price is low, you need to buy, and then wait for the price to rise to sell at a profit.

So each grid line can be traded in two ways: buy and sell. And each grid line has an inherent property, which is the price that the line marks. Take for example the representation of A/B/C/D in the figure. When we design a strategy, we first figure out what we want, and then we can do it. Write a function that constructs a grid data structure:

Function createNet(begin, diff) {// begin,diff is the parameter, begin is the initial price, Var oneSideNums = 10 // 10 lines are generated on each side of the grid (AB side,CD side), Var up = [] var down = [] var down = [] for (var I = 0; i < oneSideNums ; Var upObj = {// construct an upbound "gridline" data structure buy: Sell: false, // sell mark.... price : Begin + diff / 2 + I * diff, // } up. Push (upObj); var j = (oneSideNums - 1) -i} 9 ~ 0 var downObj = { buy : false, sell : false, price : begin - diff / 2 - j * diff, } if (downobj.price <= 0) {// price cannot be less than or equal to 0 continue} down.push(downObj)} return down.concat(up) // Add up after down to form a grid array with prices from small to large}Copy the code

You can run this function separately to see how it works. FMZ on the [debugging tool] or [back test system] are very convenient to debug this kind of small code.

You can look at the constructed data.

[ {"buy":false,"sell":false,"price":5}, {"buy":false,"sell":false,"price":15}, {"buy":false,"sell":false,"price":25}, {"buy":false,"sell":false,"price":35}, {"buy":false,"sell":false,"price":45}, {"buy":false,"sell":false,"price":55}, {"buy":false,"sell":false,"price":65}, {"buy":false,"sell":false,"price":75}, {"buy":false,"sell":false,"price":85}, {"buy":false,"sell":false,"price":95}, {"buy":false,"sell":false,"price":105}, {"buy":false,"sell":false,"price":105}, Spacing of 10 {" buy ": false," sell ": false," price ": 115}, / /... {" buy" : false, "sell" : false, "price" : 125}, {"buy":false,"sell":false,"price":135}, {"buy":false,"sell":false,"price":145}, {"buy":false,"sell":false,"price":155}, {"buy":false,"sell":false,"price":165}, {"buy":false,"sell":false,"price":175}, {"buy":false,"sell":false,"price":185}, {"buy":false,"sell":false,"price":195} ]Copy the code

3. Transaction logic analysis

After analyzing the data structure of the grid, we should consider the specific buying and selling logic of the grid strategy. In fact, the logic of buying and selling is very simple. As we have shown above, buying is going down a certain line, and selling is going up a certain line. How do you mean wear up and wear down? Again, it’s easy to just compare the price position at two points in time. I’m going to do the same thing.

T1 is a time, t2 is a time after T1, and to judge the line through C, we just need to judge P1 < C and P2 > C. Similarly, to judge the line going down through B, we just need to determine P1 is greater than B and P3 is less than B. Then we just need to iterate (in layman’s terms, iterate is to look at each line in the grid array), to judge up and down. Isn’t that easy?

Caught the price on the wear, wear the action, is it possible to place orders when these actions are triggered? It is no good for certain obviously, if the price is on a line up and down repeatedly wear below wear, that is to be traded repeatedly on a price to burn poundage. For example, {“buy”:false,”sell”:false,”price”:5}).

Thanks for reading, and we’ll continue next time.