【问题标题】:Calculate average asset price when using netting instead of hedging使用净额而不是对冲时计算平均资产价格
【发布时间】:2022-10-19 19:48:41
【问题描述】:

我试图想出一个公式来计算平均入场/头寸价格,以进一步更新我的止损和获利。

例如,当价格为 20000 时,以数量为 1 的 BTC 买入仓位。 后来,当价格下跌到 19000 时,我们使用相同数量的 1 再次买入,将头寸“平均”到中间,因此最终以 2 的数量在 19500 处建立头寸。

我苦苦挣扎的地方是,如果我们想增加每个价格的订单大小怎么办。

说 1 在 20000,1.5 在 19500,2 在 19000 等等。

或者进行相同数量但距离更短的新购买。

最初以 20000 买入。然后是 19000,然后是 19150

或者结合这两种变体。

我主要使用 Python 和 Pandas。也许后者有一些我不知道的内置功能。我检查了官方的 Pandas 文档,但发现只有常规的均值函数。

【问题讨论】:

  • 在交易中使用VWAP 成交量加权平均价格。谷歌这个。它被计算为sum(volume * price) / sum(volume). For your first example VWAP = 19388.88...` IMVHO 在“平均下降”的同时增加大小是一个非常糟糕的主意。
  • 感谢您提出建议,改进并发布答案。

标签: python pandas math trading


【解决方案1】:

感谢 Yuri 建议研究 VWAP,我想出了以下代码,它更高级,允许您使用不同的合约/交易量大小并增加/减少订单之间的“距离”。

作为一个例子,我使用了 BTC 20000 的平均价格,并使用 1.1 乘数增加了步距以及增加了交易量。以币安期货条款操作,您可以以 10 美元的价格购买至少 1 份合约。

这个想法是找到订单距离、交易量、止损和获利的最佳点,同时平均下降。

# initial entry price
initial_price = 20000

# bottom price
bottom_price = 0

# enter on every 5% price drop
step = int(initial_price*0.05)

# 1.1 to increase distance between orders, 0.9 to decrease
step_multiplier = 1.1

# initial volume size in contracts
initial_volume = 1

# volume_multiplier, can't be less than 1, in case of use float, will be rounded to decimal number
volume_multiplier = 1.1

# defining empty arrays
prices = []
volumes = []

# checking if we are going to use simple approach with 1 contract volume and no sep or volume multiplier
if step_multiplier == 1 and volume_multiplier == 1:
   prices = range(initial_price,bottom_price,-step)   
else:
   # defining current price and volume vars
   curr_price = initial_price
   curr_volume = initial_volume   
   # Checking if current price is still bigger then defined bottom price
   while curr_price > bottom_price:
      # adding current price to the list
      prices.append(curr_price)
      # calulating next order price
      curr_price = curr_price-step*step_multiplier
      # checking if volume multiplier is bigger then 1
      if volume_multiplier > 1:
         # adding current volume to the list
         volumes.append(int(curr_volume))
         # calulating next order volume         
         curr_volume = curr_volume*volume_multiplier

print("Prices:")
for price in prices:
      print(price)

print("Volumes:")
for volume in volumes:
      print(volume)      

print("Prices array length", len(prices))
print("Volumes array length", len(volumes))

a = [item1 * item2 for item1, item2 in zip(prices, volumes)]
b = volumes

print("Average position price when price will reach",prices[-1], "is", sum(a)/sum(b))

【讨论】:

    猜你喜欢
    • 2018-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-05
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多