Instantly share code, notes, and snippets.
CreatedJune 9, 2018 22:43
Save ivopetiz/051eb8dcef769e655254df21a093831a to your computer and use it in GitHub Desktop.
Saves data from Bittrex markets to csv.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
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 | |
) | |
funcmain() { | |
f,err:=os.OpenFile("/logs/altdb_coin_alt.log",os.O_WRONLY|os.O_CREATE|os.O_APPEND,0644) | |
iferr!=nil { | |
log.Println(err) | |
} | |
deferf.Close() | |
log.SetOutput(f) | |
// BITTREX | |
bittrex:=bittrex.New(API_KEY,API_SECRET) | |
interval:=5*time.Second | |
fortrue { | |
start_time:=time.Now() | |
// BITTREX PART | |
if_bittrex { | |
marketSummaries,err:=bittrex.GetMarketSummaries() | |
iferr!=nil { | |
log.Println(err) | |
} | |
today:=time.Now() | |
file_name:="_"+today.Format("02-01-2006")+".csv" | |
for_,coin:=rangemarketSummaries { | |
// Check non desired markets. | |
ifstrings.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) | |
iferr!=nil { | |
log.Println(err) | |
} | |
writer:=csv.NewWriter(file) | |
fileInfo,err:=os.Stat(HOME+"data/"+coin.MarketName+file_name) | |
iferr!=nil {log.Fatal(err)} | |
iffileInfo.Size()==0 { | |
writer.Write([]string{"","Ask","BaseVolume","Bid","High","Last","Low","OpenBuy","OpenSell","time"}) | |
} | |
vardata= []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) | |
iferr!=nil { | |
log.Println(err) | |
} | |
writer.Flush() | |
file.Close() | |
} | |
} | |
num++ | |
elapsed:=time.Since(start_time) | |
ifelapsed<interval { | |
time.Sleep(interval-elapsed) | |
} | |
} | |
} |
cryptodogofmax commentedMar 8, 2021
Thanks for sharing. May I know how to run this .go file for the crypto repository?
hello. Just need to 'go run' it and it will generate csv files with data from Bittrex.
vpolimenov commentedJul 31, 2022
what about collecting data from Binance?
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 freeto join this conversation on GitHub. Already have an account?Sign in to comment