#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jan  3 12:34:59 2017

@author: brendontucker
"""

def dailyOHLC(timestamp, instrument, side, price, size):
    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): #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
    
    return outputList2

