-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
106 lines (86 loc) · 2.13 KB
/
main.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"fmt"
"log"
"os"
"strconv"
"strings"
"time"
influxdb "github.com/influxdata/influxdb1-client/v2"
"github.com/jyap808/go-poloniex"
)
func main() {
client := poloniex.New(os.Getenv("POLONIEX_API_KEY"), os.Getenv("POLONIEX_API_SECRET"))
for {
total := 0.0
tickers, err := client.GetTickers()
if err != nil {
log.Printf("An error occured, retrying in 10s: '%v'", err)
time.Sleep(10 * time.Second)
continue
}
balances, err := client.GetBalances()
if err != nil {
log.Fatal(err)
}
batchPoints, err := influxdb.NewBatchPoints(influxdb.BatchPointsConfig{
Database: influxDatabase,
})
for name, balance := range balances {
available, _ := strconv.ParseFloat(balance.Available, 64)
onOrders, _ := strconv.ParseFloat(balance.OnOrders, 64)
if available == 0 && onOrders == 0 {
continue
}
var ticker poloniex.Ticker
if ticker, err = getTickerForBalance(tickers, name); err != nil {
log.Print(err)
continue
}
amount := ticker.Last * (available + onOrders)
if amount < 1 { // Skip too little money
continue
}
cryptoPoint, _ := influxdb.NewPoint(
"crypto",
map[string]string{
"name": name,
},
map[string]interface{}{
"total": amount,
},
time.Now(),
)
batchPoints.AddPoint(cryptoPoint)
total += amount
}
cryptoPoint, _ := influxdb.NewPoint(
"crypto",
map[string]string{
"name": "total",
},
map[string]interface{}{
"total": total,
},
time.Now(),
)
batchPoints.AddPoint(cryptoPoint)
err = influxClient.Write(batchPoints)
if err != nil {
log.Fatalf("[InfluxDB] Impossible to write points: %v", err)
}
log.Printf("Total: %f", total)
time.Sleep(time.Second * 10)
}
}
func getTickerForBalance(tickers map[string]poloniex.Ticker, balanceName string) (poloniex.Ticker, error) {
for name, ticker := range tickers {
if strings.HasSuffix(name, balanceName) {
if strings.Contains(name, "USDT") {
//log.Printf("Found %s for %s", name, balanceName)
return ticker, nil
}
}
}
return poloniex.Ticker{Last: 0}, fmt.Errorf("no ticker found for %s in USDT", balanceName)
}