Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IsComplete to candle #7

Merged
merged 2 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.idea/
vendor/
.DS_Store

2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ lint: ## Run golang lint using docker
-v ${GOPATH}/pkg/mod:/go/pkg/mod \
-v ${PWD}:/app \
-w /app \
golangci/golangci-lint:v1.55.2 \
golangci/golangci-lint:v1.56.2 \
golangci-lint run -v --modules-download-mode=readonly

test: ## Run tests
Expand Down
13 changes: 7 additions & 6 deletions timeseries/candle.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import "time"

// Candle represents trading candle
type Candle struct {
Time time.Time `json:"time" db:"time" validate:"required"`
High float64 `json:"high" db:"high" validate:"required"`
Low float64 `json:"low" db:"low" validate:"required"`
Open float64 `json:"open" db:"open" validate:"required"`
Close float64 `json:"close" db:"close" validate:"required"`
Volume int64 `json:"volume" db:"volume" validate:"required"`
Time time.Time `json:"time" db:"time" validate:"required"`
High float64 `json:"high" db:"high" validate:"required"`
Low float64 `json:"low" db:"low" validate:"required"`
Open float64 `json:"open" db:"open" validate:"required"`
Close float64 `json:"close" db:"close" validate:"required"`
Volume int64 `json:"volume" db:"volume" validate:"required"`
IsComplete bool `json:"is_complete" db:"is_complete"`
}

// NewCandle creates new candle
Expand Down
6 changes: 3 additions & 3 deletions timeseries/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func ExampleTimeSeries() {
series, _ = series.Trim(1, 0)
fmt.Printf("After trim\t= %v\n", series.Candle(0))
// Output:
// Candle = &{1970-01-01 00:00:01 +0000 UTC 1 2 3 4 5}
// Last candle = &{1970-01-01 00:00:02 +0000 UTC 6 7 8 9 10}
// Candle = &{1970-01-01 00:00:01 +0000 UTC 1 2 3 4 5 false}
// Last candle = &{1970-01-01 00:00:02 +0000 UTC 6 7 8 9 10 false}
// Length = 2
// After trim = &{1970-01-01 00:00:02 +0000 UTC 6 7 8 9 10}
// After trim = &{1970-01-01 00:00:02 +0000 UTC 6 7 8 9 10 false}
}
16 changes: 14 additions & 2 deletions timeseries/time_series.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (ts *TimeSeries) Trim(startIndex int, endIndex int) (*TimeSeries, error) {
return &newTS, nil
}

// LastCandle returns last candle in series
// LastCandle returns last candle in series or nil if series is empty
func (ts *TimeSeries) LastCandle() *Candle {
if len(ts.candles) > 0 {
return ts.candles[len(ts.candles)-1]
Expand All @@ -79,7 +79,19 @@ func (ts *TimeSeries) LastCandle() *Candle {
return nil
}

// Candle returns candle by index [0, len(series)-1]
// LastCompleteCandle returns last complete candle and index in series or nil if there are no complete candles
func (ts *TimeSeries) LastCompleteCandle() (*Candle, int) {
for i := ts.Length() - 1; i >= 0; i-- {
candle := ts.Candle(i)
if candle.IsComplete {
return candle, i
}
}

return nil, 0
}

// Candle returns candle by index [0, len(series)-1] or nil if index out of series
func (ts *TimeSeries) Candle(index int) *Candle {
if index >= 0 && index < len(ts.candles) {
return ts.candles[index]
Expand Down
30 changes: 30 additions & 0 deletions timeseries/time_series_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,33 @@ func TestTimeSeries_Length(t *testing.T) {

assert.Equal(t, 2, series.Length())
}

func TestTimeSeries_LastCompleteCandle(t *testing.T) {
t.Run("LastCandle=nil", func(t *testing.T) {
series := New()
candle, _ := series.LastCompleteCandle()
assert.Nil(t, candle)
})

t.Run("LastCompleteCandle not nil", func(t *testing.T) {
series := New()

err := series.AddCandle(createTestCandle())
assert.Nil(t, err)

completeCandle := createTestCandle()
completeCandle.Time = time.Unix(2, 0)
completeCandle.IsComplete = true
err = series.AddCandle(completeCandle)
assert.Nil(t, err)

candle := createTestCandle()
candle.Time = time.Unix(3, 0)
err = series.AddCandle(candle)
assert.Nil(t, err)

candle, index := series.LastCompleteCandle()
assert.Equal(t, completeCandle, candle)
assert.Equal(t, 1, index)
})
}
Loading