diff --git a/cmd/gctcli/commands.go b/cmd/gctcli/commands.go index 02898379..cef30dae 100644 --- a/cmd/gctcli/commands.go +++ b/cmd/gctcli/commands.go @@ -3960,6 +3960,10 @@ func gctScriptUpload(c *cli.Context) error { return nil } +const klineMessage = "%v in seconds supported values are: 15, 60(1min), 180(3min), 300(5min), 600(10min), 900(15min), " + + "1800(30min), 3600(1h), 7200(2h), 14400(4h), 21600(6h), 28800(8h), 43200(12h), 86400(1d), 259200(3d) " + + "60480(1w), 1209600(2w), 1296000(15d), 2592000(1M), 31536000(1Y)" + var candleRangeSize, candleGranularity int64 var getHistoricCandlesCommand = cli.Command{ Name: "gethistoriccandles", @@ -3987,7 +3991,7 @@ var getHistoricCandlesCommand = cli.Command{ }, cli.Int64Flag{ Name: "granularity, g", - Usage: "example values are in seconds and can be one of the following {60 (1 Minute), 300 (5 Minute), 900 (15 Minute), 3600 (1 Hour), 21600 (6 Hour), 86400 (1 Day)}", + Usage: fmt.Sprintf(klineMessage, "granularity"), Value: 86400, Destination: &candleGranularity, }, @@ -4086,8 +4090,8 @@ func getHistoricCandles(c *cli.Context) error { var getHistoricCandlesExtendedCommand = cli.Command{ Name: "gethistoriccandlesextended", - Usage: "gets historical candles extended for the specified granularity up to range size time from now", - ArgsUsage: " ", + Usage: "gets historical candles for the specified pair, asset, interval & date range", + ArgsUsage: " ", Action: getHistoricCandlesExtended, Flags: []cli.Flag{ cli.StringFlag{ @@ -4103,8 +4107,8 @@ var getHistoricCandlesExtendedCommand = cli.Command{ Usage: "the asset type of the currency pair", }, cli.Int64Flag{ - Name: "granularity, g", - Usage: "example values are in seconds and can be one of the following {60 (1 Minute), 300 (5 Minute), 900 (15 Minute), 3600 (1 Hour), 21600 (6 Hour), 86400 (1 Day)}", + Name: "interval, i", + Usage: fmt.Sprintf(klineMessage, "interval"), Value: 86400, Destination: &candleGranularity, }, @@ -4160,8 +4164,8 @@ func getHistoricCandlesExtended(c *cli.Context) error { return errInvalidAsset } - if c.IsSet("granularity") { - candleGranularity = c.Int64("granularity") + if c.IsSet("interval") { + candleGranularity = c.Int64("interval") } else if c.Args().Get(3) != "" { var err error candleGranularity, err = strconv.ParseInt(c.Args().Get(3), 10, 64) diff --git a/exchanges/kline/kline.go b/exchanges/kline/kline.go index 58aaa7d7..f638b5ec 100644 --- a/exchanges/kline/kline.go +++ b/exchanges/kline/kline.go @@ -162,6 +162,8 @@ func (i Interval) Short() string { // durationToWord returns english version of interval func durationToWord(in Interval) string { switch in { + case FifteenSecond: + return "fifteensecond" case OneMin: return "onemin" case ThreeMin: @@ -208,6 +210,8 @@ func durationToWord(in Interval) string { // TotalCandlesPerInterval turns total candles per period for interval func TotalCandlesPerInterval(start, end time.Time, interval Interval) (out uint32) { switch interval { + case FifteenSecond: + out = uint32(end.Sub(start).Seconds() / 15) case OneMin: out = uint32(end.Sub(start).Minutes()) case ThreeMin: @@ -242,6 +246,8 @@ func TotalCandlesPerInterval(start, end time.Time, interval Interval) (out uint3 out = uint32(end.Sub(start).Hours()) / (24 * 7) case TwoWeek: out = uint32(end.Sub(start).Hours() / (24 * 14)) + case OneMonth: + out = uint32(end.Sub(start).Hours() / (24 * 30)) case OneYear: out = uint32(end.Sub(start).Hours() / 8760) } diff --git a/exchanges/kline/kline_test.go b/exchanges/kline/kline_test.go index 8dd21a4c..ae401c35 100644 --- a/exchanges/kline/kline_test.go +++ b/exchanges/kline/kline_test.go @@ -138,6 +138,10 @@ func TestDurationToWord(t *testing.T) { name string interval Interval }{ + { + "FifteenSecond", + FifteenSecond, + }, { "OneMin", OneMin, @@ -210,6 +214,10 @@ func TestDurationToWord(t *testing.T) { "OneMonth", OneMonth, }, + { + "OneYear", + OneYear, + }, { "notfound", Interval(time.Hour * 1337), @@ -241,16 +249,123 @@ func TestKlineErrors(t *testing.T) { } func TestTotalCandlesPerInterval(t *testing.T) { - end := time.Now() - start := end.AddDate(-1, 0, 0) + start := time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC) + end := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC) - v := TotalCandlesPerInterval(start, end, OneYear) - if v != 1 { - t.Fatalf("unexpected result expected 1 received %v", v) + testCases := []struct { + name string + interval Interval + expected uint32 + }{ + { + "FifteenSecond", + FifteenSecond, + 2102400, + }, + { + "OneMin", + OneMin, + 525600, + }, + { + "ThreeMin", + ThreeMin, + 175200, + }, + { + "FiveMin", + FiveMin, + 105120, + }, + { + "TenMin", + TenMin, + 52560, + }, + { + "FifteenMin", + FifteenMin, + 35040, + }, + { + "ThirtyMin", + ThirtyMin, + 17520, + }, + { + "OneHour", + OneHour, + 8760, + }, + { + "TwoHour", + TwoHour, + 4380, + }, + { + "FourHour", + FourHour, + 2190, + }, + { + "SixHour", + SixHour, + 1460, + }, + { + "EightHour", + OneHour * 8, + 1095, + }, + { + "TwelveHour", + TwelveHour, + 730, + }, + { + "OneDay", + OneDay, + 365, + }, + { + "ThreeDay", + ThreeDay, + 121, + }, + { + "FifteenDay", + FifteenDay, + 24, + }, + { + "OneWeek", + OneWeek, + 52, + }, + { + "TwoWeek", + TwoWeek, + 26, + }, + { + "OneMonth", + OneMonth, + 12, + }, + { + "OneYear", + OneYear, + 1, + }, } - v = TotalCandlesPerInterval(start, end, FifteenDay) - if v != 24 { - t.Fatalf("unexpected result expected 24 received %v", v) + for x := range testCases { + test := testCases[x] + t.Run(test.name, func(t *testing.T) { + v := TotalCandlesPerInterval(start, end, test.interval) + if v != test.expected { + t.Fatalf("%v: received %v expected %v", test.name, v, test.expected) + } + }) } }