-
Notifications
You must be signed in to change notification settings - Fork 0
/
SMA.py
87 lines (67 loc) · 2.09 KB
/
SMA.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
74
75
76
77
78
79
80
81
82
83
84
85
86
from GDAX import GDAX
from datetime import datetime, timedelta
import json
class SMA(object):
"""
Simple pimple Moving average
Only in minutes
"""
def __init__(self, label, minutes = 15, base_time = "Current"):
self.GDAX = GDAX(label)
# Current price of the coin
self.current_price = 0
# Bitch you simple
self.SMA = 0
# List of closing prices
self.closing_prices = []
# Current index of the closing prices list
self.current_index = 0
# The duration of the ema should always be in minutes
self.duration = minutes
# Base time in minutes
self.base_time = base_time
# Report our discoveries
print "Creating " + str(minutes) + " minute SMA"
self.add_x_minutes(minutes)
# Calculate duh bitch
# Data in json
def generate_list(self, data):
used_times = {}
iteration = 0
for OHLCV in data:
try:
if len(self.closing_prices) < self.duration:
self.closing_prices.append(float(OHLCV[4]))
else:
self.closing_prices.pop(0)
self.closing_prices.append(float(OHLCV[4]))
except Exception as e:
continue
# returns base time object based off inputted base time
def get_base_time(self, incr = 0):
if self.base_time == "Current":
return datetime.utcnow()
else:
self.base_time = self.base_time + timedelta(minutes = incr)
return self.base_time
# Add up sum dem minutes
def add_x_minutes(self, num_minutes):
if num_minutes == 0:
return;
data = self.GDAX.make_request(self.get_base_time(num_minutes).replace(microsecond = 0, second = 0) - timedelta(seconds = num_minutes * 60),
self.get_base_time().replace(microsecond = 0, second = 0))
if data:
self.generate_list(data)
# Geeet summm
def get_SMA(self):
closing_price_sum = 0
for i in range(len(self.closing_prices)):
closing_price_sum = closing_price_sum + self.closing_prices[i]
self.SMA = closing_price_sum/self.duration
return self.SMA
# get da current price
def get_current_price(self):
return self.closing_prices[len(self.closing_prices)-1]
# Get all dem current prices
def get_closing_prices(self):
return self.closing_prices