Created
April 6, 2024 18:42
-
-
Save MarkRBest/7ed17ba33d78ae94200c402054fa2574 to your computer and use it in GitHub Desktop.
Risk manager using array indexing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from enum import IntEnum | |
class Exchange(IntEnum): | |
Binance = 0 | |
Bybit = 1 | |
Okx = 2 | |
class Currency(IntEnum): | |
BTC = 0 | |
ETH = 0 | |
SOL = 0 | |
class RiskManager: | |
def __init__(self, instruments): | |
self.lookups = {} | |
self.exchange_keys = {} | |
self.ccy_keys = {} | |
self.instrument_keys = {} | |
self.exchange_risk = [] | |
self.ccy_risk = [] | |
self.instrument_risk = [] | |
self.global_risk = 0.0 | |
for instrument, exchange, symbol, ccy in instruments: | |
if exchange not in self.exchange_keys: | |
self.exchange_keys[exchange] = len(self.exchange_keys) | |
self.exchange_risk.append(0.0) | |
if ccy not in self.ccy_keys: | |
self.ccy_keys[ccy] = len(self.ccy_keys) | |
self.ccy_risk.append(0.0) | |
if instrument not in self.instrument_keys: | |
self.instrument_keys[instrument] = len(self.instrument_keys) | |
self.instrument_risk.append(0.0) | |
# build index lookups | |
self.lookups[instrument] = (self.exchange_keys[exchange], | |
self.ccy_keys[ccy], | |
self.instrument_keys[instrument]) | |
# this assumes basically only dealing with usdt coins for netting | |
def update_risk(self, instrument, amount, price): | |
quoted_amount = amount * price | |
ex_idx, ccy_idx, iid_idx = self.lookups[instrument] | |
old_risk = self.instrument_risk[iid_idx] | |
self.global_risk -= old_risk | |
self.exchange_risk[ex_idx] -= old_risk | |
self.ccy_risk[ccy_idx] -= old_risk | |
# update with new risk | |
self.instrument_risk[iid_idx] = quoted_amount | |
self.global_risk += quoted_amount | |
self.exchange_risk[ex_idx] += quoted_amount | |
self.ccy_risk[ccy_idx] += quoted_amount | |
def get_global_risk(self): | |
return self.global_risk | |
def get_exchange_risk(self, exchange: Exchange): | |
return self.exchange_risk[self.exchange_keys[exchange]] | |
def get_currency_risk(self, ccy: Currency): | |
return self.ccy_risk[self.ccy_keys[ccy]] | |
def get_instrument_risk(self, instrument): | |
return self.instrument_risk[self.instrument_keys[instrument]] | |
if __name__ == "__main__" : | |
# this must be a complete set of all instruments that will be seen by the system | |
instruments = [(2343, Exchange.Binance, "BTCUSDT", Currency.BTC), | |
(7348, Exchange.Binance, "ETHUSDT", Currency.ETH), | |
(1298, Exchange.Bybit, "ETHUSDT", Currency.ETH), | |
(3453, Exchange.Okx, "SOL-USDT-SWAP", Currency.SOL),] | |
risk_manager = RiskManager(instruments=instruments) | |
risk_manager.update_risk(7348, 1.0, 3000.0) | |
print(risk_manager.global_risk) | |
print(risk_manager.exchange_risk) | |
print(risk_manager.ccy_risk) | |
print(risk_manager.instrument_risk) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment