当前位置:网站首页>Digital currency of quantitative transactions - generate foot print factor data
Digital currency of quantitative transactions - generate foot print factor data
2022-07-29 01:23:00 【Post-Truth】
import datetime
import time
import pandas as pd
from source_data.tqz_load_source_data import TQZSourceData
# from public_module.tqz_extern.tools.pandas_operator.pandas_operator import pandas
pd.set_option('mode.chained_assignment', None)
class TQZFootPrintTool:
@classmethod
def get_timestamp(cls, date_str: str):
dt_str = f'{date_str} 08:00:00'
s_timestamp = time.strptime(dt_str, "%Y-%m-%d %H:%M:%S")
return int(time.mktime(s_timestamp) * 1000)
if __name__ == '__main__':
symbol = 'BTCUSDT'
start_date, end_date = datetime.datetime.strptime('2022-05-01', '%Y-%m-%d').date(), datetime.datetime.strptime('2022-05-02', '%Y-%m-%d').date()
mergeTrades_data = TQZSourceData.load_mergeTrades_data(date_str=str(start_date), symbol=symbol)
interval = 15 * 60000
imbalance_price_ratio = 3
qty_digital_point_counts = 3
bar_start_timestamp = TQZFootPrintTool.get_timestamp(date_str=str(start_date))
bar_end_timestamp = int(bar_start_timestamp) + interval
daily_footPrint_df = pd.DataFrame(columns=["price", "timestamp", "bid_size", "ask_size", "bid_imbalance", "ask_imbalance"])
while True:
bar_trades_data = mergeTrades_data[(bar_start_timestamp <= mergeTrades_data['datetime']) & (mergeTrades_data['datetime'] < bar_end_timestamp)]
# 1. create bar_price_size_df
bar_price_size_df = pd.DataFrame(columns=["price", "bid_size", "ask_size", "bid_imbalance", "ask_imbalance"])
for value in list(bar_trades_data.groupby('price')):
bar_price_size_df = bar_price_size_df.append({
'price': value[0],
'ask_size': round(value[1][value[1]["isBuyerMaker"] == False]['qty'].sum(), qty_digital_point_counts),
'bid_size': round(value[1][value[1]["isBuyerMaker"] == True]['qty'].sum(), qty_digital_point_counts)
}, ignore_index=True)
bar_price_size_df['ask_size_shift_-1'] = bar_price_size_df['ask_size'].shift(-1)
bar_price_size_df['bid_size_shift_1'] = bar_price_size_df['bid_size'].shift(1)
bar_price_size_df.dropna(subset=['ask_size_shift_-1', 'bid_size_shift_1'], inplace=True)
bar_price_size_df.loc[bar_price_size_df['bid_size'] > (imbalance_price_ratio * bar_price_size_df['ask_size_shift_-1']), 'bid_imbalance'] = True
bar_price_size_df.loc[(imbalance_price_ratio * bar_price_size_df['bid_size_shift_1']) < bar_price_size_df['ask_size'], 'ask_imbalance'] = True
bar_price_size_df.fillna(False, inplace=True)
bar_price_size_df.sort_values('price', ascending=False, inplace=True)
bar_price_size_df.reset_index(inplace=True)
del bar_price_size_df['index']
del bar_price_size_df['ask_size_shift_-1']
del bar_price_size_df['bid_size_shift_1']
# 2. merge daily_footPrint_df & bar_price_size_df
bar_price_size_df['timestamp'] = bar_start_timestamp
daily_footPrint_df = pd.concat([daily_footPrint_df, bar_price_size_df], axis=0)
# 3. update bar_start_timestamp & bar_end_timestamp at last.
bar_start_timestamp = bar_end_timestamp
bar_end_timestamp = int(bar_start_timestamp) + interval
# 4. next day.
if str(pd.to_datetime(f'{bar_start_timestamp}', unit='ms').date()) != str(start_date):
# 4.1 write foot_print.csv.
daily_footPrint_df.to_csv(f'F:/tqz_test/{symbol}_{str(int(interval / 60000))}mBar_footPrint_{str(start_date)}.csv', index=False)
# 4.2 change start_date & mergeTrades_data & daily_footPrint_df.
start_date += datetime.timedelta(days=1)
mergeTrades_data = TQZSourceData.load_mergeTrades_data(date_str=str(start_date), symbol=symbol)
daily_footPrint_df = pd.DataFrame(columns=["price", "timestamp", "bid_size", "ask_size", "bid_imbalance", "ask_imbalance"])
if start_date > end_date:
break
边栏推荐
- Docker-compose安装mysql
- Transfer: cognitive subculture
- DVWA之SQL注入
- Django使用MySQL数据库已经存在的数据表方法
- Charles -- teach you how to use the packet capturing tool from 0-1
- Naver three party login
- Django uses the existing data table method of MySQL database
- 北京护照西班牙语翻译推荐
- QT static compiler (MinGW compilation)
- Common functions and usage of numpy
猜你喜欢
随机推荐
Recommended Spanish translation of Beijing passport
vm options、program arguments、environment property
ActiveMQ基本详解
[MySQL] historical cumulative de duplication of multiple indicators
Cookie和Session
Django uses the existing data table method of MySQL database
Regular checksum time formatting
Cookies and sessions
[notes for question brushing] specified interval reversal in the linked list
Machine learning | matlab implementation of RBF radial basis function neural network Newrbe parameter setting
北京护照西班牙语翻译推荐
Closures and decorators
【mysql】字符串转int
Common functions and usage of numpy
[unity] configure unity edit C as vscode
“index [hotel/jXLK5MTYTU-jO9WzJNob4w] already exists“
数字孪生轨道交通:“智慧化”监控疏通城市运行痛点
Bracket matching test
Learning summary of time complexity and space complexity
Summary of process and thread knowledge points 2






![[unity] configure unity edit C as vscode](/img/f6/5675a96115fb97737c8b36a3fcc6ed.png)


