Skip to content

Instantly share code, notes, and snippets.

@ivopetiz
Created June 9, 2018 22:43
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivopetiz/051eb8dcef769e655254df21a093831a to your computer and use it in GitHub Desktop.
Save ivopetiz/051eb8dcef769e655254df21a093831a to your computer and use it in GitHub Desktop.
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)
}
}
}
@cryptodogofmax
Copy link

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

@ivopetiz
Copy link
Author

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

@vpolimenov
Copy link

what about collecting data from Binance?

@ivopetiz
Copy link
Author

ivopetiz commented Aug 8, 2022

I didn't need to for now. Maybe implement it later but the code shouldn't vary much from the Bittrex version.

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