Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,17 @@
|
|||
from collections import deque
|
||||
|
||||
def simplemovingaverage(period):
|
||||
assert period == int(period) and period > 0, "Period must be an integer >0"
|
||||
|
||||
summ = n = 0.0
|
||||
values = deque([0.0] * period) # old value queue
|
||||
|
||||
def sma(x):
|
||||
nonlocal summ, n
|
||||
|
||||
values.append(x)
|
||||
summ += x - values.popleft()
|
||||
n = min(n+1, period)
|
||||
return summ / n
|
||||
|
||||
return sma
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
from collections import deque
|
||||
|
||||
class Simplemovingaverage():
|
||||
def __init__(self, period):
|
||||
assert period == int(period) and period > 0, "Period must be an integer >0"
|
||||
self.period = period
|
||||
self.stream = deque()
|
||||
|
||||
def __call__(self, n):
|
||||
stream = self.stream
|
||||
stream.append(n) # appends on the right
|
||||
streamlength = len(stream)
|
||||
if streamlength > self.period:
|
||||
stream.popleft()
|
||||
streamlength -= 1
|
||||
if streamlength == 0:
|
||||
average = 0
|
||||
else:
|
||||
average = sum( stream ) / streamlength
|
||||
|
||||
return average
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
if __name__ == '__main__':
|
||||
for period in [3, 5]:
|
||||
print ("\nSIMPLE MOVING AVERAGE (procedural): PERIOD =", period)
|
||||
sma = simplemovingaverage(period)
|
||||
for i in range(1,6):
|
||||
print (" Next number = %-2g, SMA = %g " % (i, sma(i)))
|
||||
for i in range(5, 0, -1):
|
||||
print (" Next number = %-2g, SMA = %g " % (i, sma(i)))
|
||||
for period in [3, 5]:
|
||||
print ("\nSIMPLE MOVING AVERAGE (class based): PERIOD =", period)
|
||||
sma = Simplemovingaverage(period)
|
||||
for i in range(1,6):
|
||||
print (" Next number = %-2g, SMA = %g " % (i, sma(i)))
|
||||
for i in range(5, 0, -1):
|
||||
print (" Next number = %-2g, SMA = %g " % (i, sma(i)))
|
||||
Loading…
Add table
Add a link
Reference in a new issue