trade_analysis.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. package trade_analysis
  2. import (
  3. "eta/eta_api/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "strings"
  7. "time"
  8. )
  9. const (
  10. TradeDataTypeNull = 0 // 无值
  11. TradeDataTypeOrigin = 1 // 原始值
  12. TradeDataTypeCalculate = 2 // 推算值
  13. WarehouseBuyChartType = 1 // 多单图
  14. WarehouseSoldChartType = 2 // 空单图
  15. WarehousePureBuyChartType = 3 // 净多单图
  16. WarehouseDefaultUnit = "手"
  17. WarehouseDefaultFrequency = "日度"
  18. GuangZhouTopCompanyAliasName = "日成交持仓排名" // 广期所TOP20对应的公司名称
  19. GuangZhouSeatNameBuy = "持买单量" // 广期所指标名称中的多单名称
  20. GuangZhouSeatNameSold = "持卖单量" // 广期所指标名称中的空单名称
  21. GuangZhouTopSeatNameBuy = "持买单量总计" // 广期所指标名称中的TOP20多单名称
  22. GuangZhouTopSeatNameSold = "持卖单量总计" // 广期所指标名称中的TOP20空单名称
  23. GuangZhouTopSeatNameDeal = "成交量总计" // 广期所指标名称中的TOP20成交量名称
  24. )
  25. const (
  26. TradeExchangeDalian = "dalian"
  27. TradeExchangeZhengzhou = "zhengzhou"
  28. TradeExchangeGuangzhou = "guangzhou"
  29. )
  30. var WarehouseTypeSuffixNames = map[int]string{
  31. WarehouseBuyChartType: "席位多单",
  32. WarehouseSoldChartType: "席位空单",
  33. WarehousePureBuyChartType: "席位净多单",
  34. }
  35. // GuangzhouSeatNameValType 广期所数据名称对应的席位方向
  36. var GuangzhouSeatNameValType = map[string]int{
  37. GuangZhouSeatNameBuy: 1,
  38. GuangZhouSeatNameSold: 2,
  39. GuangZhouTopSeatNameBuy: 1,
  40. GuangZhouTopSeatNameSold: 2,
  41. GuangZhouTopSeatNameDeal: 3,
  42. }
  43. // 合约查询方式
  44. var (
  45. ContractQueryTypeTop = 1 // 主力合约
  46. ContractQueryTypeTop2 = 2 // 成交量前2
  47. ContractQueryTypeTop3 = 3 // 成交量前3
  48. ContractQueryTypeAll = 4 // 所有合约(多个)
  49. ContractQueryTypeTotal = 5 // 合约加总(1个)
  50. )
  51. // 合约方向
  52. var (
  53. ContractPositionBuy = 1 // 多单
  54. ContractPositionSold = 2 // 空单
  55. ContractPositionPureBuy = 3 // 净多单
  56. )
  57. // TradePositionTop 持仓榜单
  58. type TradePositionTop struct {
  59. Id uint64 `gorm:"primaryKey;column:id" json:"id"`
  60. ClassifyName string `gorm:"column:classify_name" json:"classify_name"` //分类名称
  61. ClassifyType string `gorm:"column:classify_type" json:"classify_type"` //分类名称下的类型
  62. DealShortName string `gorm:"column:deal_short_name" json:"deal_short_name"` //成交量公司简称
  63. DealValue int `gorm:"column:deal_value" json:"deal_value"` //成交量
  64. DealChange int `gorm:"column:deal_change" json:"deal_change"` //成交变化量
  65. DataTime time.Time `gorm:"column:data_time" json:"data_time"` //数据日期
  66. CreateTime time.Time `gorm:"column:create_time" json:"create_time"` //插入时间
  67. ModifyTime time.Time `gorm:"column:modify_time" json:"modify_time"` //修改时间
  68. DealType int `gorm:"column:deal_type" json:"deal_type"` //交易类型:1多单,2空单,3净多单,4净空单
  69. SourceType int `gorm:"column:source_type" json:"source_type"` //数据来源,0是原始数据的值,1是由T+1日推算出的值,2是由T日的榜单数据推算出的值
  70. Rank int `gorm:"column:rank" json:"rank"` //排名
  71. }
  72. type TradeClassifyNameList struct {
  73. Exchange string `description:"交易所"`
  74. ExchangeEn string `description:"交易所英文"`
  75. Sort int `description:"排序字段" `
  76. Num int `description:"品种数量"`
  77. DataTime string `description:"最新更新时间"`
  78. CurrDate string `description:"当前日期"`
  79. Items []TradeClassifyNameListItem `description:"子类"`
  80. }
  81. type TradeClassifyNameListSort []TradeClassifyNameList
  82. func (v TradeClassifyNameListSort) Len() int {
  83. return len(v)
  84. }
  85. func (v TradeClassifyNameListSort) Swap(i, j int) {
  86. v[i], v[j] = v[j], v[i]
  87. }
  88. func (v TradeClassifyNameListSort) Less(i, j int) bool {
  89. return v[i].Sort < v[j].Sort
  90. }
  91. type TradeClassifyNameListItemSort []TradeClassifyNameListItem
  92. func (v TradeClassifyNameListItemSort) Len() int {
  93. return len(v)
  94. }
  95. func (v TradeClassifyNameListItemSort) Swap(i, j int) {
  96. v[i], v[j] = v[j], v[i]
  97. }
  98. func (v TradeClassifyNameListItemSort) Less(i, j int) bool {
  99. return v[i].ClassifyName < v[j].ClassifyName
  100. }
  101. type TradeClassifyNameListItem struct {
  102. ClassifyName string `description:"交易分类"`
  103. Items []TradeClassifyNameListItemItem `description:"合约代码"`
  104. }
  105. type TradeClassifyNameListItemItemSort []TradeClassifyNameListItemItem
  106. func (v TradeClassifyNameListItemItemSort) Len() int {
  107. return len(v)
  108. }
  109. func (v TradeClassifyNameListItemItemSort) Swap(i, j int) {
  110. v[i], v[j] = v[j], v[i]
  111. }
  112. func (v TradeClassifyNameListItemItemSort) Less(i, j int) bool {
  113. return v[i].ClassifyType < v[j].ClassifyType
  114. }
  115. type TradeClassifyNameListItemItem struct {
  116. ClassifyType string `description:"分类名称下的类型"`
  117. }
  118. type TradeClassifyName struct {
  119. ClassifyName string //分类名称
  120. ClassifyType string //分类名称下的类型
  121. LatestDate string //分类下最晚日期
  122. }
  123. // GetExchangeClassify 获取交易所分类列表
  124. func GetExchangeClassify(exchange string) (list []TradeClassifyName, err error) {
  125. tableName := "base_from_trade_" + exchange + "_index"
  126. orderStr := "classify_name DESC, classify_type asc"
  127. if exchange == "zhengzhou" {
  128. orderStr = "classify_name asc"
  129. }
  130. sql := "SELECT classify_name, classify_type FROM " + tableName + " WHERE `rank` <=20 and `rank` > 0 GROUP BY classify_name, classify_type "
  131. sql += ` ORDER BY ` + orderStr
  132. o := orm.NewOrmUsingDB("data")
  133. _, err = o.Raw(sql).QueryRows(&list)
  134. return
  135. }
  136. type LastTimeItem struct {
  137. CreateTime time.Time
  138. }
  139. // GetExchangeLastTime 获取交易所数据最晚的时间
  140. func GetExchangeLastTime(exchange string) (item LastTimeItem, err error) {
  141. tableName := "base_from_trade_" + exchange + "_index"
  142. sql := `SELECT create_time FROM ` + tableName + ` ORDER BY create_time desc`
  143. o := orm.NewOrmUsingDB("data")
  144. err = o.Raw(sql).QueryRow(&item)
  145. return
  146. }
  147. type GetPositionTopReq struct {
  148. Exchange string `json:"exchange" form:"exchange"` //交易所
  149. ClassifyName string `json:"classify_name" form:"classify_name"` //分类名称
  150. ClassifyType string `json:"classify_type" form:"classify_type"` //具体合约名称
  151. DataTime string `json:"data_time" form:"data_time"` //请求日期,如果为空,则返回最新的榜单日期
  152. }
  153. type GetPositionTopResp struct {
  154. BuyList GetPositionTopList `description:"多单列表"`
  155. SoldList GetPositionTopList `description:"空单列表"`
  156. CleanBuyList GetPositionTopList `description:"净多单列表"`
  157. CleanSoldList GetPositionTopList `description:"净空单列表"`
  158. DataTime string `description:"最新日期或者请求日期"`
  159. LastDataTime string `description:"最新日期"`
  160. }
  161. type GetPositionTopList struct {
  162. TotalDealValue int `description:"总计成交量"`
  163. TotalDealChange int `description:"校昨日变化"`
  164. List []GetPositionTopListItem `description:"榜单详情列表"`
  165. }
  166. type GetPositionTopListItem struct {
  167. DealShortName string `description:"成交量公司简称"`
  168. DealValue int `description:"成交量"`
  169. DealChange int `description:"成交变化量"`
  170. Rank int `description:"当前名次"`
  171. Rate string `description:"当前占比"`
  172. BeforeAllRate string `description:"排在前面的成交量总计占比(包含)"`
  173. BeforeAllValue int `description:"排在前面的成交量总计"`
  174. BeforeAllChange int `description:"排在前面的成交量增减总计"`
  175. }
  176. func GetTradePositionTop(exchange string, classifyName, classifyType, dataTime string) (list []TradePositionTop, err error) {
  177. tableName := "trade_position_" + exchange + "_top"
  178. sql := "SELECT * FROM " + tableName + " WHERE classify_name=? and classify_type=? and data_time=? and `rank` <=20 and `rank` > 0 ORDER BY deal_value desc"
  179. o := orm.NewOrmUsingDB("data")
  180. _, err = o.Raw(sql, classifyName, classifyType, dataTime).QueryRows(&list)
  181. return
  182. }
  183. type OriginTradeData struct {
  184. Rank int `description:"排名"`
  185. CompanyName string `description:"期货公司名称"`
  186. Val int `description:"持仓量"`
  187. ValChange int `description:"持仓增减"`
  188. DataTime time.Time `description:"数据日期"`
  189. ClassifyName string `description:"品种名称"`
  190. ClassifyType string `description:"合约代码"`
  191. ValType int `description:"数据类型: 1-多单; 2-空单"`
  192. }
  193. // BaseFromTradeCommonIndex 郑商所/大商所/上期所/上期能源指标表通用字段
  194. type BaseFromTradeCommonIndex struct {
  195. Rank int `description:"排名"`
  196. DealShortName string `description:"成交量公司简称"`
  197. DealName string `description:"成交量指标名称"`
  198. DealCode string `description:"成交量指标编码"`
  199. DealValue int `description:"成交量"`
  200. DealChange int `description:"成交变化量"`
  201. BuyShortName string `description:"持买单量公司简称"`
  202. BuyName string `description:"持买单量指标名称"`
  203. BuyCode string `description:"持买单量指标编码"`
  204. BuyValue int `description:"持买单量"`
  205. BuyChange int `description:"持买单量变化量"`
  206. SoldShortName string `description:"持卖单量公司简称"`
  207. SoldName string `description:"持卖单量指标名称"`
  208. SoldCode string `description:"持卖单量指标编码"`
  209. SoldValue int `description:"持卖单量"`
  210. SoldChange int `description:"持卖单变化量"`
  211. Frequency string `description:"频度"`
  212. ClassifyName string `description:"品种"`
  213. ClassifyType string `description:"合约"`
  214. CreateTime time.Time `description:"创建时间"`
  215. ModifyTime time.Time `description:"更新时间"`
  216. DataTime time.Time `description:"数据日期"`
  217. }
  218. // GetTradeDataByContracts 根据合约获取持仓数据
  219. func GetTradeDataByContracts(exchange string, classifyNames, contracts []string, startDate, endDate time.Time) (items []*BaseFromTradeCommonIndex, err error) {
  220. if exchange == "" {
  221. err = fmt.Errorf("数据表名称有误")
  222. return
  223. }
  224. var cond string
  225. var pars []interface{}
  226. if len(classifyNames) > 0 {
  227. cond += fmt.Sprintf(` AND classify_name IN (%s)`, utils.GetOrmInReplace(len(classifyNames)))
  228. pars = append(pars, classifyNames)
  229. }
  230. if len(contracts) > 0 {
  231. cond += fmt.Sprintf(` AND classify_type IN (%s)`, utils.GetOrmInReplace(len(contracts)))
  232. pars = append(pars, contracts)
  233. }
  234. if !startDate.IsZero() && !endDate.IsZero() {
  235. cond += ` AND (data_time BETWEEN ? AND ?)`
  236. pars = append(pars, startDate.Format(utils.FormatDate), endDate.Format(utils.FormatDate))
  237. }
  238. fields := []string{"rank", "buy_short_name", "buy_value", "buy_change", "sold_short_name", "sold_value", "sold_change", "classify_name", "classify_type", "data_time"}
  239. tableName := fmt.Sprintf("base_from_trade_%s_index", exchange)
  240. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s ORDER BY data_time DESC`, strings.Join(fields, ","), tableName, cond)
  241. _, err = orm.NewOrmUsingDB("data").Raw(sql, pars).QueryRows(&items)
  242. return
  243. }
  244. // GetZhengzhouTradeDataByContracts 郑商所-根据合约获取持仓数据
  245. func GetZhengzhouTradeDataByContracts(classifyNames []string, startDate, endDate time.Time) (items []*BaseFromTradeCommonIndex, err error) {
  246. var cond string
  247. var pars []interface{}
  248. if len(classifyNames) > 0 {
  249. cond += fmt.Sprintf(` AND classify_name IN (%s)`, utils.GetOrmInReplace(len(classifyNames)))
  250. pars = append(pars, classifyNames)
  251. }
  252. if !startDate.IsZero() && !endDate.IsZero() {
  253. cond += ` AND (data_time BETWEEN ? AND ?)`
  254. pars = append(pars, startDate.Format(utils.FormatDate), endDate.Format(utils.FormatDate))
  255. }
  256. // ps.classify_name实为合约代码
  257. fields := []string{"rank", "buy_short_name", "buy_value", "buy_change", "sold_short_name", "sold_value", "sold_change", "classify_name AS classify_type", "data_time"}
  258. sql := fmt.Sprintf(`SELECT %s FROM base_from_trade_zhengzhou_index WHERE 1=1 %s ORDER BY data_time DESC`, strings.Join(fields, ","), cond)
  259. _, err = orm.NewOrmUsingDB("data").Raw(sql, pars).QueryRows(&items)
  260. return
  261. }
  262. // ContractCompanyTradeData [合约-期货公司]持仓数据
  263. type ContractCompanyTradeData struct {
  264. Exchange string `description:"交易所"`
  265. ClassifyName string `description:"品种"`
  266. ClassifyType string `description:"合约代码"`
  267. CompanyName string `description:"期货公司名称"`
  268. IsTotal bool `description:"是否为合约加总"`
  269. StartDate time.Time `description:"数据开始日期"`
  270. EndDate time.Time `description:"数据结束日期"`
  271. DataList []*ContractCompanyTradeDataList `description:"数据序列"`
  272. }
  273. // ContractCompanyTradeDataList [合约-期货公司]持仓数据详情
  274. type ContractCompanyTradeDataList struct {
  275. Date time.Time `description:"数据日期"`
  276. BuyVal int `description:"多单持仓量"`
  277. BuyValType int `description:"多单数据类型: 0-无值; 1-原始值; 2-推算值"`
  278. BuyChange int `description:"多单持仓增减"`
  279. BuyChangeType int `description:"多单持仓增减类型: 0-无值; 1-原始值; 2-推算值"`
  280. SoldVal int `description:"空单持仓量"`
  281. SoldValType int `description:"空单数据类型: 0-无值; 1-原始值; 2-推算值"`
  282. SoldChange int `description:"空单持仓增减"`
  283. SoldChangeType int `description:"空单持仓增减类型: 0-无值; 1-原始值; 2-推算值"`
  284. PureBuyVal int `description:"净多单持仓量"`
  285. PureBuyValType int `description:"净多单数据类型: 0-无值; 1-原始值; 2-推算值"`
  286. PureBuyChange int `description:"净多单持仓增减"`
  287. PureBuyChangeType int `description:"净多单持仓增减类型: 0-无值; 1-原始值; 2-推算值"`
  288. }
  289. type BaseFromTradeGuangzhouIndex struct {
  290. BaseFromTradeGuangzhouIndexId int `orm:"column(base_from_trade_guangzhou_index_id);pk"`
  291. BaseFromTradeGuangzhouClassifyId int `description:"分类id"`
  292. IndexCode string `description:"指标编码"`
  293. IndexName string `description:"指标名称"`
  294. Frequency string `description:"频率"`
  295. Unit string `description:"单位"`
  296. StartDate string `description:"开始日期"`
  297. EndDate string `description:"结束日期"`
  298. CreateTime time.Time `description:"创建日期"`
  299. ModifyTime time.Time `description:"修改日期"`
  300. }
  301. func GetBaseFromTradeGuangzhouIndex(classifyIds []int, contracts []string, indexKeyword string) (list []*BaseFromTradeGuangzhouIndex, err error) {
  302. o := orm.NewOrmUsingDB("data")
  303. cond := ``
  304. pars := make([]interface{}, 0)
  305. if len(classifyIds) > 0 {
  306. cond += fmt.Sprintf(` AND b.base_from_trade_guangzhou_classify_id IN (%s)`, utils.GetOrmInReplace(len(classifyIds)))
  307. pars = append(pars, classifyIds)
  308. }
  309. if len(contracts) > 0 {
  310. cond += fmt.Sprintf(` AND b.contract IN (%s)`, utils.GetOrmInReplace(len(contracts)))
  311. pars = append(pars, contracts)
  312. }
  313. if indexKeyword != "" {
  314. cond += fmt.Sprintf(` AND a.index_name LIKE ?`)
  315. pars = append(pars, indexKeyword)
  316. }
  317. sql := `SELECT a.* FROM base_from_trade_guangzhou_index AS a
  318. JOIN base_from_trade_guangzhou_contract AS b ON a.base_from_trade_guangzhou_contract_id = b.base_from_trade_guangzhou_contract_id
  319. WHERE 1=1 %s`
  320. sql = fmt.Sprintf(sql, cond)
  321. _, err = o.Raw(sql, pars).QueryRows(&list)
  322. return
  323. }
  324. type BaseFromTradeGuangzhouData struct {
  325. BaseFromTradeGuangzhouDataId int `orm:"column(base_from_trade_guangzhou_data_id);pk"`
  326. BaseFromTradeGuangzhouIndexId int `description:"指标id"`
  327. IndexCode string `description:"指标编码"`
  328. DataTime time.Time `description:"数据日期"`
  329. Value float64 `description:"数据值"`
  330. QtySub float64 `description:"增减"`
  331. CreateTime time.Time `description:"创建日期"`
  332. ModifyTime time.Time `description:"修改日期"`
  333. }
  334. // GetBaseFromTradeGuangzhouDataByIndexIds 广期所-获取指标数据
  335. func GetBaseFromTradeGuangzhouDataByIndexIds(indexIds []int, startDate, endDate time.Time) (list []*BaseFromTradeGuangzhouData, err error) {
  336. if len(indexIds) == 0 {
  337. return
  338. }
  339. cond := fmt.Sprintf(` AND base_from_trade_guangzhou_index_id IN (%s)`, utils.GetOrmInReplace(len(indexIds)))
  340. pars := make([]interface{}, 0)
  341. pars = append(pars, indexIds)
  342. if !startDate.IsZero() && !endDate.IsZero() {
  343. if startDate.Equal(endDate) {
  344. cond += ` AND data_time = ?`
  345. pars = append(pars, startDate.Format(utils.FormatDate))
  346. }
  347. if !startDate.Equal(endDate) {
  348. cond += ` AND (data_time BETWEEN ? AND ?)`
  349. pars = append(pars, startDate.Format(utils.FormatDate), endDate.Format(utils.FormatDate))
  350. }
  351. }
  352. sql := fmt.Sprintf(`SELECT * FROM base_from_trade_guangzhou_data WHERE 1=1 %s ORDER BY base_from_trade_guangzhou_index_id`, cond)
  353. _, err = orm.NewOrmUsingDB("data").Raw(sql, pars).QueryRows(&list)
  354. return
  355. }
  356. // ContractTopRankData TOP20合约排名数据
  357. type ContractTopRankData struct {
  358. Exchange string `description:"交易所"`
  359. DealValue int `description:"成交量"`
  360. BuyValue int `description:"多单持仓量"`
  361. BuyChange int `description:"多单变化"`
  362. SoldValue int `description:"空单持仓量"`
  363. SoldChange int `description:"空单变化"`
  364. PureBuyValue int `description:"净多单持仓量"`
  365. PureBuyChange int `description:"净多单变化"`
  366. ClassifyName string `description:"品种名称"`
  367. ClassifyType string `description:"合约代码"`
  368. DataTime time.Time `description:"数据日期"`
  369. }
  370. // GetContractTopRankData 获取合约TOP20根据当日成交量排名
  371. func GetContractTopRankData(exchange string, classifyNames []string, dataDate time.Time) (items []*ContractTopRankData, err error) {
  372. if exchange == "" {
  373. err = fmt.Errorf("数据表名称有误")
  374. return
  375. }
  376. if len(classifyNames) == 0 {
  377. return
  378. }
  379. tableName := fmt.Sprintf("base_from_trade_%s_index", exchange)
  380. // 大商所存在TOP20的rank=0
  381. queryRank := ` rank = 999`
  382. if exchange == TradeExchangeDalian {
  383. queryRank = ` (rank = 999 OR rank = 0)`
  384. }
  385. sql := `SELECT * FROM %s WHERE data_time = ? AND classify_name IN (%s) AND %s GROUP BY classify_type ORDER BY deal_value DESC`
  386. sql = fmt.Sprintf(sql, tableName, utils.GetOrmInReplace(len(classifyNames)), queryRank)
  387. _, err = orm.NewOrmUsingDB("data").Raw(sql, dataDate.Format(utils.FormatDate), classifyNames).QueryRows(&items)
  388. return
  389. }
  390. // GetZhengzhouContractTopRankData 郑商所-获取合约根据当日成交量排名
  391. func GetZhengzhouContractTopRankData(classifyNames []string, dataDate time.Time) (items []*ContractTopRankData, err error) {
  392. if len(classifyNames) == 0 {
  393. return
  394. }
  395. sql := `SELECT * FROM base_from_trade_zhengzhou_index WHERE data_time = ? AND classify_name IN (%s) AND rank = 999 GROUP BY classify_name ORDER BY deal_value DESC`
  396. sql = fmt.Sprintf(sql, utils.GetOrmInReplace(len(classifyNames)))
  397. _, err = orm.NewOrmUsingDB("data").Raw(sql, dataDate.Format(utils.FormatDate), classifyNames).QueryRows(&items)
  398. return
  399. }
  400. // ContractCompanyTradeEdb [合约-期货公司]指标
  401. type ContractCompanyTradeEdb struct {
  402. Exchange string `description:"交易所"`
  403. ClassifyName string `description:"品种"`
  404. ClassifyType string `description:"合约代码"`
  405. CompanyName string `description:"期货公司名称"`
  406. IsTotal bool `description:"是否为合约加总"`
  407. ContractPosition int `description:"合约方向"`
  408. StartDate time.Time `description:"数据开始日期"`
  409. EndDate time.Time `description:"数据结束日期"`
  410. DataList []*ContractCompanyTradeEdbData `description:"数据序列"`
  411. }
  412. // ContractCompanyTradeEdbData [合约-期货公司]指标数据
  413. type ContractCompanyTradeEdbData struct {
  414. DataTime time.Time `description:"数据日期"`
  415. Val int `description:"数据值"`
  416. }
  417. // GetClassifyNewestDataTime 获取品种最新数据日期
  418. func GetClassifyNewestDataTime(exchange string, classifyNames []string) (dateTime time.Time, err error) {
  419. if exchange == "" {
  420. err = fmt.Errorf("数据表名称有误")
  421. return
  422. }
  423. if len(classifyNames) == 0 {
  424. return
  425. }
  426. tableName := fmt.Sprintf("base_from_trade_%s_index", exchange)
  427. sql := `SELECT data_time FROM %s WHERE classify_name IN (%s) ORDER BY data_time DESC LIMIT 1`
  428. sql = fmt.Sprintf(sql, tableName, utils.GetOrmInReplace(len(classifyNames)))
  429. err = orm.NewOrmUsingDB("data").Raw(sql, classifyNames).QueryRow(&dateTime)
  430. return
  431. }
  432. // GetGuangzhouClassifyNewestDataTime 广期所-获取品种最新数据日期
  433. func GetGuangzhouClassifyNewestDataTime(indexIds []int) (dateTime time.Time, err error) {
  434. if len(indexIds) == 0 {
  435. return
  436. }
  437. cond := fmt.Sprintf(` AND base_from_trade_guangzhou_index_id IN (%s)`, utils.GetOrmInReplace(len(indexIds)))
  438. pars := make([]interface{}, 0)
  439. pars = append(pars, indexIds)
  440. sql := fmt.Sprintf(`SELECT data_time FROM base_from_trade_guangzhou_data WHERE 1=1 %s ORDER BY data_time DESC LIMIT 1`, cond)
  441. err = orm.NewOrmUsingDB("data").Raw(sql, pars).QueryRow(&dateTime)
  442. return
  443. }