Search code examples
pythonpandasmathtrading

Calculate average asset price when using netting instead of hedging


I'm trying to come up with a formula to calculate the average entry/position price to further update my stop loss and take profit.

For example opened BTC buy position with amount of 1 when price was 20000. Later when price dropped down to 19000 we made another buy using the same amount of 1, "avereging" the position to the middle, so end up with position at 19500 with amount of 2.

Where I'm struggling is what if we want to increase the order size on each price.

Say 1 at 20000, 1.5 at 19500, 2 at 19000 and so on.

Or made new buys of the same amount but shorter distance between.

Inital buy at 20000. then 19000 then 19150

Or combine these two variants.

I use mainly Python and Pandas. Maybe the latter one has some built-in function which I'm not aware of. I checked the official Pandas docs, but found only regular mean function.


Solution

  • Thanks to Yuri's suggestion to look into VWAP, I came up with the following code, which is more advanced and allows you to use different contract/volume sizes and increase/decrease "distance" between orders.

    As an example here I used avarage price of BTC 20000 and increased steps distance using 1.1 multiplier as well as increased volume. Operated in Binance futures terms, where you can buy minimum 1 contract for 10$.

    The idea is to find sweet spot for orders distance, volume, stop loss and take profit while avereging down.

    # 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))