Changeset 1385 in algoquant
- Timestamp:
- 3/15/2013 8:35:58 PM (11 months ago)
- Location:
- core
- Files:
-
- 8 edited
-
src/main/java/com/numericalmethod/algoquant/config/ConfigProperties.java (modified) (1 diff)
-
src/main/java/com/numericalmethod/algoquant/data/calendar/holiday/HolidaysFromFile.java (modified) (8 diffs)
-
src/main/resources/config/data.properties (modified) (1 diff)
-
src/main/resources/config/haksunli/haksunli.properties (modified) (1 diff)
-
src/test/java/com/numericalmethod/algoquant/config/AlgoQuantConfigTest.java (modified) (5 diffs)
-
src/test/java/com/numericalmethod/algoquant/data/calendar/holiday/HolidaysFromFileTest.java (modified) (8 diffs)
-
test-data/config/data.properties (modified) (1 diff)
-
test-data/config/data2.properties (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
core/src/main/java/com/numericalmethod/algoquant/config/ConfigProperties.java
r1366 r1385 49 49 * The data folder location that stores holiday data files. 50 50 */ 51 HOLIDAY_DIR("HOLIDAY_DIR");51 DATA_HOLIDAYS("DATA_HOLIDAYS"); 52 52 private final String propertyName; 53 53 -
core/src/main/java/com/numericalmethod/algoquant/data/calendar/holiday/HolidaysFromFile.java
r1366 r1385 48 48 public class HolidaysFromFile implements Holidays { 49 49 50 private static final String HOLIDAY_DIR = AlgoQuantConfig.getConfiguration().getString( 51 ConfigProperties.HOLIDAY_DIR.getPropertyName(), "./log/holidays"); 50 private static final Logger LOGGER = getClassLogger(); 51 public static final String DEFAULT_HOLIDAYS_DIRNAME = "./log/holidays"; 52 private static final String HOLIDAY_DIRNAME = AlgoQuantConfig.getConfiguration().getString( 53 ConfigProperties.DATA_HOLIDAYS.getPropertyName(), DEFAULT_HOLIDAYS_DIRNAME); 52 54 private static final String MATCHING_PATTERN = "^%s(\\d{4})-(\\d{2})-(\\d{2})~(\\d{4})-(\\d{2})-(\\d{2}).csv.zip$"; 53 55 private static final String DATETIME_FORMAT = "yyyy-MM-dd"; 54 56 private static final DateTimeFormatter DATETIME_FORMATTER = DateTimeFormat.forPattern(DATETIME_FORMAT); 55 private static final Logger logger = getClassLogger();56 57 private final ExchangeIndex exchangeIndex; 58 private final boolean updateFromWeb; 57 59 private String holidayFileName = null; 58 60 private Set<DateMidnight> holidaysSet = null; 59 61 60 62 public HolidaysFromFile(ExchangeIndex exchangeIndex) { 63 this(exchangeIndex, true); 64 } 65 66 public HolidaysFromFile(ExchangeIndex exchangeIndex, boolean updateFromWeb) { 61 67 this.exchangeIndex = exchangeIndex; 68 this.updateFromWeb = updateFromWeb; 62 69 } 63 70 64 71 private boolean createHolidayFile(DateMidnight endDate) { 72 if (!updateFromWeb) { 73 LOGGER.info(String.format("holiday file is not updated from web for %s on %s", 74 exchangeIndex, endDate)); 75 return false; 76 } 65 77 66 78 try { … … 72 84 holidayFileName = getFileName(exchangeIndex, beginDate, endDate); 73 85 74 saveHolidaysFile(weekdayHolidays, HOLIDAY_DIR , holidayFileName);86 saveHolidaysFile(weekdayHolidays, HOLIDAY_DIRNAME, holidayFileName); 75 87 holidaysSet = getHolidaysFromFile(); 76 88 77 89 } catch (Exception ex) { 78 logger.info(String.format("Error updating holiday file, reason = %s", ex.getMessage())); 90 LOGGER.error(String.format( 91 "Error updating holiday file, reason = %s", ex.getMessage())); 79 92 return false; 80 93 } … … 95 108 96 109 private boolean isHolidayFileExist() { 97 File path = new File(HOLIDAY_DIR );110 File path = new File(HOLIDAY_DIRNAME); 98 111 if (!path.exists()) { 99 112 return false; … … 103 116 @Override 104 117 public boolean accept(File dir, String name) { 105 return Pattern.matches(String.format(MATCHING_PATTERN, getFilePrefix(exchangeIndex)), name); 118 return Pattern.matches( 119 String.format(MATCHING_PATTERN, getFilePrefix(exchangeIndex)), name); 106 120 } 107 121 }); … … 116 130 117 131 private boolean isHolidayFileUpdate(DateMidnight queryDate) { 118 119 132 if (holidayFileName == null) { 120 133 return false; 121 134 } 122 135 123 Pattern pattern = Pattern.compile(String.format(MATCHING_PATTERN, getFilePrefix(exchangeIndex))); 136 Pattern pattern = Pattern.compile( 137 String.format(MATCHING_PATTERN, getFilePrefix(exchangeIndex))); 124 138 Matcher matcher = pattern.matcher(holidayFileName); 125 139 matcher.matches(); … … 138 152 139 153 if (queryDate.isBefore(fromDate) || queryDate.isAfter(toDate)) { 140 new File(String.format("%s/%s", HOLIDAY_DIR , holidayFileName)).delete();154 new File(String.format("%s/%s", HOLIDAY_DIRNAME, holidayFileName)).delete(); 141 155 holidayFileName = null; 142 156 return false; … … 152 166 if (!isQueryDateInRange(queryDateMidnight)) { 153 167 // If query date outside data file range, update is not success 154 logger.warn(String.format("Query date (%s) outside data file range (%s): %s ~ %s",168 LOGGER.warn(String.format("Query date (%s) outside data file range (%s): %s ~ %s", 155 169 queryDateMidnight.toString(DATETIME_FORMAT), 156 170 getFilePrefix(exchangeIndex), … … 183 197 184 198 HolidayCsvZipFileReader reader; 199 String fileLocation = String.format("%s/%s", HOLIDAY_DIRNAME, holidayFileName); 185 200 try { 186 reader = new HolidayCsvZipFileReader( 187 String.format("%s/%s", HOLIDAY_DIR, holidayFileName)); 201 reader = new HolidayCsvZipFileReader(fileLocation); 188 202 } catch (Exception ex) { 189 logger.info(String.format("Error reading holiday file: %s", holidayFileName));203 LOGGER.info(String.format("Error reading holiday file: %s", fileLocation)); 190 204 return holidays; 191 205 } -
core/src/main/resources/config/data.properties
r1382 r1385 5 5 DATA_YAHOO = ${DATA_FOLDER}/yahoo 6 6 DATA_SGX = ${DATA_FOLDER}/raw/sgx 7 HOLIDAY_DIR = ./resources/holidays7 DATA_HOLIDAYS = ./log/holidays -
core/src/main/resources/config/haksunli/haksunli.properties
r1382 r1385 7 7 8 8 #These are the compustat files of hong kong stock data. 9 COMPUSTAT_HONGKONG_COMPANY = C:/Users/Haksun Li/Documents/Businesses/Numerical Method/data/raw/compustat/hongkong/company.csv.zip 10 COMPUSTAT_HONGKONG_ACCOUNTING = C:/Users/Haksun Li/Documents/Businesses/Numerical Method/data/raw/compustat/hongkong/monthlyAccounting.1995.2010.csv.zip 11 COMPUSTAT_HONGKONG_DAILY_RETURNS = C:/Users/Haksun Li/Documents/Businesses/Numerical Method/data/raw/compustat/hongkong/dailyReturn.1995.2011.csv.zip 9 DATA_COMPUSTAT = ${DATA_FOLDER}/raw/compustat 10 COMPUSTAT_HONGKONG_COMPANY = ${DATA_COMPUSTAT}/hongkong/company.csv.zip 11 COMPUSTAT_HONGKONG_ACCOUNTING = ${DATA_COMPUSTAT}/hongkong/monthlyAccounting.1995.2010.csv.zip 12 COMPUSTAT_HONGKONG_DAILY_RETURNS = ${DATA_COMPUSTAT}/hongkong/dailyReturn.1995.2011.csv.zip 12 13 13 14 #This folder contains the data downloaded from the HKMA website. 14 HKMA = C:/Users/Haksun Li/Documents/Businesses/Numerical Method/data/raw/HKMA15 HKMA = ${DATA_FOLDER}/raw/HKMA 15 16 -
core/src/test/java/com/numericalmethod/algoquant/config/AlgoQuantConfigTest.java
r1366 r1385 23 23 package com.numericalmethod.algoquant.config; 24 24 25 import org.junit.AfterClass; 25 26 import static org.junit.Assert.*; 27 import org.junit.BeforeClass; 26 28 import org.junit.Test; 27 29 … … 31 33 */ 32 34 public class AlgoQuantConfigTest { 35 36 public static String currentConfigFile; 37 38 @BeforeClass 39 public static void saveConfigFile() { 40 currentConfigFile = AlgoQuantConfig.getConfiguration().getConfigFilePath(); 41 } 42 43 @AfterClass 44 public static void restoreConfigFile() { 45 AlgoQuantConfig.getConfiguration().setConfigFile(currentConfigFile); 46 } 47 33 48 34 49 @Test … … 36 51 AlgoQuantConfig instance = AlgoQuantConfig.getConfiguration(); 37 52 instance.setConfigFile("config.default.xml"); 38 String HOLIDAY_DIR = instance.getString(ConfigProperties. HOLIDAY_DIR.getPropertyName());39 assertEquals("./ resources/holidays", HOLIDAY_DIR);53 String HOLIDAY_DIR = instance.getString(ConfigProperties.DATA_HOLIDAYS.getPropertyName()); 54 assertEquals("./log/holidays", HOLIDAY_DIR); 40 55 } 41 56 … … 43 58 public void testChangeConfigFile() { 44 59 AlgoQuantConfig instance = AlgoQuantConfig.getConfiguration(); 45 assertEquals("./ resources/holidays", instance.getString(ConfigProperties.HOLIDAY_DIR.getPropertyName()));60 assertEquals("./log/holidays", instance.getString(ConfigProperties.DATA_HOLIDAYS.getPropertyName())); 46 61 47 62 instance.setConfigFile("./test-data/config/test.config.xml"); 48 63 AlgoQuantConfig instance2 = AlgoQuantConfig.getConfiguration(); 49 assertEquals("./test-data", instance2.getString(ConfigProperties. HOLIDAY_DIR.getPropertyName()));64 assertEquals("./test-data", instance2.getString(ConfigProperties.DATA_HOLIDAYS.getPropertyName())); 50 65 51 66 assertEquals(instance, instance2); … … 56 71 AlgoQuantConfig instance = AlgoQuantConfig.getConfiguration(); 57 72 instance.setConfigFile("config.default.xml"); 58 assertEquals("./ resources/holidays", instance.getString(ConfigProperties.HOLIDAY_DIR.getPropertyName()));73 assertEquals("./log/holidays", instance.getString(ConfigProperties.DATA_HOLIDAYS.getPropertyName())); 59 74 60 75 instance.setString("key", "value"); -
core/src/test/java/com/numericalmethod/algoquant/data/calendar/holiday/HolidaysFromFileTest.java
r1374 r1385 24 24 25 25 import com.numericalmethod.algoquant.config.AlgoQuantConfig; 26 import com.numericalmethod.algoquant.config.ConfigProperties; 26 27 import com.numericalmethod.algoquant.data.calendar.TimeZoneUtils; 27 28 import static com.numericalmethod.nmutils.NMUtils.*; … … 50 51 public static void setUpClass() throws Exception { 51 52 AlgoQuantConfig instance = AlgoQuantConfig.getConfiguration(); 52 instance.setString("HOLIDAY_DIR", HOLIDAY_DIR); 53 NMIO.newFolder(HOLIDAY_DIR); 53 instance.setString(ConfigProperties.DATA_HOLIDAYS.getPropertyName(), HOLIDAY_DIR); 54 54 } 55 55 56 56 @Test 57 57 public void testDateOutOfRange() throws IOException { 58 59 58 //US 60 59 DateTime queryDate = JodaTimeUtils.getDate(1929, 12, 31, ExchangeIndex.NYSE.getTimeZone()); … … 75 74 @Test 76 75 public void testBuildHolidayFile() { 77 clearHolidayDir();76 NMIO.deleteDir(new File(HOLIDAY_DIR)); 78 77 79 78 //US … … 96 95 @Test 97 96 public void testUpdateExistingHolidayFile() throws IOException { 98 clearHolidayDir();97 NMIO.deleteDir(new File(HOLIDAY_DIR)); 99 98 100 99 //US … … 140 139 @Test 141 140 public void testHolidayFileExistandUpdate() { 142 143 141 //US 144 142 DateTime queryDate = JodaTimeUtils.getDate(2011, 11, 24, ExchangeIndex.NYSE.getTimeZone()); … … 166 164 } 167 165 168 // @Test 166 // @Test // TODO: fix it 169 167 public void testOffline() { 170 // Disable network interface before running this test171 clearHolidayDir();172 173 //US 174 DateTime queryDate = JodaTimeUtils.getDate(2011, 11, 24, ExchangeIndex.NYSE.getTimeZone()); 175 HolidaysFromFile holidayFile = new HolidaysFromFile(ExchangeIndex.NYSE );168 // remove all local data files first 169 NMIO.deleteDir(new File(HOLIDAY_DIR)); // TODO: sometimes fails 170 171 //US 172 DateTime queryDate = JodaTimeUtils.getDate(2011, 11, 24, ExchangeIndex.NYSE.getTimeZone()); 173 HolidaysFromFile holidayFile = new HolidaysFromFile(ExchangeIndex.NYSE, false); 176 174 assertFalse(holidayFile.isHoliday(queryDate)); 177 175 … … 181 179 //HK 182 180 queryDate = JodaTimeUtils.getDate(2004, 4, 9, ExchangeIndex.HKEX.getTimeZone()); 183 holidayFile = new HolidaysFromFile(ExchangeIndex.HKEX );181 holidayFile = new HolidaysFromFile(ExchangeIndex.HKEX, false); 184 182 assertFalse(holidayFile.isHoliday(queryDate)); 185 183 … … 189 187 //SG 190 188 queryDate = JodaTimeUtils.getDate(2005, 3, 25, ExchangeIndex.SGX.getTimeZone()); 191 holidayFile = new HolidaysFromFile(ExchangeIndex.SGX );189 holidayFile = new HolidaysFromFile(ExchangeIndex.SGX, false); 192 190 assertFalse(holidayFile.isHoliday(queryDate)); 193 191 194 192 queryDate = JodaTimeUtils.getDate(2007, 8, 9, ExchangeIndex.SGX.getTimeZone()); 195 193 assertFalse(holidayFile.isHoliday(queryDate)); 196 }197 198 private void clearHolidayDir() {199 File directory = new File(HOLIDAY_DIR);200 201 File[] files = directory.listFiles();202 203 for (File file : files) {204 file.delete();205 }206 194 } 207 195 -
core/test-data/config/data.properties
r1366 r1385 1 HOLIDAY_DIR= ./test-data1 DATA_HOLIDAYS = ./test-data 2 2 ABC_DIR = ./abc -
core/test-data/config/data2.properties
r1366 r1385 1 1 #the folder that contains the Numerical Method Inc. historical data 2 HOLIDAY_DIR = ./resources/holidays2 DATA_HOLIDAYS = ./log/holidays 3 3 ABC_DIR = ./bbc 4 4 TEST_DIR = ./hhh
Note: See TracChangeset
for help on using the changeset viewer.
