Created
December 18, 2017 23:43
-
-
Save stran12/3fb2ec8387008fde822d7054989e3085 to your computer and use it in GitHub Desktop.
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 ( | |
"fmt" | |
"net/http" | |
"log" | |
"io/ioutil" | |
"encoding/json" | |
"strconv" | |
) | |
// what and quantity | |
// btc = 4.83 | |
var holdings = map[string]float64{ | |
"bitcoin": 1.0001, | |
"litecoin": 1, | |
"stellar": 1, | |
"monero": 1, | |
} | |
const initial = 1000.00 | |
const baseAPI = "https://api.coinmarketcap.com/v1/ticker" | |
type Ticker struct { | |
ID string `json:"id"` | |
Name string `json:"name"` | |
Symbol string `json:"symbol"` | |
PriceUSD string `json:"price_usd"` | |
PercentChanged1H string `json:"percent_change_1h"` | |
PercentChanged24H string `json:"percent_change_24h"` | |
PercentChanged1D string `json:"percent_change_7d"` | |
CurrentValue float64 | |
} | |
type Resp []Ticker | |
func main() { | |
var results []Ticker | |
for k, v := range holdings { | |
url := fmt.Sprintf("%s/%s", baseAPI, k) | |
resp, err := http.Get(url) | |
if err != nil { | |
log.Println("err getting ticket", k) | |
continue | |
} | |
bytes, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
log.Println("err reading response", err, k) | |
continue | |
} | |
var t Ticker | |
var r Resp | |
err = json.Unmarshal(bytes, &r) | |
if err != nil { | |
log.Println("err unmarshaling response", err, k) | |
} | |
if len(r) == 0 { | |
log.Println("no results found", k) | |
continue | |
} else { | |
t = r[0] | |
} | |
currentValue, err := strconv.ParseFloat(t.PriceUSD, 64) | |
if err != nil { | |
log.Println("err parsing price usd", err, k) | |
} | |
t.CurrentValue = currentValue * v | |
results = append(results, t) | |
} | |
var total float64 | |
total = 0 | |
for _, v := range results { | |
total += v.CurrentValue | |
println( | |
fmt.Sprintf("%8s", v.Name), | |
fmt.Sprintf("% 16.8f", v.CurrentValue), | |
fmt.Sprintf("% 16.8s", v.PriceUSD), | |
fmt.Sprintf("% 8s", v.PercentChanged24H)) | |
} | |
println("=========================") | |
println(fmt.Sprintf("%8s", "Total"), fmt.Sprintf("% 16.8f", total)) | |
println(fmt.Sprintf("%8s", "Initial"), fmt.Sprintf("% 16.8f", initial)) | |
println("=========================") | |
println(fmt.Sprintf("%8s", "Profit"), fmt.Sprintf("% 16.8f", total-initial)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment