Skip to content

Commit

Permalink
fix(exchanges): fixing all mess
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakl committed Mar 28, 2024
1 parent a026670 commit 8d01768
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 159 deletions.
2 changes: 1 addition & 1 deletion exchanges/exchange_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from pandas import DataFrame

from exchanges.fetchers.binance_fetcher import BinanceFetcher
from exchanges.handlers.merge import MergeMarketHandler
from exchanges.handlers.future_and_options_handler import MergeMarketHandler

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
Expand Down
19 changes: 0 additions & 19 deletions exchanges/handlers/future.py

This file was deleted.

37 changes: 37 additions & 0 deletions exchanges/handlers/future_and_options_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from typing import List

import pandas as pd

from exchanges.fetchers.future_fetcher import FutureFetcher
from exchanges.fetchers.option_fetcher import OptionFetcher
from exchanges.processing import Processing


class MergeMarketHandler:
def __init__(self, exchange, market_types):
self.exchange = exchange
self.future_data_fetcher = FutureFetcher(exchange)
self.options_data_fetcher = OptionFetcher(exchange)
self.market_types = market_types
self.processing = Processing()


"""
Fetches the options and future market data for the given exchange and market types (e.g. "BTC").
"""
def handle(
self, options_market: List[str]
) -> tuple[pd.DataFrame, pd.DataFrame]:
options_data = self.options_data_fetcher.fetch_market_data(
options_market, str(self.exchange)
)
options_data = self.processing.eliminate_invalid_quotes(options_data)

"""
First, we fetch the future market symbols for the given exchange and market types (e.g. "BTC").
Then, we fetch all the implied interest rates for the future market symbols.
"""
futures_symbols = self.future_data_fetcher.fetch_future_market_symbols("BTC")
future_data = self.future_data_fetcher.fetch_all_implied_interest_rates(futures_symbols)

return options_data, future_data
26 changes: 0 additions & 26 deletions exchanges/handlers/merge.py

This file was deleted.

27 changes: 0 additions & 27 deletions exchanges/handlers/option.py

This file was deleted.

6 changes: 0 additions & 6 deletions exchanges/handlers/spot.py

This file was deleted.

75 changes: 12 additions & 63 deletions exchanges/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,69 +15,18 @@

def main():
try:
# deribit = DeribitManager(pairs_to_load=["BTC/USD:BTC"], market_types=["option"])
# binance = BinanceManager(
# pairs_to_load=["BTC/USD:BTC"], market_types=["option", "future"]
# )
# okx = OKXManager(pairs_to_load=["BTC/USD:BTC"], market_types=["option"])
# global_orderbook_options = pd.DataFrame()
# global_orderbook_futures = pd.DataFrame()
#
#
# for manager in [binance, deribit]:
# options, futures = manager.load_specific_pairs()
# global_orderbook_options = pd.concat([global_orderbook_options, options]).reset_index(drop=True)
# global_orderbook_futures = pd.concat([global_orderbook_futures, futures]).reset_index(drop=True)
#
# consolidated_options = Processing().consolidate_quotes(global_orderbook_options)
#
# global_orderbook_futures.to_json("futures.json", orient="records", indent=4)
#
# process_quotes = Processing().process_quotes(consolidated_options)
# process_quotes.to_json("quotes.json", orient="records", indent=4)
# process_quotes.to_csv("quotes.csv", index=False)
process_quotes = pd.read_json("quotes.json")
futures = pd.read_json("futures.json")
filter_near_next_term_options = Processing().filter_near_next_term_options(
process_quotes
)
near_term_options, next_term_options = filter_near_next_term_options
near_term_options.to_json("near_term_options.json", orient="records", indent=4)
next_term_options.to_json("next_term_options.json", orient="records", indent=4)
near_term_implied_forward_price = Processing().calculate_implied_forward_price(
near_term_options
)
otm_final = Processing().filter_and_sort_options(
near_term_options, near_term_implied_forward_price
)

otm_final.to_csv("otm_final.csv", index=False)

