宽客秀

宽客秀

Quant.Show的Web3站点,Archives from quant.show

ATR-based fund allocation method

When trading stocks, there are often more than one candidate stocks, usually 2-3 or even more. How to allocate funds among multiple stocks? Common methods include equal weight method, fixed quantity method, market value/circulating market value ratio method, etc. Today, we will introduce the funding allocation method based on ATR. The idea of this method comes from part of the Turtle Trading Strategy.

1. What is ATR#

Before introducing the funding allocation method based on ATR, let's first explain what ATR is.

ATR, also known as Average True Range, is one of the indicators used to measure market volatility. The calculation of ATR can be divided into two steps:

  1. Calculation of TR (True Range): MAX (the following 3 values)
  • | High price - Low price | - This is easy to understand, it represents the daily range
  • | (Yesterday's) Close price - High price | - Considering the situation of a gap-up opening and a high upward trend the next day
  • | (Yesterday's) Close price - Recent price | - Considering the situation of a gap-down opening and a low downward trend the next day
  1. ATR = TR / N, where N is the number of days, and TR represents the sum of TR for N days, usually N is set to 20.

2. What are the drawbacks of equal weight funding allocation#

If equal weight method is used for funding allocation, the funds will be allocated according to (available funds / number of stocks). For example, if you plan to buy stock 1 and stock 2 at the same time, and there is 100,000 yuan in your account, the funding allocation plan will be: stock 1: 50,000 yuan, stock 2: 50,000 yuan. This method is commonly used and convenient. However, this funding allocation method has a drawback - it ignores the volatility of individual stocks. For example, taking today as an example, the amplitude of the government bond ETF (511010) is 0.2%, while the amplitude of the AI ETF (512930) is 3%, a difference of 15 times. If the government bond ETF and the AI ETF are purchased with the same amount of funds, the losses and profits brought by the high volatility of the AI ETF will exceed those of the relatively low volatility of the government bond ETF. Assuming your win rate is 60%, but if unfortunately the market is the same as today, the government bond ETF with low volatility rises by 0.14%, while the AI ETF with high volatility falls by 2.36%, your overall account will still suffer losses.

3. Funding allocation method based on ATR#

We introduce the funding allocation plan based on ATR to avoid the above uncertainty. The purchase quantity is set as follows:

Purchase Quantity (Unit) = (Available Funds * Fixed Percentage) / 1 ATR of the stock

Assuming there is 100,000 yuan of available funds and a fixed percentage of 1% (usually set to 1%, meaning that the change in total assets due to the amplitude of the day does not exceed 1%), taking AI ETF and government bond ETF as examples,

  • The ATR of AI ETF for the day is 0.0548 yuan, equivalent to 4.08% of the closing price (1.239);
  • The purchase quantity of AI ETF = 1000 / 0.0548 = 18248, which is 18200 shares;
  • The ATR of the government bond ETF for the day is 0.4604 yuan, equivalent to 0.37% of the closing price (124.458);
  • The purchase quantity of the government bond ETF = 1000 / 0.4604 = 2172, which is 2100 shares;
  • The total funds for both stocks is about 280,000 yuan.

Through the above funding allocation, we can roughly equalize the impact of the normal volatility of these two stocks on the investment portfolio, without being overly affected by a single stock.

4. Code example of funding allocation based on ATR#

The following code is based on JoinQuant#

Calculate ATR and ATRma values#

Parameters:#

security: stock name#

atr_length: number of days for average true range#

atr_ma_length: number of days for average of ATR#

n: number of historical data#

unit: unit of value#

include_now: whether to include the current day#

def atrMa(security, atr_length=20, atr_ma_length=10, n=40, unit='1d', include_now=False):
atr_value = 0
atr_ma = 0
bar_ndarray = get_bars(security, n, unit=unit, fields=['close','high','low'], include_now=include_now)

if bar_ndarray.shape[0] == 0:
    print("The input data for stock %s is all NaN. The stock may have been delisted, not listed, or just listed. Returning NaN value data." %security)
else:
    # Latest HL
    bar_high = bar_ndarray['high'][-1]
    bar_low = bar_ndarray['low'][-1]
    bar_close = bar_ndarray['close'][-1]

    ## Technical indicator calculation
    # ATR, ATRma
    result = talib.ATR(bar_ndarray['high'], bar_ndarray['low'], bar_ndarray['close'], atr_length)
    atr_value = result[-1]
    atr_ma = result[-atr_ma_length:].mean()

return atr_value, atr_ma

Funding allocation method based on ATR#

Parameters:#

security: stock name#

unit: buying/selling unit, usually 1% of total assets. When total assets are 100,000 yuan, unit=1000#

def getAmount(security, unit=1000):
atr_value, atr_ma = atrMa(security)
amount = unit/atr_value
return amount

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.