Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Saves data from Bittrex markets to csv.
package main
import (
"os"
//"fmt"
"log"
"time"
"strings"
"strconv"
"encoding/csv"
"github.com/jyap808/go-bittrex"
//"github.com/shopspring/decimal"
)
const (
count = 10
API_KEY = ""
API_SECRET = ""
HOME = "/home/machine/market/"
)
var (
num = 0
_bittrex = true
//intrvl = 5
// MARKETS
_BTC = true
_USDT = true
)
func main() {
f, err := os.OpenFile("/logs/altdb_coin_alt.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.Println(err)
}
defer f.Close()
log.SetOutput(f)
// BITTREX
bittrex := bittrex.New(API_KEY, API_SECRET)
interval := 5 * time.Second
for true {
start_time := time.Now()
// BITTREX PART
if _bittrex {
marketSummaries, err := bittrex.GetMarketSummaries()
if err != nil {
log.Println(err)
}
today := time.Now()
file_name := "_" + today.Format("02-01-2006") + ".csv"
for _, coin := range marketSummaries {
// Check non desired markets.
if strings.HasPrefix(coin.MarketName,"ETH") {continue}
// Create file or append to an existing csv file.
file, err := os.OpenFile(HOME + "data/" + coin.MarketName + file_name, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.Println(err)
}
writer := csv.NewWriter(file)
fileInfo, err := os.Stat(HOME + "data/" + coin.MarketName + file_name)
if err != nil {log.Fatal(err)}
if fileInfo.Size()==0 {
writer.Write([]string{"","Ask","BaseVolume","Bid","High","Last","Low","OpenBuy","OpenSell","time"})
}
var data = []string{
strconv.Itoa(num),
strconv.FormatFloat(coin.Ask,'E',-1,64),
strconv.FormatFloat(coin.BaseVolume,'E',-1,64),
strconv.FormatFloat(coin.Bid,'E',-1,64),
strconv.FormatFloat(coin.High,'E',-1,64),
strconv.FormatFloat(coin.Last,'E',-1,64),
strconv.FormatFloat(coin.Low,'E',-1,64),
strconv.Itoa(coin.OpenBuyOrders),
strconv.Itoa(coin.OpenSellOrders),
coin.TimeStamp}
err = writer.Write(data)
if err != nil {
log.Println(err)
}
writer.Flush()
file.Close()
}
}
num++
elapsed := time.Since(start_time)
if elapsed < interval {
time.Sleep(interval - elapsed)
}
}
}
@xujunhuii

This comment has been minimized.

Copy link

@xujunhuii xujunhuii commented Mar 8, 2021

Thanks for sharing. May I know how to run this .go file for the crypto repository?

@ivopetiz

This comment has been minimized.

Copy link
Owner Author

@ivopetiz ivopetiz commented Mar 29, 2021

hello. Just need to 'go run' it and it will generate csv files with data from Bittrex.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment