gitee.com/quant1x/engine@v1.8.4/datasource/dfcf/margin_trading.go (about)

     1  package dfcf
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"gitee.com/quant1x/exchange"
     7  	"gitee.com/quant1x/gox/http"
     8  	urlpkg "net/url"
     9  )
    10  
    11  const (
    12  	// https://data.eastmoney.com/rzrq/detail/all.html
    13  	// https://datacenter-web.eastmoney.com/api/data/v1/get?reportName=RPTA_WEB_RZRQ_GGMX&columns=ALL&source=WEB&pageNumber=1&pageSize=10&sortColumns=rzjme&sortTypes=-1&filter=(DATE%3D%272023-12-28%27)&callback=jQuery112303199655251283524_1703887938254&_=1703887938257
    14  	urlEastMoneyApiRZRQ = "https://datacenter-web.eastmoney.com/api/data/v1/get"
    15  	rzrqPageSize        = 500
    16  )
    17  
    18  // SecurityMarginTrading 融资融券
    19  type SecurityMarginTrading struct {
    20  	DATE            string  `name:"日期" json:"DATE"`
    21  	MARKET          string  `name:"市场" json:"MARKET"`
    22  	SCODE           string  `name:"代码" json:"SCODE"`
    23  	SecName         string  `name:"证券名称" json:"SECNAME"`
    24  	RZYE            float64 `name:"融资余额(元)" json:"RZYE"`
    25  	RQYL            float64 `name:"融券余量(股)" json:"RQYL"`
    26  	RZRQYE          float64 `name:"融资融券余额(元)" json:"RZRQYE"`
    27  	RQYE            float64 `name:"融券余额(元)" json:"RQYE"`
    28  	RQMCL           float64 `name:"融券卖出量(股)" json:"RQMCL"`
    29  	RZRQYECZ        float64 `name:"融资融券余额差值(元)" json:"RZRQYECZ"`
    30  	RZMRE           float64 `name:"融资买入额(元)" json:"RZMRE"`
    31  	SZ              float64 `name:"SZ" json:"SZ"`
    32  	RZYEZB          float64 `name:"融资余额占流通市值比(%)" json:"RZYEZB"`
    33  	RZMRE3D         float64 `name:"3日融资买入额(元)" json:"RZMRE3D"`
    34  	RZMRE5D         float64 `name:"5日融资买入额(元)" json:"RZMRE5D"`
    35  	RZMRE10D        float64 `name:"10日融资买入额(元)" json:"RZMRE10D"`
    36  	RZCHE           float64 `name:"融资偿还额(元)" json:"RZCHE"`
    37  	RZCHE3D         float64 `name:"3日融资偿还额(元)" json:"RZCHE3D"`
    38  	RZCHE5D         float64 `name:"5日融资偿还额(元)" json:"RZCHE5D"`
    39  	RZCHE10D        float64 `name:"10日融资偿还额(元)" json:"RZCHE10D"`
    40  	RZJME           float64 `name:"融资净买额(元)" json:"RZJME"`
    41  	RZJME3D         float64 `name:"3日融资净买额(元)" json:"RZJME3D"`
    42  	RZJME5D         float64 `name:"5日融资净买额(元)" json:"RZJME5D"`
    43  	RZJME10D        float64 `name:"10日融资净买额(元)" json:"RZJME10D"`
    44  	RQMCL3D         float64 `name:"3日融券卖出量(股)" json:"RQMCL3D"`
    45  	RQMCL5D         float64 `name:"5日融券卖出量(股)" json:"RQMCL5D"`
    46  	RQMCL10D        float64 `name:"10日融券卖出量(股)" json:"RQMCL10D"`
    47  	RQCHL           float64 `name:"融券偿还量(股)" json:"RQCHL"`
    48  	RQCHL3D         float64 `name:"3日融券偿还量(股)" json:"RQCHL3D"`
    49  	RQCHL5D         float64 `name:"5日融券偿还量(股)" json:"RQCHL5D"`
    50  	RQCHL10D        float64 `name:"10日融券偿还量(股)" json:"RQCHL10D"`
    51  	RQJMG           float64 `name:"融券净卖出(股)" json:"RQJMG"`
    52  	RQJMG3D         float64 `name:"3日融券净卖出(股)" json:"RQJMG3D"`
    53  	RQJMG5D         float64 `name:"5日融券净卖出(股)" json:"RQJMG5D"`
    54  	RQJMG10D        float64 `name:"10日融券净卖出(股)" json:"RQJMG10D"`
    55  	SPJ             float64 `name:"收盘价" json:"SPJ"`
    56  	ZDF             float64 `name:"涨跌幅" json:"ZDF"`
    57  	RChange3DCP     float64 `name:"3日未识别" json:"RCHANGE3DCP"`
    58  	RChange5DCP     float64 `name:"5日未识别" json:"RCHANGE5DCP"`
    59  	RChange10DCP    float64 `name:"10日未识别" json:"RCHANGE10DCP"`
    60  	KCB             int     `name:"科创板"  json:"KCB"`
    61  	TradeMarketCode string  `name:"二级市场代码" json:"TRADE_MARKET_CODE"`
    62  	TradeMarket     string  `name:"二级市场" json:"TRADE_MARKET"`
    63  	FinBalanceGr    float64 `json:"FIN_BALANCE_GR"`
    64  	SecuCode        string  `name:"证券代码" json:"SECUCODE"`
    65  }
    66  
    67  type rawMarginTrading struct {
    68  	Version string `json:"version"`
    69  	Result  struct {
    70  		Pages int                     `json:"pages"`
    71  		Data  []SecurityMarginTrading `json:"data"`
    72  		Count int                     `json:"count"`
    73  	} `json:"result"`
    74  	Success bool   `json:"success"`
    75  	Message string `json:"message"`
    76  	Code    int    `json:"code"`
    77  }
    78  
    79  func rawMarginTradingList(date string, pageNumber int) ([]SecurityMarginTrading, int, error) {
    80  	tradeDate := exchange.FixTradeDate(date)
    81  	params := urlpkg.Values{
    82  		"reportName":  {"RPTA_WEB_RZRQ_GGMX"},
    83  		"columns":     {"ALL"},
    84  		"source":      {"WEB"},
    85  		"sortColumns": {"scode"},
    86  		"sortTypes":   {"1"},
    87  		"pageSize":    {fmt.Sprintf("%d", rzrqPageSize)},
    88  		"pageNumber":  {fmt.Sprintf("%d", pageNumber)},
    89  		"client":      {"WEB"},
    90  		"filter":      {fmt.Sprintf(`(DATE='%s')`, tradeDate)},
    91  	}
    92  
    93  	url := urlEastMoneyApiRZRQ + "?" + params.Encode()
    94  	data, err := http.Get(url)
    95  	if err != nil {
    96  		return nil, 0, err
    97  	}
    98  	var raw rawMarginTrading
    99  	err = json.Unmarshal(data, &raw)
   100  	if err != nil {
   101  		return nil, 0, err
   102  	}
   103  	return raw.Result.Data, raw.Result.Pages, nil
   104  }
   105  
   106  // GetMarginTradingList 获取两融列表
   107  //
   108  //	东方财富两融数据只有前一个交易日的数据
   109  func GetMarginTradingList(date string) []SecurityMarginTrading {
   110  	var list []SecurityMarginTrading
   111  	pages := 1
   112  	for i := 0; i < pages; i++ {
   113  		tmpList, tmpPages, err := rawMarginTradingList(date, i+1)
   114  		if err != nil {
   115  			break
   116  		}
   117  		list = append(list, tmpList...)
   118  		if len(tmpList) < rzrqPageSize {
   119  			break
   120  		}
   121  		if pages == 1 {
   122  			pages = tmpPages
   123  		}
   124  	}
   125  	return list
   126  }