Initial configuration support.
This commit is contained in:
87
moon/moon.go
87
moon/moon.go
@@ -10,18 +10,17 @@ import (
|
||||
)
|
||||
|
||||
type Math struct {
|
||||
Asset coindesk.Asset
|
||||
CurrentPrice coindesk.Price
|
||||
Columns []Column
|
||||
Goals []float64
|
||||
Goals []Goal
|
||||
}
|
||||
|
||||
func NewMath(goals []float64, bases []Base) (m Math) {
|
||||
if goals == nil {
|
||||
goals = DefaultGoals
|
||||
}
|
||||
if bases == nil {
|
||||
bases = DefaultBases
|
||||
func NewMath(asset coindesk.Asset, goals []Goal, bases []Base) (m Math) {
|
||||
if goals == nil || bases == nil {
|
||||
panic("goals and bases must be set")
|
||||
}
|
||||
m.Asset = asset
|
||||
m.Goals = goals
|
||||
m.Columns = make([]Column, len(bases))
|
||||
for i := range bases {
|
||||
@@ -43,7 +42,7 @@ type Projection struct {
|
||||
}
|
||||
|
||||
func ProjectDates(
|
||||
from time.Time, currentPrice float64, cdpr float64, goals []float64,
|
||||
from time.Time, currentPrice float64, cdpr float64, goals []Goal,
|
||||
) (p Projection) {
|
||||
if cdpr <= 0 {
|
||||
return
|
||||
@@ -51,7 +50,7 @@ func ProjectDates(
|
||||
logP := math.Log(currentPrice)
|
||||
logR := math.Log(cdpr)
|
||||
for _, goal := range goals {
|
||||
daysToGo := (math.Log(goal) - logP) / logR
|
||||
daysToGo := (math.Log(goal.Value) - logP) / logR
|
||||
date := from.Add(time.Hour * 24 * time.Duration(daysToGo))
|
||||
p.Dates = append(p.Dates, date)
|
||||
}
|
||||
@@ -60,11 +59,11 @@ func ProjectDates(
|
||||
}
|
||||
|
||||
func (m *Math) Refresh(ctx context.Context) (err error) {
|
||||
resp, err := coindesk.GetAssetTickers(ctx, coindesk.BTC)
|
||||
resp, err := coindesk.GetAssetTickers(ctx, m.Asset)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
m.CurrentPrice = resp.Data[coindesk.BTC].OHLC.Closing
|
||||
m.CurrentPrice = resp.Data[m.Asset].OHLC.Closing
|
||||
|
||||
tasks := pool.New().WithErrors()
|
||||
tasks.WithMaxGoroutines(len(m.Columns))
|
||||
@@ -77,7 +76,7 @@ func (m *Math) Refresh(ctx context.Context) (err error) {
|
||||
c.StartingDate = c.Base.From(now)
|
||||
nextDay := c.StartingDate.Add(time.Hour * 24)
|
||||
resp, err := coindesk.GetPriceValues(ctx,
|
||||
coindesk.BTC, c.StartingDate, nextDay)
|
||||
m.Asset, c.StartingDate, nextDay)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -107,58 +106,66 @@ type Column struct {
|
||||
Projections Projection
|
||||
}
|
||||
|
||||
var DefaultGoals = []float64{
|
||||
100000,
|
||||
150000,
|
||||
200000,
|
||||
250000,
|
||||
300000,
|
||||
500000,
|
||||
1000000,
|
||||
var DefaultGoals = []Goal{
|
||||
{"$100k", 100000},
|
||||
{"$150k", 150000},
|
||||
{"$200k", 200000},
|
||||
{"$250k", 250000},
|
||||
{"$300k", 300000},
|
||||
{"$500k", 500000},
|
||||
{"$1m", 1000000},
|
||||
}
|
||||
|
||||
var DefaultBases = []Base{
|
||||
RelativeBase{"Month", time.Duration(-30) * time.Hour * 24},
|
||||
RelativeBase{"Quarter", time.Duration(-90) * time.Hour * 24},
|
||||
RelativeBase{"Half-Year", time.Duration(-182) * time.Hour * 24},
|
||||
RelativeBase{"Year", time.Duration(-365) * time.Hour * 24},
|
||||
ConstantBase{"2020-", time.Unix(1577836800, 0)},
|
||||
ConstantBase{"2019-", time.Unix(1546300800, 0)},
|
||||
ConstantBase{"2018-", time.Unix(1514764800, 0)},
|
||||
ConstantBase{"2017-", time.Unix(1483228800, 0)},
|
||||
var DefaultConstantBases = []ConstantBase{
|
||||
{"2020-", time.Unix(1577836800, 0)},
|
||||
{"2019-", time.Unix(1546300800, 0)},
|
||||
{"2018-", time.Unix(1514764800, 0)},
|
||||
{"2017-", time.Unix(1483228800, 0)},
|
||||
}
|
||||
|
||||
var DefaultRelativeBases = []RelativeBase{
|
||||
{"Month", time.Duration(-30) * time.Hour * 24},
|
||||
{"Quarter", time.Duration(-90) * time.Hour * 24},
|
||||
{"Half-Year", time.Duration(-182) * time.Hour * 24},
|
||||
{"Year", time.Duration(-365) * time.Hour * 24},
|
||||
}
|
||||
|
||||
type Goal struct {
|
||||
Name string `koanf:"name"`
|
||||
Value float64 `koanf:"value"`
|
||||
}
|
||||
|
||||
// Base is a temporal point of comparison used for price projection.
|
||||
type Base interface {
|
||||
From(now time.Time) time.Time
|
||||
Name() string
|
||||
Label() string
|
||||
}
|
||||
|
||||
// ConstantBase is a base that is a constant time, e.g. 2020-01-01.
|
||||
type ConstantBase struct {
|
||||
name string
|
||||
time time.Time
|
||||
Name string `koanf:"name"`
|
||||
Time time.Time `koanf:"time"`
|
||||
}
|
||||
|
||||
func (cb ConstantBase) From(_ time.Time) time.Time {
|
||||
return cb.time
|
||||
return cb.Time
|
||||
}
|
||||
|
||||
func (cb ConstantBase) Name() string {
|
||||
return cb.name
|
||||
func (cb ConstantBase) Label() string {
|
||||
return cb.Name
|
||||
}
|
||||
|
||||
// RelativeBase is a base that is relative, e.g. "90 days ago."
|
||||
type RelativeBase struct {
|
||||
name string
|
||||
offset time.Duration
|
||||
Name string `koanf:"name"`
|
||||
Offset time.Duration `koanf:"offset"`
|
||||
}
|
||||
|
||||
func (rb RelativeBase) From(now time.Time) time.Time {
|
||||
then := now.Add(time.Duration(rb.offset))
|
||||
then := now.Add(time.Duration(rb.Offset))
|
||||
return then
|
||||
}
|
||||
|
||||
func (rb RelativeBase) Name() string {
|
||||
return rb.name
|
||||
func (rb RelativeBase) Label() string {
|
||||
return rb.Name
|
||||
}
|
||||
|
@@ -11,13 +11,6 @@ func TestCDPR(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestProjection(t *testing.T) {
|
||||
p := moon.ProjectDates(time.Now(), 68900, 1.0055, []float64{
|
||||
100000,
|
||||
150000,
|
||||
200000,
|
||||
250000,
|
||||
300000,
|
||||
350000,
|
||||
})
|
||||
p := moon.ProjectDates(time.Now(), 68900, 1.0055, moon.DefaultGoals)
|
||||
_ = p
|
||||
}
|
||||
|
Reference in New Issue
Block a user