bloomberg.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package pcsg
  2. import (
  3. "eta/eta_bridge/global"
  4. "eta/eta_bridge/utils"
  5. "fmt"
  6. "strings"
  7. "time"
  8. )
  9. const (
  10. TaskKey0000 = "IDpcsgDailySnap0000"
  11. SubOneDayTaskKey = "IDpcsgDailySnap0330" // 日期-1的key
  12. TaskKey0345 = "IDpcsgDailySnap0345"
  13. )
  14. // PythonBloombergGeneralData 通用数据格式(有空改成Map吧...然后做成配置方便加新字段...)
  15. type PythonBloombergGeneralData struct {
  16. NAME string `json:"NAME" description:"指标名称, 可能为null"`
  17. IDENTIFIER string `json:"IDENTIFIER" description:"指标编码"`
  18. PX_LAST_EOD *float64 `json:"PX_LAST_EOD" description:"数据值, 可能为null"`
  19. PX_VOLUME_EOD *float64 `json:"PX_VOLUME_EOD" description:"数据值, 可能为null"`
  20. PX_LAST *float64 `json:"PX_LAST" description:"数据值, 可能为null"`
  21. PX_VOLUME *float64 `json:"PX_VOLUME" description:"数据值, 可能为null"`
  22. LAST_UPDATE_DATE_EOD string `json:"LAST_UPDATE_DATE_EOD" description:"数据日期"`
  23. DATE string `json:"DATE" description:"数据日期"`
  24. PX_SETTLE *float64 `json:"PX_SETTLE" description:"数据值, 可能为null"`
  25. PX_SETTLE_LAST_DT string `json:"PX_SETTLE_LAST_DT" description:"数据日期"`
  26. OPEN_INT *float64 `json:"OPEN_INT"`
  27. OPEN_INT_DATE string `json:"OPEN_INT_DATE"`
  28. DL_SNAPSHOT_START_TIME string `json:"DL_SNAPSHOT_START_TIME" description:"数据日期"`
  29. LAST_UPDATE string `json:"LAST_UPDATE" description:"IDpcsgDailySnap0000和IDpcsgDailySnap0330判断用的"`
  30. PX_CLOSE_DT string `json:"PX_CLOSE_DT" description:"数据日期"`
  31. LAST_UPDATE_DT string `json:"LAST_UPDATE_DT" description:"数据日期"`
  32. HEATING_DAYS *float64 `json:"HEATING_DAYS" description:"数据值, 可能为null"`
  33. COOLING_DAYS *float64 `json:"COOLING_DAYS" description:"数据值, 可能为null"`
  34. }
  35. // PythonBloombergGeneralResult API响应体
  36. type PythonBloombergGeneralResult struct {
  37. Code int `json:"code"`
  38. Msg string `json:"msg"`
  39. Data []PythonBloombergGeneralData `json:"data"`
  40. }
  41. // BaseFromBloombergApiIndexAndData Bloomberg原始指标及数据
  42. type BaseFromBloombergApiIndexAndData struct {
  43. BaseFromBloombergIndexId int `description:"指标ID"`
  44. IndexCode string `description:"指标编码"`
  45. IndexName string `description:"指标名称"`
  46. Unit string `description:"单位"`
  47. Source string `description:"来源"`
  48. Frequency string `description:"频度"`
  49. CreateTime time.Time `description:"创建时间"`
  50. ModifyTime time.Time `description:"修改时间"`
  51. Data []BaseFromBloombergApiIndexData `description:"数据列表"`
  52. }
  53. // BaseFromBloombergApiIndexData Bloomberg原始指标数据
  54. type BaseFromBloombergApiIndexData struct {
  55. DataTime time.Time `description:"数据日期"`
  56. Value float64 `description:"数据值"`
  57. }
  58. func FormatPythonBloombergGeneralData2Base(origin PythonBloombergGeneralData, frequency, taskKey string) (item BaseFromBloombergApiIndexAndData) {
  59. if origin.IDENTIFIER == "" {
  60. return
  61. }
  62. item.IndexCode = strings.TrimSpace(origin.IDENTIFIER)
  63. item.IndexName = strings.TrimSpace(origin.NAME)
  64. item.Frequency = frequency
  65. item.Unit = "无"
  66. item.Data = make([]BaseFromBloombergApiIndexData, 0)
  67. // 数据来源的值和日期对应字段不同
  68. var dataTime time.Time
  69. var val *float64
  70. if origin.LAST_UPDATE_DATE_EOD != "" && origin.PX_LAST_EOD != nil {
  71. dataTime, _ = time.ParseInLocation(utils.FormatDate, origin.LAST_UPDATE_DATE_EOD, time.Local)
  72. val = origin.PX_LAST_EOD
  73. }
  74. if origin.LAST_UPDATE_DATE_EOD != "" && origin.PX_VOLUME_EOD != nil {
  75. dataTime, _ = time.ParseInLocation(utils.FormatDate, origin.LAST_UPDATE_DATE_EOD, time.Local)
  76. val = origin.PX_VOLUME_EOD
  77. }
  78. if origin.DATE != "" && origin.PX_LAST != nil {
  79. dataTime, _ = time.ParseInLocation(utils.FormatDate, origin.DATE, time.Local)
  80. val = origin.PX_LAST
  81. }
  82. if origin.DATE != "" && origin.PX_VOLUME != nil {
  83. dataTime, _ = time.ParseInLocation(utils.FormatDate, origin.DATE, time.Local)
  84. val = origin.PX_VOLUME
  85. }
  86. if origin.PX_SETTLE_LAST_DT != "" && origin.PX_SETTLE != nil {
  87. dataTime, _ = time.ParseInLocation(utils.FormatDate, origin.PX_SETTLE_LAST_DT, time.Local)
  88. val = origin.PX_SETTLE
  89. }
  90. if origin.OPEN_INT_DATE != "" && origin.OPEN_INT != nil {
  91. dataTime, _ = time.ParseInLocation(utils.FormatDate, origin.OPEN_INT_DATE, time.Local)
  92. val = origin.OPEN_INT
  93. }
  94. if taskKey == TaskKey0345 && origin.PX_CLOSE_DT != "" && origin.PX_LAST != nil {
  95. dataTime, _ = time.ParseInLocation(utils.FormatDate, origin.PX_CLOSE_DT, time.Local)
  96. val = origin.PX_LAST
  97. }
  98. if (taskKey == TaskKey0000 || taskKey == SubOneDayTaskKey) && origin.DL_SNAPSHOT_START_TIME != "" && origin.PX_LAST != nil {
  99. // 根据LAST_UPDATE的格式进行判断, 仅加入格式为hh:mm:ss的
  100. _, e := time.ParseInLocation("15:04:05", origin.LAST_UPDATE, time.Local)
  101. if e != nil {
  102. return
  103. }
  104. d, e := time.ParseInLocation("2006-01-02T15:04:05", origin.DL_SNAPSHOT_START_TIME, time.Local)
  105. if e != nil {
  106. global.LOG.Info(fmt.Sprintf("DL_SNAPSHOT_START_TIME日期解析失败, %s", origin.DL_SNAPSHOT_START_TIME))
  107. }
  108. dataTime = d
  109. // 这个任务日期需要-1
  110. if taskKey == SubOneDayTaskKey {
  111. dataTime = dataTime.AddDate(0, 0, -1)
  112. }
  113. val = origin.PX_LAST
  114. }
  115. if !dataTime.IsZero() && val != nil {
  116. item.Data = append(item.Data, BaseFromBloombergApiIndexData{
  117. DataTime: dataTime,
  118. Value: *val,
  119. })
  120. }
  121. return
  122. }
  123. func FormatPythonBloombergDailyRun11Data2Base(origin PythonBloombergGeneralData, frequency, taskKey string) (items []BaseFromBloombergApiIndexAndData) {
  124. if origin.IDENTIFIER == "" {
  125. return
  126. }
  127. if origin.LAST_UPDATE_DT == "" {
  128. return
  129. }
  130. dataTime, err := time.ParseInLocation(utils.FormatDate, origin.LAST_UPDATE_DT, time.Local)
  131. if err != nil {
  132. return
  133. }
  134. items = make([]BaseFromBloombergApiIndexAndData, 0)
  135. // WER1UK00 Index, WER1UK00 Index HDD, WER1UK00 Index CDD
  136. item := BaseFromBloombergApiIndexAndData{}
  137. item.IndexCode = strings.TrimSpace(origin.IDENTIFIER)
  138. item.IndexName = strings.TrimSpace(origin.NAME)
  139. item.Frequency = frequency
  140. item.Unit = "无"
  141. item.Data = make([]BaseFromBloombergApiIndexData, 0)
  142. itemHDD := BaseFromBloombergApiIndexAndData{}
  143. itemHDD.IndexCode = strings.TrimSpace(origin.IDENTIFIER)
  144. itemHDD.IndexCode = fmt.Sprintf("%s HDD", itemHDD.IndexCode)
  145. itemHDD.IndexName = strings.TrimSpace(origin.NAME)
  146. itemHDD.IndexName = fmt.Sprintf("%s HDD", itemHDD.IndexName)
  147. itemHDD.Frequency = frequency
  148. itemHDD.Unit = "无"
  149. itemHDD.Data = make([]BaseFromBloombergApiIndexData, 0)
  150. itemCDD := BaseFromBloombergApiIndexAndData{}
  151. itemCDD.IndexCode = strings.TrimSpace(origin.IDENTIFIER)
  152. itemCDD.IndexCode = fmt.Sprintf("%s CDD", itemCDD.IndexCode)
  153. itemCDD.IndexName = strings.TrimSpace(origin.NAME)
  154. itemCDD.IndexName = fmt.Sprintf("%s CDD", itemCDD.IndexName)
  155. itemCDD.Frequency = frequency
  156. itemCDD.Unit = "无"
  157. itemCDD.Data = make([]BaseFromBloombergApiIndexData, 0)
  158. // 数据来源的值和日期对应字段不同
  159. if origin.PX_LAST != nil {
  160. val := origin.PX_LAST
  161. item.Data = append(item.Data, BaseFromBloombergApiIndexData{
  162. DataTime: dataTime,
  163. Value: *val,
  164. })
  165. }
  166. if origin.HEATING_DAYS != nil {
  167. val := origin.HEATING_DAYS
  168. itemHDD.Data = append(itemHDD.Data, BaseFromBloombergApiIndexData{
  169. DataTime: dataTime,
  170. Value: *val,
  171. })
  172. }
  173. if origin.COOLING_DAYS != nil {
  174. val := origin.COOLING_DAYS
  175. itemCDD.Data = append(itemCDD.Data, BaseFromBloombergApiIndexData{
  176. DataTime: dataTime,
  177. Value: *val,
  178. })
  179. }
  180. items = append(items, item, itemHDD, itemCDD)
  181. return
  182. }
  183. func FormatPythonBloombergDailyRunHistV1Data2Base(origin PythonBloombergGeneralData, frequency, taskKey string) (item BaseFromBloombergApiIndexAndData) {
  184. if origin.IDENTIFIER == "" {
  185. return
  186. }
  187. item.IndexCode = strings.TrimSpace(origin.IDENTIFIER)
  188. if item.IndexCode == "TZT1 COMB Comdty" {
  189. item.IndexCode = "TZT1 COMB V Comdty"
  190. }
  191. item.IndexName = strings.TrimSpace(origin.NAME)
  192. item.Frequency = frequency
  193. item.Unit = "无"
  194. item.Data = make([]BaseFromBloombergApiIndexData, 0)
  195. // 数据来源的值和日期对应字段不同
  196. var dataTime time.Time
  197. var val *float64
  198. if origin.DATE != "" && origin.PX_VOLUME != nil {
  199. dataTime, _ = time.ParseInLocation(utils.FormatDate, origin.DATE, time.Local)
  200. val = origin.PX_VOLUME
  201. }
  202. if !dataTime.IsZero() && val != nil {
  203. item.Data = append(item.Data, BaseFromBloombergApiIndexData{
  204. DataTime: dataTime,
  205. Value: *val,
  206. })
  207. }
  208. return
  209. }