# 高频交易策略的思考(1)

Author: 小草, Created: 2023-08-03 16:14:16, Updated: 2023-12-08 16:37:21

### 要解决的问题

1.策略同时挂买单和卖单，第一个问题就是在哪里挂单。挂的离盘口越近，成交的概率越高，但在剧烈波动的行情中，瞬间成交的价格可能离盘口较远，挂的那太近没有能吃到足够的利润。挂的太远的单子成交概率又低。这是一个需要优化的问题。

2.控制仓位。为了控制风险，策略就不能长时间累计过多的仓位。可以通过控制挂单距离、挂单量、总仓位限制等办法解决。

### 需要的数据

``````from datetime import date,datetime
import time
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
``````

2. price：成交价
3. quantity：成交的数量
6. transact_time：成交时间

``````trades = pd.read_csv('COMPUSDT-aggTrades-2023-07-02.csv')
``````

664475 rows × 7 columns

120719552 52.42 22.087 207862988 207862990 1688256004603 False
120719553 52.41 29.314 207862991 207863002 1688256004623 True
120719554 52.42 0.945 207863003 207863003 1688256004678 False
120719555 52.41 13.534 207863004 207863006 1688256004680 True
121384024 68.29 10.065 210364899 210364905 1688342399863 False
121384025 68.30 7.078 210364906 210364908 1688342399948 False
121384026 68.29 7.622 210364909 210364911 1688342399979 True

### 单笔成交量建模

``````trades['date'] = pd.to_datetime(trades['transact_time'], unit='ms')
'price': 'last',
'quantity': 'sum',
'date': 'last',
'transact_time':'last'
})
'price': 'last',
'quantity': 'sum',
'date': 'last',
'transact_time':'last'
})
``````
``````print(trades.shape[0] - (buy_trades.shape[0]+sell_trades.shape[0]))
``````
``````146181
``````

``````buy_trades['quantity'].plot.hist(bins=200,figsize=(10, 5));
``````

``````buy_trades['quantity'][buy_trades['quantity']<200].plot.hist(bins=200,figsize=(10, 5));
``````

``````depths = range(0, 250, 2)
probabilities_s = np.array([(1+depth/mean_quantity)**alpha for depth in depths])

plt.figure(figsize=(10, 5))
plt.plot(depths, probabilities)
plt.plot(depths, probabilities_s)
plt.xlabel('Depth')
plt.ylabel('Probability of execution')
plt.title('Execution probability at different depths')
plt.grid(True)
``````

``````plt.figure(figsize=(10, 5))
plt.grid(True)
plt.title('Diff')
plt.plot(depths, probabilities_s-probabilities);
``````

``````depths = range(0, 250, 2)
probabilities_s = np.array([(((1+20**(-depth/mean))*depth+mean)/mean)**alpha for depth in depths])

plt.figure(figsize=(10, 5))
plt.plot(depths, probabilities)
plt.plot(depths, probabilities_s)
plt.xlabel('Depth')
plt.ylabel('Probability of execution')
plt.title('Execution probability at different depths')
plt.grid(True)
``````
``````plt.figure(figsize=(10, 5))
plt.grid(True)
plt.title('Diff')
plt.plot(depths, probabilities_s-probabilities);
``````

More

fmzero 牛逼！