Second Strategy: Automated Leveraged Farming

Leveraged farming is the process of using borrowed money for farming rewards. The user borrows funds so that they can invest more, and as a result, earn more.

Second Strategy: Automated Leveraged Farming
Photo by James Baltz / Unsplash

The information presented is for educational purposes and does not constitute investment advice or a solicitation to buy or sell assets or make financial decisions.

The first strategy posted previously was the Auto-Lending Optimiser. The second strategy described here is about automated leverage farming (LF) using Tulip, Francium and similar protocols.

What is farming?

Farming is a process in which cryptocurrencies are staked to earn more of them as rewards. These earnings can be either fixed or of variable interest.

The process is similar to holding Fiat money in a traditional savings account, where funds are deposited with a bank, who then lends out the funds as loans, mortgages etc. Loans issued by banks are repaid back with interest. The lenders get their money back plus interest while banks make a margin on the interest paid/charged.

Let's imagine the bank’s deposits as a liquidity pool (LP). When a user deposits money, they add liquidity to the bank’s pool. The user gets interest back which is the reward earned for providing liquidity.

The same concept applies for farming and leveraged farming: the user contributes to a lending platform (adding liquidity) rather than a bank. Cryptocurrency that would otherwise be sitting in an exchange or decentralised exchange is lent out on DeFi protocols (or locked into smart contracts).

What is Leveraged Farming?

Leveraged farming is the process of using borrowed money for farming rewards. The user borrows funds so that they can invest more, and as a result, earn more.

An example would be if farming with X returns Y amount of tokens, then farming with 3X leverage will return 3Y amount of tokens. The "farmer" is using leverage to borrow assets in order to multiply their farming position and produce better returns.

Where do the borrowed assets come from? They come from lenders who deposit their tokens in a lending pool. Depositors earn a rate of interest, fixed or variable, on deposits from borrowers who pay interest on borrowed funds.

With leverage farming, the farmer is the borrower, so they will pay interest on any borrowed funds, plus a fee to the protocol (such as Tulip) for its services. The user is doing so with the intention that by using leverage, they will be able to earn enough returns from the farm to pay back the interest and be profitable.

This process involves two parties:

  1. lenders who deposit tokens in the lending pool to earn returns;
  2. borrowers getting tokens from the lending pool to farm with leverage

The DBLP’s LF Strategy

The Decentralised Basis Liquidity Pool’s Leverage Farming Strategy is an automated and delta-neutral process. It allows participants to farm high APR assets while reducing risk and overall exposure by hedging their positions. This strategy eliminates a substantial amount of market risk by opening long and short positions simultaneously, automatically rebalancing to remain delta neutral.

The strategy has been backtested using both 2x and 3x leverage while assessing key features available, such as auto-compound and auto-rebalancing mechanisms. In the past, users manually operating a LF strategy needed to constantly monitor their positions (long and short), borrowing fees, liquidation price and having to remember to hedge their positions when required.

Regarding liquidation risk, it is difficult for a position to be liquidated, because even if price moves in up or down, the position is automatically balanced.

The complex part of the LF strategy was the backtesting and auto-rebalancing optimisation. This process was refined by building out different scenarios and testing these against a variety of market conditions to better understand how the strategy would function during each stress test.

A number of tests were carried out looking at different ways of auto-rebalancing the position and refined the strategy to best execute for different types of leverage at 2x or 3x.

Backtest results

To backtest a leverage farm, it is necessary to replicate the mechanics of the implementation of the smart contract. Thus, one can view the farm as deploying a long and short portfolio into the liquidity pool (LP). Depending on specific combinations of desired leverage and borrowing weights on the volatile and stable assets, it is possible to build an overall portfolio that can be delta-neutral and resistant to price volatility to a certain extent.

Let’s take the example of a 3x leverage SOL-USDC farm, borrowing twice the fund value. Let’s call this the leverage amount and then the leverage amount to borrow x% of SOL and y% of USDC in such a way that  x%+y%=100%.

