Commodity Futures R-Breaker Strategy
Tags: strategy, fmzquant, futures, commodity
Published: 2024-06-13 06:25:56

## Summary
The R-Breaker strategy was developed by Richard Saidenberg and published in 1994. It was selected as one of the top ten most profitable trading strategies by "Futures Truth" magazine in the United States for 15 consecutive years. Compared with other strategies, R-Breaker is a trading strategy that combines trend and trend-reversal. Not only can the trend market be captured to obtain large profits, but also when the market trend reverses, it can take the initiative to take profit and to track the trend reverse.

## Resistance and support
Simply put, the R-Breaker strategy is a price support and resistance strategy. It calculates seven prices based on yesterday's highest, lowest and closing prices: a central price (pivot) and three support levels (s1 s2, s3), three resistance levels (r1, r2, r3). Then according to the positional relationship between the current price and these support and resistance levels, to form the trigger conditions for buying and selling, and through a certain algorithm adjustment, adjust the distance between these seven prices, further change the trigger value of the transaction.

- Break through buying price (resistance level r3) = yesterday’s highest price + 2 * (center price - yesterday’s lowest price) / 2

- Observing selling price (resistance level r2) = center price + (yesterday's highest price - yesterday's lowest price)

- Reverse selling price (resistance level r1) = 2 * Center price - yesterday's lowest price

