"""The Galore board score. Public on purpose: app.trygalore.com/qualify/score.py serves this file as it runs. score = return (%) - half the worst drop (%). Buy and hold from the first close; cash earns 0. Going all in on one stock is allowed: if it works, it works. The same score orders the paper board's line for early access to real trading.""" def score(closes, weights): """closes: {ticker: [close per day, same days for every ticker]}. weights: {ticker: fraction of the book}, summing to at most 1.""" cash = 1 - sum(weights.values()) units = {t: w / closes[t][0] for t, w in weights.items()} days = len(next(iter(closes.values()))) values, top_shares = [], [] for d in range(days): held = {t: u * closes[t][d] for t, u in units.items()} value = cash + sum(held.values()) values.append(value) top_shares.append(max(held.values()) / value if held else 0) peak, worst = values[0], 0.0 for v in values: peak = max(peak, v) worst = max(worst, (peak - v) / peak) ret = (values[-1] / values[0] - 1) * 100 drop = worst * 100 return { 'return': ret, 'worstDrop': drop, 'topShare': max(top_shares) * 100, 'score': ret - drop / 2, 'ranked': bool(weights), }