Beispiel #1
0
def getDataDatabase(startMinuteBack, endMinuteBack):
    """
    :param startMinuteBack: first minute of the interval you want
    :param endMinuteBack: end minute of the interval desired
    :return:
    """
    global priceSymbols

    priceSymbols = PriceSymbolsUpdater.chooseUpdate('binance')

    #code for writing the values into three text files for each crypto: an open price, close price, and volume file.
    dirname = os.path.dirname(os.path.realpath(__file__))
    filename = os.path.join(dirname + '/', '')
    databasePath = os.path.join(dirname + '/', 'databases/' + 'binance.db')
    conn = sqlite3.connect(databasePath)
    cur = conn.cursor()

    openPriceDict = {}
    closePriceDict = {}
    volumeDict = {}
    highPriceDict = {}
    lowPriceDict = {}

    length = getNumRows(cur, 'openprices')
    print(length)

    startIndex = length - startMinuteBack - 1
    endIndex = length - endMinuteBack - 1
    index = startIndex

    for key, crypto in priceSymbols.items():
        openPriceDict[crypto] = []
        closePriceDict[crypto] = []
        volumeDict[crypto] = []
        highPriceDict[crypto] = []
        lowPriceDict[crypto] = []

    while (endIndex < index <= startIndex):
        for key, crypto in priceSymbols.items():
            openPrice = select_by_crypto(conn, 'openprices', crypto,
                                         index)[0][0]
            closePrice = select_by_crypto(conn, 'closeprices', crypto,
                                          index)[0][0]
            volume = select_by_crypto(conn, 'volumes', crypto, index)[0][0]
            highPrice = select_by_crypto(conn, 'highprices', crypto,
                                         index)[0][0]
            lowPrice = select_by_crypto(conn, 'lowprices', crypto, index)[0][0]

            openPriceDict[crypto].append(openPrice)
            closePriceDict[crypto].append(closePrice)
            volumeDict[crypto].append(volume)
            highPriceDict[crypto].append(highPrice)
            lowPriceDict[crypto].append(lowPrice)

        index -= 1

    return openPriceDict, closePriceDict, volumeDict, highPriceDict, lowPriceDict
Beispiel #2
0
def createVolumeDict():
    buyVolumeDict = {}
    sellVolumeDict = {}

    # creating a timestamp of the current time and finding which day of the week it is
    currentTime = datetime.datetime.now(tz=pytz.UTC)
    currentTime = currentTime.astimezone(pytz.timezone('US/Eastern'))
    hour = currentTime.strftime("%H%M")
    minute = int(currentTime.strftime("%M"))

    day = currentTime.isoweekday()
    weekday = {
        1: "Monday",
        2: "Tuesday",
        3: "Wednesday",
        4: "Thursday",
        5: "Friday",
        6: "Saturday",
        7: "Sunday"
    }[day]

    delta = minute % 10
    if(delta == 0):
        for key, value in priceSymbols.items():
            value = value.lower()
            temp1, temp2 = readPickle(value, weekday, hour)
            buyVolumeDict.update(temp1)
            sellVolumeDict.update(temp2)

    # if the time difference is greater than or equal to 5 minutes round up
    if (delta >= 5):
        currentTime = currentTime + datetime.timedelta(minutes=(10 - delta))
        hour = currentTime.strftime("%H%M")
        for key, value in priceSymbols.items():
            value = value.lower()
            temp1, temp2 = readPickle(value, weekday, hour)
            buyVolumeDict.update(temp1)
            sellVolumeDict.update(temp2)

    # if the time difference is less than 5 subtract to nearest 10 minute interval
    elif(delta < 5):
        currentTime = currentTime + datetime.timedelta(minutes=(-delta))
        hour = currentTime.strftime("%H%M")
        for key, value in priceSymbols.items():
            value = value.lower()
            temp1, temp2 = readPickle(value, weekday, hour)
            buyVolumeDict.update(temp1)
            sellVolumeDict.update(temp2)

    return buyVolumeDict, sellVolumeDict
