当前位置:网站首页>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
边栏推荐
- Django uses the existing data table method of MySQL database
- Learning summary of time complexity and space complexity
- Dart array, map, type judgment, conditional judgment operator, type conversion
- [Jenkins' notes] introduction, free space; Continuous integration of enterprise wechat; Allure reports, continuous integration of email notifications; Build scheduled tasks
- 【idea】查询字段使用位置
- How to deal with the time, scope and cost constraints in the project?
- 递归与分治
- How to create a custom 404 error page in WordPress
- 18张图,直观理解神经网络、流形和拓扑
- [notes for question brushing] delete continuous nodes with a total value of zero from the linked list
猜你喜欢

Seven SQL performance optimizations that spark 3.0 must know

How to smoothly go online after MySQL table splitting?

Linux Redis 源码安装

20220728 sorting strings that are not pure numbers

一文让你搞懂MYSQL底层原理。-内部结构、索引、锁、集群

Seven marketing strategies of NFT project

mysql分表之后怎么平滑上线?

【Jenkins笔记】入门,自由空间;持续集成企业微信;allure报告,持续集成电子邮件通知;构建定时任务

图扑软件亮相 2022 福州数博会,携手共创数字新时代

Solid smart contract tutorial (5) -nft auction contract
随机推荐
转:认知亚文化
Cookies and sessions
Prometheus 的 API 稳定性保障
[unity] configure unity edit C as vscode
Learning summary of time complexity and space complexity
Flask project architecture (First Edition
Instruction rearrangement, happens before, as if serial
Rewriting method set
solidity实现智能合约教程(5)-NFT拍卖合约
Digital twin rail transit: "intelligent" monitoring to clear the pain points of urban operation
一文搞懂ES6基本全部语法
Summary of process and thread knowledge points 2
写作作业一
Naver 三方登录
IT硬件故障的主要原因和预防的最佳实践
Day2: 130 questions in three languages
How to carry out engineering implementation of DDD Domain Driven Design
[notes for question brushing] specified interval reversal in the linked list
Brushless DC motor controller (how much is the hall charge for changing the motor)
Mathematical modeling and detailed explanation of basic knowledge (common knowledge points of Chemistry)