- Central price (pivot) = (yesterday's highest price + yesterday's closing price + yesterday's lowest price) / 3

- Reverse buying price (support level s1) = 2 * Central price - yesterday's highest price

- Observing buying price (support level s2) = central price - (yesterday's highest price - yesterday's lowest price)

- Break through selling price (support level s3) = yesterday’s lowest price - 2 * (yesterday’s highest price - center price)

From this we can see that the R-Breaker strategy draws a grid-like price line based on yesterday's price, and updates these price lines once a day. In technical analysis, the support and resistance levels, and the role of the two can be converted to each other. When the price successfully breaks up the resistance level, the resistance level becomes the support level; when the price successfully breaks down the support level, the support level becomes the resistance level.

In actual trading, these support and resistance levels indicate to the trader the direction of opening and closing positions and the precise trading points. Traders with specific opening and closing conditions can flexibly customize according to intraday prices, central prices, resistance levels, and support levels, and can also manage positions based on these grid price lines.

## Strategy logic
Next, let us see how the R-Breaker strategy uses these support and resistance levels. Its logic is not complicated at all. If there is no holding position, enter the trend mode. When the price is greater than the break-through buying price, open long position; when the price is less than the break-through selling price, open short position.

- Trend mode
open long position: if there is no holding position and the price is greater than the breakthrough buying price

open short position: if there is no holding position and the price is less than the breakthrough selling price

close long position: if you holding a long position, and the highest price of the day is greater than the observing selling price, and the price is less than the reverse selling price

close short position: if you holding a short position, and the lowest price of the day is less than the observing buying price, and the price is greater than the reverse buying price

- Reverse mode
open long position: if you holding a short position, and the lowest price of the day is less than the observing buying price, and the price is greater than the reverse buying price

open short position: if you holding a long position, and the highest price of the day is greater than the observing selling price, and the price is less than the reverse selling price

close long position: if long positions are held and the price is less than the breakthrough selling price

close short position: if short position are held and the price is greater than the breakthrough buying price

If there are holding positions, it enters the reversal mode. When there are holding long positions, and the highest price on the day is greater than the observing selling price, and the price falls below the reverse selling price, the long position will be closed and the short position will be open synchronously. When holding short positions, and the lowest price of the day is less than the observing buying price, and the price breaks through the reverse buying price, the short position will be closed and long position will be open.

## Strategy writing
```
'''backtest
start: 2019-01-01 00:00:00
end: 2020-01-01 00:00:00
period: 5m
exchanges: [{"eid":"Futures_CTP","currency":"FUTURES"}]
'''

# Strategy main function
def onTick():
    # retrieve data
    exchange.SetContractType(contract_type)   # Subscribe to futures products
    bars_arr = exchange.GetRecords(PERIOD_D1)  # Get daily K line array
    if len(bars_arr) < 2:  # If the number of K lines is less than 2
        return
    yesterday_open = bars_arr[-2]['Open']     # Yesterday's opening price
    yesterday_high = bars_arr[-2]['High']     # Yesterday's highest price
    yesterday_low = bars_arr[-2]['Low']       # Yesterday's lowest price
    yesterday_close = bars_arr[-2]['Close']   # Yesterday's closing price

    # Calculation
    pivot = (yesterday_high + yesterday_close + yesterday_low) / 3 # Pivot point
    r1 = 2 * pivot - yesterday_low # Resistance level 1
    r2 = pivot + (yesterday_high - yesterday_low) # Resistance level 2
    r3 = yesterday_high + 2 * (pivot - yesterday_low) # Resistance level 3
    s1 = 2 * pivot - yesterday_high  # Support level 1
    s2 = pivot - (yesterday_high - yesterday_low)  # Support level 2
    s3 = yesterday_low - 2 * (yesterday_high - pivot)  # Support level 3

    today_high = bars_arr[-1]['High'] # Today's highest price
    today_low = bars_arr[-1]['Low'] # Today's lowest price
    current_price = _C(exchange.GetTicker).Last # Current price

    # Get positions
    position_arr = _C(exchange.GetPosition)  # Get array of positions
    if len(position_arr) > 0:  # If the position array length is greater than 0
        for i in position_arr:
            if i['ContractType'] == contract_type:  # If the position variety equals the subscription variety
                if i['Type'] % 2 == 0:  # If it is long position
                    position = i['Amount']  # The number of assigned positions is positive
                else:
                    position = -i['Amount']  # The number of assigned positions is negative
                profit = i['Profit']  # Get position profit and loss
    else:
        position = 0  # The number of assigned positions is 0
        profit = 0  # The value of the assigned position is 0
        
    if position == 0:  # If there is no position
        if current_price > r3:  # If the current price is greater than Resistance level 3
            exchange.SetDirection("buy")  # Set transaction direction and type
            exchange.Buy(current_price + 1, 1)  # open long position
        if current_price < s3:  # If the current price is less than Support level 3
            exchange.SetDirection("sell")  # Set transaction direction and type
            exchange.Sell(current_price - 1, 1)  # open short position
        
    if position > 0:  # if holding long position
        if today_high > r2 and current_price < r1 or current_price < s3:  # If today's highest price is greater than Resistance level 2, and the current price is less than Resistance level 1
            exchange.SetDirection("closebuy")  # Set transaction direction and type
            exchange.Sell(current_price - 1, 1)  # close long position
            exchange.SetDirection("sell")  # Set transaction direction and type
            exchange.Sell(current_price - 1, 1)  # open short position

    if position < 0:  # if holding short position
        if today_low < s2 and current_price > s1 or current_price > r3:  # If today's lowest price is less than Support level 2, and the current price is greater than Support level 1
            exchange.SetDirection("closesell")  # Set transaction direction and type
            exchange.Buy(current_price + 1, 1)  # close short position
            exchange.SetDirection("buy")  # Set transaction direction and type
            exchange.Buy(current_price + 1, 1)  # open long position

            
# Program main function
def main():
    while True:     # loop
        onTick()    # Execution strategy main function
        Sleep(1000) # Sleep for 1 second
```
## Copy complete strategy
The complete strategy has been published on FMZ platform (FMZ.COM), click the link below to copy it directly, and you can backtest without configuration: https://www.fmz.com/strategy/187009

## Summary
The reason why the R-Breaker strategy is popular is that it is not purely a trend tracking strategy, but a compound strategy to earn both trend alpha and reverse alpha income. The strategy in this article is only for demonstration, without optimizing the appropriate parameters and varieties. In addition, the complete strategy must also include the stop loss function, and interested friends can improve it.

From: https://blog.mathquant.com/2020/06/02/commodity-futures-r-breaker-strategy.html