Mathematica绘制美元对人民币走势图
startdate = "12/31/1980";
enddate = DateString[{"Month", "/", "Day", "/", "Year"}];
fxdownload =
Import@StringJoin[
"http://www.federalreserve.gov/datadownload/Output.aspx?rel=H10&\
series=356f2a973bbb516442c693dc19615b69&lastObs=&from=", startdate,
"&to=", enddate,
"&filetype=csv&label=include&layout=seriescolumn"];
fxdata = Drop[fxdownload, 6];
datelistfx = Cases[fxdata, {_, _?NumberQ}];
datelistfx[[All, 1]] =
Join[ToExpression@StringSplit[#, "-"], {0, 0, 0.}] & /@
datelistfx[[All, 1]];
DateListPlot[datelistfx, Joined -> True,
PlotLabel ->
StringJoin["Daily CNY/USD from ",
DateString[datelistfx[[1, 1]], {"Day", "/", "Month", "/", "Year"}],
" to ",
DateString[
datelistfx[[-1, 1]], {"Day", "/", "Month", "/", "Year"}]]]
参考:http://mathematica.stackexchange.com/a/46841.
一个完整的例子
startdate="2015-01-10";
enddate="2015-01-12";
name="1336";
$initialUrl="http://srh.bankofchina.com/search/whpj/searchen.jsp";
$dom=Import[$initialUrl,"XMLObject"];
(*$forms=Cases[$dom,XMLElement["form",___],Infinity];
Length@$forms*)
(*Cases[$forms,XMLElement[_,attrs_,_]\[RuleDelayed]attrs]*)
(*Cases[$forms[[1]],XMLElement["input",___],Infinity]//Column*)
(*$parameters={"erectDate"\[Rule]startdate,"nothing"\[Rule]enddate,"pjname"\[Rule]name};
header=Import[$initialUrl,"Data"
,"RequestMethod"\[Rule]"POST"
,"RequestParameters"\[Rule]$parameters][[2,1]]//TableForm;
$parameters={"erectDate"\[Rule]startdate,"nothing"\[Rule]enddate,"pjname"\[Rule]name,"page"\[Rule]ToString[1]};
$results=Import[$initialUrl,"Data"
,"RequestMethod"\[Rule]"POST"
,"RequestParameters"\[Rule]$parameters][[2,2;;]];
$results//ColumnForm;
DateListPlot[TimeSeries[$results[[;;,2]]/100,{$results[[;;,7]]}]];
Export["boc"<>startdate<>"_"<>enddate<>"_"<>Block[{$DateStringFormat={"Year","-","Month","-","Day","-","Hour","-","Minute"}},
DateString[]]<>".xls",$results,"xls"]*)
For[i=0;res={},i< =45,i++;
{$parameters={"erectDate"->startdate,"nothing"->enddate,"pjname"->name,"page"->ToString[i]};
$results=Import[$initialUrl,"Data"
,"RequestMethod"->"POST"
,"RequestParameters"->$parameters][[2,2;;]];
res=res~Join~$results}];
res//Length
Export["boc"<>startdate<>"_"<>enddate<>"_"<>Block[{$DateStringFormat={"Year","-","Month","-","Day","-","Hour","-","Minute"}},
DateString[]]<>".xls",res,"xls"]
buyingrate=res[[;;,2]]/100;
timeseq=res[[;;,7]];
ts=TimeSeries[buyingrate,{timeseq}]
DateListPlot[ts,Filling->Axis]
DateListPlot[ts,Joined->True,Filling->Bottom,ImageSize->350]
(*startdate="12/31/1980";
enddate=DateString[{"Month","/","Day","/","Year"}];*)
fxdownload=Import@StringJoin["http://www.federalreserve.gov/datadownload/Output.aspx?rel=H10&series=356f2a973bbb516442c693dc19615b69&lastObs=&from=",startdate,"&to=",enddate,"&filetype=csv&label=include&layout=seriescolumn"];
fxdata=Drop[fxdownload,6];
datelistfx=Cases[fxdata,{_,_?NumberQ}];
datelistfx[[All,1]]=Join[ToExpression@StringSplit[#,"-"],{0,0,0.}]&/@datelistfx[[All,1]];
DateListPlot[datelistfx,Joined->True,PlotLabel->StringJoin["Daily CNY/USD from ",DateString[datelistfx[[1,1]],{"Day","/","Month","/","Year"}]," to ",DateString[datelistfx[[-1,1]],{"Day","/","Month","/","Year"}]]]
本作品采用创作共用版权协议, 要求署名、非商业用途和保持一致. 转载本站内容必须也遵循署名-非商业用途-保持一致的创作共用协议.

发表回复