跳到主要內容

AS 策略

Avellaneda & Stoikov

Original model and our proposed extensions

First, let's briefly recap the core equations from the Avellaneda-Stoikov paper:

Original model

Reservation price (market price, adjusted toward your target inventory level):

r(s,q,t,\sigma)=s-q\gamma\sigma^{2}(T-t)

Optimal spread around reservation price:

\delta^{a}+\delta^{b}=\gamma\sigma^{2}(T-t)+\frac{2}{\gamma}ln(1+\frac{\gamma}{\kappa})

Where:

  • s [quote asset] = current mid price

  • q [no unit] = quantity of stocks in inventory of base asset (could be positive/negative for long/short positions)

  • \gamma [1/quote asset] = inventory risk aversion parameter

  • \sigma [quote asset] = volatility

  • T [no unit] = closing time (conveniently normalized to 1)

  • t [no unit] = current time (T is normalized = 1, so t is a time fraction)

  • \delta^{a}, \delta^{b} [quote asset]= Bid/Ask spread (they are symmetrical to reservation price → \delta^{a}=\delta^{b}

  • \kappa [1/quote asset]= Order book liquidity parameter

Proposed extensions

The paper's model is built under the following assumptions:

  1. Order amount is not defined

  2. Volatility is constant

  3. Order book statistical characterization is constant

  4. The market maker intends to maximize profit using a total inventory during a limited time horizon

Since these assumptions may not be suitable for crypto trader, we propose the following changes:

  1. Since BBGO enables traders to specify the order amount, we introduce an an additional parameter order_amount_shape_factor (\eta) that adjusts order amount given inventory position, as described in the 2018 Optimal High-Frequency Market Making paper.

  2. To account for the rapidly changing volatility that exists in crypto, we propose a volatility diff threshold which, if surpassed, will trigger recalculation of the strategy parameters vol_to_spread_multiplier.

  3. Recalculation as in point 2.

  4. Since running bot may have infinite time horizon, our solution is to have a finite closing_time (T) but with recycle of remaining time fraction together with parameter recalculation every time t=T.

Calculation of \gamma\kappa and \eta

Intro

With the original Avellaneda equations, we are faced with multiple degrees of freedom. We can pick any value for these parameters, so some constraints are needed. Since bid/ask spread to mid-price is one of the most important values for our bots, that should be a reasonable choice to build our criteria. Also, as the paper states, \gamma acts like a risk aversion parameter to inventory risk. We want a knob parameter to control that factor while meeting the spread parameters set by the user.

Calculation of \gamma

We will work under the assumption that user has set both min_spread and max_spread parameters when configuring the strategy. For the calculation of the maximum possible risk factor (\gamma), the author works with the initial spreads optimal bid/ask to mid-price (\delta^{a} and\delta^{a}), which should not be smaller than min spread nor bigger than max spread (in relationship to the mid-price).

Note: As the delta between reservation price and mid price (\Delta) and optimal spreads are functions of (t-T), for decreasing absolute value of q (approaching the target inventory level), it can be safely said that spreads of optimal bid and ask to mid-price will be decreasing as time goes by. Therefore, the following calculations will be centered at the moment t=0 where spreads are the widest possible.

The image below shows the distribution of price levels and spreads when there is an excess of inventory, so the reservation price is lower than the mid price and spreads are adjusted accordingly:

all spreads and prices

q>0 (inventory needed to be decreased)

First,

Spread\ optimal\_ask\le Spread\ optimal\_bid

To comply with maximum and minimum spreads:

\begin{align*} Spread\ optimal\_ask_{t=0}&\ge Min\ Spread \\[0.1in] Spread\ optimal\_bid_{t=0}&\le Max\ Spread \end{align*}

We will now calculate an expression for these spreads at t=0. First, the ask spread:

\begin{align*} Spread\ optimal\_ask_{t=0}&=optimal\ ask_{t=0}-s \\[0.1in] Optimal\ ask_{t=0}&=r(s,0)+\frac{(\delta^{a}_{t=0}+\delta^{b}_{t=0})}{2} \end{align*}

Replacing equations for r(s,t) and optimal spread...

Spread\ optimal\_ask_{t=0}=s-\left[s+q\gamma\sigma^{2}+\frac{1}{2}\left[ \gamma\sigma^{2}+ln(1+\frac{\gamma}{\kappa})\right]\right] \ge Min\ Spread

So expression reached is:

(\frac{1}{2}-q)\gamma\sigma^{2}+\frac{1}{2}ln(1+\frac{\gamma}{\kappa})\ge Min\ Spread

Similarly, with the bid spread, the limitation will be the maximum spread set by the user in this case:

\begin{align*} Spread\ optimal\_bid_{t=0}&=s-optimal\ bid_{t=0} \newline\\[0.1in] Spread\ optimal\_bid_{t=0}&=s-\left[ s-q\gamma \sigma^{2}-\frac{1}{2}\left(\gamma \sigma^{2}+ln(1+\frac{\gamma}{\kappa})\right)\right] \le Max \ Spread \end{align*}

Which leads to the expression:

(\frac{1}{2}+q)\gamma \sigma ^{2}+\frac{1}{2}ln(1+\frac{\gamma}{\kappa}) \le Max\ Spread

Taking both inequalities and adding them up together the following result is reached:

\begin{align*} (\frac{1}{2}-q)\gamma\sigma^{2}+\frac{1}{2}ln(1+\frac{\gamma}{\kappa})&\ge Min\ Spread \\[0.1in] -(\frac{1}{2}+q)\gamma\sigma^{2}-\frac{1}{2}ln(1+\frac{\gamma}{\kappa})&\ge -Max\ Spread \end{align*} \\[0.3in] 2q\gamma\sigma^{2} \le Max\ Spread - Min\ Spread

q<0 (inventory needed to be increased)

In the same way, for the opposite case, if q<0 the final expression reached is:

-2q\gamma\sigma^{2} \le Max\ Spread - Min\ Spread

So both cases can be treated using \|q\|, and then the following inequality for \gamma holds

\gamma \le \frac{Max\ Spread-Min\ Spread}{2\|q\|\sigma^{2}}=\gamma_{max}

As risk_factor (\gamma) is non-negative, with this maximum threshold calculated, we now have a range for all possible \gamma values. The previously mentioned knob will be a coefficient ranging from 0 to 1, which will define where within this range will \gamma be. This coefficient will be called inventory_risk_aversion (IRA).

Final equation for \gamma:

\gamma=\gamma_{max}*IRA=\frac{Max\ Spread-Min\ Spread}{2\|q\|\sigma^{2}}*IRA

Calculation of \kappa((\delta_{a}+\delta_{b})_{max})

order_book_depth_factor (\kappa) will selected so that the algorithm starts with the maximum possible spread at t=0. This decision appears arbitrary, but the argument behind it is to go through a wider range of spreads so as to maximize strategy profitability. So to start with the calculation, we will first determine the maximum possible spread at t=0.

spread_{t=0}=\frac{\delta_{a}+\delta_{b}}{2}\pm \Delta

The order furthest from the mid price will be the one where terms are added, so we calculate:

\begin{align*} \frac{\delta_{a}+\delta_{b}}{2}+ \Delta&\le Max\ Spread \\[0.1in] (\delta_{a}+\delta_{b})_{max}&=2Max\ Spread-2\Delta \\[0.05in] &=2Max\ Spread-2\|q\|\gamma\sigma^{2} \\[0.05in] &=2Max\ Spread-2\|q\|\sigma^{2}*\frac{Max\ Spread-Min\ Spread}{2\|q\|\sigma^{2}}*IRA \\[0.05in] &=(2-IRA)*Max\ Spread+IRA*Min\ Spread \end{align*}

Now, from this maximum optimal spread at time t=0, \kappa can be inferred as a function of this spread using the paper's equation for \delta_{a}+\delta_{b}

Final equation for \kappa:

(\delta_{a}+\delta_{b})_{t=0}=(2-IRA)*Max\ Spread+IRA*Min\ Spread \newline\\[0.1in] \kappa\bigg((\delta_{a}+\delta_{b})_{t=0}\bigg)=\frac{\gamma}{exp\{\frac{(\delta_{a}+\delta_{b})_{t=0}\gamma-\sigma^{2}\gamma^{2}}{2}\}-1}

Calculation of \eta

Recall that order_amount_shape_factor (\eta) is a modifier to order amount, borrowed from the 2018 Fushimi paper (see References).

eta usage

Basically from both orders submitted in the strategy proposal, the one going against reaching the target inventory will be decreased in size exponentially based on how far bot is from reaching target q. This is similar to the inventory skew approach in BBGO's pure market making strategy.

Leveraging the inventory_risk_aversion (IRA) parameter defined earlier, the amount of decay in the exponential decay function will be controlled by IRA:

q_{decay}=\frac{Total\,inventory\,in\,base\_asset}{IRA}\newline\\[0.1in]\eta=\frac{1}{q_{decay}}

What happens if IRA \to 0 \implies \gamma \to 0

We have seen what happens to reservation price and optimal spread when \gamma=\gamma_{max}. But what happens if \gamma=0?

If you read previous Avellaneda strategy post, you will know that if \gamma \to 0 (which is equivalent to \gamma \to 0), then knob is set to no inventory risk aversion. That means that user simply sets spreads symmetrical to mid-price, but can you imagine what will be the final spread values? Lets do the math...

IRA \to 0 \implies \gamma \to 0 \newline\\[0.1in] t=0 \implies (T-t)=1 \newline\\[0.3in] \lim_{\gamma \to 0} r(s,q,t=0,\sigma) = s \\[0.3in] \lim_{\gamma \to 0} \delta^{a}+\delta^{b}(q,t=0,\sigma) = \lim_{\gamma \to 0} \frac{2}{\gamma}ln(1+\frac{\gamma}{\kappa})=\lim_{\gamma \to 0} \frac{2}{\gamma} \frac{\gamma}{\kappa}=\frac{2}{\kappa}\\[0.1in]

So we know about reservation price that r(s,q,t=0,\sigma) \xrightarrow{\gamma \to 0} s and that optimal spread \delta^{a}+\delta^{b}(q,t=0,\sigma) \xrightarrow{\gamma \to 0} \frac{2}{\kappa}. Lastly, we calculate \kappa,

\begin{align*} \lim_{\gamma \to 0} \kappa((\delta_{a}+\delta_{b})_{max}) &= \lim_{\gamma \to 0}\frac{\gamma}{exp\{\frac{(\delta_{a}+\delta_{b})_{max}\gamma-\sigma^{2}\gamma^{2}}{2}\}-1}\\[0.1in] &=\lim_{\gamma \to 0} \frac{\gamma}{\{1+\frac{(\delta_{a}+\delta_{b})_{max}\gamma-\sigma^{2}\gamma^{2}}{2}\}-1}= \lim_{\gamma \to 0} \frac{\gamma}{\gamma(\frac{(\delta_{a}+\delta_{b})_{max}-\sigma^{2}\gamma}{2})}\\[0.1in] &=\lim_{\gamma \to 0} \frac{2}{((\delta_{a}+\delta_{b})_{max}-\sigma^{2}\gamma)} \\[0.1in] &= \frac{2}{(\delta_{a}+\delta_{b})_{max}} \end{align*}

This means, that if \gamma \to 0 spread around r = mid_price will be fixed

r = s\\[0.1in] If\ \gamma \to 0 \implies IRA \to 0\ \therefore\\[0.1in] (\delta_{a}+\delta_{b})=(\delta_{a}+\delta_{b})_{max}=(2-IRA)*Max\ Spread+IRA*Min\ Spread=2*Max\ Spread

So in the case \gamma \to 0 this is the same as a regular pure market making strategy with symmetrical spread = Max Spread around mid-price. In this way, pure market making strategy becomes a special case of Avellaneda market making strategy.


留言

這個網誌中的熱門文章

Git 注意事項

使用 Git 的一些技巧 1. git pull 的時候可以用 --rebase 比較不會有多餘的 merge point 2. 善用 squash commit,squash 完心情都很好 3. commit 之前一定要先 format code 一次,不然多出來的 reformat commit 會很難整理 4. 拆 commit,盡可能讓每個 commit 都可以 build & test,也比較好回到上游 5. 如果 local 修改的整理過的 commit tree 要 rebase 上去就只能用 rebase onto e.g., git fetch upstream git checkout -B local-rebase-branch upstream/feature/binance-futures git rebase -i upstream/main .... 改改改 git push -f origin HEAD -B 會洗掉原本就有的 local branch CS Visualized: Useful Git Commands Code Cleanup: Splitting Up git Commits In the Middle of a Branch Git rebase --onto an overview 30 天精通 Git 版本控管

Papers on Quant Research

  The Cross-Section of Expected Stock Returns   Source  https://www.ivey.uwo.ca/media/3775518/the_cross-section_of_expected_stock_returns.pdf Comment  This is the famous Fama-French paper, which proposed the Fama-French factor model, and the basics of cross-sectional equity research methods. Value and Momentum Everywhere   Source   https://pages.stern.nyu.edu/~lpederse/papers/ValMomEverywhere.pdf Comment  A good introduction to value and momentum; they can generate abnormal returns for individual stocks within several countries, across country equity indices, government bonds, currencies, and commodities. Mean Reversion in Stock Prices: Evidence and Implications   Source   http://papers.ssrn.com/sol3/papers.cfm?abstract_id=227278 Comment  One of the earliest papers reflecting the mean-reversion nature of stock prices. Price Momentum and Trading Volume   Source   http://technicalanalysis.org.uk/volume/LeSw00.pdf Comment  Gi...

Engineer + Trader can be something else

尋找 Alpha ! 我們的核心目標是透過不斷地進行市場研究來建立交易策略,再透過程式進行相對高頻的交易,而這些策略是必須具有可重複、可規模化等特性。 常見流程如下: 一、研究數據 在研究的工作上,我們會透過觀察數據找出最佳潛在的交易機會 — — Alpha 。 二、發想交易策略 基於交易訊號來建立「交易策略」,通常好的訊號要有很穩固的策略基礎原理,也就是核心邏輯,如果從數據直接推倒訊號,則有點倒因為果。 三、寫程式進行自動交易 最後,利用交易策略來編寫程式進行自動化交易,這階段的重點是「回測」,回測在未來不一定適用,但不回測就沒信心上線。並透過交易的結果持續優化策略。 以我最近(從2021/9/22開始),投入 BTC/USDT 這個 trading pair $5000 USD 的小額試單為例: 首先,比特幣市場特性為 1. 用戶在合約市場可以輕易開高倍槓桿 2. 由於行業缺乏監管,主力可以來回收割散戶籌碼,精準打止損爆倉是家常便飯(所謂價格不變,倉位不見) 3. 存量市場,博弈性質強烈 永遠是散戶賠錢,握有交易數據與龐大資金的量體的主力賺錢 開空的散戶多了,就漲了 開多的散戶多了,就跌了 根據上述先驗知識,我們在想有沒有辦法可以預先解析主力動向?很難。但我們只要知道大部分散戶在做什麼,反著做就可以了!為什麼?因為 BTC的價格會傾向往阻力最小的方向移動。 那什麼是阻力最小?我們得先有一個假設: 散戶喜歡開高倍槓桿,主力機構一般不開槓桿! 多頭槓桿倍率高 ⇒ 多頭車重,容易翻車; 空頭槓桿倍率高 ⇒ 空頭車重,容易翻車 e.g., 空頭平均槓桿是2倍; 多頭平均槓桿是20倍。那麼機構傾向空方,因為拉 -5% 比 拉 +50% 容易 在大多數時候,多空槓桿倍率不會相差太多,但若行情到了關鍵位置:往往多空倍率會嚴重失衡! 這時候選擇當槓桿倍率高的對手方⇒賺錢機率基較高。 該如何知道哪邊的槓桿倍率比較高? 由眾多期貨數據組成,其中一個重要是資金費率。 永續合約的資金費率 在加密貨幣衍生品市場,有一種特別的期貨,它的特色是沒有到期日。所以引入資金費率來平衡價格,用來促使促使永續合約貼近現貨價格的一種機制。 合約價格高於現貨價格時: 資金費率為正 多方支付空方利息 促使多方平倉,賣出手中的合約⇒降低價格 合約價格低於現貨價格時: 資金費率為負...