Time Entrysig Exitsig
7/16/2018 12:30 LongEntry
7/16/2018 13:00 LongExit
7/16/2018 13:30
7/16/2018 14:00
7/16/2018 14:30
7/16/2018 15:00
7/16/2018 15:30 ShortEntry LongExit <---wrong
7/16/2018 17:00
7/16/2018 17:30
7/16/2018 18:00
7/16/2018 18:30
7/16/2018 19:00 LongEntry ShortExit
7/16/2018 19:30
7/16/2018 20:00
7/16/2018 20:30
7/16/2018 21:00 ShortEntry LongExit
7/16/2018 21:30
7/16/2018 22:00
7/16/2018 22:30
7/16/2018 23:00
7/16/2018 23:30
7/17/2018 0:00
7/17/2018 0:30
7/17/2018 1:00
7/17/2018 1:30
7/17/2018 2:00 LongEntry ShortExit
7/17/2018 2:30
7/17/2018 3:00
我有一个类似上面的数据框。我试图在不做循环(如果可能的话)的情况下使其他2列多头和空头。在进入多头之后应该说“多头”,并持续多长时间直到发出“长头”信号。与“短”列相同。如果不是长,短或留空,则可以为0。两种方法都可以。
注意: 我的退出信号有一个问题,即使我已经退出交易,它仍然会返回退出信号,因此它应该忽略这些信号。 (不能两次退出同一多头交易)。我已经指出了一个错误的地方。我也在努力解决此问题,但这是一个单独的问题。
输出:
Date EntrySig ExitSig Long Short
7/16/2018 12:30 LongEntry Long
7/16/2018 13:00 LongExit Long
7/16/2018 13:30
7/16/2018 14:00
7/16/2018 14:30
7/16/2018 15:00
7/16/2018 15:30 ShortEntry LongExit Short
7/16/2018 17:00 Short
7/16/2018 17:30 Short
7/16/2018 18:00 Short
7/16/2018 18:30 Short
7/16/2018 19:00 LongEntry ShortExit Long Short
7/16/2018 19:30 Long
7/16/2018 20:00 Long
7/16/2018 20:30 Long
7/16/2018 21:00 ShortEntry LongExit Long Short
7/16/2018 21:30 Short
7/16/2018 22:00 Short
7/16/2018 22:30 Short
7/16/2018 23:00 Short
7/16/2018 23:30 Short
7/17/2018 0:00 Short
7/17/2018 0:30 Short
7/17/2018 1:00 Short
7/17/2018 1:30 Short
7/17/2018 2:00 LongEntry ShortExit Long Short
7/17/2018 2:30 Long
7/17/2018 3:00 Long
我尝试使用迭代:
ls = []
trades = bt_data.reset_index()
for i in trades.index:
if trades.iloc[i, trades.columns.get_loc('EntrySignal')] == 'LongEntry':
ls.append('Long')
elif i==0:
ls.append('Flat')
elif np.logical_and(ls[i-1] == 'Long',
trades.iloc[i-1, trades.columns.get_loc('ExitSignal')]
!='LongExit'):
ls.append('Long')
else:
ls.append('Flat')
将数据作为dict:
{'Date_Time': {0: Timestamp('2018-07-16 03:30:00'), 1: Timestamp('2018-07-16 04:00:00'), 2: Timestamp('2018-07-16 04:30:00'), 3: Timestamp('2018-07-16 05:00:00'), 4: Timestamp('2018-07-16 05:30:00'), 5: Timestamp('2018-07-16 06:00:00'), 6: Timestamp('2018-07-16 06:30:00'), 7: Timestamp('2018-07-16 07:00:00'), 8: Timestamp('2018-07-16 07:30:00'), 9: Timestamp('2018-07-16 08:00:00'), 10: Timestamp('2018-07-16 08:30:00'), 11: Timestamp('2018-07-16 09:00:00'), 12: Timestamp('2018-07-16 09:30:00'), 13: Timestamp('2018-07-16 10:00:00'), 14: Timestamp('2018-07-16 10:30:00'), 15: Timestamp('2018-07-16 11:00:00'), 16: Timestamp('2018-07-16 11:30:00'), 17: Timestamp('2018-07-16 12:00:00'), 18: Timestamp('2018-07-16 12:30:00'), 19: Timestamp('2018-07-16 13:00:00'), 20: Timestamp('2018-07-16 13:30:00'), 21: Timestamp('2018-07-16 14:00:00'), 22: Timestamp('2018-07-16 14:30:00'), 23: Timestamp('2018-07-16 15:00:00'), 24: Timestamp('2018-07-16 15:30:00'), 25: Timestamp('2018-07-16 17:00:00'), 26: Timestamp('2018-07-16 17:30:00'), 27: Timestamp('2018-07-16 18:00:00'), 28: Timestamp('2018-07-16 18:30:00'), 29: Timestamp('2018-07-16 19:00:00'), 30: Timestamp('2018-07-16 19:30:00'), 31: Timestamp('2018-07-16 20:00:00'), 32: Timestamp('2018-07-16 20:30:00'), 33: Timestamp('2018-07-16 21:00:00'), 34: Timestamp('2018-07-16 21:30:00'), 35: Timestamp('2018-07-16 22:00:00'), 36: Timestamp('2018-07-16 22:30:00'), 37: Timestamp('2018-07-16 23:00:00'), 38: Timestamp('2018-07-16 23:30:00'), 39: Timestamp('2018-07-17 00:00:00'), 40: Timestamp('2018-07-17 00:30:00'), 41: Timestamp('2018-07-17 01:00:00'), 42: Timestamp('2018-07-17 01:30:00'), 43: Timestamp('2018-07-17 02:00:00'), 44: Timestamp('2018-07-17 02:30:00'), 45: Timestamp('2018-07-17 03:00:00'), 46: Timestamp('2018-07-17 03:30:00'), 47: Timestamp('2018-07-17 04:00:00'), 48: Timestamp('2018-07-17 04:30:00'), 49: Timestamp('2018-07-17 05:00:00'), 50: Timestamp('2018-07-17 05:30:00'), 51: Timestamp('2018-07-17 06:00:00'), 52: Timestamp('2018-07-17 06:30:00'), 53: Timestamp('2018-07-17 07:00:00'), 54: Timestamp('2018-07-17 07:30:00'), 55: Timestamp('2018-07-17 08:00:00'), 56: Timestamp('2018-07-17 08:30:00'), 57: Timestamp('2018-07-17 09:00:00'), 58: Timestamp('2018-07-17 09:30:00'), 59: Timestamp('2018-07-17 10:00:00'), 60: Timestamp('2018-07-17 10:30:00'), 61: Timestamp('2018-07-17 11:00:00'), 62: Timestamp('2018-07-17 11:30:00'), 63: Timestamp('2018-07-17 12:00:00'), 64: Timestamp('2018-07-17 12:30:00'), 65: Timestamp('2018-07-17 13:00:00'), 66: Timestamp('2018-07-17 13:30:00'), 67: Timestamp('2018-07-17 14:00:00'), 68: Timestamp('2018-07-17 14:30:00'), 69: Timestamp('2018-07-17 15:00:00'), 70: Timestamp('2018-07-17 15:30:00'), 71: Timestamp('2018-07-17 17:00:00'), 72: Timestamp('2018-07-17 17:30:00'), 73: Timestamp('2018-07-17 18:00:00'), 74: Timestamp('2018-07-17 18:30:00'), 75: Timestamp('2018-07-17 19:00:00')}, 'EntrySignal': {0: '', 1: '', 2: '', 3: '', 4: '', 5: '', 6: '', 7: '', 8: '', 9: '', 10: '', 11: '', 12: '', 13: '', 14: '', 15: '', 16: '', 17: '', 18: '', 19: 'LongEntry', 20: '', 21: '', 22: '', 23: '', 24: '', 25: 'ShortEntry', 26: '', 27: '', 28: '', 29: '', 30: 'LongEntry', 31: '', 32: '', 33: '', 34: 'ShortEntry', 35: '', 36: '', 37: '', 38: '', 39: '', 40: '', 41: '', 42: '', 43: '', 44: 'LongEntry', 45: '', 46: '', 47: '', 48: 'ShortEntry', 49: '', 50: 'LongEntry', 51: '', 52: 'ShortEntry', 53: '', 54: '', 55: '', 56: '', 57: '', 58: '', 59: '', 60: '', 61: 'LongEntry', 62: '', 63: '', 64: '', 65: '', 66: '', 67: '', 68: 'ShortEntry', 69: '', 70: '', 71: '', 72: '', 73: '', 74: 'LongEntry', 75: ''}, 'ExitSignal': {0: '', 1: '', 2: '', 3: '', 4: '', 5: '', 6: '', 7: '', 8: '', 9: '', 10: '', 11: '', 12: '', 13: '', 14: '', 15: '', 16: '', 17: '', 18: '', 19: 'ShortExit', 20: 'LongExit', 21: '', 22: '', 23: '', 24: '', 25: 'LongExit', 26: '', 27: '', 28: '', 29: '', 30: 'ShortExit', 31: '', 32: '', 33: '', 34: 'LongExit', 35: '', 36: '', 37: '', 38: '', 39: '', 40: '', 41: '', 42: '', 43: '', 44: 'ShortExit', 45: '', 46: '', 47: '', 48: 'LongExit', 49: '', 50: 'ShortExit', 51: '', 52: 'LongExit', 53: '', 54: '', 55: 'ShortExit', 56: '', 57: '', 58: '', 59: '', 60: '', 61: 'ShortExit', 62: 'LongExit', 63: '', 64: '', 65: '', 66: '', 67: '', 68: 'LongExit', 69: '', 70: 'ShortExit', 71: '', 72: '', 73: '', 74: 'ShortExit', 75: ''}}
答案 0 :(得分:0)
如果您可以删除错误的出口,则可以通过累加条目并累计去除出口并将值(1,0)映射到(长/短,不长/不短)来实现:
df['Long'] = (df['EntrySignal'] == 'LongEntry').cumsum() - (df['ExitSignal'] == 'LongExit').cumsum()
df['Short'] = (df['EntrySignal'] == 'ShortEntry').cumsum() - (df['ExitSignal'] == 'ShortExit').cumsum()
df['Long'] = df['Long'].map({1: 'Long', 0: ''})
df['Short'] = df['Short'].map({1: 'Short', 0: ''})
注意:错误输入的值将小于0,并且您将需要更复杂的逻辑。