import asyncio import discord from discord.ext import commands from urllib import request import json, requests import time class Cryptocoin(object): """Voor al uw crypto currency advies.""" def __init__(self, bot): self.bot = bot self.voice_states = {} def __check(self, ctx): #Todo: Is dit het geld-verdienen kanaal? return True #borrowed from: http://jmduke.com/posts/basic-linear-regressions-in-python/ 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"): url = "https://min-api.cryptocompare.com/data/pricehistorical?tsyms=" + currencyId + "&fsym=" + coinId if (time != "now"): url += "&ts=" + str(time) response = requests.get(url) try: priceData = json.loads(response.content.decode('utf-8')) return priceData[coinId][currencyId] except: return -1 def analyseCoin(self, coinId): N_DATAPOINTS = 12 TIME_BETWEEN_DATAPOINTS = 86400 * 7 #one week history = [] for j in range(0, N_DATAPOINTS): history.append(self.getCoinPrice(coinId, "USD", int(time.time()) - (N_DATAPOINTS-j)*TIME_BETWEEN_DATAPOINTS)) #print(history) 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] ]) #print("long: ", growFactorLongTerm) #print("short: ", growFactorShortTerm) #print(growFactor) if (history[N_DATAPOINTS-1] != 0): 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??? #user functions below @commands.command(pass_context=True) @asyncio.coroutine def coinprice(self, ctx, coinId: str, currencyId: str="USD"): """Geeft de huidige prijs van de desbetreffende crypto currency Bijv: !coinprice BTC Of voor de echte (Erik) Hollanders: !coinprice NLG EUR """ allowedCurrencies = ["USD", "EUR", "JPY", "GBP", "CNY", "CAD", "BTC"] coinId = coinId.upper() currencyId = currencyId.upper() if (currencyId not in allowedCurrencies): yield from ctx.channel.send("Valuta " + currencyId + " ken ik niet. :disappointed: ") return price = self.getCoinPrice(coinId, currencyId) if (price == -1): yield from ctx.channel.send("Die cryptocoin ken ik niet... :frowning2: ") return 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 """ if(coinId == ""): return #TODO 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!")