리소스 로딩... 로딩...

바이낸스 선물 다화폐 헤지 전략 연구 3부

저자:선함, 2020-05-12 12:14:29, 업데이트: 2023-11-04 19:50:43

img

대략적인 시뮬레이션으로 모든 사람들이 손실된 마진의 양에 대한 구체적인 개념을 가질 수 있습니다. 당신은 노트북을 다운로드하고 FMZ 연구 환경에 업로드하고 코드를 직접 실행할 수 있습니다.

바이낸스의 리스크 추정치 상승에 비해 짧은 판매와 추락 트렌드 전략에 비해 긴 구매

첫 번째 보고서를 보세요.https://www.fmz.com/digest-topic/5584그리고 개선된 보고서:https://www.fmz.com/digest-topic/5588

이 전략은 현재 4 일 동안 공개 공유되었습니다. 초기 단계는 높은 수익률과 약간의 리트레이싱으로 매우 잘 수행되었으므로 많은 사용자가 하루에 10%의 수익률을 내기 위해 매우 높은 레버리지를 사용하고 있습니다. 그러나 초기 보고서에서 언급했듯이 완벽한 전략은 없습니다. 상승 추세보다 짧은 판매와 추세보다 긴 구매는 Altcoin의 특성을 사용하여 함께 상승하고 떨어집니다. 통화가 독특한 추세에서 벗어나면 많은 보유 지위를 축적합니다. 초기 가격을 추적하기 위해 이동 평균이 사용되었지만 위험이 여전히 존재합니다. 이 보고서는 주로 특정 위험을 수치화하고 권장 매개 변수 trade_value가 전체 자금의 3%를 차지하는 이유를 설명합니다.

코드를 강조하기 위해, 우리는 이 부분의 고급에 넣어, 모든 사람은 먼저 다음 코드를 실행하려고해야합니다 (이포트 라이브러리 부분에서 시작).

시뮬레이션을 하기 위해서 20개의 화폐가 있다고 가정하지만 BTC와 ETH만 더하고 BTC를 사용하여 19개의 화폐를 일정한 가격으로 나타냅니다. ETH는 독립적인 트렌드 화폐를 나타냅니다. 이것은 시뮬레이션이기 때문에, 여기서 이동평균으로 초기 가격을 추적할 필요가 없습니다.

먼저, 단일 화폐의 가격이 계속 상승하는 상황을 시뮬레이션합니다. Stop_loss는 스톱 손실이 이탈한다는 것을 나타냅니다. 여기는 시뮬레이션 만입니다. 실제 상황은 간헐적 인 리트레이싱이있을 것이고, 시뮬레이션만큼 나쁘지 않을 것입니다.

이 화폐에 대한 리트레이션이 없다고 가정하자면, 스톱 로스 오차가 0.41일 때, ETH는 이 때 44% 상승했고, 결과는 결국 거래 가치의 7배, 즉, 트레이드_밸류 * 7로 손실되었습니다. 트레이드_밸류가 총 자금의 3%로 설정되면 손실 = 총 자금 * 0.03 * 7. 최대 리트레이션은 약 0.03 * 7 = 21%입니다.

아래 결과를 바탕으로 자신의 위험 내성을 추정할 수 있습니다.

btc_price = [1]*500 # Bitcoin price, always unchanged
eth_price = [i/100. for i in range(100,500)] # Ethereum, up 1% in one cycle

for stop_loss in [i/1000. for i in range(10,1500,50)]:
    e = Exchange(['BTC','ETH'],initial_balance=10000,commission=0.0005,log=False)
    trade_value  = 300 # 300 transactions
    
    for i in range(200):

        index = (btc_price[i]*19+eth_price[i])/20. # index

        e.Update(i,{'BTC':btc_price[i], 'ETH':eth_price[i]}) 

        diff_btc = btc_price[i] - index # deviation
        diff_eth = eth_price[i] - index

        btc_value = e.account['BTC']['value']*np.sign(e.account['BTC']['amount'])
        eth_value = e.account['ETH']['value']*np.sign(e.account['ETH']['amount'])

        aim_btc_value = -trade_value*round(diff_btc/0.01,1)*19 # Here BTC replaces 19 currencies
        aim_eth_value = -trade_value*round(diff_eth/0.01,1)

        if aim_btc_value - btc_value > 20:
            e.Buy('BTC',btc_price[i],(aim_btc_value - btc_value)/btc_price[i])

        if aim_eth_value - eth_value < -20 and diff_eth < stop_loss:
            e.Sell('ETH',eth_price[i], (eth_value-aim_eth_value)/eth_price[i],diff_eth)

        if diff_eth > stop_loss and eth_value < 0: # Stop loss
            stop_price = eth_price[i]
            e.Buy('ETH',eth_price[i], (-eth_value)/eth_price[i],diff_eth)

    print('Currency price:',stop_price,' Stop loss deviation:', stop_loss,'Final balance:',e.df['total'].iloc[-1], ' Multiple of losing trade volume:',round((e.initial_balance-e.df['total'].iloc[-1])/300,1))
