Created
November 17, 2020 16:45
-
-
Save whittlem/f70776cc6e1120f53b75243b77a1a274 to your computer and use it in GitHub Desktop.
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
# data: dictionary { 'dd/mm/yyy': price, 'dd/mm/yyyy': price, ... } | |
# num: range in the average calculation, normally 9 to 26 | |
def simpleMovingAverage(data, num): | |
if not isinstance(data, dict): | |
raise Exception('Dictionary input expected') | |
if not isinstance(num, int): | |
raise Exception('Integer input expected') | |
if num < 5 or num > 200: | |
raise Exception('Unusual numeric input detected') | |
if (num > len(data)): | |
raise Exception('Insufficient data for calculation') | |
data_keys = list(data.keys()) | |
data_list = list(data.values()) | |
result = {} | |
for x in range(len(data_list) - num + 1): | |
series = data_list[x:x + num] | |
result[data_keys[x + num - 1]] = round(sum(series) / num) | |
return result | |
data = cbpGetHistoricRates('BTC-GBP', 86400) | |
sma20 = simpleMovingAverage(data, 20) | |
sma50 = simpleMovingAverage(data, 50) | |
sma200 = simpleMovingAverage(data, 200) | |
print (ema12) | |
print (ema26) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment