You are viewing an archived page from the original site. Go to the new numericalmethod.com →

Changeset 1385 in algoquant


Ignore:
Timestamp:
3/15/2013 8:35:58 PM (11 months ago)
Author:
kenyiu
Message:

CHG: Changed the default holidays data folder to ./log/holidays, and the property name for the setting to DATA_HOLIDAYS.

Location:
core
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • core/src/main/java/com/numericalmethod/algoquant/config/ConfigProperties.java

    r1366 r1385  
    4949     * The data folder location that stores holiday data files. 
    5050     */ 
    51     HOLIDAY_DIR("HOLIDAY_DIR"); 
     51    DATA_HOLIDAYS("DATA_HOLIDAYS"); 
    5252    private final String propertyName; 
    5353 
  • core/src/main/java/com/numericalmethod/algoquant/data/calendar/holiday/HolidaysFromFile.java

    r1366 r1385  
    4848public class HolidaysFromFile implements Holidays { 
    4949 
    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); 
    5254    private static final String MATCHING_PATTERN = "^%s(\\d{4})-(\\d{2})-(\\d{2})~(\\d{4})-(\\d{2})-(\\d{2}).csv.zip$"; 
    5355    private static final String DATETIME_FORMAT = "yyyy-MM-dd"; 
    5456    private static final DateTimeFormatter DATETIME_FORMATTER = DateTimeFormat.forPattern(DATETIME_FORMAT); 
    55     private static final Logger logger = getClassLogger(); 
    5657    private final ExchangeIndex exchangeIndex; 
     58    private final boolean updateFromWeb; 
    5759    private String holidayFileName = null; 
    5860    private Set<DateMidnight> holidaysSet = null; 
    5961 
    6062    public HolidaysFromFile(ExchangeIndex exchangeIndex) { 
     63        this(exchangeIndex, true); 
     64    } 
     65 
     66    public HolidaysFromFile(ExchangeIndex exchangeIndex, boolean updateFromWeb) { 
    6167        this.exchangeIndex = exchangeIndex; 
     68        this.updateFromWeb = updateFromWeb; 
    6269    } 
    6370 
    6471    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        } 
    6577 
    6678        try { 
    … …  
    7284            holidayFileName = getFileName(exchangeIndex, beginDate, endDate); 
    7385 
    74             saveHolidaysFile(weekdayHolidays, HOLIDAY_DIR, holidayFileName); 
     86            saveHolidaysFile(weekdayHolidays, HOLIDAY_DIRNAME, holidayFileName); 
    7587            holidaysSet = getHolidaysFromFile(); 
    7688 
    7789        } 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())); 
    7992            return false; 
    8093        } 
    … …  
    95108 
    96109    private boolean isHolidayFileExist() { 
    97         File path = new File(HOLIDAY_DIR); 
     110        File path = new File(HOLIDAY_DIRNAME); 
    98111        if (!path.exists()) { 
    99112            return false; 
    … …  
    103116            @Override 
    104117            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); 
    106120            } 
    107121        }); 
    … …  
    116130 
    117131    private boolean isHolidayFileUpdate(DateMidnight queryDate) { 
    118  
    119132        if (holidayFileName == null) { 
    120133            return false; 
    121134        } 
    122135 
    123         Pattern pattern = Pattern.compile(String.format(MATCHING_PATTERN, getFilePrefix(exchangeIndex))); 
     136        Pattern pattern = Pattern.compile( 
     137            String.format(MATCHING_PATTERN, getFilePrefix(exchangeIndex))); 
    124138        Matcher matcher = pattern.matcher(holidayFileName); 
    125139        matcher.matches(); 
    … …  
    138152 
    139153        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(); 
    141155            holidayFileName = null; 
    142156            return false; 
    … …  
    152166        if (!isQueryDateInRange(queryDateMidnight)) { 
    153167            // 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", 
    155169                                      queryDateMidnight.toString(DATETIME_FORMAT), 
    156170                                      getFilePrefix(exchangeIndex), 
    … …  
    183197 
    184198        HolidayCsvZipFileReader reader; 
     199        String fileLocation = String.format("%s/%s", HOLIDAY_DIRNAME, holidayFileName); 
    185200        try { 
    186             reader = new HolidayCsvZipFileReader( 
    187                 String.format("%s/%s", HOLIDAY_DIR, holidayFileName)); 
     201            reader = new HolidayCsvZipFileReader(fileLocation); 
    188202        } 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)); 
    190204            return holidays; 
    191205        } 
  • core/src/main/resources/config/data.properties

    r1382 r1385  
    55DATA_YAHOO = ${DATA_FOLDER}/yahoo 
    66DATA_SGX = ${DATA_FOLDER}/raw/sgx 
    7 HOLIDAY_DIR = ./resources/holidays 
     7DATA_HOLIDAYS = ./log/holidays 
  • core/src/main/resources/config/haksunli/haksunli.properties

    r1382 r1385  
    77 
    88#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 
     9DATA_COMPUSTAT = ${DATA_FOLDER}/raw/compustat 
     10COMPUSTAT_HONGKONG_COMPANY = ${DATA_COMPUSTAT}/hongkong/company.csv.zip 
     11COMPUSTAT_HONGKONG_ACCOUNTING = ${DATA_COMPUSTAT}/hongkong/monthlyAccounting.1995.2010.csv.zip 
     12COMPUSTAT_HONGKONG_DAILY_RETURNS = ${DATA_COMPUSTAT}/hongkong/dailyReturn.1995.2011.csv.zip 
    1213 
    1314#This folder contains the data downloaded from the HKMA website. 
    14 HKMA = C:/Users/Haksun Li/Documents/Businesses/Numerical Method/data/raw/HKMA 
     15HKMA = ${DATA_FOLDER}/raw/HKMA 
    1516 
  • core/src/test/java/com/numericalmethod/algoquant/config/AlgoQuantConfigTest.java

    r1366 r1385  
    2323package com.numericalmethod.algoquant.config; 
    2424 
     25import org.junit.AfterClass; 
    2526import static org.junit.Assert.*; 
     27import org.junit.BeforeClass; 
    2628import org.junit.Test; 
    2729 
    … …  
    3133 */ 
    3234public 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     
    3348 
    3449    @Test 
    … …  
    3651        AlgoQuantConfig instance = AlgoQuantConfig.getConfiguration(); 
    3752        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); 
    4055    } 
    4156 
    … …  
    4358    public void testChangeConfigFile() { 
    4459        AlgoQuantConfig instance = AlgoQuantConfig.getConfiguration(); 
    45         assertEquals("./resources/holidays", instance.getString(ConfigProperties.HOLIDAY_DIR.getPropertyName())); 
     60        assertEquals("./log/holidays", instance.getString(ConfigProperties.DATA_HOLIDAYS.getPropertyName())); 
    4661 
    4762        instance.setConfigFile("./test-data/config/test.config.xml"); 
    4863        AlgoQuantConfig instance2 = AlgoQuantConfig.getConfiguration(); 
    49         assertEquals("./test-data", instance2.getString(ConfigProperties.HOLIDAY_DIR.getPropertyName())); 
     64        assertEquals("./test-data", instance2.getString(ConfigProperties.DATA_HOLIDAYS.getPropertyName())); 
    5065 
    5166        assertEquals(instance, instance2); 
    … …  
    5671        AlgoQuantConfig instance = AlgoQuantConfig.getConfiguration(); 
    5772        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())); 
    5974 
    6075        instance.setString("key", "value"); 
  • core/src/test/java/com/numericalmethod/algoquant/data/calendar/holiday/HolidaysFromFileTest.java

    r1374 r1385  
    2424 
    2525import com.numericalmethod.algoquant.config.AlgoQuantConfig; 
     26import com.numericalmethod.algoquant.config.ConfigProperties; 
    2627import com.numericalmethod.algoquant.data.calendar.TimeZoneUtils; 
    2728import static com.numericalmethod.nmutils.NMUtils.*; 
    … …  
    5051    public static void setUpClass() throws Exception { 
    5152        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); 
    5454    } 
    5555 
    5656    @Test 
    5757    public void testDateOutOfRange() throws IOException { 
    58  
    5958        //US 
    6059        DateTime queryDate = JodaTimeUtils.getDate(1929, 12, 31, ExchangeIndex.NYSE.getTimeZone()); 
    … …  
    7574    @Test 
    7675    public void testBuildHolidayFile() { 
    77         clearHolidayDir(); 
     76        NMIO.deleteDir(new File(HOLIDAY_DIR)); 
    7877 
    7978        //US 
    … …  
    9695    @Test 
    9796    public void testUpdateExistingHolidayFile() throws IOException { 
    98         clearHolidayDir(); 
     97        NMIO.deleteDir(new File(HOLIDAY_DIR)); 
    9998 
    10099        //US 
    … …  
    140139    @Test 
    141140    public void testHolidayFileExistandUpdate() { 
    142  
    143141        //US 
    144142        DateTime queryDate = JodaTimeUtils.getDate(2011, 11, 24, ExchangeIndex.NYSE.getTimeZone()); 
    … …  
    166164    } 
    167165 
    168 //    @Test 
     166//    @Test // TODO: fix it 
    169167    public void testOffline() { 
    170         //Disable network interface before running this test 
    171         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); 
    176174        assertFalse(holidayFile.isHoliday(queryDate)); 
    177175 
    … …  
    181179        //HK 
    182180        queryDate = JodaTimeUtils.getDate(2004, 4, 9, ExchangeIndex.HKEX.getTimeZone()); 
    183         holidayFile = new HolidaysFromFile(ExchangeIndex.HKEX); 
     181        holidayFile = new HolidaysFromFile(ExchangeIndex.HKEX, false); 
    184182        assertFalse(holidayFile.isHoliday(queryDate)); 
    185183 
    … …  
    189187        //SG 
    190188        queryDate = JodaTimeUtils.getDate(2005, 3, 25, ExchangeIndex.SGX.getTimeZone()); 
    191         holidayFile = new HolidaysFromFile(ExchangeIndex.SGX); 
     189        holidayFile = new HolidaysFromFile(ExchangeIndex.SGX, false); 
    192190        assertFalse(holidayFile.isHoliday(queryDate)); 
    193191 
    194192        queryDate = JodaTimeUtils.getDate(2007, 8, 9, ExchangeIndex.SGX.getTimeZone()); 
    195193        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         } 
    206194    } 
    207195 
  • core/test-data/config/data.properties

    r1366 r1385  
    1 HOLIDAY_DIR = ./test-data 
     1DATA_HOLIDAYS = ./test-data 
    22ABC_DIR = ./abc 
  • core/test-data/config/data2.properties

    r1366 r1385  
    11#the folder that contains the Numerical Method Inc. historical data 
    2 HOLIDAY_DIR = ./resources/holidays 
     2DATA_HOLIDAYS = ./log/holidays 
    33ABC_DIR = ./bbc 
    44TEST_DIR = ./hhh 
Note: See TracChangeset for help on using the changeset viewer.