#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Dec 15 11:47:09 2016

@author: brendontucker

pretty sure this whole thing could be solved with 
vectors in NumPy a lot quicker..
something something linear algebra
"""

timestamp = [801840015, 1346505615, 1346505615, 
             1346505615, 1346505615, 1346516000, 1592632799, 1592632799, 1592669600, 1592669600]

instrument =["LAG", 
 "HPQ", 
 "HPQ", 
 "HPQ", 
 "AAOL", 
 "LAG", 
 "LAG", 
 "GOOG", 
 "AAOL", 
 "HPQ"]

side = ["sell", 
 "sell", 
 "buy", 
 "sell", 
 "sell", 
 "buy", 
 "sell", 
 "sell", 
 "buy", 
 "sell"]
price = [50.4, 75.64, 42.62, 92.69, 48.53, 20.85, 61.33, 32.61, 36.53, 28.74]
size = [543, 448, 140, 889, 857, 270, 305, 115, 243, 450]

expected_output = [["1995-05-30","LAG","50.40","50.40","50.40","50.40"], 
 ["2012-09-01","AAOL","48.53","48.53","48.53","48.53"], 
 ["2012-09-01","HPQ","75.64","92.69","42.62","92.69"], 
 ["2012-09-01","LAG","20.85","20.85","20.85","20.85"], 
 ["2020-06-20","AAOL","36.53","36.53","36.53","36.53"], 
 ["2020-06-20","GOOG","32.61","32.61","32.61","32.61"], 
 ["2020-06-20","HPQ","28.74","28.74","28.74","28.74"], 
 ["2020-06-20","LAG","61.33","61.33","61.33","61.33"]]
 
import datetime
#convert unix time
timeList =[]
for x in timestamp:
    timeList.append(datetime.datetime.fromtimestamp((x)).strftime('%Y-%m-%d'))

length1 = len(timeList)
checkCounter = 0
incCounter = 1

timeList2 = list()
for x in timeList:
    timeList2.append(x)
#find unique times
while checkCounter < len(timeList):
    while incCounter < len(timeList):
        if timeList[checkCounter] == timeList[incCounter]:
            timeList.remove(timeList[incCounter])
            incCounter = incCounter
        else:
            incCounter = incCounter + 1
    checkCounter = checkCounter + 1
    incCounter = 0
    incCounter = checkCounter + 1
#organize indexes of unique times
timeList3 = [[] for x in range(len(timeList))]
incCounter = 0 
checkCounter = 0
while checkCounter < len(timeList):
    incCounter = 0
    while incCounter < len(timeList2):
        if timeList[checkCounter] == timeList2[incCounter]:
            timeList3[checkCounter].append(incCounter)
            incCounter = incCounter + 1
        else:
            incCounter = incCounter + 1
    checkCounter = checkCounter + 1
        #where is teh restart value?

instrumentList = list()
for x in instrument:
    instrumentList.append(x)

checkCounter = 0
incCounter = 1
#creates a list of all unique instruments 
#creation of extra data as an effort to not touch input data and easier prototyping
#maybe alter original instrument list if memory demands still too high and time limit
#exceeded
instrumentList.sort()
while checkCounter < len(instrumentList):
    while incCounter < len(instrumentList):
        if instrumentList[checkCounter] == instrumentList[incCounter]:
            instrumentList.remove(instrumentList[incCounter])
            incCounter = incCounter
        else:
            incCounter = incCounter + 1
    checkCounter = checkCounter + 1
    incCounter = 0
    incCounter = checkCounter + 1           

instrumentList2 = [[] for x in range(len(instrumentList))]

incCounter = 0 
checkCounter = 0
while checkCounter < len(instrumentList):
    incCounter = 0
    while incCounter < len(instrument):
        if instrumentList[checkCounter] == instrument[incCounter]:
            instrumentList2[checkCounter].append(incCounter)
            incCounter = incCounter + 1
        else:
            incCounter = incCounter + 1
    checkCounter = checkCounter + 1
#need to lexicographically sort here 
checkCounter = 0
incCounter = 0
listChecker = 0
timeCounter = 0
appendPos = 0 #where to append instrument/time independent entries
outputList = [[] for x in range(length1)]
#instrumentList2.sort()
#timeList3.sort()
while checkCounter < len(instrumentList2):
    if len(instrumentList2[checkCounter]) == 1:
        outputList[appendPos].append(instrumentList2[checkCounter][0])
        appendPos = appendPos + 1
    elif len(instrumentList2[checkCounter]) > 1:
        incCounter = 0
        listChecker = 0
        timeCounter = 0
        while incCounter < len(instrumentList2[checkCounter]):
            if instrumentList2[checkCounter][incCounter] in timeList3[listChecker]: 
                outputList[appendPos].append(instrumentList2[checkCounter][incCounter])
                incCounter = incCounter + 1 
            else:
                listChecker = listChecker + 1
                if outputList[appendPos] == []:
                    appendPos = appendPos
                else:
                    appendPos = appendPos + 1
        appendPos = appendPos + 1 
        
    checkCounter = checkCounter + 1
#outputList.sort()

incCounter = 0 #just using to delete now
#deletes uneeded list openings 
while incCounter < len(outputList):
    if outputList[incCounter] == []:
        outputList.remove(outputList[incCounter])
        incCounter = incCounter
    else:
        incCounter = incCounter + 1
 
outputList2 = [[]for x in range(len(outputList))]

listChecker = 0
incChecker = 0

while listChecker < len(outputList):
    if len(outputList[listChecker]) == 1:
        #date--append the timestamp (str), get from timelist2, the value is taken from 
        #outputList[listChecker][0]
        outputList2[listChecker].append(timeList2[outputList[listChecker][0]])
        #instrument value--same method as before
        #outputList has all the answers bro
        outputList2[listChecker].append(instrument[outputList[listChecker][0]])
        #opening value
        outputList2[listChecker].append('{:.2f}'.format(price[outputList[listChecker][0]]))
        #high value--for length 1, going to be the same for all four...
        outputList2[listChecker].append('{:.2f}'.format(price[outputList[listChecker][0]]))
        outputList2[listChecker].append('{:.2f}'.format(price[outputList[listChecker][0]]))
        outputList2[listChecker].append('{:.2f}'.format(price[outputList[listChecker][0]]))
    elif len(outputList[listChecker]) > 1:
        #group of sublists should be date and instrument coherent, so first entry
        #is fine for date and instrumment
        outputList2[listChecker].append(timeList2[outputList[listChecker][0]])
        outputList2[listChecker].append(instrument[outputList[listChecker][0]])
        #organized by time, so entry zerp should be open
        outputList2[listChecker].append('{:.2f}'.format(price[outputList[listChecker][0]]))
        #need max() and min() now
        startRange = outputList[listChecker][0]
        endRange = outputList[listChecker][-1] + 1 #not inclusive, have to add one
        maxValue = max(price[startRange:endRange])
        minValue = min(price[startRange:endRange])
        outputList2[listChecker].append('{:.2f}'.format(maxValue))
        outputList2[listChecker].append('{:.2f}'.format(minValue))
        outputList2[listChecker].append('{:.2f}'.format(price[endRange - 1]))
        
        
        
        

    listChecker = listChecker + 1
        

       

