func run_with(base string, length int, base_history *map[int]float64) { notUsd := true if *base_history == nil { notUsd = false } base_price := 1.0 if notUsd { base_price = 0.0 } pivots := make([]float64, 0) mmas := ma.New(1827) var price, m2, wk float64 var pv bool var nmbr int sma := make(map[int]float64) lsma := make(map[int]float64) mma := make(map[int]float64) lmma := make(map[int]float64) for _, day := range keys { if notUsd { price = (*base_history)[day] if price > 0.0 { base_price = price } } if base_price == 0.0 { continue } price = stock_history[day] / base_price if price <= 0.0 { panic("WUT!???") } mmas.Add(price) wk, m2, pv = mmas.Rapideco() m2 /= wk // relative momentum if pv { pivots = append(pivots, wk) } for i := month; i <= quint; i++ { nmbr = number[i] sma[nmbr] = mmas.SMA(nmbr) lsma[nmbr] = mmas.LSMA(nmbr) mma[nmbr] = mmas.MMA(nmbr) lmma[nmbr] = mmas.LMMA(nmbr) } } sort.Float64s(pivots) pivots = fat.Agglomerate(pivots, delta) fmt.Printf("### %s / %s ###\n", stock, base) fmt.Println("# Moving Averages:") for i := month; i <= quint; i++ { nmbr = number[i] fmt.Printf("# %d Days:\n", nmbr) fmt.Printf("$%.2f\t\t%v\tSMA(%d)\n", sma[nmbr]*base_price, sma[nmbr], nmbr) fmt.Printf("$%.2f\t\t%v\tLSMA(%d)\n", lsma[nmbr]*base_price, lsma[nmbr], nmbr) fmt.Printf("$%.2f\t\t%v\tMMA(%d)\n", mma[nmbr]*base_price, mma[nmbr], nmbr) fmt.Printf("$%.2f\t\t%v\tLMMA(%d)\n", lmma[nmbr]*base_price, lmma[nmbr], nmbr) } fmt.Println("# Levels:") rounded0 := "" for _, pivot := range pivots { rounded := fmt.Sprintf("%.2f", pivot*base_price) if rounded != rounded0 { rounded0 = rounded fmt.Printf("$%s\t\t%v\n", rounded, pivot) } } }
func run_with(base string, length int, base_history *map[int]float64) { notUsd := true if *base_history == nil { notUsd = false } pivots := make([]float64, 0) weight, score := 0.0, 0.0 kick, base_price := 1.0, 1.0 if notUsd { base_price = 0.0 } mmas := ma.New(1827) var mltp, price, m2, wk float64 var pv bool var nmbr int for _, day := range keys { if day >= start_day { weight += mltps * kick } if notUsd { price = (*base_history)[day] if price > 0.0 { base_price = price } } if base_price == 0.0 { continue } price = stock_history[day] / base_price if price <= 0.0 { panic("WUT!???") } mmas.Add(price) wk, m2, pv = mmas.Rapideco() m2 /= wk // relative momentum if pv { pivots = append(pivots, wk) } // Scaled Relative Momentum Squared, m2. m2 = momentum * m2 * m2 for i := month; i <= quint; i++ { mltp, nmbr = multiple[i], number[i] if length < nmbr { break } // mma not warmed up score += mltp * kick * z(price, mmas.SMA(nmbr), m2) score += mltp * kick * z(price, mmas.LSMA(nmbr), m2) score += mltp * kick * z(price, mmas.MMA(nmbr), m2) score += mltp * kick * z(price, mmas.LMMA(nmbr), m2) } // We do the above to warm up the MMAs... if day < start_day { score = 0.0 // ...but points before start day don't count continue } kick *= dk } mltp = multiple[level] for _, pivot := range pivots { weight += 2.0 * kick score += mltp * kick * z(price, pivot, m2) score += mltp * kick * z(wk, pivot, m2) } score /= weight scores_mutex.Lock() scores[base] = score scores_mutex.Unlock() if trace { ln(base, math.Log(score)) } threads.Minus() }
func run(stock *Stock) { var ohlc, hlc, cls float64 avg := ma.New(2000) if base == nil { stock.Jot(Sf("# %s", stock.Symbol)) } else { stock.Jot(Sf("# %s / %s", stock.Symbol, base.Symbol)) } stock.Jot(Sf("# %s", Jn(stock.Keys, SEP))) n := 0 fields_length := len(stock.Keys) for stock.Scan() { day := stock.Day() d, o, h, l, c, v := day.Date, day.Open, day.High, day.Low, day.Close, day.Volume ohlc = (o + (2.0 * (h + l)) + (3.0 * c)) / 8.0 hlc = (h + l + c) / 3.0 cls = c // assume cls is c if stock.Ohlc { cls = ohlc } if stock.Hlc { cls = hlc } avg.Add(cls) col := make([]string, fields_length) for i, key := range stock.Keys { switch key { case "D": col[i] = d case "n": n++ col[i] = Sf("%v", n) case "d": // DATE col[i] = d[0:4] + DSEP + d[4:6] + DSEP + d[6:8] case "o": // OPEN col[i] = Sf("%v", o) cls = o case "h": // HIGH col[i] = Sf("%v", h) cls = h case "l": // LOW col[i] = Sf("%v", l) cls = l case "c": // CLOSE col[i] = Sf("%v", c) cls = c case "v": // VOLUME if v > 1000.0 { col[i] = Sf("%.1f", v) } else { col[i] = Sf("%v", v) } case "hlc": col[i] = Sf("%v", hlc) cls = hlc case "ohlc": col[i] = Sf("%v", ohlc) cls = ohlc case "cf": col[i] = Sf("%v", cls*v) case "j": col[i] = Sf("%d", day.Jd()) case "R": rx, rv, rp := avg.Rapideco() rt := "F" if rp { rt = "T" } col[i] = Sf("%s %.2f %v", rt, rx, rv) default: col[i] = Sf("%v", avg.Get(key)) } } stock.Jot(Jn(col, SEP)) } }