Multi-level percentage take profit strategy
Tags: profit, trading, strategy, fmzquant
Published: 2024-06-11 02:25:11

## Summary
Stanley Kroll mentioned in his book "Kroll on Futures Trading Strategy" that his taking profit methods is divided into three parts: when the target price is reached, one third of the position will be closed; another one third of position will be closed when the long-term resistance and support price range are broken; the last one third of position will follow the trend until the stop loss be triggered.

The strategy shared by this article is based on this principle. The moving average line is used as the direction of the trend. The relationship between the closing price, the highest price and the lowest price is used as the signal to open the position. Under the premise that the price trend has not changed significantly, proactively take profit in batches according to the percentage.

## Why do we need stop loss and take profit?
There is an old saying in the trading world: "everyone knows how to open positions, only pros know how to close positions." As these words suggests, how to leave the market is the key factor of trading, because when you opening positions, you only need to judge whether the market trend starts. But once you jump into the market, you need to judge whether the trend is turning or not, and you need to control the risk at all times. I believe that many traders have experienced the "roller coaster" market, you jump into the train and ended up with a small profit or even a loss.

Simply put, closing position are nothing more than two situations: take profit and stop loss. For example, If you are lucky, the price starts to rise after buying. At this time, you must consider the problem of take profit. Otherwise, we may only make money on the "look", didn't take profit at the right place, and finally lose it. If you are not lucky, the price will start to fall shortly after the buying. At this time, you should consider the stop loss, or you should consider the stop loss options before you open the position, otherwise the small loss will accumulate a large loss.

From a statistical point of view, most of the loss positions will return to the cost price in the future market. However, if you encounter a small probability of a large reverse trend, you may lose all previous profits or even the whole fund. Therefore, for our retail investors, we can make big profits; we can make small profits; we can preforming small losses, but we can never lose big money. In a word: Stop loss keeps you alive, and take profit makes you live better.

## Strategy logic
Sometimes when we intuitively take profit, there may be a large wave of market price movement that we only earn a small amount of it. Although it's not a fail transaction, there will be a kind of regret from the mentality, so this strategy will using the multi-level take profit method, that is to say, when the floating profit reaches 5%, the first-level active take-profit mode is turned on. Once the 100% is withdrawn from the highest point of the floating profit, take the profit and close position; when the floating profit reaches 10%, the second-level active take-profit mode is activated. Once the 50% is retreated from the highest point of the floating profit, take the profit and close position; When the floating profit reaches 20%, the three-level active profit-taking mode is activated. Once the 20% is withdrawn from the highest point of the floating profit, take the profit and close position. This way not only protects the floating profit, but also does not only earn a little when the big market trend comes. Here is the logic of this strategy:

- Define the upper rail

- Define the lower rail

- Define the moving average

- Long position open: the closing price is greater than the upper rail, and the upper rail is greater than the moving average

- Short position open: closing price is lower than lower rail, and lower rail is smaller than moving average

- Long position close: The closing price is lower than the lower track, or the closing price is less than the moving average

- Short position close: The closing price is greater than the upper rail, or the closing price is greater than the moving average

- Level 1 long position take profit: the highest price after opening the position is greater than or equal to the opening price multiplied by the first level of profit start, and the lowest price is less than or equal to the highest price after opening the position minus the floating profit multiplied by the first level take profit trigger value

- Level 2 long position take profit: the highest price after opening the position is greater than or equal to the opening price multiplied by the second level of profit start, and the lowest price is less than or equal to the highest price after opening the position minus the floating profit multiplied by the second level take profit trigger value

- Level 3 long position take profit: the highest price after opening the position is greater than or equal to the opening price multiplied by the third level of profit start, and the lowest price is less than or equal to the highest price after opening the position minus the floating profit multiplied by the third level take profit trigger value

- Level 1 short position take profit: the lowest price after opening the position is less than or equal to the opening price multiplied by the first level of profit start, and the highest price is greater than or equal to the lowest price after opening the position plus the floating profit multiplied by the first level take profit trigger value.

- Level 2 short position take profit: the lowest price after opening the position is less than or equal to the opening price multiplied by the second level of profit start, and the highest price is greater than or equal to the lowest price after opening the position plus the floating profit multiplied by the second level take profit trigger value.

- Level 3 short position take profit: the lowest price after opening the position is less than or equal to the opening price multiplied by the third level of profit start, and the highest price is greater than or equal to the lowest price after opening the position plus the floating profit multiplied by the third level take profit trigger value

- Long position Stop Loss: The closing price is less than or equal to the opening price multiplied by the stop loss factor

- Short position Stop Loss: The closing price is less than or equal to the opening price multiplied by the stop loss factor

## Strategy code
Based on the above strategy logic, we can implement this strategy on the FMZ Quant platform. Open: fmz.com > Login > Dashboard > Strategy Library > New Strategy > Click on the drop-down menu in the upper right corner to select My language, start writing the strategy, and pay attention to the comments in the code below.

