From 5bb68ef2869cc69d51796acbf449115506bb39cd Mon Sep 17 00:00:00 2001 From: JP Date: Wed, 14 Feb 2018 21:24:06 +0100 Subject: [PATCH] crypto: stabilitycheck v1 --- Cryptocoin.py | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/Cryptocoin.py b/Cryptocoin.py index 39f8eb1..41e4d19 100644 --- a/Cryptocoin.py +++ b/Cryptocoin.py @@ -5,6 +5,7 @@ from urllib import request import json, requests import time import random +import statistics class Cryptocoin(object): """Voor al uw crypto currency advies.""" @@ -31,6 +32,14 @@ class Cryptocoin(object): 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 + + #get the stability based on the standard deviation + #the closer to 1, the better. (can be < 0, by the way. sue me.) + def getCoinStability(self, history): + histMean = statistics.mean(history) + if(histMean == 0): + return 0 + return 1 - (statistics.stdev(history) / histMean) def getCoinPrice(self, coinId, currencyId="USD", time="now"): @@ -74,7 +83,10 @@ class Cryptocoin(object): growPercentageShortTerm = growFactorShortTerm / history[N_DATAPOINTS-1] growPercentage = growPercentageLongTerm*0.3 + growPercentageShortTerm*0.7 #weighted average - return growPercentage, history + + stability = self.getCoinStability(history) + + return growPercentage, stability, history #TODO: # - iets met stabiliteit? @@ -124,20 +136,19 @@ class Cryptocoin(object): tempName = coinlist[randIndex]["Name"] #analyse: - growPercentage, tempHistory = self.analyseCoin(tempId) - + growPercentage, stability, tempHistory = self.analyseCoin(tempId) #For the first few iterations we're picky - if (growPercentage > 0.1): + if (growPercentage > 0.1 and stability > 0.5): return (tempName + " (" + tempId + ") gaat to the moon!") - if (growPercentage > 0.06): + if (growPercentage > 0.06 and stability > 0.25): 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): + if (growPercentage > 0.015 and stability > 0.15): return ("Ken je " + tempName + " (" + tempId + ") al? Best leuk") if(i > 4): #After a while, we're happy to give less useful advice @@ -204,7 +215,7 @@ class Cryptocoin(object): coinId = coinId.upper() - growPercentage, dummy = self.analyseCoin(coinId) + growPercentage, stability, dummy = self.analyseCoin(coinId) if (growPercentage < -0.1): yield from ctx.channel.send("Sell! Sell! Sell! " + coinId + " gaat erg slecht.")