Currency price: 1.02  Stop loss deviation: 0.01 Final balance: 9968.840396  Multiple of losing trade volume: 0.1
Currency price: 1.07  Stop loss deviation: 0.06 Final balance: 9912.862738  Multiple of losing trade volume: 0.3
Currency price: 1.12  Stop loss deviation: 0.11 Final balance: 9793.616067  Multiple of losing trade volume: 0.7
Currency price: 1.17  Stop loss deviation: 0.16 Final balance: 9617.477263  Multiple of losing trade volume: 1.3
Currency price: 1.23  Stop loss deviation: 0.21 Final balance: 9337.527299  Multiple of losing trade volume: 2.2
Currency price: 1.28  Stop loss deviation: 0.26 Final balance: 9051.5166  Multiple of losing trade volume: 3.2
Currency price: 1.33  Stop loss deviation: 0.31 Final balance: 8721.285267  Multiple of losing trade volume: 4.3
Currency price: 1.38  Stop loss deviation: 0.36 Final balance: 8350.582251  Multiple of losing trade volume: 5.5
Currency price: 1.44  Stop loss deviation: 0.41 Final balance: 7856.720861  Multiple of losing trade volume: 7.1
Currency price: 1.49  Stop loss deviation: 0.46 Final balance: 7406.412066  Multiple of losing trade volume: 8.6
Currency price: 1.54  Stop loss deviation: 0.51 Final balance: 6923.898356  Multiple of losing trade volume: 10.3
Currency price: 1.59  Stop loss deviation: 0.56 Final balance: 6411.276143  Multiple of losing trade volume: 12.0
Currency price: 1.65  Stop loss deviation: 0.61 Final balance: 5758.736222  Multiple of losing trade volume: 14.1
Currency price: 1.7  Stop loss deviation: 0.66 Final balance: 5186.230956  Multiple of losing trade volume: 16.0
Currency price: 1.75  Stop loss deviation: 0.71 Final balance: 4588.802975  Multiple of losing trade volume: 18.0
Currency price: 1.81  Stop loss deviation: 0.76 Final balance: 3841.792751  Multiple of losing trade volume: 20.5
Currency price: 1.86  Stop loss deviation: 0.81 Final balance: 3193.215479  Multiple of losing trade volume: 22.7
Currency price: 1.91  Stop loss deviation: 0.86 Final balance: 2525.155765  Multiple of losing trade volume: 24.9
Currency price: 1.96  Stop loss deviation: 0.91 Final balance: 1837.699982  Multiple of losing trade volume: 27.2
Currency price: 2.02  Stop loss deviation: 0.96 Final balance: 988.009942  Multiple of losing trade volume: 30.0
Currency price: 2.07  Stop loss deviation: 1.01 Final balance: 260.639618  Multiple of losing trade volume: 32.5
Currency price: 2.12  Stop loss deviation: 1.06 Final balance: -483.509646  Multiple of losing trade volume: 34.9
Currency price: 2.17  Stop loss deviation: 1.11 Final balance: -1243.486107  Multiple of losing trade volume: 37.5
Currency price: 2.24  Stop loss deviation: 1.16 Final balance: -2175.438384  Multiple of losing trade volume: 40.6
Currency price: 2.28  Stop loss deviation: 1.21 Final balance: -2968.19255  Multiple of losing trade volume: 43.2
Currency price: 2.33  Stop loss deviation: 1.26 Final balance: -3774.613275  Multiple of losing trade volume: 45.9
Currency price: 2.38  Stop loss deviation: 1.31 Final balance: -4594.305499  Multiple of losing trade volume: 48.6
Currency price: 2.44  Stop loss deviation: 1.36 Final balance: -5594.651063  Multiple of losing trade volume: 52.0
Currency price: 2.49  Stop loss deviation: 1.41 Final balance: -6441.474964  Multiple of losing trade volume: 54.8
Currency price: 2.54  Stop loss deviation: 1.46 Final balance: -7299.652662  Multiple of losing trade volume: 57.7

지속적인 하락의 상황을 시뮬레이션하는 경우, 하락은 계약의 가치의 감소와 함께 발생하므로 위험이 상승보다 높고 가격이 하락함에 따라 손실 증가율이 가속화됩니다. 스톱 손실 편차 값이 -0.31일 때, 통화 가격이 이 시점에서 33% 떨어지고 손실은 6.5 거래입니다. 거래 금액이 전체 자금의 3%로 설정되면 최대 리트레이스는 약 0.03 * 6.5 = 19.5%입니다.

btc_price = [1]*500 # Bitcoin price, always unchanged
eth_price = [2-i/100. for i in range(100,200)] # Ethereum

for stop_loss in [-i/1000. for i in range(10,1000,50)]:
    e = Exchange(['BTC','ETH'],initial_balance=10000,commission=0.0005,log=False)
    trade_value  = 300 # 300 transactions
    
    for i in range(100):

        index = (btc_price[i]*19+eth_price[i])/20. # index

        e.Update(i,{'BTC':btc_price[i], 'ETH':eth_price[i]}) 

        diff_btc = btc_price[i] - index # deviation
        diff_eth = eth_price[i] - index

        btc_value = e.account['BTC']['value']*np.sign(e.account['BTC']['amount'])
        eth_value = e.account['ETH']['value']*np.sign(e.account['ETH']['amount'])

        aim_btc_value = -trade_value*round(diff_btc/0.01,1)*19 # Here BTC replaces 19 currencies
        aim_eth_value = -trade_value*round(diff_eth/0.01,1)
        
        if aim_btc_value - btc_value < -20:
            e.Sell('BTC',btc_price[i],-(aim_btc_value - btc_value)/btc_price[i])
    
        if aim_eth_value - eth_value > 20 and diff_eth > stop_loss:
            e.Buy('ETH',eth_price[i], -(eth_value-aim_eth_value)/eth_price[i],diff_eth)

        if diff_eth < stop_loss and eth_value > 0:
            e.Sell('ETH',eth_price[i], (eth_value)/eth_price[i],diff_eth)
            stop_price = eth_price[i]
        
    print('Currency price:',round(stop_price,2),' Stop loss deviation:', stop_loss,'Final balance:',e.df['total'].iloc[-1], ' Multiple of losing trade volume:',round((e.initial_balance-e.df['total'].iloc[-1])/300,1))
