pu369com

go csv 处理 - 下载股票历史数据

感谢网易,历史数据下载(CSV格式)

下面是获取工商银行0601398,从2014年07月20日到2015年05月08日之间的历史数据,文件为CSV格式

http://quotes.money.163.com/service/chddata.html?code=0601398&start=20140720&end=20150508

code表示股票代码, 上海股票最前边加0,深圳股票最前边加1

创业版股票代码是以3开头的,为深圳股票,所以code=1300142.

上海股票代码都是以6开头的,code=0601398.

深圳股票代码非创业版股票以0开头,code=1000089.
————————————————
下面,用代码

从http://quotes.money.163.com/service/chddata.html?code=1000002&start=20190102&end=20200608

访问csv并打印输出

package main

import (
    "bytes"
    "encoding/csv"
    "fmt"
    "io/ioutil"
    "net/http"
    "strconv"
    "strings"
    "time"
)

func main() {
    now := time.Now()
    end := now.Format("20060102")
    start := now.AddDate(0, 0, -120).Format("20060102")
    url := "http://quotes.money.163.com/service/chddata.html?code=0600519&start=" + start + "&end=" + end
    req, _ := http.NewRequest("GET", url, nil)
    // 设置header
    req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 6.1;WOW64) AppleWebKit/537.36 (KHTML,like GeCKO) Chrome/45.0.2454.85 Safari/537.36 115Broswer/6.0.3")
    req.Header.Set("Referer", "https://movie.douban.com/")
    req.Header.Set("Connection", "keep-alive")
    resp, err := (&http.Client{}).Do(req)
    if err != nil {
        panic(err)
    }
    csv1, _ := ioutil.ReadAll(resp.Body)
    r2 := csv.NewReader(strings.NewReader(string(csv1)))
    ss, _ := r2.ReadAll()

    sz := len(ss)
    buf := new(bytes.Buffer)
    maxv := 0.0
    minv := 4096.0
    todayp, _ := strconv.ParseFloat(ss[1][3], 64)
    for i := 1; i < sz; i++ { //第0行是标题,从第1行取数据
        buf.WriteString(" " + ss[i][3] + " ")
        buf.WriteString("\r\n")
        curp, _ := strconv.ParseFloat(ss[i][3], 64)
        if curp > maxv {
            maxv = curp
        }
        if curp < minv {
            minv = curp
        }
    }
    fmt.Println(buf.String())
    fmt.Printf("最大:%.2f", maxv)
    fmt.Printf("最小:%.2f", minv)
    fmt.Printf("今日:%.2f", todayp)

}

 

 

 

练练手而已。

当然,用通达信自编指标会更方便。

不要迷信指标。

 

 

参考:

https://blog.csdn.net/u011331731/article/details/101353475

https://blog.csdn.net/fengmm521/article/details/78446501

https://www.cnblogs.com/lj0019/p/7992320.html

https://blog.csdn.net/weixin_44766484/article/details/105802794

https://www.cnblogs.com/zhangym/p/6142738.html

posted on 2020-06-08 17:16  pu369com  阅读(516)  评论(0编辑  收藏  举报

导航