1
1
package csvsource
2
2
3
3
import (
4
- "encoding/csv"
5
4
"time"
6
5
7
6
"github.com/c9s/bbgo/pkg/fixedpoint"
@@ -11,14 +10,12 @@ import (
11
10
type ICSVTickConverter interface {
12
11
LatestKLine () (k * types.KLine )
13
12
GetKLineResult () []types.KLine
14
- CsvTickToKLine (tick * CsvTick , interval types.Interval )
13
+ CsvTickToKLine (tick * CsvTick , interval types.Interval ) ( closesKLine bool )
15
14
}
16
15
17
16
// CSVTickConverter takes a tick and internally converts it to a KLine slice
18
17
type CSVTickConverter struct {
19
- csv * csv.Reader
20
- decoder CSVTickDecoder
21
- klines []types.KLine
18
+ klines []types.KLine
22
19
}
23
20
24
21
func NewCSVTickConverter () ICSVTickConverter {
@@ -41,7 +38,7 @@ func (c *CSVTickConverter) GetKLineResult() []types.KLine {
41
38
}
42
39
43
40
// Convert ticks to KLine with interval
44
- func (c * CSVTickConverter ) CsvTickToKLine (tick * CsvTick , interval types.Interval ) {
41
+ func (c * CSVTickConverter ) CsvTickToKLine (tick * CsvTick , interval types.Interval ) ( closesKLine bool ) {
45
42
var (
46
43
currentCandle = types.KLine {}
47
44
high = fixedpoint .Zero
@@ -50,15 +47,25 @@ func (c *CSVTickConverter) CsvTickToKLine(tick *CsvTick, interval types.Interval
50
47
isOpen , t := c .detCandleStart (tick .Timestamp .Time (), interval )
51
48
52
49
if isOpen {
50
+ k := c .LatestKLine ()
53
51
c .klines = append (c .klines , types.KLine {
54
- StartTime : types .NewTimeFromUnix (t .Unix (), 0 ),
55
- EndTime : types .NewTimeFromUnix (t .Add (interval .Duration ()).Unix (), 0 ),
56
- Open : tick .Price ,
57
- High : tick .Price ,
58
- Low : tick .Price ,
59
- Close : tick .Price ,
60
- Volume : tick .HomeNotional ,
52
+ Exchange : tick .Exchange ,
53
+ Symbol : tick .Symbol ,
54
+ Interval : interval ,
55
+ StartTime : types .NewTimeFromUnix (t .Unix (), 0 ),
56
+ EndTime : types .NewTimeFromUnix (t .Add (interval .Duration ()).Unix (), 0 ),
57
+ Open : tick .Price ,
58
+ High : tick .Price ,
59
+ Low : tick .Price ,
60
+ Close : tick .Price ,
61
+ Volume : tick .HomeNotional ,
62
+ QuoteVolume : tick .ForeignNotional ,
63
+ Closed : false ,
61
64
})
65
+ if k != nil {
66
+ k .Closed = true // k is pointer
67
+ closesKLine = true
68
+ }
62
69
return
63
70
}
64
71
@@ -77,14 +84,21 @@ func (c *CSVTickConverter) CsvTickToKLine(tick *CsvTick, interval types.Interval
77
84
}
78
85
79
86
c .klines [len (c .klines )- 1 ] = types.KLine {
80
- StartTime : currentCandle .StartTime ,
81
- EndTime : currentCandle .EndTime ,
82
- Open : currentCandle .Open ,
83
- High : high ,
84
- Low : low ,
85
- Close : tick .Price ,
86
- Volume : currentCandle .Volume .Add (tick .HomeNotional ),
87
+ StartTime : currentCandle .StartTime ,
88
+ EndTime : currentCandle .EndTime ,
89
+ Exchange : tick .Exchange ,
90
+ Symbol : tick .Symbol ,
91
+ Interval : interval ,
92
+ Open : currentCandle .Open ,
93
+ High : high ,
94
+ Low : low ,
95
+ Close : tick .Price ,
96
+ Volume : currentCandle .Volume .Add (tick .HomeNotional ),
97
+ QuoteVolume : currentCandle .QuoteVolume .Add (tick .ForeignNotional ),
98
+ Closed : false ,
87
99
}
100
+
101
+ return
88
102
}
89
103
90
104
func (c * CSVTickConverter ) detCandleStart (ts time.Time , interval types.Interval ) (isOpen bool , t time.Time ) {
0 commit comments