from datetime import datetime
from itertools import groupby


timestamp = [1450625399, 1450625400, 1450625500, 1450625550, 1451644200, 1451690100, 1451691000]
instrument = ["HPQ", "HPQ", "HPQ", "HPQ", "AAPL", "HPQ", "GOOG"]
side = ["sell", "buy", "buy", "sell", "buy", "buy", "buy"]
price = [10, 20.3, 35.5, 8.65, 20, 10, 100.35]
size = [10, 1, 2, 3, 5, 1, 10]

result = [["2015-12-20", "HPQ", "10.00", "35.50", "8.65", "8.65"],
          ["2016-01-01", "AAPL", "20.00", "20.00", "20.00", "20.00"],
          ["2016-01-01", "GOOG", "100.35", "100.35", "100.35", "100.35"],
          ["2016-01-01", "HPQ", "10.00", "10.00", "10.00", "10.00"]]

timestamp = [1450625399, 1450625399, 1450625399, 1450625399, 1450625399, 1450625399, 1450625399, 1450625399, 1450625399, 1450625399]
instrument = ["AAPL", "AAPL", "AAPL", "HPQ", "AAPL", "HPQ", "AAPL", "HPQ", "AAPL", "AAPL"]
price = [1, 10, 2, 9, 3, 8.8, 4.44, 7, 5, 6]

get_date_from_unix = lambda x: datetime.utcfromtimestamp(x).strftime('%Y-%m-%d')
unique = lambda l: list(set(l))
dates = [get_date_from_unix(x) for x in timestamp]
unique_dates = unique(dates)
unique_instruments = unique(instrument)
zipped_info = zip(dates, instrument, range(0, len(timestamp)), price)
# [print(item) for item in zipped_info]

it = [(x, list(y)) for x,y in groupby(sorted(zipped_info), key=lambda x: (x[0], x[1]))]
[print(item) for item in it]

open_price = ['%0.2f'%(list(g)[0][3]) for _, g in it]
high_price = ['%0.2f'%(max(item[3] for item in g)) for _, g in it]
low_price = ['%0.2f'%(min(item[3] for item in g)) for _, g in it]
close_price = ['%0.2f'%(list(g)[-1][3]) for _, g in it]

zipped_result = zip([item[0][0] for item in it], [item[0][1] for item in it], open_price, high_price, low_price, close_price)

print(sorted(list(zipped_result)))

print(list(zipped_info))
print(open_price)
print(high_price)
print(low_price)
print(close_price)
print(it)

print(round(10, 2))
