-
Notifications
You must be signed in to change notification settings - Fork 1
/
macd.py
73 lines (63 loc) · 2.19 KB
/
macd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# -*- coding: utf-8 -*-
import logging
from datetime import datetime, timedelta
import crypto
class MACD:
"""computes MACD"""
def __init__(self, market_symbol):
self.market_symbol = market_symbol
self.timeframe = '1d'
self.limit = None
self.crypto_client = crypto.Crypto(None, 'bittrex') # defaults to using Bittrex client for looking up exchange rates to USDT
self.init_logger()
def log(self, msg):
msg = str(msg)
print(msg)
self.log_buffer.append(msg)
self.logger.info(msg)
def init_logger(self):
self.log_buffer = []
self.logger = logging.getLogger(__name__)
self.logger.setLevel(logging.INFO)
# create a file handler
handler = logging.FileHandler('./crypto.log')
handler.setLevel(logging.INFO)
# create a logging format
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
# add the handlers to the logger
self.logger.addHandler(handler)
def computeMACD(self):
ema26 = self.getEMA(26)
ema12 = self.getEMA(12)
macd = [(ema26[i] - ema12[i]) for i in range(0, len(ema26))]
ema9 = self.getEMA(9)
self.log("MACD: %s" % (macd))
self.log("9d EMA: %s" % (ema9))
def calculator(self, days, data):
alpha = 2 / (days + 1)
previous = 0
initialAccumulator = 0
skip = 0
results = []
for i in range(0, len(data)):
v = data[i][2]
# if not previous and not v:
# skip += 1
if i < (days + skip - 1):
initialAccumulator += v
elif i == (days + skip - 1):
initialAccumulator += v
initialValue = initialAccumulator / days;
previous = initialValue
results.append(initialValue)
else:
nextValue = v * alpha + (1 - alpha) * previous;
previous = nextValue
results.append(nextValue)
return results
def getEMA(self, days):
since_dt = datetime.now() - timedelta(days)
data = self.crypto_client.fetch_ohlcv(self.market_symbol, self.timeframe, since_dt, self.limit);
self.log(data)
return self.calculator(days, data)