Currency price: 0.98  Stop loss deviation: -0.01 Final balance: 9983.039091  Multiple of losing trade volume: 0.1
Currency price: 0.93  Stop loss deviation: -0.06 Final balance: 9922.200148  Multiple of losing trade volume: 0.3
Currency price: 0.88  Stop loss deviation: -0.11 Final balance: 9778.899361  Multiple of losing trade volume: 0.7
Currency price: 0.83  Stop loss deviation: -0.16 Final balance: 9545.316075  Multiple of losing trade volume: 1.5
Currency price: 0.77  Stop loss deviation: -0.21 Final balance: 9128.800213  Multiple of losing trade volume: 2.9
Currency price: 0.72  Stop loss deviation: -0.26 Final balance: 8651.260863  Multiple of losing trade volume: 4.5
Currency price: 0.67  Stop loss deviation: -0.31 Final balance: 8037.598952  Multiple of losing trade volume: 6.5
Currency price: 0.62  Stop loss deviation: -0.36 Final balance: 7267.230651  Multiple of losing trade volume: 9.1
Currency price: 0.56  Stop loss deviation: -0.41 Final balance: 6099.457595  Multiple of losing trade volume: 13.0
Currency price: 0.51  Stop loss deviation: -0.46 Final balance: 4881.767442  Multiple of losing trade volume: 17.1
Currency price: 0.46  Stop loss deviation: -0.51 Final balance: 3394.414792  Multiple of losing trade volume: 22.0
Currency price: 0.41  Stop loss deviation: -0.56 Final balance: 1575.135344  Multiple of losing trade volume: 28.1
Currency price: 0.35  Stop loss deviation: -0.61 Final balance: -1168.50508  Multiple of losing trade volume: 37.2
Currency price: 0.29  Stop loss deviation: -0.66 Final balance: -4071.007983  Multiple of losing trade volume: 46.9
Currency price: 0.25  Stop loss deviation: -0.71 Final balance: -7750.361195  Multiple of losing trade volume: 59.2
Currency price: 0.19  Stop loss deviation: -0.76 Final balance: -13618.366286  Multiple of losing trade volume: 78.7
Currency price: 0.14  Stop loss deviation: -0.81 Final balance: -20711.473968  Multiple of losing trade volume: 102.4
Currency price: 0.09  Stop loss deviation: -0.86 Final balance: -31335.965608  Multiple of losing trade volume: 137.8
Currency price: 0.04  Stop loss deviation: -0.91 Final balance: -51163.223715  Multiple of losing trade volume: 203.9
Currency price: 0.04  Stop loss deviation: -0.96 Final balance: -81178.565715  Multiple of losing trade volume: 303.9
# Libraries to import
import pandas as pd
import requests
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
%matplotlib inline
price_usdt = pd.read_csv('https://www.fmz.com/upload/asset/20227de6c1d10cb9dd1.csv ', index_col = 0)
price_usdt.index = pd.to_datetime(price_usdt.index)
price_usdt_norm = price_usdt/price_usdt.fillna(method='bfill').iloc[0,]
price_usdt_btc = price_usdt.divide(price_usdt['BTC'],axis=0)
price_usdt_btc_norm = price_usdt_btc/price_usdt_btc.fillna(method='bfill').iloc[0,]
class Exchange:
    
    def __init__(self, trade_symbols, leverage=20, commission=0.00005,  initial_balance=10000, log=False):
        self.initial_balance = initial_balance # Initial asset
        self.commission = commission
        self.leverage = leverage
        self.trade_symbols = trade_symbols
        self.date = ''
        self.log = log
        self.df = pd.DataFrame(columns=['margin','total','leverage','realised_profit','unrealised_profit'])
        self.account = {'USDT':{'realised_profit':0, 'margin':0, 'unrealised_profit':0, 'total':initial_balance, 'leverage':0, 'fee':0}}
        for symbol in trade_symbols:
            self.account[symbol] = {'amount':0, 'hold_price':0, 'value':0, 'price':0, 'realised_profit':0, 'margin':0, 'unrealised_profit':0,'fee':0}
            
    def Trade(self, symbol, direction, price, amount, msg=''):
        if self.date and self.log:
            print('%-20s%-5s%-5s%-10.8s%-8.6s %s'%(str(self.date), symbol, 'buy' if direction == 1 else 'sell', price, amount, msg))
            
        cover_amount = 0 if direction*self.account[symbol]['amount'] >=0 else min(abs(self.account[symbol]['amount']), amount)
        open_amount = amount - cover_amount
        
        self.account['USDT']['realised_profit'] -= price*amount*self.commission # Minus handling fee
        self.account['USDT']['fee'] += price*amount*self.commission
        self.account[symbol]['fee'] += price*amount*self.commission
        
        if cover_amount > 0: # close positions first
            self.account['USDT']['realised_profit'] += -direction*(price - self.account[symbol]['hold_price'])*cover_amount  # profit
            self.account['USDT']['margin'] -= cover_amount*self.account[symbol]['hold_price']/self.leverage # Free margin
            
            self.account[symbol]['realised_profit'] += -direction*(price - self.account[symbol]['hold_price'])*cover_amount
            self.account[symbol]['amount'] -= -direction*cover_amount
            self.account[symbol]['margin'] -=  cover_amount*self.account[symbol]['hold_price']/self.leverage
            self.account[symbol]['hold_price'] = 0 if self.account[symbol]['amount'] == 0 else self.account[symbol]['hold_price']
            
        if open_amount > 0:
            total_cost = self.account[symbol]['hold_price']*direction*self.account[symbol]['amount'] + price*open_amount
            total_amount = direction*self.account[symbol]['amount']+open_amount
            
            self.account['USDT']['margin'] +=  open_amount*price/self.leverage            
            self.account[symbol]['hold_price'] = total_cost/total_amount
            self.account[symbol]['amount'] += direction*open_amount
            self.account[symbol]['margin'] +=  open_amount*price/self.leverage
            
        self.account[symbol]['unrealised_profit'] = (price - self.account[symbol]['hold_price'])*self.account[symbol]['amount']
        self.account[symbol]['price'] = price
        self.account[symbol]['value'] = abs(self.account[symbol]['amount'])*price
        
        return True
    
    def Buy(self, symbol, price, amount, msg=''):
        self.Trade(symbol, 1, price, amount, msg)
        
    def Sell(self, symbol, price, amount, msg=''):
        self.Trade(symbol, -1, price, amount, msg)
        
    def Update(self, date, close_price): # Update assets
        self.date = date
        self.close = close_price
        self.account['USDT']['unrealised_profit'] = 0
        for symbol in self.trade_symbols:
            if np.isnan(close_price[symbol]):
                continue
            self.account[symbol]['unrealised_profit'] = (close_price[symbol] - self.account[symbol]['hold_price'])*self.account[symbol]['amount']
            self.account[symbol]['price'] = close_price[symbol]
            self.account[symbol]['value'] = abs(self.account[symbol]['amount'])*close_price[symbol]
            self.account['USDT']['unrealised_profit'] += self.account[symbol]['unrealised_profit']
        
        self.account['USDT']['total'] = round(self.account['USDT']['realised_profit'] + self.initial_balance + self.account['USDT']['unrealised_profit'],6)
        self.account['USDT']['leverage'] = round(self.account['USDT']['margin']/self.account['USDT']['total'],4)*self.leverage
        self.df.loc[self.date] = [self.account['USDT']['margin'],self.account['USDT']['total'],self.account['USDT']['leverage'],self.account['USDT']['realised_profit'],self.account['USDT']['unrealised_profit']]

관련

더 많은