| from utils.auxiliaries import create_dataframe |
| from scrapper.scrapper import Polygon |
| import pandas as pd |
| import numpy as np |
| import os |
|
|
|
|
| def calculate_ofi_and_delta(ticker, date, timeframe): |
|
|
| def tick_rule(dataframe): |
| return np.sign(dataframe["price"].diff().fillna(0).values) |
|
|
| def order_flow_imbalance(group): |
| group = quotes_dataframe.iloc[group.index] |
| try: |
|
|
| first_term = (group['bid_price'] >= group['bid_price'].shift(1)) * group['bid_size'] |
| second_term = (group['bid_price'] <= group['bid_price'].shift(1)) * group['bid_size'].shift(1) |
| third_term = (group['ask_price'] <= group['ask_price'].shift(1)) * group['ask_size'] |
| fourth_term = (group['ask_price'] >= group['ask_price'].shift(1)) * group['ask_size'].shift(1) |
|
|
| return pd.Series((first_term - second_term - third_term + fourth_term).sum()) |
|
|
| except KeyError: |
| pass |
|
|
| try: |
| PATH = f'data\\{ticker}\\trade' |
| file_name = f'{ticker}_trade_from_{date}_to_{date}.csv' |
| trades_dataframe = pd.read_csv(os.path.join(PATH, file_name), index_col=0, parse_dates=True) |
| trades_dataframe['participant_timestamp'] = pd.to_datetime(trades_dataframe['participant_timestamp']) |
|
|
| except FileNotFoundError: |
| trades_dataframe = Polygon().get_trades(ticker=ticker, from_date=date, to_date=date) |
|
|
| try: |
| PATH = f'data\\{ticker}\\quote' |
| file_name = f'{ticker}_quote_from_{date}_to_{date}.csv' |
| quotes_dataframe = pd.read_csv(os.path.join(PATH, file_name), index_col=0, parse_dates=True) |
| quotes_dataframe['participant_timestamp'] = pd.to_datetime(quotes_dataframe['participant_timestamp']) |
|
|
| except FileNotFoundError: |
| quotes_dataframe = Polygon().get_quotes(ticker=ticker, from_date=date, to_date=date) |
|
|
| trades_dataframe['type'] = tick_rule(trades_dataframe) |
| trades_dataframe['type'] = trades_dataframe['type'].replace(0, np.nan).ffill() |
| trades_dataframe = trades_dataframe.groupby([pd.Grouper(key='participant_timestamp', freq=timeframe)]).apply(create_dataframe) |
|
|
| try: |
| trades_dataframe = trades_dataframe.droplevel(1) |
| except IndexError: |
| pass |
|
|
| trades_dataframe['OFI'] = quotes_dataframe.groupby([pd.Grouper(key='participant_timestamp', freq=timeframe)]).apply(order_flow_imbalance) |
| trades_dataframe['return'] = trades_dataframe['close'].pct_change() |
| trades_dataframe.index.name = 'date' |
|
|
| return trades_dataframe |
|
|