Suppose I collected (in a list) all trades that occurred within a certain period of time (say first 5mins after 11AM) for n stocks (I'll make n=2 for simplicity and adapt later). Say we have firm AAA and firm BBB (if it helps, liststocks=['AAA', 'BBB']). The list will look somehow like:
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]]
i.e., 2 trades for stock AAA and 3 trades for stock BBB. Picking the last trade of each stock causes a lack of synchronicity problem. The idea is to pick the last trade of each stock and find the earliest (['AAA', '2011-01-03', '11:04:20', 21.55]). Then pick transactions of all other stocks with time as close as possible to '11:04:20', which would cause us to choose ['BBB', '2011-01-03','11:04:19', 32.01]. The output should be a list like:
C=[['AAA', '2011-01-03', '11:04:20', 21.55],['BBB', '2011-01-03','11:04:19', 32.01]]
Thanks a lot!
Its not that hard if you use
sortedwith thekeyparameter.Here is the code if you don't want to read, I'll explain after:
What this does is it first converts each trade from using a string representation of the time to a datetime object. It does this with the
datetime.strptimeclass method. Then it calculates the liquidity of the stocks by sorting trades. The*othersgeneralizes tonstocks. Then it just filters theless_liquidtrades and then sorts them by the time parameter. Then it filters by name beingmore_liquidand sorts by the absolute difference between it and theAtrade.So the object you want are
AandB. They won't be exactly what you specified, since they will have datetime instead of strings, but that should be easy to fix with thedatetime.strftimefunction.