Each portfolio is composed of assets and debts. As x% of SOL and y% of USDC has been borrowed (hence the leverage), this becomes debt. As a side note, the cost of that debt is not cast in stone as it accrues according to the evolution of the borrowing rate for each asset, which evolves according to the utilisation rate of their respective lending pool. This can therefore impact the profitability of the farm.

On the one hand, a long portfolio is created where the leverage position (y% fund * leverage) is split into equal amounts of both SOL and USDC assets, which will be provided into the LP. Against these leverage assets, you have the y% borrowed amount in USDC.  

On the other hand, a short portfolio is also created where the leverage position (x% fund * leverage) is split into equal amounts of both SOL and USDC, which will be also provided into the LP.  Against these leverage assets, you have the x% borrowed amount in SOL.

As time goes by, each portfolio’s position in SOL and USDC in the LP changes, according to SOL’s price change. As the LP uses an automated market making mechanism, the resulting position in SOL and USDC can be calculated. Eventually, the net delta (or imbalance) between the SOL asset and SOL debt will start to increase. The higher that delta, the higher the farmer's exposure to SOL performance.

This will result in a change of the leverage or debt ratio of the farm, which can be monitored and be acted upon when reaching a certain level or threshold.

If the farm becomes under or over leverage, one can manage the farm through rebalancing, by borrowing more of the assets to bring the leverage back up to the desired level, or by adding more of the stable asset to reduce the leverage.

The following metrics are evaluated:

  • Return %  (P&L / Capital)
  • Total number of rebalancing / farm reopening during the period
  • Weekly number of rebalancing
  • Mean time between rebalancing / farm reopening
  • Maximum drawdown
  • Mean and max HighWaterMark recovery (how long it takes to return to our highest P&L)
  • Sharpe ratio (ratio of the strategy daily return over the square root of the volatility’s daily return)
  • Calmar ratio  (P&L over maximum drawdown)
  • Asset’s hourly volatility during the period
  • Mean borrow % (the amount of fresh USDC needed to rebalance the farm relative to the fund deployed)
  • Effective borrow APR (annualised effective borrow APR from the pool, in other words, the sum of both volatile borrow and stable borrow cost over average fund deployed during the period)
  • Effective return APR (annualised effective return APR from the pool, in other words, the sum of return profit over average fund deployed during the period)

P&L is also broken down into different components:

  • Borrow cost: accrued cost of borrowing the volatile asset + cost of borrowing the stable amount
  • Swap cost: accrued cost of swapping the assets when rebalancing or reopening the farm
  • Returns P&L: accrued profit from return/trading fees generated by the DeFI application.
  • Liquidity Provision P&L: what remains of the profit/loss from running the farm (evolution of the running imbalance of the farm portfolio and resulting exposure to the volatile asset)

Study #1: backtesting based on different rebalancing thresholds

Backtesting was performed with real hourly price data to simulate the expected returns for different time intervals: 3 months, 6 months and 12 months

Specific periods have been selected for backtesting the strategy in a way to cover a wide range of volatility states in the underlying assets. For instance, the 3-month period had an historical average hourly volatility of 1.14%, the 6-month period had 1.18% and the 12-month period had 1.55%.

Several strategies have been tested by implementing a farm in SOL-USDC with either 2x or 3x leverage and using different thresholds that were fed into the auto-rebalancing mechanism.

Let’s have a look at the results for each period based on relatively average APRs, both in terms of pool’s trading fees and emissions, i.e. total APR, and borrowing APRs, i.e. volatile APR and Stable APR. The backtest assumed that the APRs were constant throughout the period.

Considering primarily the results for the 3-month and 6-month periods, given lower volatility in the SOL price, ideal thresholds were able to produce an average APR from 25% to 30% using 2x leverage and from 35% to 50% using 3x leverage.

These strategies were also able to limit draw downs to less than 2% of capital and could recover in 4 to 5 days on average. The auto-rebalance mechanism therefore adds significant value as it limits the extent of drawdown and how long it takes to recover.