Beispiel #3
0
def getDataBinance(numDays):
    """
    :param numDays:
    :return:
    """
    global priceSymbols
    noData = {}

    priceSymbols = PriceSymbolsUpdater.chooseUpdate('binance')

    #the absolute end time for all data
    absEndTime = requests.get("https://api.binance.com/api/v1/time")
    absEndTime = absEndTime.json()

    #code for writing the values into three text files for each crypto: an open price, close price, and volume file.
    for key, currencyname in priceSymbols.items():

        noDataCount = 0
        #creating the file path lengths and opening them
        openPriceCryptoPath = os.path.join(cryptoPaths, currencyname + "OpenPrice" + ".txt")
        closePriceCryptoPath = os.path.join(cryptoPaths, currencyname + "ClosePrice" + ".txt")
        volumeCryptoPath = os.path.join(cryptoPaths, currencyname + "Volume" + ".txt")
        highCryptoPath = os.path.join(cryptoPaths, currencyname + "High" + ".txt")
        lowCryptoPath = os.path.join(cryptoPaths, currencyname + "Low" + ".txt")
        oprice = open(openPriceCryptoPath, "w")
        cprice = open(closePriceCryptoPath, "w")
        volume = open(volumeCryptoPath, "w")
        highPrice = open(highCryptoPath, "w")
        lowPrice = open(lowCryptoPath, "w")

        #while loop with a counter to make sure that the start and endtime stay one day apart but go backwards in time, numdays amount of days
        timeBackwards = 0
        while(timeBackwards < ONE_DAY*numDays):
            endTime = absEndTime['serverTime'] - timeBackwards
            startTime = endTime - ONE_THIRD_DAY
            parameters = {'symbol': currencyname, 'startTime': startTime, 'endTime': endTime, 'interval': '1m'}
            data = requests.get("https://api.binance.com/api/v1/klines", params=parameters)
            data = data.json()

            if(len(data) == 0):
                noDataCount+=1
                noData.update({currencyname: noDataCount})


            print("Length of data set: {} coin associated with data set: {} data set: {}".format(len(data), currencyname, data))
            for i in reversed(data):
                oprice.write("{},".format(i[1]))
                highPrice.write("{}, ".format(i[2]))
                lowPrice.write("{}, ".format(i[3]))
                cprice.write("{},".format(i[4]))
                volume.write("{},".format(i[5]))
            timeBackwards += ONE_THIRD_DAY

    print(noData)
    #closing all the files once we're done
    oprice.close()
    highPrice.close()
    lowPrice.close()
    cprice.close()
    volume.close()
Beispiel #4
0
def calcVolumes(timestamp, maxLoss):
    threads = []

    for key, currency in priceSymbols.items():
        thread = volumeThread(currency, timestamp, -1)
        thread.start()
        threads.append(thread)

    for thread in threads:
        thread.join()
Beispiel #5
0
def testCalcMinBidandAsk(acceptedLossPercentage, levelsToAccess):
    prices = {}
    minBid = 0.0
    minAsk = 0.0
    global basesource

    for key, currencyname in priceSymbols.items():
        source = basesource + '/ws/' + str(currencyname) + '@depth' + str(
            levelsToAccess)
        # try to open with urllib (if source is http, ftp, or file URL)
        try:
            print(source)
        except (IOError, OSError):
            print('error')
            pass
Beispiel #6
0
def generatePriceSymbols(desiredVolume, maxLoss, website='binance'):

    """
    :param desiredVolume:
    :param maxLoss:
    :param website:
    :return:
    """
    global priceSymbols

    #list to hold all the threads
    threads = []

    #get an updated version of price symbols
    symbols = PriceSymbolsUpdater.chooseUpdate(website)

    #get the prices to lowercase
    priceSymbols = getLowerCaseDict(symbols)

    #iterate through the price symbols dictionary and create a thread to find the the depth of that crypto then append the thread to the list of threads
    for key, currencyname in priceSymbols.items():
        thread = depthThread(currencyname, desiredVolume, maxLoss, price)
        thread.start()
        threads.append(thread)
        #asyncio.get_event_loop().run_until_complete(getDepth(value, 10000, -1))

    #wait for all the threads to finish
    for thread in threads:
        thread.join()

    #uppercase the symbols
    for i in possibleBuyCryptos:
        i.upper()

    for i in possibleSellCryptos:
        i.upper()

    print("Buy List: {}".format(possibleBuyCryptos))
    print("Num Items: {}".format(len(possibleBuyCryptos)))
    print("Sell List: {}".format(possibleSellCryptos))
    print('Num Items: {}'.format(len(possibleSellCryptos)))
    return possibleBuyCryptos
