def autoDayLoop(broker, connection):
    """
    autoDayLoop: automatically buys and sells for the day
    broker - broker name to run as
    connection - database connection object
    return: none
    """
    # fetch broker info [name, bank, date] and owned stocks
    brokerInfo = fetchSqlData.fetchBrokerInfo(broker, connection)
    ownedStocks = fetchSqlData.queryAllOwned(broker, connection)
    today = brokerInfo[2]
    quarterDate = brokerInfo[2] - timedelta(days=90)
    yearDate = brokerInfo[2] - timedelta(days=365)
    # check to sell
    if ownedStocks:
        # get 90 days ago
        for stocks in ownedStocks:
            # get 60 day pointset
            points = fetchSqlData.getPointsDateRange(quarterDate, today,
                                                     stocks[0],
                                                     ["Date", "AdjClose"],
                                                     connection)
            kmeans = CurveFit.getKmean(points)
            # if its above high kmean sellall
            if points[-1][1] > kmeans[0]:
                sellStock(brokerInfo, stocks[0], stocks[1], connection)
                print(stocks, "SOLD")
    # get stock list
    stockList = fetchSqlData.getStockList(connection)
    highScores = []
    totalNumStocks = len(stockList)
    onStockNum = 0
    oldfraction = 0
    loadingBar(0)
    # score all stocks
    for stocks in stockList:
        onStockNum += 1
        #print(stocks[0])
        # if stock exists on dates
        if fetchSqlData.stockDateExists(
                today, stocks[0], connection) and fetchSqlData.stockDateExists(
                    yearDate, stocks[0], connection):
            # load pointests
            pointSet = fetchSqlData.getPointsDateRange(yearDate, today,
                                                       stocks[0],
                                                       ["Date", "AdjClose"],
                                                       connection)
            secondSet = fetchSqlData.getPointsDateRange(
                quarterDate, today, stocks[0], ["Date", "AdjClose"],
                connection)
            # score and append if higher
            score = CurveFit.compositeScore(pointSet, secondSet)
            CurveFit.appendHigher(
                [stocks, score[0], score[1], score[2], pointSet[-1][1]],
                highScores, 10)
            fraction = int((onStockNum / totalNumStocks) * 50)
            if oldfraction != fraction:
                loadingBar(fraction)
            oldfraction = fraction
    stdout.write("\r")
    stdout.flush()
    # check for which stock to buy
    for goodStocks in highScores:
        #newSet = fetchSqlData.getPointsDateRange(quarterDate, today, goodStocks[0][0], ["Date","AdjClose"], connection)
        #newPoly = CurveFit.polyFit(newSet,9)
        #CurveFit.drawPoints(newSet,newPoly)
        #newKmean = CurveFit.getKmean(newSet)
        # buy if below kmean
        if goodStocks[2] > goodStocks[4] and goodStocks[3] > goodStocks[4]:
            # check bank for how much to buy
            brokerInfo = fetchSqlData.fetchBrokerInfo(broker, connection)
            totalMoney = (valueStocks(broker, brokerInfo[2], connection) +
                          brokerInfo[1]) / 10
            toBuy = int(totalMoney / goodStocks[4])
            if toBuy > 1000:
                toBuy = 1000
            # Buy stock
            if toBuy > 0:
                if buyStock(brokerInfo, goodStocks[0][0], toBuy, connection):
                    print(goodStocks, toBuy)
                    # Done if stock was bought
                    break