Quantitative typing rate trading strategy
Tags: trading, strategy, cryptocurrency, fmzquant
Published: 2024-06-19 02:25:07

## About us
We are a team that has been committed to researching quantitative trading strategies for a long time.

Last year, we have achieved a excellent results in the Tokeninsight Quantitative Contest.

Thanks you FMZ community for providing such a platform.
In order to better support the construction of quantitative communities, the design concept and design ideas of this strategy are now publicly published here.
I hope you can learn the design and application of quantitative trading.

## The origin of the quantitative rate trading strategy
The inspiration for the quantitative typing rate system is mainly from physics

The definition of speed in physics is: the distance moved per unit time.
If you regard price as distance, then in the financial market, the definition of speed is the size of price change per unit time.

If the price changes greatly in a unit time, such a market is usually called a rapid market; if the price changes in a unit time is small, such a market is called a slow market. Therefore, speed is a natural law that integrates time and price. A deep understanding of speed can help us understand the market to a greater extent.

If the rate increases, it means that the energy is increasing and can effectively predict the upward trend of the market.

If the rate drops, it means energy failure and the risk of flat or falling market conditions can be perceived.

Each transaction uses a certain number of lots for the transaction, so it is called a quantitative type rate trading system.

## Knowledge to be prepared
Highest price (HHV): The highest price reached in a specific period.
Lowest price (LLV): The lowest price reached in a specific period.
Moving Average (MA): A line connecting the average closing price of a specific period.
Slope of regression (SLOPE): The slope of a linear regression with a specific period. (That’s what we call rate)

The linear equation OLS slope formula is as follows:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i7zmy29oo9kt6ai2em8c.png)

The mathematical formula is very complicated, but the FMZ platform has already written the grammar formula (SLOPE) of the M language for us.

We can see that the algorithm is as follows:

- SLOPE
SLOPE, the slope of linear regression. SLOPE(X,N): Obtains the slope of the linear regression for the N periods of X.

Note:
1. N includes the current k-line.
2. N is a valid value, but if the current number of k-lines is less than N, the function will return a null value.
3. If N is 'e', the function will return a null value.
4. If N is a null value, the function will return a null value.
5. N can be a variable.

Example:
Using the least squares method to calculate the value of SLOPE(CLOSE,5) on the most recent k-line:
1. Establish a univariate linear equation: close = a + slope*i + m
2. Estimated value of close: close(i)^ = a + slope*i
3. Calculate the residual: m = close(i) - close(i) = close(i) - a - slope*i
4. Sum of squared errors:
Q = m(1)m(1) + ... + m(5)m(5) = [close(1) - a - slope1][close(1) - a - slope1] + ... + [close(5) - a - slope5][close(5) - a - slope5]
5. Take the first-order partial derivative of the parameters a and slope in the linear equation:
2*{[close(1) - a - slope1] + ... + [close(5) - a - slope5]}(-1) = 0
2{[close(1) - a - slope1] + ... + [close(5) - a - slope5]}*(-5) = 0
6. Solve the above two equations simultaneously to find the value of slope:
slope = {[5close(1) + ... + 1close(5)] - [close(1) + ... + close(5)](1+2+3+4+5)/5} / [(11 + ... + 5*5) - (1+...+5)(1+...+5)/5]

The above formula can be expressed using the Ma Language function as follows:
((5C + 4REF(C,1) + 3REF(C,2) + 2REF(C,3) + 1REF(C,4)) - SUM(C,5)(1+2+3+4+5)/5) / ((SQUARE(1) + SQUARE(2) + SQUARE(3) + SQUARE(4) + SQUARE(5)) - SQUARE(1+2+3+4+5)/5)

Example:
SLOPE(CLOSE,5); represents finding the slope of the 5-period linear regression line of the closing price.

The process is a bit more complicated, but everyone does not have to think about it. Just call the formula directly.

## Indicator design:
1. First calculate the highest and lowest prices in a certain period of time
2. Take the average of these 2 prices
3. Calculate a moving average line on the average
4. Find the regression slope of the moving average
```
len:=35;//Design cycles

hh^^HHV(H,len);//Take the highest price in a certain period
ll^^LLV(L,len);//Take the lowest price in a certain period
hl2^^(hh+ll)/2;//Average of highest price and lowest price
avg^^MA(hl2,5);//Calculate the moving average line of the average

ss:SLOPE(avg,len);//Calculate the regression slope of the moving average line
```
Through the design of indicators, we can see that in the main chart, we have the highest point (yellow line), the lowest point (green line), their average (red line), and the Smoothed price moving average calculated by red line (thick purple line)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jkjyl73hcklqhar3at0l.png)

Then we can calculate the regression slope ss in the attached figure, which represents the rising and falling rate of the moving average.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/69jssnqw7r38j8uhslai.png)

## Trading strategy design
As can be seen from the figure above, the green arrows indicate the inflection points at the lowest slope, and the orange arrows indicate the inflection points at the highest slope.

The reaction along the chart is on the k line, and the weakening of the rise and the weakening of the decline can also be clearly felt.

If you buy and sell at the inflection point, you can effectively operate the trading at the early stage, instead of chasing the rise or the fall at the high or low point.

- The design idea is:
The rising slope means that the market momentum is increasing, which may stop falling or start to rise.
The continuous decline of the slope means that the market momentum is weak, and may stop rising or start to fall.

The design and expression using M language are as follows:
```
len:=35;//Design cycles

hh^^HHV(H,len);//Take the highest price in a certain period
ll^^LLV(L,len);//Take the lowest price in a certain period
hl2^^(hh+ll)/2;//Average of highest price and lowest price
avg^^MA(hl2,5);//Calculate the moving average line of the average

ss:SLOPE(avg,len);//Calculate the regression slope of the moving average line


ss<REF(ss,1),SPK;//When the slope becomes smaller, it indicates that the market momentum is weakened, close long positions and open short positions.
ss>REF(ss,1),BPK;//When the slope becomes larger, it indicates that the market momentum is enhanced, close short positions and open long positions.
AUTOFILTER;
```
## Backtest and summary
In this way, we have completed the design of this algorithm, and then we will use the system to backtest the situation for one year.

The subject matter is okex quarterly contract btc;

The backtest period is from January 1, 2019 to present, and the time period is 1 hour;

3 btc for initial account, handling fee of 0.05%;

Set a fixed number of 200 lots per transaction.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9ojrmjv3z2tulgyne7r5.png)

It can be seen from the backtest that this income is relatively smooth and stable.

In this backtest, 1261 transactions were made throughout the year;
Estimated income of 4.68 crypto currency;
Annualized income is about 140%;
The maximum drawdown is 14%;
Sharpe ratio is 0.117.

## Source code sharing
Click to go to copy strategy https://www.fmz.com/strategy/183416

The above sharing is some ideas and content of my design, the following is the entire code of M language,
For your reference, study and research. If you need to reprint, please indicate the source. Thank you.
```
len:=35;//Design cycles

hh^^HHV(H,len);//Take the highest price in a certain period
ll^^LLV(L,len);//Take the lowest price in a certain period
hl2^^(hh+ll)/2;//Average of highest price and lowest price
avg^^MA(hl2,5);//Calculate the moving average line of the average

ss:SLOPE(avg,len);//Calculate the regression slope of the moving average line


ss<REF(ss,1),SPK;//When the slope becomes smaller, it indicates that the market momentum is weakened, close long positions and open short positions.
ss>REF(ss,1),BPK;//When the slope becomes larger, it indicates that the market momentum is enhanced, close short positions and open long positions.
AUTOFILTER;
```

From: https://www.fmz.com/digest-topic/5951