Java Class for price data access

Discussion in 'Data Sets and Feeds' started by agrau, Aug 26, 2003.

  1. agrau

    agrau

    Hello world \n

    I am wondering if there is any useable Java class out there that is designed to maintain and access historical price data. By this I mean a n-tier data object spefically for security data that hides the actual price history and fundamental database(s) and make the data available in a uniform format, regardless of true underlying database.

    Ideally, I would be looking for a xml-based solution, but this may be a dream only.

    Additionally, what other trading/trader relevant classes do you know off? Open-Source stuff preferred, but not mandantory. A point&figure charting package would we one of my most wanted classes, for example.

    Thanks for your input!

    Best, Andreas
     
  2. GIG

    GIG

    You can search around on SourceForge for some open source packages, but I don't think you'll find any 'securities downloading' packages. You will most likely have to write that yourself.

    I use JFreeChart for charting, you can look them up at
    www.object-refinery.com

    Regards,

    Brandon
     
  3. corvus

    corvus

    I am working on something like this, but I am not sure if it will be open or not yet. No ETA either right now...
     
  4. fleance

    fleance

  5. t0yland

    t0yland

    Heres a sample that you can use to create your own :/
    ------------------------------------------------------------------------

    import java.io.*;
    import java.net.*;
    import java.text.*;

    public class Webcat {



    public String GetString(String urlString, String preString, String postString)

    {

    // Get the web page from the url defined by urlString

    String webPageHTML;

    String DesiredString;

    try {

    webPageHTML = GetWebPageFromUrl(urlString);

    DesiredString = GetStringFromHTML(webPageHTML, preString, postString);

    }

    catch (GetStringException e)

    {

    return "0";

    }

    catch (Exception e1)

    {

    // Catch any other exception that might occur

    return "0";

    }



    return DesiredString;

    }



    //====================================================================

    // Worker methods -- these can be private

    //====================================================================

    //====================================================================

    // GetWebPageFromUrl()

    //====================================================================

    private String GetWebPageFromUrl(String urlString) throws GetStringException

    {

    // Create a URL object from urlString

    URL PageURL;

    try {

    PageURL = new URL(urlString);

    }

    catch (MalformedURLException e)

    {

    String msg = "Invalid url: " + urlString;

    throw new GetStringException(msg);

    }



    // Open a connection to the URL

    URLConnection PageConnection;

    try {

    PageConnection = PageURL.openConnection();

    }

    catch (IOException e)

    {

    String msg = "Can't open connection to " + urlString;

    throw new GetStringException(msg);

    }



    // Get the InputStream from the URL connection

    InputStream webPageInputStream;

    try {

    webPageInputStream = PageConnection.getInputStream();

    }

    catch (IOException e)

    {

    // Could be any server error, but the most likely is 404

    String msg = "404 File Not Found: " + urlString;

    throw new GetStringException(msg);

    }



    // Read the web page via the InputStream

    StringBuffer webPageData = new StringBuffer(32000);

    int totalBytesRead = 0;

    boolean moreToRead = true;

    byte[] readBuf = new byte[4096]; // Read the web page in 4K chunks



    while (moreToRead)

    {

    int numBytesRead = 0;

    try {

    numBytesRead = webPageInputStream.read(readBuf);

    }

    catch (IOException e)

    {

    moreToRead = false;

    numBytesRead = -1;

    }



    if (numBytesRead > 0)

    {

    totalBytesRead += numBytesRead;

    webPageData.append(new String(readBuf, 0, numBytesRead));

    }

    else

    moreToRead = false;

    }



    try {

    webPageInputStream.close();

    }

    catch (IOException e)

    {

    // Ignore any exception that might occur

    }



    webPageData.setLength(totalBytesRead);



    return webPageData.toString();

    }



    //====================================================================

    // GetStringFromHTML()

    //====================================================================

    private String GetStringFromHTML(String webPageHTML, String preString, String postString) throws GetStringException

    {

    // Look for the preString

    int preStringLoc = webPageHTML.indexOf(preString);

    if (preStringLoc == -1)

    {

    String msg = "Couldn't find the preString " + preString;

    throw new GetStringException(msg);

    }



    // Found the preString, so look for the postString

    int postStringLoc = webPageHTML.indexOf(postString, preStringLoc);

    if (postStringLoc == -1)

    {

    String msg = "Couldn't find the postString " + postString;

    throw new GetStringException(msg);

    }



    // The stock price is between the preString and postString

    String DesiredString = webPageHTML.substring(preStringLoc + preString.length(), postStringLoc);

    return DesiredString;

    }

    }



    //====================================================================

    //

    // class GetStringException

    //

    // Convenience class to make it easy to return errors from our

    // worker methods. This let's us separate StockGrabber errors

    // from coding errors (what, coding errors?!).

    //

    //====================================================================





    class GetStringException extends Exception

    {

    public GetStringException (String msg)

    {

    super(msg);

    }

    }
     
  6. agrau

    agrau

    Many thanks to all who have replied. I started building my data access classes from scratch to implement uniform access for different datasources. The links and sample certainly help to avoid the most stupid design errors.

    I am thinking to implement a 3-tier application:

    Tier-1: Database Access (starting with EOD on TC2000, Metastock, Yahoo, Flat-File)

    Tier-2: Data-Proxy using XML-RPC for communication with Tier-3

    Tier-3: Application

    For Tier-2 I intend to implement a factory for Tier-1 classes based on class name parsing, so adding Tier-1 interfaces to additional back-end data sources should be painless.

    For internal data handling, I am thinking to use hsqldb (hsqldb.sourceforge.net)

    Any thoughts on these first ideas?

    Best, Andreas
     
  7. mubs

    mubs