First, the parameters that need to be used in the strategy: the average line length, the stop loss range, the take profit parameter, etc., are all defined as external parameters to facilitate test debugging and optimization.
```
/ / Define parameters
LENGTH := 100; // moving average parameter
STOP_LOSS := 3; // Stop Loss range

// Define the take profit parameter
STARTPER1 := 5; // Level 1 tracking take profit, start from profit reaches 5%
STOPPER1 := 100; // Level 1 tracking take profit, profit retracement 100% triggers it
STARTPER2 := 10; // Level 2 tracking take profit, start from profit reaches 10%
STOPPER2 := 50; // Level 2 tracking take profit, profit retracement 50% trigger it
STARTPER3 := 20; // Level 3 tracking take profit, start from profit reaches 20%
STOPPER3 := 20; // Level 3 tracking take profit, profit retracement 20% trigger
```

Next, set a price range based on the price of today and yesterday, and the fluctuation of the price yesterday. Through this price range and the relative positional relationship with the moving average, not only the buying and selling open positions signal can be tracked well, but also reduce the number of open positions in the shock period and the magnitude of the withdrawal as well.
```
/ / Define the upper and lower intervals
NN := BARSLAST(DATE <> REF(DATE, 1)) + 1; // current number of cycles
TODAY_OPEN := VALUEWHEN(NN = 1, O); // Opening price of the day
TODAY_HIGH := HHV(H, NN); // The highest price of the day
TODAY_LOW := LLV(L, NN); // lowest price of the day
YESTERDAY_HIGH := REF(TODAY_HIGH, NN); // Yesterday's highest price
YESTERDAY_LOW := REF(TODAY_LOW, NN); // yesterday's lowest price
BAND := YESTERDAY_HIGH - YESTERDAY_LOW; // Yesterday amplitude
UPPERLINE : TODAY_OPEN + BAND; // upper line
LOWERLINE : TODAY_OPEN - BAND; // lower line
MYMA:MA(CLOSE, LENGTH); // Moving average
```

Then, it is the logic code for opening and closing positions. When the closing price is greater than the upper rail and the upper rail is greater than the moving average, open the long position; when the closing price is lower than the lower rail and the lower rail is smaller than the moving average, open the short position; the closing position condition is just opposite to the opening position condition: when the closing price is less than The lower rail, or the closing price is less than the moving average, close long position; when the closing price is greater than the upper rail, or the closing price is greater than the moving average, close short position.
```
// open the position
C > UPPERLINE AND UPPERLINE > MYMA, BK; // Open long position
C < LOWERLINE AND LOWERLINE < MYMA, SK; // Open short position

// close the position
C < LOWERLINE OR C < MYMA, SP; // Close long position
C > UPPERLINE OR C > MYMA, BP; // Close short position
```

Finally, it's the stop-loss and take-profit part we mentioned in this article. Whether it is long or short position to take profit, it is divided into three stages. Each stage is automatically adjusted according to current market price fluctuations and profitability. And this adjustment is set to external parameters, you can make fine adjustments according to different market conditions and variety status.

Stop loss is also a part of our strategy need to consider, because it is impossible to make money by opening any position. Sometimes the market is contrary to our expectations, so stop loss is absolutely necessary. The stop lost of this article is simple and "violent", that is, when the floating loss reaches a certain level, all positions will be closed.
```
// long position take profit
BKHIGH >= BKPRICE * (1 + 0.01 * STARTPER1) AND LOW <= BKHIGH - (BKHIGH - BKPRICE) * 0.01 * STOPPER1, SP;  // level 1
BKHIGH >= BKPRICE * (1 + 0.01 * STARTPER2) AND LOW <= BKHIGH - (BKHIGH - BKPRICE) * 0.01 * STOPPER2, SP;  // level 2
BKHIGH >= BKPRICE * (1 + 0.01 * STARTPER3) AND LOW <= BKHIGH - (BKHIGH - BKPRICE) * 0.01 * STOPPER3, SP;  // level 3

// short position take profit
SKLOW <= SKPRICE * (1 - 0.01 * STARTPER1) AND HIGH >= SKLOW + (SKPRICE - SKLOW) * 0.01 * STOPPER1, BP;  // level 1
SKLOW <= SKPRICE * (1 - 0.01 * STARTPER2) AND HIGH >= SKLOW + (SKPRICE - SKLOW) * 0.01 * STOPPER2, BP;  // level 2
SKLOW <= SKPRICE * (1 - 0.01 * STARTPER3) AND HIGH >= SKLOW + (SKPRICE - SKLOW) * 0.01 * STOPPER3, BP;  // level 3

// stop loss
C <= BKPRICE * (1 - STOP_LOSS * 0.01), SP;  // long position
C >= SKPRICE * (1 + STOP_LOSS * 0.01), BP;  // short position
```

In addition, we also set the order delegation method, as well as signal filtering, to make the processing more complete.
```
// Set the order commission method
SETSIGPRICETYPE(BK,NEW_ORDER);
SETSIGPRICETYPE(SK,NEW_ORDER);
SETSIGPRICETYPE(BP,NEW_ORDER);
SETSIGPRICETYPE(SP,NEW_ORDER);

// Set the signal filtering method
AUTOFILTER;
```

## Strategy backtest
Test environment

- Trading variety: rebar index
- Time: February 22, 2015 ~ September 18, 2019
- Cycle: one hour
- Slippage: 2 pips for opening and closing positions price
- Fee: 2 times of the normal exchange standard

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

**Performance report**

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

**Fund curve**

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

## Copy strategy
Click to copy the full strategy source without configuring https://www.fmz.com/strategy/166753

From: https://blog.mathquant.com/2019/09/25/multi-level-percentage-take-profit-strategy.html