find_missing_expiries_near_term = Processing().find_missing_expiries(
otm_final, futures
)
interpolate_implied_interest_rates_near_term = (
Processing().interpolate_implied_interest_rates(
futures, find_missing_expiries_near_term
)
)
calculate_wij = Processing().calculate_wij(
otm_final, interpolate_implied_interest_rates_near_term
)
calculate_sigma_it_squared = Processing().calculate_sigma_it_squared(
calculate_wij, otm_final
)
calculate_sigma_it_squared.to_csv("sigma_near_squared.csv", index=False)
calculate_wij.to_csv("calculate_wij.csv", index=False)

interpolate_implied_interest_rates_near_term.to_csv(
"interpolate_implied_interest_rates_near_term.csv", index=False
)
interpolate_implied_interest_rates_near_term.to_json(
"interpolate_implied_interest_rates_near_term.json",
orient="records",
indent=4,
)
deribit = DeribitManager(pairs_to_load=["BTC/USD:BTC"], market_types=["option"])
binance = BinanceManager(
pairs_to_load=["BTC/USD:BTC"], market_types=["option", "future"]
)
okx = OKXManager(pairs_to_load=["BTC/USD:BTC"], market_types=["option"])
global_orderbook_options = pd.DataFrame()
global_orderbook_futures = pd.DataFrame()

for manager in [binance, deribit]:
options, futures = manager.load_specific_pairs()
global_orderbook_options = pd.concat([global_orderbook_options, options]).reset_index(drop=True)
global_orderbook_futures = pd.concat([global_orderbook_futures, futures]).reset_index(drop=True)

except Exception as e:
logger.error(f"An unexpected error occurred in the main function: {e}")
Expand Down
30 changes: 13 additions & 17 deletions exchanges/processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,44 +146,40 @@ def filter_and_sort_options(df, Fimp):
@staticmethod
def calculate_wij(strike_prices_df, interest_rates_df):
interest_rates_df["expiry"] = pd.to_datetime(interest_rates_df["expiry"])
strike_prices_df["expiry"] = pd.to_datetime(strike_prices_df["expiry"])

strike_prices_df.sort_values(by=["expiry", "strike"], inplace=True)

merged_df = strike_prices_df.merge(
interest_rates_df, on="expiry", how="left", suffixes=("_x", "_y")
)

merged_df["K_prev"] = merged_df["strike"].shift(1)
merged_df["K_next"] = merged_df["strike"].shift(-1)

merged_df["Delta_K"] = (merged_df["K_next"] - merged_df["K_prev"]) / 2

merged_df["Delta_K"].fillna(method="bfill", inplace=True)
merged_df["Delta_K"].fillna(method="ffill", inplace=True)


merged_df["w_ij"] = (
np.exp(merged_df["implied_interest_rate"] * merged_df["years_to_expiry"])
* merged_df["Delta_K"]
) / (merged_df["strike"] ** 2)

return merged_df[["expiry", "strike", "w_ij"]]
return merged_df

@staticmethod
def calculate_sigma_it_squared(w_ij_df, option_prices_df):
option_prices_df["expiry"] = pd.to_datetime(option_prices_df["expiry"])
option_prices_df.sort_values(by=["expiry", "strike"], inplace=True)
option_prices_df.to_csv("option_prices.csv", index=False)

merged_df = w_ij_df.merge(
option_prices_df, on=["expiry", "strike"], how="left", suffixes=("_x", "_y")
)

merged_df["sigma_it_squared"] = (1 / merged_df["years_to_expiry"]) * (
0.5 * (merged_df["w_ij"] * merged_df["mid_price"]).sum()
- (merged_df["Fimp"] / merged_df["KATM"] - 1) ** 2
def calculate_sigma_it_squared_for_all(w_ij_df):
T_i = w_ij_df['years_to_expiry'].mean()
F_i = w_ij_df['Fimp'].mean()
K_i_ATM = w_ij_df['KATM'].mean()

sigma_squared = (1 / T_i) * (
np.sum(0.5 * w_ij_df['w_ij'] * w_ij_df['mid_price']) -
((F_i / K_i_ATM) - 1) ** 2 * len(w_ij_df)
)

return merged_df[["expiry", "strike", "sigma_it_squared"]]

return sigma_squared
@staticmethod
def find_missing_expiries(options_df, futures_df):
options_expiries = options_df["expiry"].unique()
Expand Down

0 comments on commit 8d01768

Please sign in to comment.