Skip to content
Browse files

Cleanup, implementation of historic averages series


* Removal of much dead code. YAGNI (but I will)
* Return (dt, price) where appropriate, calling that pair a "point"
* `SymbolComparison.normalized_price_history_averages`, spits out points of (dt, price) where "price" is the average of all known symbols
  • Loading branch information
stnbu committed Sep 7, 2018
1 parent 6cf2a47 commit 92def93d5257fe32e3fa0a6435cb4dd99ec0282a
Showing with 18 additions and 53 deletions.
  1. +18 −53 coincharts/
@@ -17,19 +17,6 @@ class SymbolInfo(object):
def __init__(self, symbol):
self.symbol = symbol

def normalize_price(self, price):
return (price - self.min) / (self.max - self.min)

def normalized_price_history(self):
return map(self.normalize_price, self.history)

def dt_range(self):
start = self.history[0].dt
end = self.history[len(self.history)-1].dt
return start, end

def min(self):
@@ -46,65 +33,43 @@ def history(self):
return Prices.objects.filter(symbol=self.symbol)

def normalized_price_history(self):
def normalized_history(self):
price_delta = self.max - self.min
for price in self.history:
yield (price.price - self.min) / price_delta
for record in self.history:
yield record.dt, (record.price - self.min) / price_delta

class SymbolComparison(dict):

def __init__(self, *args, **kwargs):
self.update(*args, **kwargs)

def start_dt_indexes(self):
indexes = {}
def normalized_history_averages(self):
normalized_history_generators = []
for symbol, data in self.items():
indexes[symbol] = [s.dt for s in data.history].index(self.earliest_common_dt)
except ValueError:
raise ValueError('Could not find datetime {} in history of {}'.format(
self.earliest_common_dt, symbol))
return indexes

def earliest_common_dt(self):
return sorted([symbol.history[0].dt for symbol in self.values()])[0]

def normalized_price_history_averages(self):
normalized_price_history_generators = []
for symbol, data in self.items():
num_prices = len(self)

while True:
prices = []
for gen in normalized_price_history_generators:
for gen in normalized_history_generators:
point = gen.__next__()
# this means we end up using the dt value of the last point we get.
# this shouldn't matter, since they are *supposed* to all be the same.
# the validation of this and other things should be implemented.
dt, price = point
except StopIteration:
yield sum(prices) / num_prices
yield dt, sum(prices) / num_prices

if __name__ == '__main__':

symbol_info = SymbolComparison()
comparison = SymbolComparison()
for symbol in symbols:
symbol_info[symbol] = SymbolInfo(symbol)

# print('name\t\t\tmin\tmax\t\t\trange')
# for name, info in symbol_info.items():
# print(name,
# info.min,
# info.max, '\t',
# info.dt_range, sep='\t')

comparison = SymbolComparison(symbol_info)
comparison[symbol] = SymbolInfo(symbol)

for p in comparison.normalized_price_history_averages():
for p in comparison.normalized_history_averages():

0 comments on commit 92def93

Please sign in to comment.