Skip to content

Commit

Permalink
feat(exchanges): consolidate data
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakl committed Mar 11, 2024
1 parent e01eb08 commit 4e83e7f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
5 changes: 2 additions & 3 deletions exchanges/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ def main():

consolidated_options = Processing().consolidate_quotes(global_orderbook_options)

global_orderbook_futures.to_json("futures.json", orient="records", indent=4)
global_orderbook_options.to_json("options.json", orient="records", indent=4)
consolidated_options.to_json("consolidated_options.json", orient="records", indent=4)
process_quotes = Processing().process_quotes(consolidated_options)
process_quotes.to_json("quotes.json", orient="records", indent=4)

except Exception as e:
logger.error(f"An unexpected error occurred in the main function: {e}")
Expand Down
39 changes: 24 additions & 15 deletions exchanges/processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,31 @@ def eliminate_invalid_quotes(df):
return df

@staticmethod
def calculate_spreads(df):
df = df.copy()
df["bid_spread"] = (df["mark_price"] - df["bid"]).clip(lower=0)
df["ask_spread"] = (df["ask"] - df["mark_price"]).clip(lower=0)
df["spread"] = df["bid_spread"] + df["ask_spread"]
return df
def process_quotes(df: pd.DataFrame) -> pd.DataFrame:
# Calculate spreads
df['bid_spread'] = df['mark_price'] - df['bid']
df['ask_spread'] = df['ask'] - df['mark_price']

@staticmethod
def remove_large_spreads(df):
df = df.copy()
df["max_spread"] = (
df[["bid_spread", "ask_spread"]].min(axis=1) * SPREAD_MULTIPLIER
)
df = df[df["spread"] <= df["max_spread"]]
df["global_max_spread"] = SPREAD_MIN * SPREAD_MULTIPLIER
df = df[df["spread"] <= df["global_max_spread"]]
# Set spreads to zero if negative
df['bid_spread'] = df['bid_spread'].apply(lambda x: x if x > 0 else 0)
df['ask_spread'] = df['ask_spread'].apply(lambda x: x if x > 0 else 0)

# Calculate total spread
df['spread'] = df['bid_spread'] + df['ask_spread']

# Calculate MAS
df['MAS'] = df[['bid_spread', 'ask_spread']].min(axis=1) * SPREAD_MULTIPLIER

# Calculate GMS
GMS = SPREAD_MIN * SPREAD_MULTIPLIER

# Filter using corrected conditions
df = df[(df['spread'] <= GMS) | (df['spread'] <= df['MAS'])].index

# Ensure modifications are made on the original DataFrame to avoid the warning
df["mid_price"] = (df["bid"] + df["ask"]) / 2

# Return the filtered DataFrame without temporary columns
return df

@staticmethod
Expand Down

0 comments on commit 4e83e7f

Please sign in to comment.