├── README.md ├── delta_hedge.py └── requirements.txt /README.md: -------------------------------------------------------------------------------- 1 | # Deribit Delta-Hedger 2 | ### Disclaimer: This tool is only for demonstration purposes and is not financial advice. Use at your own risk. 3 | 4 | A rebalancing tool to delta-hedge a portfolio of cryptocurrency options on Deribit Exchange. 5 | 6 | ## Overview 7 | 8 | Delta-hedging is a technique which removes a trader’s exposure to directional moves in the underlying asset. Traders who delta-hedge their portfolios are not concerned about the ***price*** of an asset going up or down, rather their focus is on how the ***volatility*** of an asset changes based on their option position. 9 | 10 | If a trader were to identify a mis-pricing of volatility for a particular option, they can buy or sell the option and then delta-hedge this position to remove any price exposure. Many volatility traders constantly monitor their portfolio delta and rebalance accordingly when the exposure becomes too large. 11 | 12 | To avoid having to constantly watch open positions, this tool calculates the portfolio delta every 30 seconds and automatically rebalances in the case a delta threshold level is breached. The portfolio is delta-hedged using the chosen asset’s perpetual futures contract on Deribit. 13 | 14 | ## Function Parameters 15 | - `api_id` (string): The ID can be found under API management under account settings on the Deribit website. 16 | - `api_secret` (string): The secret can be found under API management under account settings on the Deribit website. 17 | - `symbol` (string): The asset you wish to delta-hedge. Currently only "BTC" and "ETH" are supported with the default value set to "BTC". 18 | - `threshold` (float): The maximum absolute value of delta exposure to have at any given time. The default value is currently 0.10 which means the portfolio delta will fluctuate between -0.10 to 0.10 of whichever asset you are trading. Any breach beyond this level will result in the portfolio being delta-hedged. 19 | 20 | ## Example 21 | In the example below, the script is setup to delta-hedge Bitcoin (BTC) exposures and rebalance the portfolio in case the delta exceeds +/- 0.10 BTC. 22 | ``` python 23 | >>> import delta_hedge 24 | >>> id = "replace_this_with_id" # replace your `api_id` in the quotes 25 | >>> secret = "replace_this_with_secret" # replace your `api_secret` in the quotes 26 | >>> dh = delta_hedge.Hedge(api_id=id, api_secret=secret, symbol="BTC", threshold=0.10) 27 | 28 | # Get current total portfolio delta exposure for the chosen asset 29 | >>> dh.current_delta() 30 | 0.065 31 | 32 | # Run continuous delta-hedging. Terminal log example shown below: 33 | >>> dh.run_loop() 34 | ''' 35 | No need to hedge. Current portfolio delta: 0.0122 36 | No need to hedge. Current portfolio delta: 0.0136 37 | No need to hedge. Current portfolio delta: 0.0224 38 | No need to hedge. Current portfolio delta: 0.0163 39 | No need to hedge. Current portfolio delta: 0.0536 40 | # When delta rises above threshold (0.10 in this case) 41 | Rebalancing trade to achieve delta-neutral portfolio: sell 0.1000 BTC 42 | No need to hedge. Current portfolio delta: 0.0055 43 | No need to hedge. Current portfolio delta: 0.0073 44 | ''' 45 | ``` 46 | ## Installation of dependencies 47 | Run the following command in terminal to install all of the required packages. Users will likely experience errors if they install a different version of the `CCXT` library compared to the version listed in `requirements.txt`. 48 | 49 | ``` 50 | pip install -r requirements.txt 51 | ``` 52 | -------------------------------------------------------------------------------- /delta_hedge.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import ccxt 3 | import time 4 | 5 | class Hedge: 6 | def __init__(self, api_id, api_secret, symbol="BTC", threshold=0.10): 7 | """ 8 | Initializing Hedge class. 9 | Parameters 10 | ---------- 11 | api_id: string 12 | The `api_id` can be found under API management under account settings. 13 | api_secret: string 14 | The `api_secret` can be found under API management under account settings. 15 | symbol: string (default "BTC") 16 | The asset you wish to delta-hedge. Currently only "BTC" and "ETH" are supported. 17 | threshold: float (default 0.10) 18 | The maximum absolute value of delta exposure to have at any given time. The default 19 | value is currently 0.10 which means the portfolio delta will fluctuate between -0.10 to 0.10 20 | of whichever asset you are trading. Any breach beyond this level will result in the portfolio 21 | being delta-hedged. 22 | 23 | Example 24 | --------- 25 | >>> import delta_hedge 26 | >>> id = "..." # replace your `api_id` in the quotes 27 | >>> secret = "..." # replace your `api_secret` in the quotes 28 | >>> dh = delta_hedge.Hedge(api_id=id, api_secret=secret, symbol="BTC", threshold=0.10) 29 | """ 30 | self.load = ccxt.deribit({'apiKey':api_id, 'secret':api_secret}) 31 | self.symbol = symbol 32 | self.threshold = abs(float(threshold)) 33 | 34 | if ((self.symbol != 'BTC') and (self.symbol !='ETH')): 35 | raise ValueError("Incorrect symbol - please choose between 'BTC' or 'ETH'") 36 | 37 | def current_delta(self): 38 | """ 39 | Retrives the current portfolio delta. 40 | 41 | Example 42 | --------- 43 | >>> dh.current_delta() 44 | 0.065 45 | """ 46 | return self.load.fetch_balance({'currency': str(self.symbol)})['info']['result']['delta_total'] 47 | 48 | def delta_hedge(self): 49 | """ 50 | Rebalances entire portfolio to be delta-neutral based on current delta exposure. 51 | """ 52 | current_delta = self.current_delta() 53 | # if delta is negative, we must BUY futures to hedge our negative exposure 54 | if current_delta < 0: sign = 'buy' 55 | # if delta is positive, we must SELL futures to hedge our positive exposure 56 | if current_delta > 0: sign = 'sell' 57 | # retrieve the average price of the perpetual future contract for the asset 58 | avg_price = np.mean(self.load.fetch_ohlcv(str(self.symbol)+"-PERPETUAL", limit=10)[-1][1:5]) 59 | # if the absolute delta exposure is greater than our threshold then we place a hedging trade 60 | if abs(current_delta) >= self.threshold: 61 | asset = str(self.symbol) + "-PERPETUAL" 62 | order_size = abs(current_delta*avg_price) 63 | self.load.create_market_order(asset, sign, order_size) 64 | print("Rebalancing trade to achieve delta-neutral portfolio:", str(sign), str(order_size/avg_price), str(self.symbol)) 65 | else: 66 | pass 67 | print("No need to hedge. Current portfolio delta:", current_delta) 68 | 69 | def run_loop(self): 70 | """ 71 | Runs the delta-hedge script in continuous loop. 72 | """ 73 | while True: 74 | try: 75 | self.delta_hedge() 76 | time.sleep(30) 77 | except: 78 | print("Script is broken - trying again in 30 seconds. Current portfolio delta:", self.current_delta()) 79 | time.sleep(30) 80 | pass 81 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | ccxt==1.30.71 2 | numpy==1.19.0 --------------------------------------------------------------------------------