Created
April 21, 2018 11:31
-
-
Save stealthbomber10/38ef70d78b664eb8cd41827104f801c9 to your computer and use it in GitHub Desktop.
all creds to cubie tom
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Created on Thu Mar 22 15:12:20 2018 | |
@author: Cubie, Tom Aarsen | |
""" | |
import requests, time | |
from multiprocessing import Pool | |
#Edit the variable 'processes' to speed up or slow down the script. | |
#The higher the variable process, the faster the script will calculate, | |
#but the slower the computer will run. | |
global processes | |
processes = 4 | |
toSymbols = "BTC" | |
global cryptoList | |
cryptoList = [] | |
global symbolList | |
symbolList = [] | |
url = "https://min-api.cryptocompare.com/data/histominute" | |
exchangeList = ["Binance", "BitTrex", "Kucoin", "Bitfinex", "Cryptopia"] | |
class Crypto: | |
def __init__(self, symbol, endTime, startTime): | |
self.startPrice = 0 | |
self.endPrice = 0 | |
self.symbol = symbol | |
self.percentageDif = 0 | |
self.exchange = [] | |
self.nonExchangePrice1 = -1 | |
self.nonExchangePrice2 = -1 | |
self.times = [startTime, endTime] | |
def setStartPrice(self,amt): | |
self.startPrice = amt | |
def setEndPrice(self,amt): | |
self.endPrice = amt | |
self.setPercentageDif() | |
def setPercentageDif(self): | |
if(self.startPrice != 0): | |
self.percentageDif = (self.endPrice/self.startPrice)*100-100 | |
def setSymbol(self,string): | |
self.symbol = string | |
def addExchange(self,string): | |
self.exchange.append(string) | |
def setNonExchangePrice1(self): | |
self.nonExchangePrice1 = 1 | |
def setNonExchangePrice2(self): | |
self.nonExchangePrice2 = 1 | |
def Store(self, jsonDataOutput, failOutput, index): | |
print((8 - len("%.2f" % ((index/len(cryptoList))*100)))*" " + str("%.2f" % ((index/len(cryptoList))*100) + "% | " + self.symbol + (6 - len(self.symbol))*" " + "|")) | |
self.setStartPrice(jsonDataOutput[0]) | |
self.setEndPrice(jsonDataOutput[1]) | |
if(failOutput[0]): | |
self.setNonExchangePrice1() | |
if(failOutput[1]): | |
self.setNonExchangePrice2() | |
def printLine(): | |
print("----------+-------+-------------+-------------+") | |
def printFirstLine(): | |
print("----------+-------+-------------+") | |
def Fetch(self): | |
jsonDataOutput = [] | |
failOutput = [False,False] | |
for i in range(2): | |
jsonData = [] | |
url = "https://min-api.cryptocompare.com/data/histominute?fsym=" + self.symbol | |
url += "&tsym=" + "BTC" | |
url += "&limit=1&toTs=" + str(self.times[i]) | |
url += "&extraParams=CChecker" | |
url += "&e=" + exchangeList[self.exchange[0]] | |
if((time.time() - self.times[i]) > 604800 or (time.time() - self.times[i]) < -604800): | |
#print("Hourly") | |
url = url.replace("minute","hour") | |
index = 0 | |
while(True): | |
try: | |
jsonData = requests.get(url).json() | |
except requests.exceptions.ConnectionError: | |
print("Connection refused") | |
time.sleep(1) | |
jsonData = requests.get(url).json() | |
if(jsonData["Response"]!="Success"): | |
if "market does not exist" in jsonData["Message"]: | |
url = "https://min-api.cryptocompare.com/data/histominute?fsym=" + self.symbol | |
url += "&tsym=" + "BTC" | |
url += "&limit=1&toTs=" + str(self.times[i]) | |
url += "&extraParams=CChecker" | |
if((time.time() - self.times[i]) > 604800 or (time.time() - self.times[i]) < -604800): | |
#print("Hourly") | |
url = url.replace("minute","hour") | |
failOutput[i] = True | |
elif(jsonData["Response"]=="Success"): | |
if(i == 0): | |
jsonDataOutput.append(jsonData["Data"][0]["open"]) | |
else: | |
jsonDataOutput.append(jsonData["Data"][0]["close"]) | |
break | |
if(index > 5): | |
print("Failed") | |
time.sleep(5) | |
#break | |
if(index > 20): | |
print("Quitting") | |
break | |
index+= 1 | |
return [jsonDataOutput,failOutput] | |
def FillCryptoList(symbol, exchange, offsetEpoch, endEpoch): | |
if symbol in symbolList: | |
for i, j in enumerate(symbolList): #Todo: .index() | |
if(j == symbol): | |
#print(str(i) + " " + str(len(cryptoList))) | |
if exchange not in cryptoList[i].exchange: | |
cryptoList[i].addExchange(exchange) | |
break | |
return False | |
symbolList.append(symbol) | |
cryptoList.append(Crypto(symbol=str(symbol), startTime = endEpoch - offsetEpoch, endTime = endEpoch)) | |
cryptoList[-1].addExchange(exchange) | |
return True | |
def FindSymbols(url): | |
index = 0 | |
exchange = [] | |
while(True): | |
try: | |
exchange = requests.get(url) | |
except requests.exceptions.ConnectionError: | |
print("Sleeping due to Connection refusal") | |
time.sleep(2) | |
exchange = requests.get(url) | |
if exchange.status_code == 200: | |
return exchange | |
if(index > 2): | |
print("Failed, Shutting down") | |
break | |
index += 1 | |
def Chunks(l, n): | |
for i in range(0, len(l), n): | |
yield l[i:i + n] | |
def Main(): | |
global cryptoList | |
global symbolList | |
before = time.time() | |
print("API Information from CryptoCompare.com") | |
ExchangeSetting() | |
IncludeSmallSetting() | |
TimeFrameSetting() | |
exchangeCoinAmountList = [0,0,0,0,0] | |
exchanges = FindSymbols("https://min-api.cryptocompare.com/data/all/exchanges").json() | |
for x, i in enumerate(exchangeList): | |
if str(x+1) in exchangeString: | |
for symbol in exchanges[i]: | |
if toSymbols in exchanges[i][symbol]: | |
if(FillCryptoList(str(symbol),x, offsetEpoch, endEpoch)): | |
exchangeCoinAmountList[x] += 1 | |
sortedList = sorted(cryptoList, key=lambda crypto: crypto.exchange, reverse=False) | |
splitList = [[],[],[],[],[]] | |
for item in sortedList: | |
splitList[item.exchange[0]].append(item) | |
#print(exchangeCoinAmountList) | |
progressList = [0,0,0,0,0] | |
global processes | |
progress = 0 | |
pool = Pool(processes) | |
for z, i in enumerate(Chunks(cryptoList,processes)): | |
try: | |
results = pool.map(Fetch,i) | |
for x, j in enumerate(results): | |
progress += 1 | |
i[x].Store(j[0],j[1],progress) | |
progressList[i[x].exchange[0]] += 1 | |
finally: | |
pass | |
pool.close() | |
pool.join() | |
index = 0 | |
for i in splitList: | |
splitList[index] = sorted(i, key=lambda crypto: crypto.percentageDif, reverse=True) | |
index+=1 | |
printLine() | |
print(" | " + str(len(splitList[0])+len(splitList[1])+len(splitList[2])+len(splitList[3])+len(splitList[4])) + " " * (6-len(str(len(splitList[0])+len(splitList[1])+len(splitList[2])+len(splitList[3])+len(splitList[4])))) + "|") | |
for x, j in enumerate(splitList): | |
if(len(j) != 0): | |
printLine() | |
print(" " + exchangeList[x] + (9 - len(exchangeList[x])) * " " + "| " + str(len(j)) + (6 - len(str(len(j)))) * " " + "| " + time.strftime('%Y-%m-%d %H:%M', time.gmtime(endEpoch-offsetEpoch)) + " To |\n | | " + time.strftime('%Y-%m-%d %H:%M', time.gmtime(endEpoch)) + " |") | |
printLine() | |
print(" Percent | Coins | Start Price | End Price |") | |
printLine() | |
averagePercentage = 0 | |
for i in j: | |
percentage = "%.2f" % i.percentageDif | |
averagePercentage += i.percentageDif | |
if(includeSmall or i.startPrice > 0.000000040): | |
print((7-(len(percentage)))*" " + " " +percentage + "%" + " | " + i.symbol + (6 - len(i.symbol))*" " + "| " + '{:.9f}'.format(i.startPrice) + "*" * i.nonExchangePrice1 + " " * (-1 * i.nonExchangePrice1) + "| " + '{:.9f}'.format(i.endPrice) + "*" * i.nonExchangePrice2 + " " * (-1 * i.nonExchangePrice2) + "| ") | |
if(len(j) > 0): | |
printLine() | |
print(" " * (8 - len("%.2f" % (averagePercentage/len(j)))) + "%.2f" % (averagePercentage/len(j)) + "% | Total |") | |
print("Time: " + str(time.strftime("%H:%M:%S",time.gmtime(time.time() - before)))) | |
input(">") | |
def ExchangeSetting(): | |
print("1: Binance") | |
print("2: Bittrex") | |
print("3: Kucoin") | |
print("4: Bitfinex") | |
print("5: Cryptopia") | |
global exchangeString | |
print("Type the numbers of the Exchanges to include.") | |
exchangeString = input(">") | |
if(exchangeString == "" or exchangeString == " " or exchangeString == "All" or exchangeString == "all"): | |
exchangeString = "12345" | |
def IncludeSmallSetting(): | |
print("Include small coins, <40 sats?[Y/N]") | |
includeSmallString = input(">") | |
global includeSmall | |
if(includeSmallString == "Yes" or includeSmallString == "yes" or includeSmallString == "1" or includeSmallString == '' or includeSmallString == "Y" or includeSmallString == "y"): | |
includeSmall = True | |
else: | |
includeSmall = False | |
def TimeFrameSetting(): | |
print("Please write input exactly as format") | |
startDate = input("Start timeframe - DD-MM-YYYY hh:mm ") | |
endDate = input("End timeframe - DD-MM-YYYY hh:mm ") | |
pattern = '%d-%m-%Y %H:%M' | |
global endEpoch | |
global offsetEpoch | |
if(startDate == endDate == ''): | |
endEpoch = round(time.time()) | |
offsetEpoch = 86400 | |
else: | |
if(endDate == "now" or endDate == "Now"): | |
endEpoch = round(time.time()) | |
else: | |
endEpoch = int(time.mktime(time.strptime(endDate, pattern))) | |
offsetEpoch = endEpoch - int(time.mktime(time.strptime(startDate, pattern))) | |
''' | |
def ToDatabase(splitList): | |
dataset = { | |
"Exchange" : [], | |
"Percentage": [], | |
"Symbol": [], | |
"Start Price": [], | |
"End Price": [] | |
} | |
for j in splitList: | |
for i in j: | |
dataset["Exchange"].append(exchangeList[i.exchange[0]]) | |
dataset["Percentage"].append(i.percentageDif) | |
dataset["Symbol"].append(i.symbol) | |
dataset["Start Price"].append(i.startPrice) | |
dataset["End Price"].append(i.endPrice) | |
dataFrame = pd.DataFrame(dataset, columns = ['Exchange', 'Percentage', 'Symbol', 'Start Price', 'End Price']) | |
print(" ") | |
print(dataFrame) | |
dataFrame.to_csv('CryptoDataFrame.csv') | |
''' | |
if __name__ == '__main__': | |
Main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment