-
Notifications
You must be signed in to change notification settings - Fork 1
/
tools.go
232 lines (210 loc) · 6.11 KB
/
tools.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
package main
import (
"database/sql"
"fmt"
"io/ioutil"
"strings"
"time"
_ "github.com/go-sql-driver/mysql"
)
func minValue(values []float64) (int, float64) {
min := values[0]
idx := 0
for i, v := range values {
if v > 0 && v < min {
min = v
idx = i
}
}
return idx, min
}
func maxValue(values []float64) (int, float64) {
max := values[0]
idx := 0
for i, v := range values {
if v > 0 && v > max {
max = v
idx = i
}
}
return idx, max
}
func transCode(dmcode string) string {
code := dmcode[0:6]
if strings.Contains(dmcode, "XSHG") {
code = "1" + code
} else {
code = "0" + code
}
return code
}
func saveEBK(cont string, fileName string) {
var d1 = []byte(cont)
err2 := ioutil.WriteFile(fileName, d1, 0666) //写入文件(字节数组)
check(err2)
}
func check(e error) {
if e != nil {
panic(e)
}
}
func Decimal(value float64) string {
value1 := fmt.Sprintf("%.2f", value)
return value1
}
// 获取日期
func getRqsByDB(db *sql.DB, rq string, limit string) []string { //300859-西域; 600718-东软
var rqs []string
//dmstmt, err := db.Prepare(`select distinct code as dm from dayline where code = '688339.XSHG' `) // "'002037"`)
dmstmt, err := db.Prepare(`select distinct date as rq from dayline where date<='` + rq + `' order by date desc limit ` + limit)
if err != nil {
fmt.Printf("query prepare err:%s\n", err.Error())
return rqs
}
//defer dmstmt.Close()
dmrows, err := dmstmt.Query()
if err != nil {
fmt.Printf("query err:%s\n", err.Error())
}
for dmrows.Next() {
var rq string
dmrows.Scan(&rq)
rqs = append(rqs, rq)
}
defer Closedb(dmstmt, dmrows)
return rqs
}
func getTpDmByDate(db *sql.DB, rq string) []map[string]string { //300859-西域; 600718-东软
var dms []map[string]string
sql := `select distinct a.code from tp a where a.date='` + rq + `' `
dmstmt, err := db.Prepare(sql)
if err != nil {
fmt.Printf("query prepare err:%s\n", err.Error())
return dms
}
//defer dmstmt.Close()
dmrows, err := dmstmt.Query()
if err != nil {
fmt.Printf("query err:%s\n", err.Error())
}
for dmrows.Next() {
dmMap := make(map[string]string)
var code string
dmrows.Scan(&code)
dmMap["code"] = code
dms = append(dms, dmMap)
}
defer Closedb(dmstmt, dmrows)
return dms
}
func tvLog(funcName string, code string, day string) {
if strings.Contains(code, ".JP") {
code = code[0:4]
fmt.Println(funcName, day, "https://www.tradingview.com/chart/CFSEAW1L/?symbol=TSE%3A"+code)
//https://www.tradingview.com/chart/CFSEAW1L/?symbol=TSE%3A1375
} else {
//code = code[0:6]
//fmt.Println(funcName, day, code)
}
}
func getDm(db *sql.DB, rq string, tname string) []map[string]interface{} { //300859-西域; 600718-东软
//var dms []string
var dms []map[string]interface{}
// 回踩缺口 605289 002932 600889 a.code like '002155%' and 002155%
// (a.code like '002128%' or a.code like '002155%' or a.code like '603353%' ) and a.code like '000655%' and
sql := `select distinct a.code from ` + tname + ` a where a.paused='0' and a.date='` + rq + `' `
if tname == "dayline" {
sql = `select distinct a.code from ` + tname + ` a where a.code not like '688%' and a.code not like '3%' and a.paused='0' and a.date='` + rq + `' `
}
// fmt.Println(sql)
//sql := `select distinct a.code from dayline a where a.code like '688%' and a.date='` + rq + `' `
dmstmt, err := db.Prepare(sql)
//dmstmt, err := db.Prepare(`select distinct a.code ,b.pe_ratio, b.turnover_ratio,c.zjw_name, c.name from dayline a ,valuation b ,industry c where a.code=b.code and b.pe_ratio>0 and a.paused='0' and b.code=c.code and c.name not like '%ST%' and a.date=b.day and a.date='` + rq + `' and a.code like '002413%'`)
if err != nil {
fmt.Printf("query prepare err:%s\n", err.Error())
return dms
}
//defer dmstmt.Close()
dmrows, err := dmstmt.Query()
if err != nil {
fmt.Printf("query err:%s\n", err.Error())
}
for dmrows.Next() {
dmMap := make(map[string]interface{})
var code string
// var pe_ratio float64
// var turnover_ratio float64
// var zjw_name string
// var name string
// var cnt int
// var inc_revenue_year_rank int
// var inc_revenue_annual_rank int
// var cfo_sales_rank int
// var leverage_ratio_rank int
//dmrows.Scan(&code, &pe_ratio, &turnover_ratio, &zjw_name, &name, &cnt, &inc_revenue_year_rank, &inc_revenue_annual_rank, &cfo_sales_rank, &leverage_ratio_rank)
dmrows.Scan(&code)
dmMap["code"] = code
// dmMap["pe_ratio"] = pe_ratio
// dmMap["turnover_ratio"] = turnover_ratio
// dmMap["zjw_name"] = zjw_name
// dmMap["name"] = name
// dmMap["cnt"] = cnt
// dmMap["inc_revenue_year_rank"] = inc_revenue_year_rank
// dmMap["inc_revenue_annual_rank"] = inc_revenue_annual_rank
// dmMap["cfo_sales_rank"] = cfo_sales_rank
// dmMap["leverage_ratio_rank"] = leverage_ratio_rank
//c.cnt,inc_revenue_year_rank,inc_revenue_annual_rank,cfo_sales_rank ,leverage_ratio_rank
dms = append(dms, dmMap)
}
defer Closedb(dmstmt, dmrows)
return dms
}
func reveSlice(s []string) []string {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
//fmt.Println(s)
return s
}
func reveSliceF(s []float64) []float64 {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
//fmt.Println(s)
return s
}
func getRqs(buyDate string) []string {
currentTime := time.Now()
var rqs []string
i := -2
for {
oldTime := currentTime.AddDate(0, 0, i)
if int(oldTime.Weekday()) == 0 || int(oldTime.Weekday()) == 6 {
i--
continue
}
oldTimeStr := oldTime.Format("2006-01-02")
rqs = append(rqs, oldTimeStr)
//fmt.Println(oldTimeStr, oldTime.Weekday(), int(oldTime.Weekday()), buyDate)
if oldTimeStr == buyDate || i == -100 {
break
}
i--
}
return reveSlice(rqs)
//return rqs
}
func setDataMap(day string, code string, pa string, market string) map[string]string {
dataMap := make(map[string]string)
dataMap["price_id"] = "0"
dataMap["day"] = day
dataMap["code"] = code
dataMap["user_id"] = "1"
dataMap["category_id"] = "xxx"
dataMap["pattern"] = "1"
dataMap["market"] = market
dataMap["remark"] = "auto"
dataMap["created_at"] = time.Now().Format("2006-01-02 15:04:05")
//dataMap["updated_at"] = "auto"
return dataMap
}