基金投资入门txt,基金投资基础txt电子书下载

  

  本系列以前的文章:   

  

  使用某基金网站的API查询基金净值历史数据如何使用程序将某基金的历史净值导入本地进行分析在前文中,我们已经通过分页API调用将田甜基金在线上某基金的历史净值全部下载到本地:   

  

     

  

  用分号分隔的四个字段是:   

  

  历史日期的基金单位净值,当日基金累计净值的增减,每个文本文件存储一页API调用结果。   

  

  为了方便接下来的统计,我使用nodejs将这些小文本文件合并成一个大文本文件。   

  

  这个逻辑位于函数mergeFiles中。首先调用fs.readdirSync,通过同步读取的方式读出data文件夹中的所有文件名,存储在array subs中。然后,遍历这个数组,读出每个文本文件的所有内容,并将它们存储在变量sWholeContent中。这些文件的内容由换行符分隔\n,所以在parseFileContent函数中,我首先使用\n来反汇编每一行的内容:   

  

     

  

  然后使用“;”作为分隔符,读出当日基金净值的历史数据:   

  

     

  

  最终合并的单个文本文件如下图所示:   

  

     

  

  完整的代码如下:   

  

  const fs=require(' fs ');var ValuePerDay=require('./model/valueperday . js ');var fileWriteTool=require('。/file write tool . js’);/*return:包含基金价值数据的数组*/function merge files(sFundCode){ let pwd=process . CWD()'/data ';设sMergedFilePath=pwd '/' sFundCode '。txt ';let a result=;设subs=fs . readdirsync(pwd);//我们不需要显式排序,因为readdirSync//在文件系统中使用相同的顺序for(var I=0;I子长度;I){ let sFilePath=pwd '/' subs;设swhole content=fs . read file sync(sFilePath,' utf-8 ');a result . push(parsefile content(sWholeContent));}fileWriteTool(aResult,sMergedFilePath);}函数parse file content(s content){ const aLines=s content . split(' \ n ');for(var I=0;i aLines .长度;I){ const aFundValue=alines . split(';'));const of und value=new value perday(afundvalue 0,aFundValue1,aFundValue2,format rate(afundvalue 3));返回最终值;} }函数format rate(sRaw){ if(sRaw===' '){ return 0;} return parse float(sraw . replace(' % ',' ');} module.exports=mergeFiles   

相关文章