-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
256 lines (226 loc) · 6.71 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package main
import (
"CoinRecord/chart"
"CoinRecord/coinPrice"
"CoinRecord/global"
"CoinRecord/models"
"embed"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"text/template"
"time"
)
const StartTime = "2021-08-19"
//go:embed template/*
var fs embed.FS
func main() {
var getCurrentDirectory = func() string {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatal(err)
}
return strings.Replace(dir, "\\", "/", -1)
}
currentDir := getCurrentDirectory()
fmt.Println("current DIR:", currentDir)
global.LoadApiKey(currentDir)
err := coinPrice.GetAllCoinPrice()
err = coinPrice.GetCoinMarketCap()
if err != nil {
log.Fatal(err)
return
}
holdsDir := filepath.Join(currentDir, "holds")
rd, err := ioutil.ReadDir(holdsDir)
if err != nil {
log.Fatal(err)
return
}
var allHolds []models.Holds
for _, fi := range rd {
if fi.IsDir() {
continue
}
fmt.Println(fi.Name())
filePath := filepath.Join(holdsDir, fi.Name())
f, err := os.Open(filePath)
if err != nil {
log.Fatal(err)
return
}
content, err := ioutil.ReadAll(f)
if err != nil {
log.Fatal(err)
return
}
var holds models.Holds
err = json.Unmarshal(content, &holds)
if err != nil {
log.Fatal(err)
return
}
fmt.Printf("%+v \n", holds)
allHolds = append(allHolds, holds)
}
var coinInfoVos []models.CoinInfoVo
var coinNames []string
var coinCosts []float64
var coinProfits []float64
for i, v := range allHolds {
fmt.Printf("id: %d,value:%+v \n", i, v)
//遍历所有记录计算投资总额,成本价,币的数量
var coinAmount float64
var name string
var avgPrice float64
var coinCost float64
var coinSum float64
var profit float64
var yield float64
var priceUsd float64
for _, coin := range v.Records {
switch coin.Operate {
case "+":
coinAmount += coin.Amount
coinCost += coin.Sum
case "-":
coinAmount -= coin.Amount
coinCost -= coin.Sum
}
}
name = v.Name
// 单个币平均成本价格 = 总成本/币的数量
avgPrice = coinCost / coinAmount
coinInfo := coinPrice.GetCoinPrice(name)
priceUsd = ProcessFloat(coinInfo.Quote.USD.Price)
coinSum = priceUsd * coinAmount
// 利润 = 持仓现价 - 投资总额
profit = coinSum - coinCost
// 收益率 = 利润/投资总额
yield = profit / coinCost
coinInfoVo := models.CoinInfoVo{
Name: name,
Amount: ProcessFloat(coinAmount),
Sum: ProcessFloat(coinSum),
NowPrice: ProcessFloat(priceUsd),
AvgPrice: ProcessFloat(avgPrice),
Cost: ProcessFloat(coinCost),
Profit: ProcessFloat(profit),
Yield: fmt.Sprintf("%.2f", yield*100) + "%",
CmcRank: coinInfo.CmcRank,
}
// 添加pie数组
coinNames = append(coinNames, name)
coinCosts = append(coinCosts, ProcessFloat(coinCost))
coinProfits = append(coinProfits, ProcessFloat(profit))
coinInfoVo.MarketCap = ConvertFloatToString(coinInfo.Quote.USD.MarketCap)
coinInfoVo.Volume24H = ConvertFloatToString(coinInfo.Quote.USD.Volume24H)
per24h := coinInfo.Quote.USD.PercentChange24H
per7d := coinInfo.Quote.USD.PercentChange7D
coinInfoVo.PercentChange24H = fmt.Sprintf("%.2f", per24h) + "%"
coinInfoVo.PercentChange7D = fmt.Sprintf("%.2f", per7d) + "%"
//原单价 = 现单价 / (1+昨日涨幅)
//(现单价 - 原单价) * 持有数量 = 昨日收益
yesPrice := coinInfo.Quote.USD.Price / (1 + (per24h * 0.01))
coinInfoVo.Profit24h = ProcessFloat((coinInfo.Quote.USD.Price - yesPrice) * coinAmount)
coinInfoVo.Change24hColor = models.ShallowRed
coinInfoVo.DeepChange24hColor = models.Red
coinInfoVo.YieldColor = models.ShallowRed
coinInfoVo.DeepYieldColor = models.Red
coinInfoVo.DeepChange7DColor = models.Red
if per24h > 0 {
coinInfoVo.Change24hColor = models.ShallowGreen
coinInfoVo.DeepChange24hColor = models.Green
}
if per7d > 0 {
coinInfoVo.DeepChange7DColor = models.Green
}
if yield > 0 {
coinInfoVo.YieldColor = models.ShallowGreen
coinInfoVo.DeepYieldColor = models.Green
}
coinInfoVos = append(coinInfoVos, coinInfoVo)
}
mdDir := filepath.Join(currentDir, "records")
_ = os.MkdirAll(mdDir, 0666)
mdFileName := time.Now().Format("20060102150405") + ".md"
mdFilePath := filepath.Join(mdDir, mdFileName)
fmt.Println(mdFilePath)
mdFile, err := os.OpenFile(mdFilePath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
log.Fatal(err)
}
defer mdFile.Close()
var totalProfit float64
var totalCost float64
var totalYield float64
var totalSum float64
var yesProfit float64
for _, v := range coinInfoVos {
totalProfit += v.Profit
totalCost += v.Cost
totalSum += v.Sum
yesProfit += v.Profit24h
}
totalYield = totalProfit / totalCost
totalYieldString := fmt.Sprintf("%.2f", totalYield*100) + "%"
var templateValue = models.TemplateValue{
StartTime: StartTime,
NowTime: TimeFormat(time.Now()),
NowDay: time.Now().Format("2006-01-02"),
TotalProfit: ProcessFloat(totalProfit),
TotalCost: ProcessFloat(totalCost),
TotalYield: totalYieldString,
TotalSum: ProcessFloat(totalSum),
}
templateValue.IncomeData = coinInfoVos
//保存pie图的信息
templateValue.PieContent = chart.GetPieString(coinCosts, coinNames)
//保存柱状图信息
templateValue.BarContent = chart.GetBarString(coinProfits, coinNames)
templateValue.YesterdayProfit = yesProfit
templateValue.Days = timeSub(time.Now(), StringToTime(StartTime))
templateValue.YesterdayProfitColor = models.Red
templateValue.TotalYieldClolor = models.Red
if totalYield > 0 {
templateValue.TotalYieldClolor = models.Green
}
if templateValue.YesterdayProfit > 0 {
templateValue.YesterdayProfitColor = models.Green
}
//保存行情信息
templateValue.TotalMarketCap = ConvertFloatToString(coinPrice.GRes.Data.Quote.USD.TotalMarketCap)
tmcpc := coinPrice.GRes.Data.Quote.USD.TotalMarketCapYesterdayPercentageChange
if tmcpc > 0 {
templateValue.TotalMarketColor = models.Green
} else {
templateValue.TotalMarketColor = models.Red
}
templateValue.TotalMarketCapYesterdayPercentageChange = fmt.Sprintf("%.2f", tmcpc) + "%"
templateValue.TotalVolume24h = ConvertFloatToString(coinPrice.GRes.Data.Quote.USD.TotalVolume24H)
tvcpc := coinPrice.GRes.Data.Quote.USD.TotalVolume24HYesterdayPercentageChange
if tvcpc > 0 {
templateValue.TotalVolumeColor = models.Green
} else {
templateValue.TotalVolumeColor = models.Red
}
templateValue.TotalVolume24hYesterdayPercentageChange = fmt.Sprintf("%.2f", tvcpc) + "%"
// 把模板编进二进制文件中
tmpl, err := template.ParseFS(fs, "template/*.tmpl")
if err != nil {
log.Fatal(err)
return
}
err = tmpl.Execute(mdFile, templateValue)
if err != nil {
log.Fatal(err)
return
}
fmt.Println("按任意键继续...")
var input string
_, _ = fmt.Scanln(&input)
}