Beispiel #7
0
def getbinancedistribution(lossallowed):
    """
    :param lossallowed: the percentage loss allowed
    :return:
    """
    priceSymbols = updatePriceSymbolsBinance()
    # start of the main code
    # compare current time against 4 pm to see if its within a ten minute interval
    baseTS = datetime.datetime(2018, 5, 25, 16, 0, 0)
    baseTS = baseTS.astimezone(pytz.timezone('US/Eastern'))

    threads = []
    price = [0.0]
    pThread = priceThread(price)
    pThread.start()

    buffertimestart = time.time()

    while(True):
        # get the date current time and set it to US Eastern time
        currentTime = datetime.datetime.now(tz=pytz.UTC)
        currentTime = currentTime.astimezone(pytz.timezone('US/Eastern'))

        # find time delta between current time and the base timestamp
        timedelta = currentTime.minute - baseTS.minute
        print('Time delta = {}'.format(timedelta))
        if (timedelta % 10 == 0):
            print("Time Difference is 10 minutes")
            threads = []

            for key, currency in priceSymbols.items():
                currency = currency.lower()
                thread = volumeThread(currency, currentTime, lossallowed, pThread)
                thread.start()
                threads.append(thread)

            for thread in threads:
                thread.join()

        time.sleep(60.0 - ((time.time() - buffertimestart) % 60.0))
def getDataBinance(numDays):
    """
    :param numDays:
    :return:
    """
    # code for writing the values into three text files for each crypto: an open price, close price, and volume file.
    for key, currencyname in priceSymbols.items():
        # creating the file path lengths and opening them
        openPriceCryptoPath = os.path.join(cryptoPaths,
                                           currencyname + "OpenPrice" + ".txt")
        closePriceCryptoPath = os.path.join(
            cryptoPaths, currencyname + "ClosePrice" + ".txt")
        volumeCryptoPath = os.path.join(cryptoPaths,
                                        currencyname + "Volume" + ".txt")
        highCryptoPath = os.path.join(cryptoPaths,
                                      currencyname + "High" + ".txt")
        lowCryptoPath = os.path.join(cryptoPaths,
                                     currencyname + "Low" + ".txt")

        oprice = open(openPriceCryptoPath, "w")
        cprice = open(closePriceCryptoPath, "w")
        volume = open(volumeCryptoPath, "w")
        highPrice = open(highCryptoPath, "w")
        lowPrice = open(lowCryptoPath, "w")

        endTime = requests.get("https://api.binance.com/api/v1/time")
        endTime = endTime.json()
        endTime = endTime['serverTime']
        print("Time object: {} Type: {}".format(endTime, type(endTime)))

        # while loop with a counter to make sure that the start and endtime stay one day apart but go backwards in time, numdays amount of days
        timeBackwards = numDays * ONE_DAY
        endTime = endTime - timeBackwards

        time.sleep(1)

        while (timeBackwards > 0):
            endTime += ONE_THIRD_DAY
            startTime = endTime - ONE_THIRD_DAY
            parameters = {
                'symbol': currencyname,
                'startTime': startTime,
                'endTime': endTime,
                'interval': '1m'
            }
            data = requests.get("https://api.binance.com/api/v1/klines",
                                params=parameters)
            print("Start Time: {} End Time: {}".format(startTime, endTime))
            endTimeDate = datetime.datetime.fromtimestamp(endTime / 1000.0)
            startTimeDate = datetime.datetime.fromtimestamp(startTime / 1000.0)
            print("Start Time: {} End Time: {}".format(startTimeDate,
                                                       endTimeDate))
            data = data.json()
            print("Data: {}".format(data))
            print("Type: {}".format(type(data)))
            for i in data:
                if (i[1] != '[]'):
                    oprice.write("{},".format(i[1]))
                if (i[2] != '{}'):
                    highPrice.write("{}, ".format(i[2]))
                if (i[3] != '{}'):
                    lowPrice.write("{}, ".format(i[3]))
                if (i[4] != '[]'):
                    cprice.write("{},".format(i[4]))
                if (i[5] != '[]'):
                    volume.write("{},".format(i[5]))
            timeBackwards -= ONE_THIRD_DAY

    # closing all the files once we're done
    oprice.close()
    highPrice.close()
    lowPrice.close()
    cprice.close()
    volume.close()
