再接着就是从上面的清单中,抓取这些标的的历史日K数据
请注意这个日K数据是设置某一个当前日期,然后倒推的。 设置天数为负数,比如-180天- use utf8;
- use Encode;
- use Modern::Perl;
- use File::Slurp;
- use File::Path qw/make_path/;
- use Mojo::UserAgent;
- use Date::Format; # time2str
- use Date::Parse; # str2time
- use JSON qw/from_json to_json/;
- STDOUT->autoflush(1);
-
- my $wdir = "D:/Local/雪球数据/股票数据-日K-20230302-180DAY";
- make_path( gbk($wdir) ) unless -d gbk($wdir);
-
- # 提取某个日期之前的日K数据
- my %args = (
- 'symbol' => '',
- 'begin' => time() * 1000 + 3600*24,
- 'period' => 'day',
- 'type' => 'before',
- 'count' => '-185', # 倒推天数
- 'indicator' => 'kline,pe,pb,ps,pcf,market_capital,agt,ggt,balance',
- );
-
- my $ua = Mojo::UserAgent->new();
-
- $ua->cookie_jar->add(
- Mojo::Cookie::Response->new(
- name => "xq_a_token",
- value => "这里改成自己的token",
- domain => 'stock.xueqiu.com',
- path => '/',
- )
- );
-
- my @list = read_file("StockList.txt"); # "ETF.txt"
- my $n = 0;
- for my $e ( @list )
- {
- $n++;
- $e =~ s/\r?\n//;
- my ( $code, $name ) = split(/\s+/, uni($e) );
- my $export = gbk("${wdir}/${code}.json");
-
- # 忽略科创板和创业板
- next if $code =~ /[A-Z]+(688|300)/;
-
- if ( -f $export )
- {
- printf "[%d/%d] %s %s already exists\n", $n, scalar(@list), $code, gbk( $name );
- next;
- }
-
- printf "[%d/%d] %s %s\n", $n, scalar(@list), $code, gbk( $name );
- $args{'symbol'} = $code;
- my $res = $ua->get( "https://stock.xueqiu.com/v5/stock/chart/kline.json", form => \%args )->result;
- my $data = $res->json;
- if ( $data->{'error_code'} == 0 )
- {
- $data->{'data'}{'name'} = $name;
- write_file( $export, utf8( to_json( $data) ) );
- }
- }
-
- sub gbk { encode('gbk', $_[0]) }
- sub utf8 { encode('utf8', $_[0]) }
- sub u2gbk { encode('gbk', decode('utf8', $_[0])) }
- sub uni { decode('utf8', $_[0]) }
复制代码
|