Note that the auto-rebalance feature needs to be calibrated according to the underlying asset’s volatility, user’s risk tolerance, pool’s APR and cost of rebalancing. The more volatile the asset, the more the farm will be rebalanced under tighter rebalancing thresholds.

This means that the cost of rebalancing will eat some of the farm profits. It also means that there is a minimum pool APR required for the strategy to be profitable as it needs to be high enough to compensate for both the cost of providing liquidity in the pool (primary factor of imbalances in the farm) and the cost of borrowing the leverage amount.

It explains why the strategies with more risk-averse features in the 12 months backtest were less profitable than the risk-taking ones.

3 months backtest

SOL-USDC 3 month backtest

6 months Backtest

SOL-USDC 6 month backtest

12 months Backtest

SOL-USDC 12 month backtest

Study #2: real-life farm performance versus backtest

As a test case, we deployed some USDC into a RAY-USDC farm and ran until a discretionary buffer amount, set aside before deploying the farm, had been exhausted.

This farm was opened on the 23rd of June of 2022 and was eventually closed on the 5th of July. It had a return of 0.82% in 12 days, equivalent to 25% APR.

The backtested results for the same period offer a very similar return 0.83% - 25% APR.

Metrics

  • farm_return: 0.83%
  • APR: 25%
  • total_rebalancing: 2
  • mean_rebalancing: 6 days
  • weekly_rebalancing: 1
  • total_closing: 1
  • Mean_closing: NA
  • weekly_closing: 1
  • sharpe_ratio: 9.68
  • calmar_ratio: 1.30
  • invested_days: 12
  • drawdown_max_pct: -0.6%
  • HWM_recovery_mean: 4 days
  • HWM_recovery_max: 4 days
  • borrow_weekly_mean: 12.2%
  • borrow_cost: -1.04%
  • swap_cost: -0.02%
  • yield_profit: -3.27%
  • lp_pnl: -1.38%
  • effective_borrow_apr: 32.8%
  • effective_yield_apr: 102.9%
  • asset_hourly_volatility: 0.9%

The chart below displays the evolution of several metrics. Imbalances start to build up within the first two days, letting our farm get a shorter exposure to RAY. The upper threshold is then reached, the auto-rebalancing mechanism thus acts by supplying additional capital into the farm (using roughly half the buffer) to bring down the farm’s leverage so it can be close to delta-neutral.

As RAY price keeps dropping (its value decreased by 25%), the farm’s long exposure is increasing and thus the farm sustains a drawdown of 0.6%, nonetheless limited in comparison to RAY. At this point, the auto-rebalancing mechanism kicks off another rebalancing so that the farm becomes again delta-neutral.

RAY volatility becomes relatively subdued for the next few days, enabling the farm to accumulate additional profit and recovers fully after 4 days. Eventually, the farm is closed down as there is not enough buffer to perform the next required rebalancing.

In conclusion, this backtest confirms the importance of rebalances and that the return provided by the pool needs to be large enough to compensate for the risk of providing liquidity (IL) and borrowing cost.

RAY-USDC farming results

Risks

Smart contract risk: All smart contracts are in the process of being audited at the time of this writing (July 2022). Smart contracts from supported protocols (Francium and Tulip) have been previously audited, however they may contain vulnerabilities. Like all other strategies, only audited protocols are supported.

Price volatility: The strategy provided optimal returns during periods of low volatility. In periods of high volatility, rebalancing the positions may occur too often, which incurs greater transaction costs and can realise impermanent loss. When backtesting was undertaken, a greater focus was given to these market periods and the length of time it took to recoup losses during these periods.

Different variables: There are several variables observed in this strategy: APR, borrowing fees, price volatility and swap costs. During the backtesting of the LYF strategy, analysis of what happened when these variables fell or rose to pessimistic values and whether the strategy remained viable or not. Tweaking certain variables can eventually make the strategy unprofitable.

Solana TPS: This strategy is executed on-chain, there may eventually be problems with transactions on the solana network (low TPS). There may be a problem when trying to rebalance the position but the network is congested.

Recommended reading

For leveraged farming: https://thedefiant.io/leveraged-yield-farming/