Merge remote-tracking branch 'remotes/origin/crypto'
This commit is contained in:
+176
-4
@@ -3,6 +3,8 @@ import discord
|
|||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
from urllib import request
|
from urllib import request
|
||||||
import json, requests
|
import json, requests
|
||||||
|
import time
|
||||||
|
import random
|
||||||
|
|
||||||
class Cryptocoin(object):
|
class Cryptocoin(object):
|
||||||
"""Voor al uw crypto currency advies."""
|
"""Voor al uw crypto currency advies."""
|
||||||
@@ -13,20 +15,151 @@ class Cryptocoin(object):
|
|||||||
def __check(self, ctx): #Todo: Is dit het geld-verdienen kanaal?
|
def __check(self, ctx): #Todo: Is dit het geld-verdienen kanaal?
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def getCoinPrice(self, coinId, currencyId="USD", time="now"):
|
|
||||||
url = "https://min-api.cryptocompare.com/data/pricehistorical?tsyms=" + currencyId + "&fsym=" + coinId
|
|
||||||
|
|
||||||
if (time != "now"):
|
#borrowed from: http://jmduke.com/posts/basic-linear-regressions-in-python/
|
||||||
url += "&ts=" + str(time)
|
def basic_linear_regression(self, x, y):
|
||||||
|
# Basic computations to save a little time.
|
||||||
|
length = len(x)
|
||||||
|
sum_x = sum(x)
|
||||||
|
sum_y = sum(y)
|
||||||
|
|
||||||
|
# Sx^2, and Sxy respectively.
|
||||||
|
sum_x_squared = sum(map(lambda a: a * a, x))
|
||||||
|
sum_of_products = sum([x[i] * y[i] for i in range(length)])
|
||||||
|
|
||||||
|
# Magic formulae!
|
||||||
|
a = (sum_of_products - (sum_x * sum_y) / length) / (sum_x_squared - ((sum_x ** 2) / length))
|
||||||
|
b = (sum_y - a * sum_x) / length
|
||||||
|
return a, b
|
||||||
|
|
||||||
|
def getCoinPrice(self, coinId, currencyId="USD", time="now"):
|
||||||
|
|
||||||
|
if (time == "now"): #for the most current price, don't use the history API
|
||||||
|
url = "https://min-api.cryptocompare.com/data/price?tsyms=" + currencyId + "&fsym=" + coinId
|
||||||
|
else:
|
||||||
|
url = "https://min-api.cryptocompare.com/data/pricehistorical?tsyms=" + currencyId + "&fsym=" + coinId + "&ts=" + str(time)
|
||||||
|
|
||||||
response = requests.get(url)
|
response = requests.get(url)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
priceData = json.loads(response.content.decode('utf-8'))
|
priceData = json.loads(response.content.decode('utf-8'))
|
||||||
|
if (time == "now"):
|
||||||
|
return priceData[currencyId]
|
||||||
|
else:
|
||||||
return priceData[coinId][currencyId]
|
return priceData[coinId][currencyId]
|
||||||
except:
|
except:
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
|
#dermines the expected growth of coin
|
||||||
|
def analyseCoin(self, coinId):
|
||||||
|
N_DATAPOINTS = 12
|
||||||
|
TIME_BETWEEN_DATAPOINTS = 86400 * 7 #one week
|
||||||
|
|
||||||
|
#Get the price history:
|
||||||
|
history = []
|
||||||
|
for j in range(0, N_DATAPOINTS-1):
|
||||||
|
history.append(self.getCoinPrice(coinId, "USD", int(time.time()) - (N_DATAPOINTS-j)*TIME_BETWEEN_DATAPOINTS))
|
||||||
|
history.append(self.getCoinPrice(coinId, "USD")) #now
|
||||||
|
|
||||||
|
#Check the growth. Do some linear regression
|
||||||
|
growFactorLongTerm, dummy = self.basic_linear_regression(range(0, N_DATAPOINTS), history)
|
||||||
|
growFactorShortTerm, dummy = self.basic_linear_regression(range(0, 3),
|
||||||
|
[ history[N_DATAPOINTS-3], history[N_DATAPOINTS-2], history[N_DATAPOINTS-1] ])
|
||||||
|
|
||||||
|
if (history[N_DATAPOINTS-1] == 0):
|
||||||
|
growPercentageLongTerm = 0
|
||||||
|
growPercentageShortTerm = 0
|
||||||
|
else:
|
||||||
|
growPercentageLongTerm = growFactorLongTerm / history[N_DATAPOINTS-1]
|
||||||
|
growPercentageShortTerm = growFactorShortTerm / history[N_DATAPOINTS-1]
|
||||||
|
|
||||||
|
growPercentage = growPercentageLongTerm*0.3 + growPercentageShortTerm*0.7 #weighted average
|
||||||
|
return growPercentage, history
|
||||||
|
|
||||||
|
#TODO:
|
||||||
|
# - iets met stabiliteit?
|
||||||
|
# - https://en.wikipedia.org/wiki/Head_and_shoulders_(chart_pattern)
|
||||||
|
# - neural networks???
|
||||||
|
|
||||||
|
|
||||||
|
#will return some random advice about a semi-random crypto coin
|
||||||
|
def getRandomCoinAdvice(self):
|
||||||
|
#get all the coins!
|
||||||
|
coinlistUrl = "https://min-api.cryptocompare.com/data/all/coinlist"
|
||||||
|
coinlistResponse = requests.get(coinlistUrl)
|
||||||
|
|
||||||
|
popularCoins = [
|
||||||
|
{"Symbol" : "BTC", "Name" : "BitCoin"},
|
||||||
|
{"Symbol" : "ETH", "Name" : "Ethereum"},
|
||||||
|
{"Symbol" : "ETC", "Name" : "Ethereum Classic"},
|
||||||
|
{"Symbol" : "LTC", "Name" : "LiteCoin"},
|
||||||
|
{"Symbol" : "XMR", "Name" : "Monero"},
|
||||||
|
{"Symbol" : "XRP", "Name" : "Ripple"},
|
||||||
|
{"Symbol" : "DOGE", "Name" : "Dogecoin"},
|
||||||
|
{"Symbol" : "NLG", "Name" : "Gulden"}
|
||||||
|
]
|
||||||
|
|
||||||
|
if (coinlistResponse.ok):
|
||||||
|
coinlistData = json.loads(coinlistResponse.content.decode('utf-8'))
|
||||||
|
coinlist = []
|
||||||
|
|
||||||
|
for coin in coinlistData["Data"]:
|
||||||
|
coinlist.append({
|
||||||
|
"Symbol" : coinlistData["Data"][coin]["Symbol"] ,
|
||||||
|
"Name" : coinlistData["Data"][coin]["CoinName"]
|
||||||
|
})
|
||||||
|
|
||||||
|
#Analyse until you've found something interesting
|
||||||
|
for i in range(0, 99):
|
||||||
|
|
||||||
|
#pick a random coin
|
||||||
|
if (random.randint(0, 4) == 4):
|
||||||
|
#kans van 1 op 5 dat we naar een populaire coin kijken
|
||||||
|
randIndex = random.randint(0, len(popularCoins)-1)
|
||||||
|
tempId = popularCoins[randIndex]["Symbol"]
|
||||||
|
tempName = popularCoins[randIndex]["Name"]
|
||||||
|
else:
|
||||||
|
randIndex = random.randint(0, len(coinlist)-1)
|
||||||
|
tempId = coinlist[randIndex]["Symbol"]
|
||||||
|
tempName = coinlist[randIndex]["Name"]
|
||||||
|
|
||||||
|
#analyse:
|
||||||
|
growPercentage, tempHistory = self.analyseCoin(tempId)
|
||||||
|
|
||||||
|
|
||||||
|
#For the first few iterations we're picky
|
||||||
|
if (growPercentage > 0.1):
|
||||||
|
return (tempName + " (" + tempId + ") gaat to the moon!")
|
||||||
|
if (growPercentage > 0.06):
|
||||||
|
return ("Ik zou zeker een paar " + tempName + " (" + tempId + ") kopen.")
|
||||||
|
|
||||||
|
if (i > 2):
|
||||||
|
#Later, negative advices are also acceptable...
|
||||||
|
if (growPercentage < -0.2):
|
||||||
|
return ("Ik zou zeker " + tempName + " (" + tempId + ") afraden!")
|
||||||
|
if (growPercentage > 0.015):
|
||||||
|
return ("Ken je " + tempName + " (" + tempId + ") al? Best leuk")
|
||||||
|
if(i > 4):
|
||||||
|
#After a while, we're happy to give less useful advice
|
||||||
|
if (growPercentage > 0.008):
|
||||||
|
return (tempName + " (" + tempId + ") kan interessant worden...")
|
||||||
|
if (growPercentage < -0.06):
|
||||||
|
return ("Als je toevallig " + tempName + " (" + tempId + ") hebt: verkopen.")
|
||||||
|
if(i > 5):
|
||||||
|
#Anything goes. We're desperate.
|
||||||
|
if (tempHistory[ len(tempHistory)-1 ] == 0):
|
||||||
|
return (tempId + " is echt niks waard...")
|
||||||
|
if (growPercentage > 0.005):
|
||||||
|
return (tempName + " (" + tempId + ") is een crypto currency. Wist je dat?")
|
||||||
|
|
||||||
|
time.sleep(0.1) #don't overload the API
|
||||||
|
|
||||||
|
return "Sorry. De crypto adviezen zijn uitverkocht vandaag."
|
||||||
|
else:
|
||||||
|
raise Exception("API error")
|
||||||
|
|
||||||
|
#user functions below
|
||||||
|
|
||||||
@commands.command(pass_context=True)
|
@commands.command(pass_context=True)
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def coinprice(self, ctx, coinId: str, currencyId: str="USD"):
|
def coinprice(self, ctx, coinId: str, currencyId: str="USD"):
|
||||||
@@ -38,6 +171,9 @@ class Cryptocoin(object):
|
|||||||
"""
|
"""
|
||||||
allowedCurrencies = ["USD", "EUR", "JPY", "GBP", "CNY", "CAD", "BTC"]
|
allowedCurrencies = ["USD", "EUR", "JPY", "GBP", "CNY", "CAD", "BTC"]
|
||||||
|
|
||||||
|
coinId = coinId.upper()
|
||||||
|
currencyId = currencyId.upper()
|
||||||
|
|
||||||
if (currencyId not in allowedCurrencies):
|
if (currencyId not in allowedCurrencies):
|
||||||
yield from ctx.channel.send("Valuta " + currencyId + " ken ik niet. :disappointed: ")
|
yield from ctx.channel.send("Valuta " + currencyId + " ken ik niet. :disappointed: ")
|
||||||
return
|
return
|
||||||
@@ -50,5 +186,41 @@ class Cryptocoin(object):
|
|||||||
|
|
||||||
yield from ctx.channel.send(coinId + " is nu " + str(price) + " " + currencyId + " waard.")
|
yield from ctx.channel.send(coinId + " is nu " + str(price) + " " + currencyId + " waard.")
|
||||||
|
|
||||||
|
@commands.command(pass_context=True)
|
||||||
|
@asyncio.coroutine
|
||||||
|
def coinadvice(self, ctx, coinId: str=""):
|
||||||
|
"""Voor al uw adviezen in monopolygeld
|
||||||
|
Vraag Stroop's mening over een specifieke coin:
|
||||||
|
!coinadvice NLG
|
||||||
|
Of vraag gewoon advies in het algemeen:
|
||||||
|
!coinadvice
|
||||||
|
(Over deze laatste optie moet soms even nagedacht worden)
|
||||||
|
"""
|
||||||
|
|
||||||
|
#Geen specieke coin? Doe random.
|
||||||
|
if(coinId == ""):
|
||||||
|
yield from ctx.channel.send( self.getRandomCoinAdvice() )
|
||||||
|
return
|
||||||
|
|
||||||
|
coinId = coinId.upper()
|
||||||
|
|
||||||
|
growPercentage, dummy = self.analyseCoin(coinId)
|
||||||
|
|
||||||
|
if (growPercentage < -0.1):
|
||||||
|
yield from ctx.channel.send("Sell! Sell! Sell! " + coinId + " gaat erg slecht.")
|
||||||
|
elif (growPercentage < -0.03):
|
||||||
|
yield from ctx.channel.send("Als je " + coinId + " hebt, verkopen die handel.")
|
||||||
|
elif (growPercentage < 0):
|
||||||
|
yield from ctx.channel.send("Het gaat niet zo goed met " + coinId + ".")
|
||||||
|
elif (growPercentage < 0.015):
|
||||||
|
yield from ctx.channel.send(coinId + " lijkt redelijk stabiel.")
|
||||||
|
elif (growPercentage < 0.035):
|
||||||
|
yield from ctx.channel.send("Tijd om " + coinId + " te kopen.")
|
||||||
|
elif (growPercentage < 0.06):
|
||||||
|
yield from ctx.channel.send(coinId + " groeit snel")
|
||||||
|
elif (growPercentage < 0.1):
|
||||||
|
yield from ctx.channel.send("Het gaat heel goed met " + coinId + "!")
|
||||||
|
else:
|
||||||
|
yield from ctx.channel.send("To the moon! " + coinId + " is niet meer te stoppen!")
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user