Skip to content

Commit

Permalink
Command to display PnL in USD
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenwilkin committed Aug 19, 2020
1 parent 11935f5 commit 3259b05
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
29 changes: 29 additions & 0 deletions cmd/treasury/pnl.go
Expand Up @@ -43,3 +43,32 @@ var pnlCmd = &cobra.Command{
fmt.Printf("PnL %%: %.2f\n", pm.PnlPercentage)
},
}

var pnlUsdCmd = &cobra.Command{
Use: "usd",
Short: "Retrieve USD PnL",
Run: func(cmd *cobra.Command, args []string) {
resp, err := client.Get("http://unix/pnl/usd")
if err != nil {
panic(err)
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}

var pm pnlMessage

json.Unmarshal(body, &pm)
if err != nil {
panic(err)
}

fmt.Printf("Cost: %f\n", pm.Cost)
fmt.Printf("Value: %f\n", pm.Value)
fmt.Printf("PnL: %f\n", pm.Pnl)
fmt.Printf("PnL %%: %.2f\n", pm.PnlPercentage)
},
}
1 change: 1 addition & 0 deletions cmd/treasury/root.go
Expand Up @@ -19,4 +19,5 @@ func init() {

assetsCmd.AddCommand(setAssetsCmd)
alertsCmd.AddCommand(alertsPriceCmd, alertsClearCmd, alertsFundingCmd)
pnlCmd.AddCommand(pnlUsdCmd)
}
21 changes: 21 additions & 0 deletions cmd/treasuryd/handlers.go
Expand Up @@ -123,6 +123,26 @@ func pnlHandler(w http.ResponseWriter, r *http.Request) {
w.Write(b)
}

func pnlUsdHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")

usdThb := statum.Symbol(symbol.USDTHB)

pm := pnlMessage{
Cost: statum.Cost / usdThb,
Value: statum.TotalValue() / usdThb,
Pnl: statum.Pnl() / usdThb,
PnlPercentage: statum.PnlPercentage(),
}

b, err := json.Marshal(pm)
if err != nil {
log.Error(err)
}

w.Write(b)
}

func alertsHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")

Expand Down Expand Up @@ -193,6 +213,7 @@ func controlHandlers() *http.ServeMux {
mux.HandleFunc("/set", setHandler)
mux.HandleFunc("/cost", costHandler)
mux.HandleFunc("/pnl", pnlHandler)
mux.HandleFunc("/pnl/usd", pnlUsdHandler)
mux.HandleFunc("/alerts", alertsHandler)
mux.HandleFunc("/alerts/clear", clearAlertsHandler)
mux.HandleFunc("/alerts/price", priceAlertsHandler)
Expand Down

0 comments on commit 3259b05

Please sign in to comment.