Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
shikingram committed Aug 21, 2021
1 parent 6ce4d8a commit a96acfd
Show file tree
Hide file tree
Showing 23 changed files with 1,262 additions and 0 deletions.
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# 小工具---自动生成虚拟币收益数据
## 下载
- [windows](https://github.com/K1ngram4/CoinRecord/releases/download/V1.0/CoinRecord_win.zip)
- [osx](https://github.com/K1ngram4/CoinRecord/releases/download/V1.0/CoinRecord_macos.zip)

执行可执行文件即可在records目录下生成md文件,记录收益数据

- 效果

| 币种 | 持有数量 | 现价 | 总金额 | 持仓均价 | 成本 | 利润 | 收益率 |
| ---- | -------------- | ------------ | ----------- | ------------ | ----------- | --------- | ------ |
| btc | 0.013343 | 45168.320300 | 602.680898 | 41737.240501 | 556.900000 | 45.780898 | 8.22% |
| doge | 8058.726300 | 0.299873 | 2416.596306 | 0.297150 | 2394.650000 | 21.946306 | 0.92% |
| icp | 0.718261 | 57.868641 | 41.564765 | 57.138036 | 41.040000 | 0.524765 | 1.28% |
| shib | 3460860.410000 | 0.000008 | 28.326569 | 0.000008 | 28.240000 | 0.086569 | 0.31% |
| win | 9991.000000 | 0.000635 | 6.340803 | 0.000621 | 6.200000 | 0.140803 | 2.27% |


## 配置说明
- 配置文件在holds目录下,json格式,多条记录分开
```json
{
"name": "btc",
"records": [
{
"operate": "+",
"amount": 0.01329424,
"sum": 613.82
},
{
"operate": "-",
"amount": 0.01329424,
"sum": 613.82
}
]
}
```
- name 币种
- records 表示操作记录
- opetare 操作 “+”买入 “-”卖出
- amount 数量
- sum 总金额 单位usdt

**一个币种一个json文件**
119 changes: 119 additions & 0 deletions coinPrice/coinMarketCap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package coinPrice

type CoinMarketCapLastListResult struct {
Data []SingleCoinInfo `json:"data"`
Status State `json:"status"`
}

type State struct {
Timestamp string `json:"timestamp"`
ErrorCode int `json:"error_code"`
ErrorMessage string `json:"error_message"`
Elapsed int `json:"elapsed"`
CreditCount int `json:"credit_count"`
}

type SingleCoinInfo struct {
ID int `json:"id"`
Name string `json:"name"`
Symbol string `json:"symbol"`
Slug string `json:"slug"`
//市值排名
CmcRank int `json:"cmc_rank"`
NumMarketPairs int `json:"num_market_pairs"`
CirculatingSupply float64 `json:"circulating_supply"`
//目前存在的硬币总量
TotalSupply float64 `json:"total_supply"`

MaxSupply float64 `json:"max_supply"`
LastUpdated string `json:"last_updated"`
DateAdded string `json:"date_added"`
Tags []string `json:"tags"`
Platform quoteInfo `json:"-"`
Quote quoteInfos `json:"quote"`
}

type quoteInfos struct {
BTC quoteInfo
USD quoteInfo
}

type quoteInfo struct {
//价格
Price float64 `json:"price"`
// 24小时交易量
Volume24H float64 `json:"volume_24h"`
//1小时变化
PercentChange1H float64 `json:"percent_change_1h"`
//24小时变化
PercentChange24H float64 `json:"percent_change_24h"`
//7天变化
PercentChange7D float64 `json:"percent_change_7d"`
//市值
MarketCap float64 `json:"market_cap"`
MarketCapDominance float64 `json:"market_cap_dominance"`
FullyDilutedMarketCap float64 `json:"fully_diluted_market_cap"`
LastUpdated string `json:"last_updated"`
}

//===========
type GlobalMetricsResult struct {
Data GlobalMetricsData `json:"data"`
Status State `json:"status"`
LastUpdated string `json:"last_updated"`
}

type GlobalMetricsData struct {
ActiveCryptocurrencies int `json:"active_cryptocurrencies"`
TotalCryptocurrencies int `json:"total_cryptocurrencies"`
ActiveMarketPairs int `json:"active_market_pairs"`
ActiveExchanges int `json:"active_exchanges"`
TotalExchanges int `json:"total_exchanges"`
EthDominance float64 `json:"eth_dominance"`
BtcDominance float64 `json:"btc_dominance"`
EthDominanceYesterday float64 `json:"eth_dominance_yesterday"`
BtcDominanceYesterday float64 `json:"btc_dominance_yesterday"`
EthDominance24HPercentageChange float64 `json:"eth_dominance_24h_percentage_change"`
BtcDominance24HPercentageChange float64 `json:"btc_dominance_24h_percentage_change"`
DefiVolume24H float64 `json:"defi_volume_24h"`
DefiVolume24HReported float64 `json:"defi_volume_24h_reported"`
DefiMarketCap float64 `json:"defi_market_cap"`
Defi24HPercentageChange float64 `json:"defi_24h_percentage_change"`
StablecoinVolume24H float64 `json:"stablecoin_volume_24h"`
StablecoinVolume24HReported float64 `json:"stablecoin_volume_24h_reported"`
StablecoinMarketCap float64 `json:"stablecoin_market_cap"`
Stablecoin24HPercentageChange float64 `json:"stablecoin_24h_percentage_change"`
DerivativesVolume24H float64 `json:"derivatives_volume_24h"`
DerivativesVolume24HReported float64 `json:"derivatives_volume_24h_reported"`
Derivatives24HPercentageChange float64 `json:"derivatives_24h_percentage_change"`
Quote GlobalMetricsQuoteInfo `json:"quote"`
}

type GlobalMetricsQuoteInfo struct {
USD GlobalMetricsQuoteInfoUsd
}

type GlobalMetricsQuoteInfoUsd struct {
TotalMarketCap float64 `json:"total_market_cap"`
TotalVolume24H float64 `json:"total_volume_24h"`
TotalVolume24HReported float64 `json:"total_volume_24h_reported"`
AltcoinVolume24H float64 `json:"altcoin_volume_24h"`
AltcoinVolume24HReported float64 `json:"altcoin_volume_24h_reported"`
AltcoinMarketCap float64 `json:"altcoin_market_cap"`
DefiVolume24H float64 `json:"defi_volume_24h"`
DefiVolume24HReported float64 `json:"defi_volume_24h_reported"`
Defi24HPercentageChange float64 `json:"defi_24h_percentage_change"`
DefiMarketCap float64 `json:"defi_market_cap"`
StablecoinVolume24H float64 `json:"stablecoin_volume_24h"`
StablecoinVolume24HReported float64 `json:"stablecoin_volume_24h_reported"`
Stablecoin24HPercentageChange float64 `json:"stablecoin_24h_percentage_change"`
StablecoinMarketCap float64 `json:"stablecoin_market_cap"`
DerivativesVolume24H float64 `json:"derivatives_volume_24h"`
DerivativesVolume24HReported float64 `json:"derivatives_volume_24h_reported"`
Derivatives24HPercentageChange float64 `json:"derivatives_24h_percentage_change"`
LastUpdated string `json:"last_updated"`
TotalMarketCapYesterday float64 `json:"total_market_cap_yesterday"`
TotalVolume24HYesterday float64 `json:"total_volume_24h_yesterday"`
TotalMarketCapYesterdayPercentageChange float64 `json:"total_market_cap_yesterday_percentage_change"`
TotalVolume24HYesterdayPercentageChange float64 `json:"total_volume_24h_yesterday_percentage_change"`
}
27 changes: 27 additions & 0 deletions coinPrice/coinMarketCap_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package coinPrice

import (
"CoinRecord/httpUtil"
"encoding/json"
"fmt"
"testing"
)

func TestGetCoinPrice(t *testing.T) {
url := "https://pro-api.coinmarketcap.com/v1/global-metrics/quotes/latest"
m := make(map[string]string)
m2 := make(map[string]string)
//m["start"] = "1"
//m["limit"] = "100"
b, err := httpUtil.HttpGet(url, m, m2)
if err != nil {
fmt.Printf("%+v", err)
}
//fmt.Println(string(b))
res := &GlobalMetricsResult{}
err = json.Unmarshal(b, res)
if err != nil {
fmt.Printf("%+v",err)
}
fmt.Printf("%+v",res)
}
72 changes: 72 additions & 0 deletions coinPrice/coinPrice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package coinPrice

import (
"CoinRecord/httpUtil"
"encoding/json"
"fmt"
"strings"
"time"
)

// coincap.io
//var url = "https://api.coincap.io/v2/assets"
var url = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest"
var coinMarketCapUrl = "https://pro-api.coinmarketcap.com/v1/global-metrics/quotes/latest"

var res CoinMarketCapLastListResult
var GRes GlobalMetricsResult

func GetAllCoinPrice() error {
//api比较卡,简单加个重试机制
var count int
var params = make(map[string]string)
params["limit"] = "1000"
params["start"] = "1"
var headers = make(map[string]string)
for {
if count > 10 {
break
}
resBytes, err := httpUtil.HttpGet(url, params, headers)
if err != nil {
count++
fmt.Printf("%+v \n", err)
time.Sleep(time.Millisecond * 10)
continue
}

err = json.Unmarshal(resBytes, &res)
if err != nil {
count++
fmt.Printf("%+v \n", err)
time.Sleep(time.Millisecond * 10)
continue
}
break
}
return nil
}

func GetCoinPrice(id string) SingleCoinInfo {
for _, coinInfo := range res.Data {
if coinInfo.Name == id || coinInfo.Slug == id || coinInfo.Symbol == strings.ToUpper(id) {
return coinInfo
}
}

return SingleCoinInfo{}
}

func GetCoinMarketCap () error {
var params = make(map[string]string)
var headers = make(map[string]string)
resBytes, err := httpUtil.HttpGet(coinMarketCapUrl, params, headers)
if err != nil {
return err
}
err = json.Unmarshal(resBytes, &GRes)
if err != nil {
return err
}
return nil
}
17 changes: 17 additions & 0 deletions floatprocess.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import (
"fmt"
"math/big"
"strconv"
)

func ProcessFloat(f float64) float64 {
num2, _ := strconv.ParseFloat(fmt.Sprintf("%.8f", f), 64)
return num2
}

//big.NewRat(1, 1).SetFloat64(coinPrice.GRes.Data.Quote.USD.TotalVolume24H).FloatString(0)
func ConvertFloatToString(f float64) string{
return big.NewRat(1, 1).SetFloat64(f).FloatString(0)
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module CoinRecord

go 1.16

require github.com/pkg/errors v0.9.1
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
10 changes: 10 additions & 0 deletions holds/bitcoin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "btc",
"records": [
{
"operate": "+",
"amount": 0.002185,
"sum": 100
}
]
}
10 changes: 10 additions & 0 deletions holds/bnb.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "bnb",
"records": [
{
"operate": "+",
"amount": 0.095403,
"sum": 40
}
]
}
10 changes: 10 additions & 0 deletions holds/dot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "dot",
"records": [
{
"operate": "+",
"amount": 1.559096,
"sum": 40
}
]
}
10 changes: 10 additions & 0 deletions holds/eth.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "eth",
"records": [
{
"operate": "+",
"amount": 0.032844,
"sum": 100
}
]
}
10 changes: 10 additions & 0 deletions holds/ftt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "ftt",
"records": [
{
"operate": "+",
"amount": 0.840243,
"sum": 40
}
]
}
10 changes: 10 additions & 0 deletions holds/link.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "link",
"records": [
{
"operate": "+",
"amount": 1.526624,
"sum": 40
}
]
}
10 changes: 10 additions & 0 deletions holds/uni.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "uni",
"records": [
{
"operate": "+",
"amount": 1.497,
"sum": 40
}
]
}
Loading

0 comments on commit a96acfd

Please sign in to comment.