Hello everyone! This post is all about implementing a Grid Trading strategy on ALGOGENE. If you are interested to learn how this strategy works or if you are a new joiner of the platform, this post is for you!
What is Grid Trading?
A Grid Trading Strategy places a set of buy and sell orders in regular intervals above and below the current price. As the price ranges up and down, long and short orders are being executed and will generate a profit as the price continues to move up and down the grid.
There are 2 versions of grid strategy for
- Ranging Market
- Trend Market
For more details, you can refer to my previous post "The Opportunities of a Grid Trading System".
In this post, I will show you how to implement a grid trading strategy for ranging market. The trading logic goes as follows:
- Define our grid strategy parameters:
- Grid Size: 1%
- Number of grid: 5
- Take profit: 1% above and below the grid
- Stop loss: 1% above and below the grid
- Grid reset period: 7 days
- We will use the Chop Indicator to determine whether the current market is trend or ranging.
- If current market is ranging, then we open 5 buy and 5 sell limit orders above and below the current market price, and also to set correponding take profit and stoploss level 1% above and below the limit price.
- We will reset the grid every 7 days and cancel any unfilled limit orders.
Step 1. Calculate Chop series
First of all, we define the grid parameters at initialization.
We use the API function getHistoricalBar to collect historical observations. Then, we apply a python library 'pandas_ta.chop' to calculate the chop series.
Step 2. Determine ranging market and setup grid
The chop indicator is a value between 0 to 100. It is said to be in a ranging market when the value is closed to 100, while it is a trend market if the value is closed to 0.
Thus, in below logic (i.e. line 21-23), we regard the market is in a ranging zone when the previous chop value is below 50 and current value is above 50.
The setup of limit buy order grid refer to line 32-45; limit sell order grid in line 47-60.
Step 3. Reset grid
In our strategy logic, we will reset our grid every 7 days. In our "reset_grid()" function, it will cancel all unfilled orders..
Full Source Code
Combining all above, the full script is presented below.
Let's try backtesting the script.
- Instrument: BTCUSD
- Period: 2021.01 - 2021.12
- Initial Capital: US$100,000
- Data Interval: 1-hour bar
- Allow Shortsell: True