#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Dec 21 08:41:01 2016

@author: brendontucker
"""

timestamp = [1450625399, 1450625399, 1450625399, 1450625399, 1450625399, 
             1450625399, 1450625399, 1450625399, 1450625399, 1450625399]

instrument = ["AAPL", 
 "AAPL", 
 "AAPL", 
 "HPQ", 
 "AAPL", 
 "HPQ", 
 "AAPL", 
 "HPQ", 
 "AAPL", 
 "AAPL"]
side = ["sell", 
 "sell", 
 "buy", 
 "buy", 
 "buy", 
 "buy", 
 "buy", 
 "buy", 
 "buy", 
 "sell"]
price = [1, 10, 2, 9, 3, 8.8, 4.44, 7, 5, 6]
size = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

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
#copy of timeList
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()
#why did I have to sort!? v confusing... need to investigate further in future
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

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 #once inc spills over, new instrument is being looked
        #at, so app must incerase 
        
    checkCounter = checkCounter + 1

#outputList.sort()    

incCounter = 0 #just using to delete now
while incCounter < len(outputList):
    if outputList[incCounter] == []:
        outputList.remove(outputList[incCounter])
        incCounter = incCounter
    else:
        incCounter = incCounter + 1

        
outputList3 = [] #[[] for x in range(length1)]
counter = 0
counter2 = 0
while counter < len(timeList3):
    counter2 = 0
    while counter2 < len(outputList):
        if outputList[counter2][0] in timeList3[counter]:
            outputList3.append(outputList[counter2])
            counter2 = counter2 + 1
        else:
            counter2 = counter2 + 1
    counter = counter + 1

outputList2 = [[]for x in range(len(outputList))]

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