package tick import "strings" import "errors" import "strconv" import "time" import "log" type SinaFutureDS struct { *DSBase } var sinaFutureSymbols []string func init() { drivers[SinaFuture] = newSinaFutureDS } func newSinaFutureDS(conf *DsConf) (DataSource, error) { return &SinaFutureDS{ DSBase: NewDsBase(conf), // lmax 自己下载历史数据, 所以参数db==nil }, nil } func (lds *SinaFutureDS) Name() string { return SinaFuture } func (lds *SinaFutureDS) Run() { if !lds.conf.Run { log.Println("SinaFutureDS.run config NOT run") return } log.Println("SinaFutureDS.run") for i, _ := range SinaFutureSymbols { sinaFutureSymbols = append(sinaFutureSymbols, i) } fn := func() { url := getfetchurl() lastMarket = make(map[string]string) datach := make(chan []string, 20) // 登录产生新的Session go func() { for { fetchurl(IntSinaFuture, url, datach) time.Sleep(time.Millisecond * 200) } }() for { item := <-datach //log.Println("sinafuture parse begin") newdata := getnewdata(IntSinaFuture, item) for i := 0; i < len(newdata); i++ { //log.Println("sinafuture save begin") lds.Save(newdata[i]) } } } fn() } func getfetchurl() string { baseurl := "http://hq.sinajs.cn/list=" query := strings.Join(sinaFutureSymbols, ",") return baseurl + query } func parsemarketfuture(name string, data string) (*Market, error) { data = strings.Trim(data, "\";\r\n") datas := strings.Split(data, ",") if len(datas) < 27 { return nil, errors.New("makret data error") } var market Market var err error market.Open, err = strconv.ParseFloat(datas[2], 64) if err != nil { return nil, err } market.LastPrice, err = strconv.ParseFloat(datas[8], 64) if err != nil { return nil, err } market.Close, err = strconv.ParseFloat(datas[5], 64) if err != nil { return nil, err } market.High, err = strconv.ParseFloat(datas[3], 64) if err != nil { return nil, err } market.Low, err = strconv.ParseFloat(datas[4], 64) if err != nil { return nil, err } //market.AllVolume, err = strconv.ParseFloat(datas[8], 64) //if err != nil { //return nil, err //} //market.AllAmount, err = strconv.ParseFloat(datas[9], 64) //if err != nil { //return nil, err //} bids := make([]PP, 5) //bids[0][1], err = strconv.ParseFloat(datas[10], 64) //if err != nil { //return nil, err //} bids[0][0], err = strconv.ParseFloat(datas[7], 64) if err != nil { return nil, err } //bids[1][1], err = strconv.ParseFloat(datas[12], 64) //if err != nil { //return nil, err //} //bids[1][0], err = strconv.ParseFloat(datas[13], 64) //if err != nil { //return nil, err //} //bids[2][1], err = strconv.ParseFloat(datas[14], 64) //if err != nil { //return nil, err //} //bids[2][0], err = strconv.ParseFloat(datas[15], 64) //if err != nil { //return nil, err //} //bids[3][1], err = strconv.ParseFloat(datas[16], 64) //if err != nil { //return nil, err //} //bids[3][0], err = strconv.ParseFloat(datas[17], 64) //if err != nil { //return nil, err //} //bids[4][1], err = strconv.ParseFloat(datas[18], 64) //if err != nil { //return nil, err //} //bids[4][0], err = strconv.ParseFloat(datas[19], 64) //if err != nil { //return nil, err //} market.Bids = bids asks := make([]PP, 5) //asks[0][1], err = strconv.ParseFloat(datas[6], 64) //if err != nil { //return nil, err //} asks[0][0], err = strconv.ParseFloat(datas[6], 64) if err != nil { return nil, err } //asks[1][1], err = strconv.ParseFloat(datas[22], 64) //if err != nil { //return nil, err //} //asks[1][0], err = strconv.ParseFloat(datas[23], 64) //if err != nil { //return nil, err //} //asks[2][1], err = strconv.ParseFloat(datas[24], 64) //if err != nil { //return nil, err //} //asks[2][0], err = strconv.ParseFloat(datas[25], 64) //if err != nil { //return nil, err //} //asks[3][1], err = strconv.ParseFloat(datas[26], 64) //if err != nil { //return nil, err //} //asks[3][0], err = strconv.ParseFloat(datas[27], 64) //if err != nil { //return nil, err //} //asks[4][1], err = strconv.ParseFloat(datas[28], 64) //if err != nil { //return nil, err //} //asks[4][0], err = strconv.ParseFloat(datas[29], 64) //if err != nil { //return nil, err //} market.Asks = asks //t, err := time.Parse(time.RFC3339, datas[30]+"T"+datas[31]+"+08:00") //if err != nil { //return nil, err //} //market.Timestamp = t.Unix() * 1000 market.Timestamp = time.Now().Unix() * 1000 //name = name[2:] //market.InsId, err = strconv.ParseInt(name, 10, 64) //if err != nil { //return nil, err //} var id int var ok bool if id, ok = SinaFutureSymbols[name]; !ok { return nil, err } market.InsId = 1000000 + int64(id) market.Type = IntSinaFuture return &market, nil }