commodity_trade_cffex.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. package services
  2. import (
  3. "encoding/xml"
  4. "eta/eta_crawler/models"
  5. "eta/eta_crawler/utils"
  6. "fmt"
  7. "github.com/mozillazg/go-pinyin"
  8. "io/ioutil"
  9. netHttp "net/http"
  10. "strings"
  11. "time"
  12. )
  13. type XmlItem struct {
  14. XMLName xml.Name `xml:"positionRank"`
  15. Data []struct {
  16. ContractCode string `xml:"instrumentid"`
  17. Value string `xml:"Value,attr"`
  18. Text string `xml:"Text,attr"`
  19. Tradingday string `xml:"tradingday"`
  20. Datatypeid string `xml:"datatypeid"`
  21. Rank int `xml:"rank"`
  22. ShortName string `xml:"shortname"`
  23. Volume int `xml:"volume"`
  24. Varvolume int `xml:"varvolume"`
  25. ProductId string `xml:"productid"`
  26. } `xml:"data"`
  27. }
  28. var cffexIndexCode string
  29. var cffexIndexCodeMap = make(map[string]string)
  30. var cffexActionCodeMap = make(map[string]map[string]int)
  31. func cffexIndexCodeGenerator(shortName, indexName, contractCode, suffix string) string {
  32. if shortName == "" {
  33. cffexIndexCode = ""
  34. return cffexIndexCode
  35. }
  36. //取公司全拼
  37. strResult := ""
  38. a := pinyin.NewArgs()
  39. rows := pinyin.LazyPinyin(shortName, a)
  40. for i := 0; i < len(rows); i++ {
  41. strResult += rows[i]
  42. }
  43. cffexIndexCode, _ := cffexIndexCodeMap[indexName]
  44. if cffexIndexCode == "" {
  45. cffexIndexCode = strResult + contractCode + suffix
  46. cffexIndexCodeMap[indexName] = cffexIndexCode
  47. err := models.AddBaseFromTradeMapping(indexName, cffexIndexCode, "CFFEX")
  48. if err != nil {
  49. fmt.Println("add Code err:", err)
  50. }
  51. }
  52. return cffexIndexCode
  53. }
  54. // SyncRankingFromCffex 中国金融期货交易所
  55. func SyncRankingFromCffex() {
  56. n := utils.GetRandInt(10, 120)
  57. time.Sleep(time.Duration(n) * time.Second)
  58. allCode, err := models.GetIndexCodeFromMapping("CFFEX")
  59. if err != nil {
  60. fmt.Println("select Code err:", err)
  61. }
  62. for _, item := range allCode {
  63. cffexIndexCodeMap[item.IndexName] = item.IndexCode
  64. }
  65. for i := 2; i >= 0; i-- {
  66. zzUrl := "http://www.cffex.com.cn/sj/ccpm/%s/%s/"
  67. date := time.Now().AddDate(0, 0, -i)
  68. dateStr := date.Format(utils.FormatDateUnSpace)
  69. pre := dateStr[:6]
  70. sub := dateStr[6:]
  71. zzUrl = fmt.Sprintf(zzUrl, pre, sub)
  72. fmt.Println(zzUrl)
  73. var xmlItems = make([]*XmlItem, 8)
  74. xmlItems[0] = GetXmlItem(zzUrl, "IF")
  75. xmlItems[1] = GetXmlItem(zzUrl, "IC")
  76. xmlItems[2] = GetXmlItem(zzUrl, "IH")
  77. xmlItems[3] = GetXmlItem(zzUrl, "TS")
  78. xmlItems[4] = GetXmlItem(zzUrl, "TF")
  79. xmlItems[5] = GetXmlItem(zzUrl, "T")
  80. xmlItems[6] = GetXmlItem(zzUrl, "IM")
  81. xmlItems[7] = GetXmlItem(zzUrl, "TL")
  82. //获取新的指标信息
  83. var sRank string
  84. //获取所有指标信息
  85. allIndex, err := models.GetBaseFromTradeCffexIndexAll(dateStr)
  86. if err != nil {
  87. fmt.Println("select err:", err)
  88. }
  89. existDealMap := make(map[string]int)
  90. existBuyMap := make(map[string]int)
  91. existSoldMap := make(map[string]int)
  92. for _, v := range allIndex {
  93. existDealMap[v.DealName] = v.BaseFromTradeCffexIndexId
  94. existBuyMap[v.BuyName] = v.BaseFromTradeCffexIndexId
  95. existSoldMap[v.SoldName] = v.BaseFromTradeCffexIndexId
  96. sRank = fmt.Sprintf("%d", v.Rank)
  97. time := strings.Replace(v.DataTime, "-", "", -1)
  98. cffexActionCodeMap[sRank+v.ClassifyType+time] = map[string]int{"0": v.DealValue, "1": v.BuyValue, "2": v.SoldValue, "id": v.BaseFromTradeCffexIndexId}
  99. }
  100. shortNameColum := map[string]string{"0": "deal_short_name", "1": "buy_short_name", "2": "sold_short_name"}
  101. nameColum := map[string]string{"0": "deal_name", "1": "buy_name", "2": "sold_name"}
  102. codeColum := map[string]string{"0": "deal_code", "1": "buy_code", "2": "sold_code"}
  103. valueColum := map[string]string{"0": "deal_value", "1": "buy_value", "2": "sold_value"}
  104. changeColum := map[string]string{"0": "deal_change", "1": "buy_change", "2": "sold_change"}
  105. var dataName string
  106. var dataCode string
  107. var indexKey string
  108. for _, xmlItem := range xmlItems {
  109. for _, i := range xmlItem.Data {
  110. i.ShortName = strings.Replace(i.ShortName,"(经纪)","",-1)
  111. i.ShortName = strings.Replace(i.ShortName,"(代客)","",-1)
  112. var item = new(models.BaseFromTradeCffexIndex)
  113. item.DealValue = -1
  114. item.BuyValue = -1
  115. item.SoldValue = -1
  116. item.Rank = i.Rank
  117. switch i.Value {
  118. case "0":
  119. item.DealShortName = i.ShortName
  120. item.DealName = fmt.Sprintf("%s", i.ShortName+"_"+i.ContractCode+"_成交量(手)")
  121. item.DealCode = cffexIndexCodeGenerator(item.DealShortName, item.DealName, i.ContractCode, "deal")
  122. item.DealValue = i.Volume
  123. item.DealChange = i.Varvolume
  124. dataName = item.DealName
  125. dataCode = item.DealCode
  126. case "1":
  127. item.BuyShortName = i.ShortName
  128. item.BuyName = fmt.Sprintf("%s", i.ShortName+"_"+i.ContractCode+"_持买单量(手)")
  129. item.BuyCode = cffexIndexCodeGenerator(item.BuyShortName, item.BuyName, i.ContractCode, "buy")
  130. item.BuyValue = i.Volume
  131. item.BuyChange = i.Varvolume
  132. dataName = item.BuyName
  133. dataCode = item.BuyCode
  134. case "2":
  135. item.SoldShortName = i.ShortName
  136. item.SoldName = fmt.Sprintf("%s", i.ShortName+"_"+i.ContractCode+"_持卖单量(手)")
  137. item.SoldCode = cffexIndexCodeGenerator(item.SoldShortName, item.SoldName, i.ContractCode, "sold")
  138. item.SoldValue = i.Volume
  139. item.SoldChange = i.Varvolume
  140. dataName = item.SoldName
  141. dataCode = item.SoldCode
  142. default:
  143. fmt.Println("No data value")
  144. }
  145. item.Frequency = "日度"
  146. item.ClassifyName = i.ProductId
  147. item.ClassifyType = i.ContractCode
  148. item.CreateTime = time.Now()
  149. item.ModifyTime = time.Now()
  150. item.DataTime = i.Tradingday
  151. valueMap := map[string]int{"0": item.DealValue, "1": item.BuyValue, "2": item.SoldValue}
  152. //检查在actionCodeMap中是否已经有了
  153. indexKey = fmt.Sprintf("%d", item.Rank) + item.ClassifyType + item.DataTime
  154. if existMap, ok := cffexActionCodeMap[indexKey]; !ok {
  155. //没有,新增
  156. newID, err := models.AddBaseFromTradeCffexIndex(item)
  157. if err != nil {
  158. fmt.Println("insert error:", err)
  159. }
  160. fmt.Println("insert new indexID:", newID)
  161. existMap = make(map[string]int)
  162. existMap[i.Value] = i.Volume
  163. existMap["id"] = int(newID)
  164. cffexActionCodeMap[indexKey] = existMap
  165. } else if existMap != nil && existMap[i.Value] != i.Volume {
  166. //更新
  167. var columnList = [5]string{
  168. shortNameColum[i.Value],
  169. nameColum[i.Value],
  170. codeColum[i.Value],
  171. valueColum[i.Value],
  172. changeColum[i.Value],
  173. }
  174. var dataList = [5]interface{}{
  175. i.ShortName,
  176. dataName,
  177. dataCode,
  178. valueMap[i.Value],
  179. i.Varvolume,
  180. }
  181. err := models.ModifyBaseFromTradeCffexIndex(columnList, dataList, existMap["id"])
  182. if err != nil {
  183. fmt.Println("data update err:", err)
  184. }
  185. cffexActionCodeMap[indexKey][i.Value] = i.Volume
  186. } else if _, ok := existDealMap[item.DealName]; !ok {
  187. //更新
  188. var columnList = [5]string{
  189. shortNameColum[i.Value],
  190. nameColum[i.Value],
  191. codeColum[i.Value],
  192. valueColum[i.Value],
  193. changeColum[i.Value],
  194. }
  195. var dataList = [5]interface{}{
  196. i.ShortName,
  197. dataName,
  198. dataCode,
  199. valueMap[i.Value],
  200. i.Varvolume,
  201. }
  202. err := models.ModifyBaseFromTradeCffexIndex(columnList, dataList, existMap["id"])
  203. if err != nil {
  204. fmt.Println("data update err:", err)
  205. }
  206. cffexActionCodeMap[indexKey][i.Value] = i.Volume
  207. } else if _, ok := existBuyMap[item.BuyName]; !ok {
  208. //更新
  209. var columnList = [5]string{
  210. shortNameColum[i.Value],
  211. nameColum[i.Value],
  212. codeColum[i.Value],
  213. valueColum[i.Value],
  214. changeColum[i.Value],
  215. }
  216. var dataList = [5]interface{}{
  217. i.ShortName,
  218. dataName,
  219. dataCode,
  220. valueMap[i.Value],
  221. i.Varvolume,
  222. }
  223. err := models.ModifyBaseFromTradeCffexIndex(columnList, dataList, existMap["id"])
  224. if err != nil {
  225. fmt.Println("data update err:", err)
  226. }
  227. cffexActionCodeMap[indexKey][i.Value] = i.Volume
  228. } else if _, ok := existSoldMap[item.SoldName]; !ok {
  229. //更新
  230. var columnList = [5]string{
  231. shortNameColum[i.Value],
  232. nameColum[i.Value],
  233. codeColum[i.Value],
  234. valueColum[i.Value],
  235. changeColum[i.Value],
  236. }
  237. var dataList = [5]interface{}{
  238. i.ShortName,
  239. dataName,
  240. dataCode,
  241. valueMap[i.Value],
  242. i.Varvolume,
  243. }
  244. err := models.ModifyBaseFromTradeCffexIndex(columnList, dataList, existMap["id"])
  245. if err != nil {
  246. fmt.Println("data update err:", err)
  247. }
  248. cffexActionCodeMap[indexKey][i.Value] = i.Volume
  249. }
  250. }
  251. }
  252. }
  253. }
  254. func GetXmlItem(url, contract string) *XmlItem {
  255. zzUrl := url + contract + ".xml"
  256. fmt.Println(zzUrl)
  257. //body, err := http.Get(zzUrl)
  258. //if err != nil {
  259. // fmt.Println("err:", err)
  260. //}
  261. req, _ := netHttp.NewRequest("GET", zzUrl, nil)
  262. req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36")
  263. client := netHttp.Client{}
  264. res, err := client.Do(req)
  265. if err != nil {
  266. fmt.Println(err)
  267. return nil
  268. }
  269. defer res.Body.Close()
  270. // 读取响应的内容
  271. body, err := ioutil.ReadAll(res.Body)
  272. if err != nil {
  273. fmt.Println(err)
  274. return nil
  275. }
  276. xmlItem := new(XmlItem)
  277. err = xml.Unmarshal(body, &xmlItem)
  278. if err != nil {
  279. fmt.Println("xml.Unmarshal:", err.Error())
  280. }
  281. return xmlItem
  282. }