PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/EODData/Perl/GetEODData.pl

http://quantkit.googlecode.com/
Perl | 266 lines | 204 code | 26 blank | 36 comment | 1 complexity | e0c29d956d02c7e1436ee77c73d17420 MD5 | raw file
  1. # Copyright (c) <2010> <Nick A Torenvliet>
  2. #
  3. # Permission is hereby granted, free of charge, to any person
  4. # obtaining a copy of this software and associated documentation
  5. # files (the "Software"), to deal in the Software without
  6. # restriction, including without limitation the rights to use,
  7. # copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. # copies of the Software, and to permit persons to whom the
  9. # Software is furnished to do so, subject to the following
  10. # conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be
  13. # included in all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  17. # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  19. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20. # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. # OTHER DEALINGS IN THE SOFTWARE.
  23. #!/usr/bin/perl
  24. use File::Copy;
  25. use File::Path;
  26. use DBI;
  27. sub init(){
  28. #get your options here
  29. use Getopt::Long;
  30. my $opt_string = 'hu=sp=sd=sv=sq=s:';
  31. GetOptions( "u=s" => \$userName, "p=s" => \$passWord,"d=s" => \$dbName,"v=s" => \$mySQLUserName,"q=s" => \$mySQLPassWord,"h" ) or usage();
  32. usage() if $opt_h;
  33. usage() unless($dbName);
  34. usage() unless($mySQLPassWord);
  35. usage() unless($userName);
  36. usage() unless($passWord);
  37. }
  38. sub usage(){
  39. print STDERR << "EOF";
  40. $0 logs into ftp.eoddata.com and downloads daily information.
  41. Market and fundamental indicator data are inserted into db tables as specified
  42. and split, names and technical indicators stay in text file format.
  43. All text files are archived.
  44. -h : this (help) message
  45. -u : www.eoddata.com username - required
  46. -p : www.eoddata.com password - required
  47. -d : local MySQL database name - required
  48. -v : local MySQL database user name - required
  49. -q : local MySQL database password - required
  50. example: $0 -u myEODDataUserName -p myEODDataPassWord -d myLocalMySQLDataBase -v myLocalMySQLDBUserName -p myLocalMySQLDBPassWord
  51. EOF
  52. exit;
  53. }
  54. sub getEODData(){
  55. do ".config";
  56. chdir("$BASEDIR");
  57. chdir("Data");
  58. system("wget -r -v -nc --ftp-user=$userName --ftp-password=$passWord ftp://ftp.eoddata.com/");
  59. mkdir("ftp.eoddata.com/Archive");
  60. mkdir("ftp.eoddata.com/Archive/$date");
  61. unlink("ftp.eoddata.com/terms.txt");
  62. unlink("ftp.eoddata.com/readme.txt");
  63. rmtree("ftp.eoddata.com/Software");
  64. }
  65. sub insertFundamentalsData(){
  66. my $dbh = DBI->connect('DBI:mysql:markets:localhost',$mySQLUserName,$mySQLPassWord)
  67. or die "Couldn't connect to database: " . DBI->errstr;
  68. chdir("ftp.eoddata.com");
  69. chdir("Fundamentals");
  70. unlink("terms.txt");
  71. unlink("readme.txt");
  72. @files = <*>;
  73. foreach $file (@files) {
  74. if($file =~ /.txt/){
  75. @marketString = split("\\.",$file);
  76. open(FH,$file);
  77. my $i = 0;
  78. foreach $line (<FH>) {
  79. if($i!=0){
  80. @dataString = split(/\t/,$line);
  81. my $sth = $dbh->prepare("INSERT INTO fundamentals (date,market,symbol,name,sector,industry,PE,EPS,divYield,shares,DPS,PEG,PTS,PTB) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  82. or die "Couldn't prepare statement: " . $dbh->errstr;
  83. $sth->execute($date,@marketString[0],@dataString[0],@dataString[1],@dataString[2],@dataString[3],@dataString[4],@dataString[5],@dataString[6],@dataString[7],@dataString[8],@dataString[9],@dataString[10],@dataString[11]);
  84. }
  85. $i++;
  86. }
  87. close(FH);
  88. }
  89. }
  90. $dbh->disconnect;
  91. #move the directory into the archives and drop back into EODData
  92. chdir("..");
  93. mkdir("Archive/$date");
  94. mkdir("Archive/$date/Fundamentals");
  95. move("Fundamentals","Archive/$date/Fundamentals");
  96. chdir("..");
  97. }
  98. sub insertTechnicalsData(){
  99. print "here\n";
  100. my $dbh = DBI->connect('DBI:mysql:markets:localhost',$mySQLUserName,$mySQLPassWord)
  101. or die "Couldn't connect to database: " . DBI->errstr;
  102. chdir("ftp.eoddata.com");
  103. chdir("Technical");
  104. unlink("terms.txt");
  105. unlink("readme.txt");
  106. @files = <*>;
  107. foreach $file (@files) {
  108. if($file =~ /.txt/){
  109. @marketString = split("\\.",$file);
  110. open(FH,$file);
  111. my $i = 0;
  112. foreach $line (<FH>) {
  113. if($i!=0){
  114. @dataString = split(/\t/,$line);
  115. my $sth = $dbh->prepare("INSERT INTO technicals (date,market,symbol,previous,delta,volumeChange,weekHigh,weekLow,weekChange,avgWeekChange,avgWeekVolume,monthHigh,monthLow,monthChange,avgMonthChange,avgMonthVolume,yearHigh,yearLow,yearChange,avgYearChange,avgYearVolume,MA5,MA20,MA50,MA100,MA200,RSI14,STO9,WPR14,MTM14,ROC14,PTC) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  116. or die "Couldn't prepare statement: " . $dbh->errstr;
  117. $sth->execute($date,@marketString[0],@dataString[0],@dataString[1],@dataString[2],@dataString[3],@dataString[4],@dataString[5],@dataString[6],@dataString[7],@dataString[8],@dataString[9],@dataString[10],@dataString[11],@dataString[12],@dataString[13],@dataString[14],@dataString[15],@dataString[16],@dataString[17],@dataString[18],@dataString[19],@dataString[20],@dataString[21],@dataString[22],@dataString[23],@dataString[24],@dataString[25],@dataString[26],@dataString[27],@dataString[28],@dataString[29]);
  118. }
  119. $i++;
  120. }
  121. close(FH);
  122. }
  123. }
  124. $dbh->disconnect;
  125. #move the directory into the archives and drop back into EODData
  126. chdir("..");
  127. mkdir("Archive/$date");
  128. mkdir("Archive/$date/Technical");
  129. move("Technical","Archive/$date/Technical");
  130. chdir("..");
  131. }
  132. sub insertSplitsData(){
  133. my $dbh = DBI->connect('DBI:mysql:markets:localhost',$mySQLUserName,$mySQLPassWord)
  134. or die "Couldn't connect to database: " . DBI->errstr;
  135. chdir("ftp.eoddata.com");
  136. chdir("Splits");
  137. unlink("terms.txt");
  138. unlink("readme.txt");
  139. @files = <*>;
  140. foreach $file (@files) {
  141. if($file =~ /.txt/){
  142. @marketString = split("\\.",$file);
  143. open(FH,$file);
  144. my $i = 0;
  145. foreach $line (<FH>) {
  146. if($i!=0){
  147. @dataString = split(/\t/,$line);
  148. my $sth = $dbh->prepare("INSERT INTO splits (date,market,symbol,ratio) values (?,?,?,?)")
  149. or die "Couldn't prepare statement: " . $dbh->errstr;
  150. $sth->execute(@dataString[1],@marketString[0],@dataString[0],@dataString[2]);
  151. }
  152. $i++;
  153. }
  154. close(FH);
  155. }
  156. }
  157. $dbh->disconnect;
  158. #move the directory into the archives and drop back into EODData
  159. chdir("..");
  160. mkdir("Archive/$date");
  161. mkdir("Archive/$date/Splits");
  162. move("Splits","Archive/$date/Splits");
  163. chdir("..");
  164. }
  165. sub insertNamesData(){
  166. my $dbh = DBI->connect('DBI:mysql:markets:localhost',$mySQLUserName,$mySQLPassWord)
  167. or die "Couldn't connect to database: " . DBI->errstr;
  168. chdir("ftp.eoddata.com");
  169. chdir("Names");
  170. unlink("terms.txt");
  171. unlink("readme.txt");
  172. @files = <*>;
  173. foreach $file (@files) {
  174. if($file =~ /.txt/){
  175. @marketString = split("\\.",$file);
  176. open(FH,$file);
  177. my $i = 0;
  178. foreach $line (<FH>) {
  179. if($i!=0){
  180. @dataString = split(/\t/,$line);
  181. my $sth = $dbh->prepare("INSERT INTO names (market,symbol,name,date) values (?,?,?,?)")
  182. or die "Couldn't prepare statement: " . $dbh->errstr;
  183. $sth->execute(@marketString[0],@dataString[0],@dataString[1],$date);
  184. }
  185. $i++;
  186. }
  187. close(FH);
  188. }
  189. }
  190. $dbh->disconnect;
  191. #move the directory into the archives and drop back into EODData
  192. chdir("..");
  193. mkdir("Archive/$date");
  194. mkdir("Archive/$date/Names");
  195. move("Names","Archive/$date/Names");
  196. chdir("..");
  197. }
  198. sub insertEODData(){
  199. my $dbh = DBI->connect('DBI:mysql:markets:localhost',$mySQLUserName,$mySQLPassWord)
  200. or die "Couldn't connect to database: " . DBI->errstr;
  201. chdir("ftp.eoddata.com");
  202. @files = <*>;
  203. foreach (@files) {
  204. if($_ =~ /.txt/){
  205. @marketString = split(/_/,$_);
  206. open(FH,$_);
  207. my $i = 0;
  208. foreach $line (<FH>) {
  209. if($i!=0){
  210. @dataString = split(/,/,$line);
  211. my $sth = $dbh->prepare("INSERT INTO endOfDayData (date,market,symbol,open,high,low,close,volume) values (?,?,?,?,?,?,?,?)")
  212. or die "Couldn't prepare statement: " . $dbh->errstr;
  213. $sth->execute(@dataString[1],@marketString[0],@dataString[0],@dataString[2],@dataString[3],@dataString[4],@dataString[5],@dataString[6]);
  214. }
  215. $i++;
  216. }
  217. close(FH);
  218. move("$_","./History/$_");
  219. }
  220. }
  221. $dbh->disconnect;
  222. }
  223. #parse parameters
  224. init();
  225. #Provide us a global date just in case we are working near midnight.
  226. ($Second, $Minute, $Hour, $Day, $Month, $Year, $WeekDay, $DayOfYear, $IsDST) = localtime(time);
  227. local $date = sprintf '%02d%02d%02d', $Year +1900, $Month +1 , $Day;
  228. #get data from ftp.eoddata.com
  229. getEODData;
  230. #insert end of day market data
  231. insertEODData;
  232. #insert fundamental indicators data
  233. insertFundamentalsData;
  234. #insert technical indicators data
  235. insertTechnicalsData;
  236. #insert splits data
  237. insertSplitsData;
  238. #insert names data
  239. insertNamesData;