Beispiel #9
0
def getDatabaseValues(numDays, priceSymbols, startHour, startMin):

    #find the current time to compare the delta between when it should have started and now.
    currentTime = datetime.datetime.now(tz=pytz.UTC)
    currentTime = currentTime.astimezone(pytz.timezone('US/Eastern'))

    #find the hour and min deltas
    hourDelta = currentTime.hour - startHour
    minDelta = currentTime.minute - startMin

    #if there was an hour delta convert to min and add to delta
    if (hourDelta > 0):
        hourInMin = hourDelta * 60
        minDelta += hourInMin

    print(minDelta)

    #calculate how many days we need to go back in minutes
    daysInMin = 1440 * numDays

    #create path to connect to database and create a cursor object to the database
    dirname = os.path.dirname(os.path.realpath(__file__))
    filename = os.path.join(dirname + '/', '')
    databasePath = os.path.join(dirname + '/', 'databases/' + 'binance.db')
    conn = sqlite3.connect(databasePath)
    cursor = conn.cursor()

    #instantiate all the dictionaries for the various values
    openPriceDict = {}
    closePriceDict = {}
    volumeDict = {}
    highPriceDict = {}
    lowPriceDict = {}

    #find the length of all the dictionaries so we know how far back we have to move the index.
    openPriceLen = getNumRows(cursor, 'openprices')
    closePriceLen = getNumRows(cursor, 'closeprices')
    volumeLen = getNumRows(cursor, 'volumes')
    highPriceLen = getNumRows(cursor, 'highPrices')
    lowPriceLen = getNumRows(cursor, 'lowPrices')

    #iterate through all the cryptos and add their values to the dictionaries with the key value pair of {crypto name: [list of values]}
    for key, crypto in priceSymbols.items():
        #the index starts at the end of the database - whatever time delta there is - 1 because its an index
        index = openPriceLen - minDelta - 1

        #these cryptos are not part of the database yet
        if (crypto == 'KEYBTC' or crypto == 'NASBTC' or crypto == 'MFTBTC'
                or crypto == 'DENTBTC'):
            continue

        #iterator object to make sure that we go back far enough
        #todo change openPriceLen in the conditional to daysInMin
        iterator = 0
        while (iterator < openPriceLen):
            #if its the first item we are adding use update to add the key value pair with a list of one value
            if (index == openPriceLen - minDelta - 1):
                openPriceDict.update({
                    crypto:
                    [select_by_crypto(conn, 'openprices', crypto, index)]
                })
                closePriceDict.update({
                    crypto:
                    [select_by_crypto(conn, 'closeprices', crypto, index)]
                })
                volumeDict.update({
                    crypto: [select_by_crypto(conn, 'volumes', crypto, index)]
                })
                highPriceDict.update({
                    crypto:
                    [select_by_crypto(conn, 'highprices', crypto, index)]
                })
                lowPriceDict.update({
                    crypto:
                    [select_by_crypto(conn, 'lowprices', crypto, index)]
                })

            #after the first value just append to the list in the key value pair
            else:
                openPriceDict[crypto].append(
                    select_by_crypto(conn, 'openprices', crypto, index))
                closePriceDict[crypto].append(
                    select_by_crypto(conn, 'closeprices', crypto, index))
                volumeDict[crypto].append(
                    select_by_crypto(conn, 'volumes', crypto, index))
                highPriceDict[crypto].append(
                    select_by_crypto(conn, 'highprices', crypto, index))
                lowPriceDict[crypto].append(
                    select_by_crypto(conn, 'lowprices', crypto, index))

            #iterate index backwards farther into the database and iterator forwards towards the min total we are supposed to go back
            index -= 1
            iterator += 1

    return openPriceDict, closePriceDict, volumeDict, highPriceDict, lowPriceDict
Beispiel #10
0
def get_num_prices():
    count = 0
    for key, value in priceSymbols.items():
        count += 1
    print(count)