Skip to content

Commit

Permalink
Add IsComplete to candle
Browse files Browse the repository at this point in the history
  • Loading branch information
evsamsonov committed Mar 6, 2024
1 parent 73c1010 commit c62f508
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 9 deletions.
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
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)
})
}

0 comments on commit c62f508

Please sign in to comment.