本期分析一下两个平台的价差
之前收集过订单薄数据和交易数据,请见:
价差图分析
首先清洗数据,保留前3的ask和bid
import os
import pandas as pd
# 输入文件夹和输出文件夹的相对路径
input_folder = 'D:\DYDX_data\depthdata' # 输入文件夹位于当前工作目录下
output_folder = 'D:\DYDX_data\depthdata\datafilter' # 输出文件夹位于当前工作目录下
# 获取输入文件夹中的所有CSV文件
input_files = [f for f in os.listdir(input_folder) if f.endswith('.csv')]
# 遍历每个CSV文件
for input_file in input_files:
input_file_path = os.path.join(input_folder, input_file)
# 从CSV文件加载数据
df = pd.read_csv(input_file_path)
# 使用groupby和cumcount来分组并获取前3个bid和ask
df['row_num'] = df.groupby(['Timestamp', 'Type']).cumcount() + 1
result = df[df['row_num'] <= 3]
# 删除辅助列row_num
result = result.drop(columns=['row_num'])
# 更改输出文件名,加上'_filter'前缀
output_file = input_file.replace('.csv', '_filter.csv')
output_file_path = os.path.join(output_folder, output_file)
# 确保输出文件夹存在
os.makedirs(output_folder, exist_ok=True)
# 将处理后的数据保存到新的CSV文件
result.to_csv(output_file_path, index=False)
print(f"Processed {input_file} and saved as {output_file}")
print("All files processed.")
而后生成价差图
import pandas as pd
import matplotlib.pyplot as plt
# 读取第一个表的数据
data1 = pd.read_csv("order_book_BTC-USD_dydx_filter.csv") # 替换为第一个表格文件的路径
# 读取第二个表的数据
data2 = pd.read_csv("order_book_BTCUSDC_apex_filter.csv") # 替换为第二个表格文件的路径
# 分别获取ask和bid的数据
ask_data1 = data1[data1["Type"] == "ask"]
ask_data2 = data2[data2["Type"] == "ask"]
bid_data1 = data1[data1["Type"] == "bid"]
bid_data2 = data2[data2["Type"] == "bid"]
# 确定处理的数据量,以数据较少的表为基准
min_data_length = min(len(ask_data1), len(ask_data2), len(bid_data1), len(bid_data2))
# 仅保留相同数量的数据,并按每3个ask和3个bid为一组
ask_data1 = ask_data1[:min_data_length].iloc[::3]
bid_data1 = bid_data1[:min_data_length].iloc[::3]
ask_data2 = ask_data2[:min_data_length].iloc[::3]
bid_data2 = bid_data2[:min_data_length].iloc[::3]
# 将Unix时间戳转换为标准时间
ask_data1["Timestamp"] = pd.to_datetime(ask_data1["Timestamp"], unit='s')
bid_data2["Timestamp"] = pd.to_datetime(bid_data2["Timestamp"], unit='s')
ask_data2["Timestamp"] = pd.to_datetime(ask_data2["Timestamp"], unit='s')
bid_data1["Timestamp"] = pd.to_datetime(bid_data1["Timestamp"], unit='s')
# 计算价格差值
dydx_apex_diff = ask_data1["Price"].values - bid_data2["Price"].values
apex_dydx_diff = ask_data2["Price"].values - bid_data1["Price"].values
# 绘制价差图
plt.figure(figsize=(10, 6))
plt.subplot(2, 1, 1)
plt.plot(ask_data1["Timestamp"], dydx_apex_diff, label="Price Difference")
plt.title("DYDX-APEX Price Difference Chart")
plt.xlabel("Timestamp")
plt.legend()
plt.subplot(2, 1, 2)
plt.plot(ask_data2["Timestamp"], apex_dydx_diff, label="Price Difference")
plt.title("APEX-DYDX Price Difference Chart")
plt.xlabel("Timestamp")
plt.legend()
plt.tight_layout()
plt.show()
小问题:
价差应是A的买一价-B的卖一价,一开始我写的A的买一价-B的买一价。
BTC价差图如下图:
ETH价差图如下图:
对价差分布区间进行分析:
import pandas as pd
import matplotlib.pyplot as plt
# 读取第一个表的数据
data1 = pd.read_csv("order_book_ETH-USD_dydx_filter.csv") # 替换为第一个表格文件的路径
# 读取第二个表的数据
data2 = pd.read_csv("order_book_ETHUSDC_apex_filter.csv") # 替换为第二个表格文件的路径
# 分别获取ask和bid的数据
ask_data1 = data1[data1["Type"] == "ask"]
ask_data2 = data2[data2["Type"] == "ask"]
bid_data1 = data1[data1["Type"] == "bid"]
bid_data2 = data2[data2["Type"] == "bid"]
# 确定处理的数据量,以数据较少的表为基准
min_data_length = min(len(ask_data1), len(ask_data2), len(bid_data1), len(bid_data2))
# 仅保留相同数量的数据,并按每3个ask和3个bid为一组
ask_data1 = ask_data1[:min_data_length].iloc[::3]
bid_data1 = bid_data1[:min_data_length].iloc[::3]
ask_data2 = ask_data2[:min_data_length].iloc[::3]
bid_data2 = bid_data2[:min_data_length].iloc[::3]
# 将Unix时间戳转换为标准时间
ask_data1["Timestamp"] = pd.to_datetime(ask_data1["Timestamp"], unit='s')
bid_data2["Timestamp"] = pd.to_datetime(bid_data2["Timestamp"], unit='s')
ask_data2["Timestamp"] = pd.to_datetime(ask_data2["Timestamp"], unit='s')
bid_data1["Timestamp"] = pd.to_datetime(bid_data1["Timestamp"], unit='s')
# 计算价格差值
dydx_apex_diff = ask_data1["Price"].values - bid_data2["Price"].values
apex_dydx_diff = ask_data2["Price"].values - bid_data1["Price"].values
# 绘制价差直方图 ,range根据具体品种调整
plt.figure(figsize=(10, 6))
plt.subplot(2, 1, 1)
plt.hist(dydx_apex_diff, bins=100, range=(-5, 5), edgecolor='k')
plt.title("DYDX-APEX Price Difference Distribution ")
plt.xlabel("Price Difference")
plt.ylabel("Frequency")
plt.subplot(2, 1, 2)
plt.hist(apex_dydx_diff, bins=100, range=(-5, 5), edgecolor='k')
plt.title("APEX-DYDX Price Difference Distribution ")
plt.xlabel("Price Difference")
plt.ylabel("Frequency")
plt.tight_layout()
plt.show()
生成的价差分布图如下:
ETH:
BTC:
由于DYDX和APEX基于以太坊二层,gas费用较低,故而是有利可图的,具体gas得发交易测试一下。
gas费
ETH-USDC swap ~10U
DYDX
authorization ~5U (一次性花费)
deposit 8~15U ( >500U的存款免gas费,每周限免3次)
交易费率(无gas费,只收手续费):
APEX
authorization ~5U
deposit 8~15U
交易费率(无gas费,只收手续费):
Maker fees are at 0.02% and taker fees are at 0.05%.