0

假设我收集(在列表中)在特定时间段内(比如上午 11 点后的前 5 分钟)发生的所有交易,用于 n 个股票(为简单起见,我将 n=2 并稍后调整)。假设我们有坚定的 AAA 和坚定的 BBB(如果有帮助,liststocks=['AAA', 'BBB'])。该列表看起来像:

    trades=[['AAA', '2011-01-03', '11:03:51', 21.5],['BBB', '2011-01-03','11:03:57', 31.5],
['AAA', '2011-01-03', '11:04:20', 21.55],
['BBB', '2011-01-03','11:04:19', 32.01], ['BBB', '2011-01-03','11:04:52', 31.7]]

即,股票 AAA 的 2 笔交易和股票 BBB 的 3 笔交易。选择每只股票的最后一笔交易会导致缺乏同步性问题。这个想法是选择每只股票的最后一笔交易并找到最早的交易(['AAA', '2011-01-03', '11:04:20', 21.55])。然后选择时间尽可能接近'11:04:20'的所有其他股票的交易,这将导致我们选择['BBB', '2011-01-03','11:04:19', 32.01 ]。输出应该是一个列表,如:

    C=[['AAA', '2011-01-03', '11:04:20', 21.55],['BBB', '2011-01-03','11:04:19', 32.01]]

非常感谢!

4

2 回答 2

1

如果sortedkey参数一起使用,它并不难。

不想看的话,下面是代码,后面我会解释:

from datetime import datetime

trades=[['AAA', '2011-01-03', '11:03:51', 21.5],['BBB', '2011-01-03','11:03:57', 31.5],
['AAA', '2011-01-03', '11:04:20', 21.55],
['BBB', '2011-01-03','11:04:19', 32.01], ['BBB', '2011-01-03','11:04:52', 31.7]]

trades=[[i[0], datetime.strptime(i[1]+" "+i[2], "%Y-%m-%d %H:%M:%S"), i[3]] for i in trades]

most_liquid, *others, least_liquid = sorted(set(i[0] for i in trades), key=trades.count)

A=sorted((i for i in trades if i[0]==least_liquid), key=lambda n: n[1])[-1]
B=sorted((i for i in trades if i[0]==most_liquid), key=lambda n: abs(n[1]-A[1]))[0]

这样做是它首先将每笔交易从使用时间的字符串表示形式转换为日期时间对象。它使用datetime.strptime类方法来做到这一点。然后它通过对交易进行排序来计算股票的流动性。*others泛化到n股票。然后它只是过滤less_liquid交易,然后按时间参数对它们进行排序。然后它按名称过滤并按其与交易more_liquid之间的绝对差异进行排序。A

所以你想要的对象是Aand B。它们不会完全是您指定的,因为它们将具有日期时间而不是字符串,但这应该很容易使用该datetime.strftime函数修复。

于 2015-06-08T02:55:01.047 回答
0

2个股票的解决方案是

    from datetime import *
    trades=[['AAA', '2011-01-03', '11:03:51', 21.5],['BBB', '2011-01-03','11:03:57', 31.5],
['AAA', '2011-01-03', '11:04:20', 21.55],
['BBB', '2011-01-03','11:04:19', 32.01], ['BBB', '2011-01-03','11:04:52', 31.7]]

stocknames = ['AAA','BBB']
A=[]
lastofeach=[]
for stock in stocknames:
    for t in trades:
        if t[0]==stock:
            A.append(t)
    A.sort(key=lambda e:(e[1], e[2]))
    lastofeach.append(A[-1])
    A[:]=[]
lastofeach.sort(key=lambda e:e[2])  

lastofeach=[[i[0], datetime.strptime(i[1]+" "+i[2], "%Y-%m-%d %H:%M:%S"), i[3]] for i in lastofeach]
trades=[[i[0], datetime.strptime(i[1]+" "+i[2], "%Y-%m-%d %H:%M:%S"), i[3]] for i in trades]


A=lastofeach[0]
B=(sorted((i for i in trades if i[0]!=A[0]), key=lambda n: abs(n[1]-A[1]))[0])
C=[A,B]
print (C)

(对于完全相同的答案,只需应用 datetime.strftime(A[1],"%Y-%m-%d %H:%M:%S"),将其拆分并保存。

非常感谢 n>2 的解决方案。也欢迎任何大的 O 优化。

谢谢

于 2015-06-08T05:36:01.347 回答