# Algorithm Balanced Funding Rate (ABFR)

The fundamental principle underlying ABFR is to safeguard the interests of SLP holders by dynamically adapting the funding rate according to the specific direction of a trading pair. When traders exhibit net profitability in a given trading pair, fees are gathered from them and provided as support to LPs. When traders face an overall loss, fees are acquired from LPs and allocated to traders. This mechanism guarantees that the combined unrealized and realized PNL of each trading pair tends to balance out to approximately zero over the long run. In this way, LPs wouldn't experience excessive returns but rather would generate income from fees (transaction fee + borrowing interest). The subsequent steps outline the overall process for calculating these dynamic funding fees.

#### Calculate the total profit and loss of LP at time t on the the trading pair

$$\begin{aligned}sign&=\begin{cases}1\&short\\-1\&long\end{cases}\\\Delta\_t&=openAmount\_t \times price\_t - openSize\_t\pnl\_t&=realizedPnl+fundingFee+sign\times\Delta\_t\end{aligned}$$

{% hint style="info" %}

* Realized PNL signifies the actual profit and loss experienced by LPs due to changes in traders' positions, excluding the influence of fees.
* FundingFee embodies the cumulative funding fees that have been gathered or distributed.
  {% endhint %}

When PNL\_t < 0:

$$ABFR\_t=\begin{aligned}min\left{K\_1\times log\left|PNL\_t\right|,R\_x\right}\end{aligned}$$

When PNL\_t > 0:

$$ABFR\_t=\begin{aligned}-min\left{K\_2\times log\left|PNL\_t\right|,R\_y\right}\end{aligned}$$

{% hint style="info" %}
K1, K2 are constant coefficients
{% endhint %}

* The reason for using the logarithm of pnl is to make ABFR change significantly when pnl is small and change slowly when pnl is large
* ABFR has upper and lower bounds, with corresponding upper and lower rate limits Rx, Ry when pnl < 0 and pnl > 0, respectively

If a trader opens a position at time t1 and closes it at time t2, with a position size of ***size*****,** the funding fee to be paid/received by the trader is

$$fundingFee=size\times\int\_{t\_1}^{t\_2}ABFR\_t\ dt$$

When the fee is positive, trader pays the funding fee to the LP

When the fee is negative, LP pays the funding fee to the trader


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.sudo.finance/feature-details/algorithm-balanced-funding-rate-abfr.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
