forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
trade_types.go
70 lines (59 loc) · 1.75 KB
/
trade_types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package trade
import (
"errors"
"sync"
"time"
"github.com/gofrs/uuid"
"github.com/idoall/gocryptotrader/currency"
"github.com/idoall/gocryptotrader/exchanges/asset"
"github.com/idoall/gocryptotrader/exchanges/order"
)
// DefaultProcessorIntervalTime is the default timer
// to process queued trades and save them to the database
const DefaultProcessorIntervalTime = time.Second * 15
var (
processor Processor
// BufferProcessorIntervalTime is the interval to save trade buffer data to the database.
// Change this by changing the runtime param `-tradeprocessinginterval=15s`
BufferProcessorIntervalTime = DefaultProcessorIntervalTime
// ErrNoTradesSupplied is returned when an attempt is made to process trades, but is an empty slice
ErrNoTradesSupplied = errors.New("no trades supplied")
)
// Trade used to hold data and methods related to trade dissemination and
// storage
type Trade struct {
exchangeName string
dataHandler chan interface{}
tradeFeedEnabled bool
}
// Data defines trade data
type Data struct {
ID uuid.UUID `json:"ID,omitempty"`
TID string
Exchange string
CurrencyPair currency.Pair
AssetType asset.Item
Side order.Side
Price float64
Amount float64
Timestamp time.Time
}
// Processor used for processing trade data in batches
// and saving them to the database
type Processor struct {
mutex sync.Mutex
started int32
bufferProcessorInterval time.Duration
buffer []Data
}
// ByDate sorts trades by date ascending
type ByDate []Data
func (b ByDate) Len() int {
return len(b)
}
func (b ByDate) Less(i, j int) bool {
return b[i].Timestamp.Before(b[j].Timestamp)
}
func (b ByDate) Swap(i, j int) {
b[i], b[j] = b[j], b[i]
}