我尝试构建向量化/并行股票回测程序。我实现了带有循环的顺序版本,但是现在我被困在矢量化功能上。 我正在为此使用Pandas / Numpy,这是一个简短的概述:
给定2列,左侧为订单数量(要添加到头寸),右侧为止损位(如果止损位为1,头寸将重置为0)
M = [[0.1, 0], # left column is order quantity, right is stop
[0.1, 0],
[0.5, 0],
[0.5, 0],
[0.3, 0],
[-0.3, 0], # negative order quantity means short or sell
[-0.1, 1]] # right column (stop) is 1, so position is reset to 0
我想根据初始矩阵M计算2列: 左列是基于订单数量的位置(范围从-1到1,但不能超过),右列则是已执行的订单数量
R = [[0.1, 0.1],
[0.2, 0.1],
[0.7, 0.5], # position (left column) is equal to cumsum of order quantity (from last stop trigger)
[1, 0.3], # executed quantity is < order quantity as it's the remainder to position's max of 1
[1, 0],
[0.7, -0.3],
[-0.1, -0.8]] # stop triggered, so position is reset to 0, and then -0.1 in order quantity is executed
问题在于每个条件都基于另一个条件。这是否意味着该任务不能同时解决?
我可以想象一种方法,该方法具有数量累加和索引,并在其中触发了止损,并应用于该累加以计算已执行的数量。 我希望您能找到解决此问题的优雅方法的任何提示。也许除了cumsum之外,还要研究哪些Numpy功能。
编辑:顺序版本的非常简化的版本:
orders = [{'quantity': 0.1,'stop': 0},{'quantity': 0.1,'stop': 0},{'quantity': 0.5,'stop': 0},{'quantity': 0.5,'stop': 0},{'quantity': 0.3,'stop': 0},{'quantity': -0.3,'stop': 0},{'quantity': -0.1,'stop': 1}]
position = 0
for order in orders:
position_beginning = position
if order['stop'] == 1:
position = 0
if order['quantity']+position <= 1 and order['quantity']+position >= -1:
position += order['quantity']
elif position < 0 and order['quantity'] < 0:
position = -1
elif position > 0 and order['quantity'] > 0:
position = 1
executed_quantity = abs(position - position_beginning) * (1 if position > position_beginning else -1)
print(position, executed_quantity)
在实际应用中,订单数量要复杂得多,例如分为子数量。回测器必须以子数量运行数百万个订单,这一事实使使用这种循环方法的过程变得非常缓慢。