def enrich(raw_prices): en_prices = [] for i, price in enumerate(raw_prices[0: -1]): #get the past 30 day's daily change past30_changes = [float(p["currentValue"]) - float(p["previousClosePrice"]) for p in raw_prices[i + 1: i + 30]] day_change = float(price["currentValue"]) - float(price["previousClosePrice"]) z30 = calculator.calZscore(past30_changes, day_change) past90_changes = [float(p["currentValue"]) - float(p["previousClosePrice"]) for p in raw_prices[i + 1: i + 90]] z90 = calculator.calZscore(past90_changes, day_change) en_message = {} en_message["postDate"] = price["date"][0:10] en_message["zscore30"] = round(z30, 4) en_message["zscore90"] = round(z90, 4) en_message["derivedFrom"] = {"derivedIds": [price["embersId"]]} en_message["type"] = price["type"] en_message["currentValue"] = price["currentValue"] en_message["name"] = price["name"] en_message["previousCloseValue"] = price["previousClosePrice"] en_message["oneDayChange"] = round(day_change, 4) en_message["changePercent"] = round(day_change / price["currentValue"], 4) en_message["embersId"] = hashlib.sha1(str(en_message)).hexdigest() en_prices.append(en_message) return en_prices
def getZscore(t_domain, cur_date, stock_index, cur_diff, duration): scores = [] sql = "select oneDayChange from t_enriched_bloomberg_prices where postDate<'{}' and name = '{}' order by postDate desc".format(cur_date, stock_index) rows = t_domain.select(sql, max_items=duration) for row in rows: scores.append(float(row['oneDayChange'])) zscore = calculator.calZscore(scores, cur_diff) return zscore
def getZscore(conn,cur_date,currency_index,cur_diff,duration): t_domain = get_domain(conn,"t_enriched_bloomberg_prices") scores = [] sql = "select oneDayChange from t_enriched_bloomberg_prices where post_date<'{}' and name = '{}' order by post_date desc".format(cur_date,currency_index) rows = t_domain.select(sql,max_items=duration) for row in rows: scores.append(row["oneDayChange"]) zscore = calculator.calZscore(scores, cur_diff) return zscore
def getZscore(conn,cur_date,stock_index,cur_diff,duration): cur = conn.cursor() scores = [] sql = "select one_day_change from t_enriched_bloomberg_prices where post_date<? and name = ? order by post_date desc limit ?" cur.execute(sql,(cur_date,stock_index,duration)) rows = cur.fetchall() for row in rows: scores.append(row[0]) zscore = calculator.calZscore(scores, cur_diff) return zscore