This repository has been archived on 2022-11-30. You can view files and clone it, but cannot push or open issues or pull requests.
tacitus/cmd/interval/main.go

83 lines
1.5 KiB
Go

package main
import (
"encoding/csv"
"fmt"
"github.com/shopspring/decimal"
"log"
"os"
"strconv"
"time"
"github.com/kcotugno/tacitus/postgres"
)
func main() {
client := postgres.NewClient()
client.Name = "gdax"
client.User = "gdax"
err := client.Open()
if err != nil {
log.Fatal(err)
}
var start time.Time
var end time.Time
var done bool
file, err := os.Create("btcusd_30_sec.csv")
if err != nil {
log.Fatal(err)
}
csv := csv.NewWriter(file)
first, _ := client.TradeService().Trade(1)
start = first.Timestamp.Truncate(30 * time.Second)
end = start.Add(30 * time.Second)
err = csv.Write([]string{"timestamp", "price", "volume", "transactions"})
if err != nil {
log.Fatal(err)
}
var lastPrice string
for !done {
trades, _ := client.TradeService().TradesInDateRange("BTCUSD", start, end)
if len(trades) > 0 {
var volume decimal.Decimal
var transactions int
for _, t := range trades {
volume = volume.Add(t.Size)
transactions += 1
lastPrice = t.Price.String()
}
data := []string{end.String(), lastPrice, volume.String(), strconv.Itoa(transactions)}
if err = csv.Write(data); err != nil {
log.Fatal(err)
}
fmt.Println(end)
} else {
data := []string{end.String(), lastPrice, "0", "0"}
if err = csv.Write(data); err != nil {
log.Fatal(err)
}
}
start = end
end = end.Add(30 * time.Second)
if start.After(time.Date(2017, 8, 30, 16, 13, 50, 0, time.UTC)) {
done = true
}
}
csv.Flush()
file.Close()
}