分类
互联网

Mathematica绘制美元对人民币走势图

startdate = "12/31/1980";
enddate = DateString[{"Month", "/", "Day", "/", "Year"}];

fxdownload = 
  [email protected][
    "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[[email protected][#, "-"], {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];
L[email protected]$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"}];*)

[email protected]["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[[email protected][#,"-"],{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"}]]]

发表回复

您的电子邮箱地址不会被公开。

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据