trade_analysis.go 17 KB

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