trade_analysis.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. package trade_analysis
  2. import (
  3. "eta/eta_api/models/data_manage/trade_analysis"
  4. "eta/eta_api/utils"
  5. "fmt"
  6. "sort"
  7. "strings"
  8. "time"
  9. )
  10. func GetClassifyName(lang string) (list trade_analysis.TradeClassifyNameListSort, err error, errMsg string) {
  11. //定义交易所
  12. exchanges := map[string]string{
  13. "zhengzhou": "郑商所",
  14. "dalian": "大商所",
  15. "shanghai": "上期所",
  16. "cffex": "中金所",
  17. "ine": "上期能源",
  18. "guangzhou": "广期所",
  19. }
  20. exchangesEn := map[string]string{
  21. "郑商所": "Zhengzhou ComEx",
  22. "大商所": "Dalian ComEx",
  23. "上期所": "SH Intl Energy Ex",
  24. "中金所": "China Fin Futures Ex",
  25. "上期能源": "SH Energy Ex",
  26. }
  27. exchangesSortMap := map[string]int{
  28. "zhengzhou": 1,
  29. "dalian": 2,
  30. "shanghai": 3,
  31. "cffex": 4,
  32. "ine": 5,
  33. "guangzhou": 6,
  34. }
  35. //查询所有交易所下的分类
  36. classifyExchangeList, tmpErr := trade_analysis.GetAllBaseFromTradeClassify()
  37. if tmpErr != nil {
  38. err = tmpErr
  39. errMsg = "查询交易所最新更新时间失败"
  40. return
  41. }
  42. // 每个交易所的分类信息
  43. classifyExchangeMap := make(map[string][]trade_analysis.TradeClassifyName)
  44. // 每个交易所的最新更新时间
  45. timeLastMap := make(map[string]time.Time)
  46. for _, v := range classifyExchangeList {
  47. tmpExchange := v.Exchange
  48. // 分类
  49. tmpList, ok := classifyExchangeMap[tmpExchange]
  50. if !ok {
  51. tmpList = make([]trade_analysis.TradeClassifyName, 0)
  52. }
  53. tmpList = append(tmpList, trade_analysis.TradeClassifyName{
  54. ClassifyName: v.ClassifyName,
  55. ClassifyType: v.ClassifyType,
  56. LatestDate: v.LatestDate.Format(utils.FormatDate),
  57. })
  58. classifyExchangeMap[v.Exchange] = tmpList
  59. // 时间
  60. if tmpLastTime, ok2 := timeLastMap[tmpExchange]; !ok2 {
  61. timeLastMap[tmpExchange] = v.ModifyTime
  62. } else {
  63. if v.ModifyTime.After(tmpLastTime) {
  64. timeLastMap[tmpExchange] = v.ModifyTime
  65. }
  66. }
  67. }
  68. i := 0
  69. currDate := time.Now().Format(utils.FormatDate)
  70. for k, v := range exchanges {
  71. tmp := trade_analysis.TradeClassifyNameList{
  72. Exchange: v,
  73. ExchangeEn: exchangesEn[v],
  74. Items: nil,
  75. Sort: exchangesSortMap[k],
  76. CurrDate: currDate,
  77. }
  78. nameList, ok := classifyExchangeMap[k]
  79. if !ok {
  80. err = fmt.Errorf("查询交易所分类信息失败")
  81. errMsg = "查询交易所分类信息失败"
  82. return
  83. }
  84. // 查询交易所最新更新时间失败
  85. if timeLast, ok := timeLastMap[k]; ok {
  86. tmp.DataTime = timeLast.Format(utils.FormatDateTime)
  87. } else {
  88. err = fmt.Errorf("查询交易所最新更新时间失败")
  89. errMsg = "查询交易所最新更新时间失败"
  90. return
  91. }
  92. classifyMap := make(map[string][]trade_analysis.TradeClassifyNameListItemItem)
  93. if len(nameList) > 0 {
  94. if k == "zhengzhou" {
  95. for _, item := range nameList {
  96. classifyName := GetZhengzhouClassifyName(item.ClassifyName)
  97. tmpItemItem := trade_analysis.TradeClassifyNameListItemItem{
  98. ClassifyType: item.ClassifyName,
  99. }
  100. classifyMap[classifyName] = append(classifyMap[classifyName], tmpItemItem)
  101. }
  102. } else {
  103. for _, item := range nameList {
  104. tmpItemItem := trade_analysis.TradeClassifyNameListItemItem{
  105. ClassifyType: item.ClassifyType,
  106. }
  107. classifyMap[item.ClassifyName] = append(classifyMap[item.ClassifyName], tmpItemItem)
  108. }
  109. }
  110. for n, l := range classifyMap {
  111. sort.Sort(trade_analysis.TradeClassifyNameListItemItemSort(l))
  112. tmpItems := trade_analysis.TradeClassifyNameListItem{
  113. ClassifyName: n,
  114. Items: l,
  115. }
  116. tmp.Items = append(tmp.Items, tmpItems)
  117. tmp.Num++
  118. }
  119. }
  120. sort.Sort(trade_analysis.TradeClassifyNameListItemSort(tmp.Items))
  121. list = append(list, tmp)
  122. i++
  123. }
  124. if len(list) > 0 {
  125. sort.Sort(list)
  126. }
  127. return
  128. }
  129. func GetZhengzhouClassifyName(code string) (name string) {
  130. if strings.HasPrefix(code, "PTA") {
  131. name = "PTA"
  132. return
  133. }
  134. if strings.HasPrefix(code, "TA") {
  135. name = "PTA"
  136. return
  137. }
  138. if strings.HasPrefix(code, "ZC") {
  139. name = "动力煤"
  140. return
  141. }
  142. if strings.HasPrefix(code, "WH") {
  143. name = "强麦"
  144. return
  145. }
  146. if strings.HasPrefix(code, "UR") {
  147. name = "尿素"
  148. return
  149. }
  150. if strings.HasPrefix(code, "SR") {
  151. name = "白糖"
  152. return
  153. }
  154. if strings.HasPrefix(code, "SM") {
  155. name = "锰硅"
  156. return
  157. }
  158. if strings.HasPrefix(code, "SF") {
  159. name = "硅铁"
  160. return
  161. }
  162. if strings.HasPrefix(code, "SA") {
  163. name = "纯碱"
  164. return
  165. }
  166. if strings.HasPrefix(code, "RS") {
  167. name = "油菜籽"
  168. return
  169. }
  170. if strings.HasPrefix(code, "RM") {
  171. name = "菜籽粕"
  172. return
  173. }
  174. if strings.HasPrefix(code, "RI") {
  175. name = "早籼稻"
  176. return
  177. }
  178. if strings.HasPrefix(code, "PM") {
  179. name = "普麦"
  180. return
  181. }
  182. if strings.HasPrefix(code, "PK") {
  183. name = "花生"
  184. return
  185. }
  186. if strings.HasPrefix(code, "PF") {
  187. name = "涤纶短纤"
  188. return
  189. }
  190. if strings.HasPrefix(code, "OI") {
  191. name = "菜油"
  192. return
  193. }
  194. if strings.HasPrefix(code, "MA") {
  195. name = "甲醇"
  196. return
  197. }
  198. if strings.HasPrefix(code, "LR") {
  199. name = "晚籼稻"
  200. return
  201. }
  202. if strings.HasPrefix(code, "JR") {
  203. name = "粳稻"
  204. return
  205. }
  206. if strings.HasPrefix(code, "FG") {
  207. name = "玻璃"
  208. return
  209. }
  210. if strings.HasPrefix(code, "CY") {
  211. name = "棉纱"
  212. return
  213. }
  214. if strings.HasPrefix(code, "CJ") {
  215. name = "红枣"
  216. return
  217. }
  218. if strings.HasPrefix(code, "CF") {
  219. name = "棉花"
  220. return
  221. }
  222. if strings.HasPrefix(code, "AP") {
  223. name = "苹果"
  224. return
  225. }
  226. if strings.HasPrefix(code, "PX") {
  227. name = "PX"
  228. return
  229. }
  230. if strings.HasPrefix(code, "SH") {
  231. name = "烧碱"
  232. return
  233. }
  234. if strings.HasPrefix(code, "PR") {
  235. name = "瓶片"
  236. return
  237. }
  238. if name == "" {
  239. utils.FileLog.Info(fmt.Sprintf("郑商所-合约暂未归类: %s", code))
  240. }
  241. return
  242. }
  243. func GetPositionTopDetail(req trade_analysis.GetPositionTopReq) (ret trade_analysis.GetPositionTopResp, err error, errMsg string) {
  244. //定义交易所
  245. exchanges := map[string]string{
  246. "郑商所": "zhengzhou",
  247. "大商所": "dalian",
  248. "上期所": "shanghai",
  249. "中金所": "cffex",
  250. "上期能源": "ine",
  251. "广期所": "guangzhou",
  252. }
  253. exchange, ok := exchanges[req.Exchange]
  254. if !ok {
  255. errMsg = "请输入正确的交易所名称"
  256. err = fmt.Errorf(errMsg)
  257. return
  258. }
  259. dataTimeStr := req.DataTime
  260. var lastDataTime, dataTime time.Time
  261. //查询最新的时间
  262. lastItem, tmpErr := trade_analysis.GetTradeTopLastDataTime(exchange, req.ClassifyName, req.ClassifyType)
  263. if tmpErr != nil {
  264. errMsg = "查询最新的榜单信息失败"
  265. err = tmpErr
  266. return
  267. }
  268. lastDataTime = lastItem.LatestDate
  269. // 如果没有传入日期,那么就用最晚的的日期
  270. if dataTimeStr == "" {
  271. dataTime = lastDataTime
  272. } else {
  273. dataTime, err = time.ParseInLocation(utils.FormatDate, dataTimeStr, time.Local)
  274. if err != nil {
  275. errMsg = "请输入正确的时间格式"
  276. return
  277. }
  278. }
  279. dataTimeStr = dataTime.Format(utils.FormatDate)
  280. ret.DataTime = dataTimeStr
  281. ret.LastDataTime = lastDataTime.Format(utils.FormatDate)
  282. //遇到周末则跳过当天
  283. weekStr := dataTime.Weekday().String()
  284. if weekStr == "Sunday" || weekStr == "Saturday" {
  285. /*errMsg = "日期不正确"
  286. err = fmt.Errorf(errMsg)*/
  287. return
  288. }
  289. classifyName := req.ClassifyName
  290. classifyType := req.ClassifyType
  291. if exchange == "zhengzhou" {
  292. classifyName = classifyType
  293. var typeItem *trade_analysis.TradeClassifyName
  294. typeItem, err = trade_analysis.GetClassifyTypeByClassifyName(exchange, classifyName)
  295. if err != nil {
  296. if err.Error() == utils.ErrNoRow() {
  297. errMsg = "该合约不存在"
  298. return
  299. }
  300. errMsg = "查询类型信息出错"
  301. return
  302. }
  303. classifyType = typeItem.ClassifyType
  304. }
  305. //查询当日榜单列表
  306. dataList, tmpErr := trade_analysis.GetTradePositionTop(exchange, classifyName, classifyType, dataTimeStr)
  307. if tmpErr != nil {
  308. errMsg = "查询榜单列表失败"
  309. err = tmpErr
  310. return
  311. }
  312. if len(dataList) <= 0 {
  313. return
  314. }
  315. totalMap := make(map[int]int)
  316. totalChangeMap := make(map[int]int)
  317. totalTmpMap := make(map[int]int)
  318. totalChangeTmpMap := make(map[int]int)
  319. detailList := make(map[int][]trade_analysis.GetPositionTopListItem)
  320. //统计汇总数据
  321. for _, v := range dataList {
  322. if t, ok1 := totalMap[v.DealType]; ok1 {
  323. totalMap[v.DealType] = t + v.DealValue
  324. } else {
  325. totalMap[v.DealType] = v.DealValue
  326. }
  327. if t, ok1 := totalChangeMap[v.DealType]; ok1 {
  328. totalChangeMap[v.DealType] = t + v.DealChange
  329. } else {
  330. totalChangeMap[v.DealType] = v.DealChange
  331. }
  332. }
  333. _, okTmp1 := totalMap[1]
  334. _, okTmp2 := totalMap[2]
  335. _, okTmp3 := totalMap[3]
  336. _, okTmp4 := totalMap[4]
  337. if !okTmp1 || !okTmp2 || (!okTmp3 && !okTmp4) {
  338. errMsg = "榜单数据缺失"
  339. err = fmt.Errorf(errMsg)
  340. return
  341. }
  342. for k, v := range dataList {
  343. k++
  344. if t, ok1 := totalTmpMap[v.DealType]; ok1 {
  345. totalTmpMap[v.DealType] = t + v.DealValue
  346. } else {
  347. totalTmpMap[v.DealType] = v.DealValue
  348. }
  349. if t, ok1 := totalChangeTmpMap[v.DealType]; ok1 {
  350. totalChangeTmpMap[v.DealType] = t + v.DealChange
  351. } else {
  352. totalChangeTmpMap[v.DealType] = v.DealChange
  353. }
  354. tmp := trade_analysis.GetPositionTopListItem{
  355. DealShortName: v.DealShortName,
  356. DealValue: v.DealValue,
  357. DealChange: v.DealChange,
  358. Rank: v.Rank,
  359. BeforeAllValue: totalTmpMap[v.DealType],
  360. BeforeAllChange: totalChangeTmpMap[v.DealType],
  361. }
  362. //统计占比
  363. rate := fmt.Sprintf("%.2f", float64(tmp.DealValue)/float64(totalMap[v.DealType])) // 保留2位小数
  364. beforeAllRate := fmt.Sprintf("%.2f", float64(tmp.BeforeAllValue)/float64(totalMap[v.DealType])) // 保留2位小数
  365. tmp.Rate = rate
  366. tmp.BeforeAllRate = beforeAllRate
  367. if tmp.DealShortName == "-" {
  368. continue
  369. }
  370. detailList[v.DealType] = append(detailList[v.DealType], tmp)
  371. }
  372. ret.BuyList.TotalDealValue = totalMap[1]
  373. ret.BuyList.TotalDealChange = totalChangeMap[1]
  374. ret.BuyList.List = detailList[1]
  375. ret.SoldList.TotalDealValue = totalMap[2]
  376. ret.SoldList.TotalDealChange = totalChangeMap[2]
  377. ret.SoldList.List = detailList[2]
  378. ret.CleanBuyList.TotalDealValue = totalMap[3]
  379. ret.CleanBuyList.TotalDealChange = totalChangeMap[3]
  380. ret.CleanBuyList.List = detailList[3]
  381. ret.CleanSoldList.TotalDealValue = totalMap[4]
  382. ret.CleanSoldList.TotalDealChange = totalChangeMap[4]
  383. ret.CleanSoldList.List = detailList[4]
  384. return
  385. }