C# Bitcoin Grid Trading Strategy Implementation and Backtest Analysis - Based on OKex Exchange

Grid trading method or grid trading strategy, there are many introductions on the Internet, here is the strategy and programmatic trading of Bitcoin grid trading using C#, and using the K-line data on OKex to conduct backtest verification, and also Visual K-line display. The grid trading strategy is simply a strategy of buying low and selling high. The meaning of grid refers to the means of controlling the buying and selling range and positions. For example, here I have 80,000 funds, the total number of grids is defined as 4 grids, the interval of each grid is defined as 5%, and the benchmark price of Bitcoin is 40,000 yuan. The position of each grid of the strategy is 20,000 yuan (80,000 divided by 4), and one grid is bought for every 5% drop, that is, the first grid is bought when Bitcoin falls to 38,000, and the buying amount is 20,000 yuan. In this way, when it falls to 36,000 yuan, buy the second grid, the position is also 20,000 yuan, the third grid, the fourth grid and so on. This is a buying strategy. The selling strategy is also strictly carried out in accordance with the strategy set for each grid. The strategy is the opposite of buying, but the defined selling price is not based on the strategy of selling every time the price rises. Strategy. For example, the buying price of the last bar is 32000, and the selling price for every 3 bars is 32000+32000*0.15=36800 yuan. Base price and grid width are flexible and adjustable during program development. The specific positions and trading points are as follows: 

lattice buy price 5% width position selling price
first frame 38000 -5% 20000 43700
second grid 36000 -10% 20000 41400
third grid 34000 -15% 20000 39100
fourth grid 32000 -20% 20000 36800

The entire program is developed in C#. In addition to implementing grid strategy implementation and backtesting code, in order to perform strategy backtesting, K-line data such as 1 minute, 3 minutes, 5 minutes, 1 day, and 3 days on OKex are also obtained. . The 1-day K-line data starts from October-11, 2017. As shown in the figure:

 The grid definition and the main backtest interface are in the same window. Strategy definition and generation mainly include asset type, currency selection, moving average (you can select the benchmark price through the moving average) selection, total position, buy and sell position, network The number of grids, grid width and stop loss point (currently no stop loss control) and other parameters, here we test the total number of grids we selected 4 grids, the width is 0.05 (ie 5%), click the "Generate grid" button will Generate four grid strategy data, each of which mainly includes buy point price and sell point price. As shown below: 


The backtest function includes the time selection of the backtest data, including the start time and end time, as well as the K-line data type of the test, such as 1 day, 30 minutes, etc. The backtest results include the number of transactions, the total profit, the percentage of profit, the transaction list, etc. data. The transaction list mainly includes the buying point price, buying point time, selling point price, selling point time, and transaction cost of each transaction (here is calculated by the 0.02% handling fee for each transaction on OKex). The backtest data we choose here is the time period from 2017-12-17 to 2018-4-14, the daily data is used as the backtest data, the total capital is 200,000, the grid is 4 grids, and the grid width is 3%. As a result of backtesting, there were 17 transactions in total, with a profit of 47600, 23.8%; the current quilt was covered 3 times, with a loss of 2569, 1.2%; after removing the loss, the total profit of the backtest: 45030.8, 22.5%. As shown below: 


K-line analysis, the grid buying point, grid selling point and actual buying point and actual selling point are visually marked on the K-line, such as: 


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324451522&siteId=291194637