示例#1
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()
示例#2
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
示例#3
0
def main():

    global priceSymbols
    priceSymbols = PriceSymbolsUpdater.chooseUpdate('binance', list=True)

    #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, timeout=720)
    cursor = conn.cursor()

    openP, close, volume, highP, lowP = getDataDatabase(0, 60)
    print("Open Price: {} Open Price ETHBTC Length: {}".format(
        openP, len(openP['ETHBTC'])))
示例#4
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
示例#5
0
def main():
    runTime = time.time() * 1000

    global priceSymbols
    priceSymbols = PriceSymbolsUpdater.chooseUpdate('binance')
示例#6
0
#one day in min
ONE_DAY_MIN = 1440

#one third day in ms
ONE_THIRD_DAY = 28800000

#one third day in min
ONE_THIRD_MIN = 480

#one minute in ms
ONE_MIN_MS = 60000

#one second in ms
ONE_SEC_MS = 1000

priceSymbols = PriceSymbolsUpdater.updatePriceSymbolsBinance()

def main():
    #grabbing the starttime of the data desired and the current time (endtime)
    endTime = requests.get("https://api.binance.com/api/v1/time")
    endTime = endTime.json()
    endTime = endTime['serverTime']
    startTime = endTime - ONE_THIRD_DAY

    #temporary dictionaries for each of the five types of data
    openpricedict = {}
    closepricedict = {}
    highpricedict = {}
    lowpricedict = {}
    volumedict = {}
示例#7
0
def main():
    global priceSymbols

    #read in any passed parameters
    params = readParams(defaultdatastreamparamspassed)

    #this price symbols is different and is just a list of the names
    #we also store them so that any file needing the symbols can pull them from storage
    #thus whatever symbols are used in making the table are also used by scripts requesting data from that table
    priceSymbols = PriceSymbolsUpdater.chooseUpdate(params['website'],
                                                    list=True,
                                                    store=True)
    #store the price symbols dictionary version as well
    PriceSymbolsUpdater.chooseUpdate(params['website'], list=False, store=True)

    #quit()

    #the different table names
    tablenames = [
        'openprices', 'closeprices', 'highprices', 'lowprices', 'volumes'
    ]

    db_file = setupdbfiles()

    connection = create_connection_db(db_file)

    setuptables(connection, priceSymbols)

    connection.commit()

    cursor = connection.cursor()

    if (params['freshrun']):
        for name in tablenames:
            delete_rows(connection, name)

        connection.commit()

    numRows = getNumRows(cursor, 'openprices')

    if (isinstance(numRows, int)):
        #set the minutes past to be the number of rows already in each table
        params['mins'] = numRows

    #uncommon if you want to prime the database (TODO use to prime with params['hourprime'] = hourstoprime and params['freshrun'] = true

    #set the database up with 240 minutes of data
    primeDatabase(connection, priceSymbols, params)

    #set the mins passed to reflect the new data
    params['mins'] += params['minstoprime']

    #waits for one minute - time spent priming database with 2 hours of data so that the next data we grab is a full minute
    #after we have primed
    time.sleep(60.0 - ((time.time() - buffertimestart) % 60.0))

    #commit any changes
    connection.commit()

    #loop that runs every minute to grab another row of data
    starttime = time.time()
    while int(params['mins']) < int(params['minmax']):
        getcurrentmindata(connection, priceSymbols)
        connection.commit()
        time.sleep(60.0 - ((time.time() - starttime) % 60.0))

        params['mins'] += 1

    connection.commit()
    close_connection(connection)