示例#1
0
func Scrape(last_timestamp int64) ([]model.Trade, error) {
	url := fmt.Sprintf("https://api.bitfinex.com/v1/trades/btcusd?timestamp=%d", last_timestamp+1)
	content, err := restclient.GetContent(url)
	if err != nil {
		return nil, err
	}

	bitfinex_trades := make([]BitfinexTrade, 0)
	err = json.Unmarshal(content, &bitfinex_trades)
	if err != nil {
		//fmt.Printf("%s", string(content))
		return nil, err
	}

	//Bitfinex trade response has latest trade first, reverse
	trades := []model.Trade{}
	last := len(bitfinex_trades) - 1
	for i, _ := range bitfinex_trades {
		trade := bitfinex_trades[last-i]

		var t model.Trade
		t.Timestamp = trade.Timestamp
		t.TradeId = trade.Tid
		t.Exchange = "bitfinex"
		t.Type = trade.Type
		t.Price, _ = strconv.ParseFloat(trade.Price, 64)
		t.Amount, _ = strconv.ParseFloat(trade.Amount, 64)

		trades = append(trades, t)
	}

	return trades, nil
}
示例#2
0
func LoadFromCsv(file string) ([]model.Trade, error) {
	trades := []model.Trade{}

	csvfile, err := os.Open(file)
	if err != nil {
		return trades, err
	}
	defer csvfile.Close()

	reader := csv.NewReader(csvfile)
	reader.FieldsPerRecord = -1 // see the Reader struct information below

	rawCSVdata, err := reader.ReadAll()
	if err != nil {
		return trades, err
	}

	var trade model.Trade
	for _, record := range rawCSVdata {
		trade.Timestamp, _ = strconv.ParseInt(record[0], 10, 64)
		trade.TradeId, _ = strconv.ParseInt(record[1], 10, 64)
		trade.Exchange = record[2]
		trade.Type = record[3]
		trade.Amount, _ = strconv.ParseFloat(record[4], 64)
		trade.Price, _ = strconv.ParseFloat(record[5], 64)
		trades = append(trades, trade)
	}

	return trades, nil
}
示例#3
0
func Scrape(last_timestamp int64) ([]model.Trade, error) {
	url := "https://www.bitstamp.net/api/transactions/" //defaults to all trades from last hour
	content, err := restclient.GetContent(url)
	if err != nil {
		return nil, err
	}

	bitstamp_trades := make([]BitstampTrade, 0)
	err = json.Unmarshal(content, &bitstamp_trades)
	if err != nil {
		return nil, err
	}

	//Bitstamp trade response has latest trade first, reverse
	trades := []model.Trade{}
	last := len(bitstamp_trades) - 1
	for i, _ := range bitstamp_trades {
		trade := bitstamp_trades[last-i]

		var t model.Trade
		t.Timestamp, _ = strconv.ParseInt(trade.Date, 10, 64)
		t.TradeId = trade.Tid
		t.Exchange = "bitstamp"
		if trade.Type == 0 {
			t.Type = "buy"
		} else if trade.Type == 1 {
			t.Type = "sell"
		}
		t.Price, _ = strconv.ParseFloat(trade.Price, 64)
		t.Amount, _ = strconv.ParseFloat(trade.Amount, 64)

		if t.Timestamp > last_timestamp {
			trades = append(trades, t)
		}
	}

	return trades, nil
}