commodity_trade_cffex.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package services
  2. import (
  3. "encoding/xml"
  4. "fmt"
  5. "hongze/hongze_data_crawler/models"
  6. "hongze/hongze_data_crawler/utils"
  7. "rdluck_tools/http"
  8. "strconv"
  9. "time"
  10. )
  11. type XmlItem struct {
  12. XMLName xml.Name `xml:"positionRank"`
  13. Data []struct {
  14. ContractCode string `xml:"instrumentid"`
  15. Value string `xml:"Value,attr"`
  16. Text string `xml:"Text,attr"`
  17. Tradingday string `xml:"tradingday"`
  18. Datatypeid string `xml:"datatypeid"`
  19. Rank int `xml:"rank"`
  20. ShortName string `xml:"shortname"`
  21. Volume int `xml:"volume"`
  22. Varvolume int `xml:"varvolume"`
  23. ProductId string `xml:"productid"`
  24. } `xml:"data"`
  25. }
  26. var cffexIndexCode string
  27. var cffexIndexCodeMap = make(map[string]string)
  28. var cffexActionCodeMap = make(map[string]map[string]int)
  29. func cffexIndexCodeGenerator(indexName string) string {
  30. cffexIndexCode,_ := cffexIndexCodeMap[indexName]
  31. if cffexIndexCode == "" {
  32. cffexIndexCode = fmt.Sprintf("CFFEX%s", time.Now().Format(utils.FormatDateTimeUnSpace)+strconv.Itoa(utils.GetRandInt(1, 100)))
  33. cffexIndexCodeMap[indexName] = cffexIndexCode
  34. err := models.AddBaseFromTradeMapping(indexName, cffexIndexCode, "CFFEX")
  35. if err != nil {
  36. fmt.Println("add Code err:", err)
  37. }
  38. }
  39. return cffexIndexCode
  40. }
  41. // SyncRankingFromCffex 中国金融期货交易所
  42. func SyncRankingFromCffex() {
  43. for i := 180; i > 0; i-- {
  44. zzUrl := "http://www.cffex.com.cn/sj/ccpm/%s/%s/"
  45. date := time.Now().AddDate(0, 0, -i)
  46. dateStr := date.Format(utils.FormatDateUnSpace)
  47. pre := dateStr[:6]
  48. sub := dateStr[6:]
  49. zzUrl = fmt.Sprintf(zzUrl, pre, sub)
  50. fmt.Println(zzUrl)
  51. var xmlItems = make([]*XmlItem, 6)
  52. xmlItems[0] = GetXmlItem(zzUrl, "IF")
  53. xmlItems[1] = GetXmlItem(zzUrl, "IC")
  54. xmlItems[2] = GetXmlItem(zzUrl, "IH")
  55. xmlItems[3] = GetXmlItem(zzUrl, "TS")
  56. xmlItems[4] = GetXmlItem(zzUrl, "TF")
  57. xmlItems[5] = GetXmlItem(zzUrl, "T")
  58. //获取新的指标信息
  59. var sRank string
  60. existIndexMap := make(map[string]*models.BaseFromTradeCffexIndex)
  61. //获取所有指标信息
  62. allIndex, err := models.GetBaseFromTradeCffexIndexAll(dateStr)
  63. if err != nil {
  64. fmt.Println("select err:", err)
  65. }
  66. for _, v := range allIndex {
  67. indexKey := v.DealName + v.BuyName + v.SoldName + v.DataTime
  68. existIndexMap[indexKey] = v
  69. sRank = fmt.Sprintf("%d", v.Rank)
  70. cffexActionCodeMap[sRank+v.ClassifyType+v.DataTime] = map[string]int{"0": v.DealValue, "1": v.BuyValue, "2": v.SoldValue, "id": v.BaseFromTradeCffexIndexId}
  71. }
  72. shortNameColum := map[string]string{"0": "deal_short_name", "1": "buy_short_name", "2": "sold_short_name"}
  73. nameColum := map[string]string{"0": "deal_name", "1": "buy_name", "2": "sold_name"}
  74. codeColum := map[string]string{"0": "deal_code", "1": "buy_code", "2": "sold_code"}
  75. valueColum := map[string]string{"0": "deal_value", "1": "buy_value", "2": "sold_value"}
  76. changeColum := map[string]string{"0": "deal_change", "1": "buy_change", "2": "sold_change"}
  77. var dataName string
  78. var dataCode string
  79. var indexKey string
  80. for _, xmlItem := range xmlItems {
  81. for _, i := range xmlItem.Data {
  82. var item = new(models.BaseFromTradeCffexIndex)
  83. item.Rank = i.Rank
  84. switch i.Value {
  85. case "0":
  86. item.DealShortName = i.ShortName
  87. item.DealName = fmt.Sprintf("%s", i.ShortName+"_成交量_"+i.ContractCode)
  88. item.DealCode = cffexIndexCodeGenerator(item.DealName)
  89. item.DealValue = i.Volume
  90. item.DealChange = i.Varvolume
  91. dataName = item.DealName
  92. dataCode = item.DealCode
  93. case "1":
  94. item.BuyShortName = i.ShortName
  95. item.BuyName = fmt.Sprintf("%s", i.ShortName+"_持买单量_"+i.ContractCode)
  96. item.BuyCode = cffexIndexCodeGenerator(item.BuyName)
  97. item.BuyValue = i.Volume
  98. item.BuyChange = i.Varvolume
  99. dataName = item.BuyName
  100. dataCode = item.BuyCode
  101. case "2":
  102. item.SoldShortName = i.ShortName
  103. item.SoldName = fmt.Sprintf("%s", i.ShortName+"_持卖单量_"+i.ContractCode)
  104. item.SoldCode = cffexIndexCodeGenerator(item.SoldName)
  105. item.SoldValue = i.Volume
  106. item.SoldChange = i.Varvolume
  107. dataName = item.SoldName
  108. dataCode = item.SoldCode
  109. default:
  110. fmt.Println("No data value")
  111. }
  112. item.Frequency = "日度"
  113. item.ClassifyName = i.ProductId
  114. item.ClassifyType = i.ContractCode
  115. item.CreateTime = time.Now()
  116. item.ModifyTime = time.Now()
  117. item.DataTime = i.Tradingday
  118. valueMap := map[string]int{"0": item.DealValue, "1": item.BuyValue, "2": item.SoldValue}
  119. //检查在actionCodeMap中是否已经有了
  120. indexKey = fmt.Sprintf("%d", item.Rank) + item.ClassifyType + item.DataTime
  121. if existMap, ok := cffexActionCodeMap[indexKey]; !ok {
  122. //没有,新增
  123. newID, err := models.AddBaseFromTradeCffexIndex(item)
  124. if err != nil {
  125. fmt.Println("insert error:", err)
  126. }
  127. fmt.Println("insert new indexID:", newID)
  128. existMap = make(map[string]int)
  129. existMap[i.Value] = i.Volume
  130. existMap["id"] = int(newID)
  131. cffexActionCodeMap[indexKey] = existMap
  132. } else if existMap != nil && existMap[i.Value] != i.Volume {
  133. //更新
  134. var columnList = [5]string{
  135. shortNameColum[i.Value],
  136. nameColum[i.Value],
  137. codeColum[i.Value],
  138. valueColum[i.Value],
  139. changeColum[i.Value],
  140. }
  141. var dataList = [5]interface{}{
  142. i.ShortName,
  143. dataName,
  144. dataCode,
  145. valueMap[i.Value],
  146. i.Varvolume,
  147. }
  148. err := models.ModifyBaseFromTradeCffexIndex(columnList, dataList, existMap["id"])
  149. if err != nil {
  150. fmt.Println("data update err:", err)
  151. }
  152. cffexActionCodeMap[indexKey][i.Value] = i.Volume
  153. }
  154. }
  155. }
  156. }
  157. }
  158. func GetXmlItem(url, contract string) *XmlItem {
  159. zzUrl := url + contract + ".xml"
  160. fmt.Println(zzUrl)
  161. body, err := http.Get(zzUrl)
  162. if err != nil {
  163. fmt.Println("err:", err)
  164. }
  165. xmlItem := new(XmlItem)
  166. err = xml.Unmarshal(body, &xmlItem)
  167. if err != nil {
  168. fmt.Println("xml.Unmarshal:", err.Error())
  169. }
  170. return xmlItem
  171. }