Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8174563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:29:33+00:00 2026-06-06T22:29:33+00:00

I am trying to overlay a moving average over an OHLC chart but it

  • 0

I am trying to overlay a moving average over an OHLC chart but it does not seem to work.

I provide two critical functions of the code :

private static OHLCDataset createPriceDataset(String filename)
{   
    OHLCSeries s1 = new OHLCSeries(filename);

    try {
        BufferedReader in = new BufferedReader(new FileReader(filename));
        DateFormat df = new SimpleDateFormat("yyyyMMdd");
        String inputLine;
        in.readLine();
        while ((inputLine = in.readLine()) != null) {
            StringTokenizer st = new StringTokenizer(inputLine, ",");
            Date date       = df.parse( st.nextToken() );
            double open     = Double.parseDouble( st.nextToken() );
            double high     = Double.parseDouble( st.nextToken() );
            double low      = Double.parseDouble( st.nextToken() );
            double close    = Double.parseDouble( st.nextToken() );
            double volume   = Double.parseDouble( st.nextToken() );
            //double adjClose = Double.parseDouble( st.nextToken() );
            s1.add(new Day(date), open, high, low, close);
        }
        in.close();
    }
    catch (Exception e) {
        e.printStackTrace();
    }

    OHLCSeriesCollection dataset = new OHLCSeriesCollection();
    dataset.addSeries(s1);

    return dataset;
}



private static JFreeChart createCombinedChart()
{
    OHLCDataset data1 = createPriceDataset(filename);

    XYItemRenderer renderer1 = new HighLowRenderer();
    renderer1.setBaseToolTipGenerator(new StandardXYToolTipGenerator(StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT, new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0.00")));
    renderer1.setSeriesPaint(0, Color.blue);
    DateAxis domainAxis = new DateAxis("Date");
    NumberAxis rangeAxis = new NumberAxis("Price");
    rangeAxis.setNumberFormatOverride(new DecimalFormat("$0.00"));
    rangeAxis.setAutoRange(true);
    rangeAxis.setAutoRangeIncludesZero(false);
    XYPlot plot1 = new XYPlot(data1, domainAxis, rangeAxis, renderer1);
    plot1.setBackgroundPaint(Color.lightGray);
    plot1.setDomainGridlinePaint(Color.white);
    plot1.setRangeGridlinePaint(Color.white);
    plot1.setRangePannable(true);

    //Overlay the Long-Term Trend Indicator
    XYDataset dataset3 = MovingAverage.createMovingAverage(data1, "LT", 49.0, 49.0);
    plot1.setDataset(1, dataset3);
    plot1.setRenderer(1, new StandardXYItemRenderer());

    //add a second dataset (volume) and renderer
    IntervalXYDataset data2 = createVolumeDataset(filename);
    XYBarRenderer renderer2 = new XYBarRenderer();
    renderer2.setDrawBarOutline(false);
    renderer2.setBaseToolTipGenerator(new StandardXYToolTipGenerator(StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT, new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0,000.00")));
    renderer2.setSeriesPaint(0, Color.red);

    XYPlot plot2 = new XYPlot(data2, null, new NumberAxis("Volume"), renderer2);
    plot2.setBackgroundPaint(Color.lightGray);
    plot2.setDomainGridlinePaint(Color.white);
    plot2.setRangeGridlinePaint(Color.white);

    CombinedDomainXYPlot cplot = new CombinedDomainXYPlot(domainAxis);
    cplot.add(plot1, 3);
    cplot.add(plot2, 2);
    cplot.setGap(8.0);
    cplot.setDomainGridlinePaint(Color.white);
    cplot.setDomainGridlinesVisible(true);
    cplot.setDomainPannable(true);


    //return the new combined chart
    JFreeChart chart = new JFreeChart("Sun Microsystems (SUNW)", JFreeChart.DEFAULT_TITLE_FONT, cplot, false);

    ChartUtilities.applyCurrentTheme(chart);
    renderer2.setShadowVisible(false);
    renderer2.setBarPainter(new StandardXYBarPainter());

    return chart;
}

Any ideas?

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T22:29:35+00:00Added an answer on June 6, 2026 at 10:29 pm

    I’ve added the following code and it seems to work.
    If anybody disagrees on this solution just let me know.
    Here it goes :

    public class PriceVolumeChart2 extends ApplicationFrame{
    
    final static String filename = "A.txt";
    *static TimeSeries t1 = new TimeSeries("49-day moving average");*
    
    /**
     * Default constructor
     */
    public PriceVolumeChart2(String title)
    {
        super(title);
        JPanel panel = createDemoPanel();
        panel.setPreferredSize(new Dimension(500, 270));
        setContentPane(panel);
    }
    
    //create price dataset
    //hard-coded here
    private static OHLCDataset createPriceDataset(String filename)
    {
        //the following data is taken from http://finance.yahoo.com/
        //for demo purposes...
    
        OHLCSeries s1 = new OHLCSeries(filename);
    
        try {
            BufferedReader in = new BufferedReader(new FileReader(filename));
            DateFormat df = new SimpleDateFormat("yyyyMMdd");
            String inputLine;
            in.readLine();
            while ((inputLine = in.readLine()) != null) {
                StringTokenizer st = new StringTokenizer(inputLine, ",");
                Date date       = df.parse( st.nextToken() );
                double open     = Double.parseDouble( st.nextToken() );
                double high     = Double.parseDouble( st.nextToken() );
                double low      = Double.parseDouble( st.nextToken() );
                double close    = Double.parseDouble( st.nextToken() );
                double volume   = Double.parseDouble( st.nextToken() );
                //double adjClose = Double.parseDouble( st.nextToken() );
                s1.add(new Day(date), open, high, low, close);
                *t1.add(new Day(date), close);*
            }
            in.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    
    
    
        OHLCSeriesCollection dataset = new OHLCSeriesCollection();
        dataset.addSeries(s1);
    
        return dataset;
    }
    
    
    //create volume dataset
    private static IntervalXYDataset createVolumeDataset(String filename)
    {
        //create dataset 2...
        TimeSeries s1 = new TimeSeries("Volume");
    
        try {
            BufferedReader in = new BufferedReader(new FileReader(filename));
            DateFormat df = new SimpleDateFormat("yyyyMMdd");
            String inputLine;
            in.readLine();
            while ((inputLine = in.readLine()) != null) {
                StringTokenizer st = new StringTokenizer(inputLine, ",");
                Date date = df.parse( st.nextToken() );
                st.nextToken();
                st.nextToken();
                st.nextToken();
                st.nextToken();
                double volume   = Double.parseDouble( st.nextToken() );
                //double adjClose = Double.parseDouble( st.nextToken() );
                s1.add(new Day(date), volume);
            }
            in.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    
        return new TimeSeriesCollection(s1);
    }
    
    private static JFreeChart createCombinedChart()
    {
        OHLCDataset data1 = createPriceDataset(filename);
    
        XYItemRenderer renderer1 = new HighLowRenderer();
        renderer1.setBaseToolTipGenerator(new StandardXYToolTipGenerator(
            StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,
            new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0.00")));
        renderer1.setSeriesPaint(0, Color.blue);
        DateAxis domainAxis = new DateAxis("Date");
        NumberAxis rangeAxis = new NumberAxis("Price");
        rangeAxis.setNumberFormatOverride(new DecimalFormat("$0.00"));
        rangeAxis.setAutoRange(true);
        rangeAxis.setAutoRangeIncludesZero(false);
        XYPlot plot1 = new XYPlot(data1, domainAxis, rangeAxis, renderer1);
        plot1.setBackgroundPaint(Color.lightGray);
        plot1.setDomainGridlinePaint(Color.white);
        plot1.setRangeGridlinePaint(Color.white);
        plot1.setRangePannable(true);
    
        //Overlay the Long-Term Trend Indicator
        *TimeSeries dataset3 = MovingAverage.createMovingAverage(t1, "LT", 49, 49);
        TimeSeriesCollection collection = new TimeSeriesCollection();
        collection.addSeries(dataset3);
        plot1.setDataset(1, collection);*
        plot1.setRenderer(1, new StandardXYItemRenderer());
    
        //add a second dataset (volume) and renderer
        IntervalXYDataset data2 = createVolumeDataset(filename);
        XYBarRenderer renderer2 = new XYBarRenderer();
        renderer2.setDrawBarOutline(false);
        renderer2.setBaseToolTipGenerator(new StandardXYToolTipGenerator(
            StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,
            new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0,000.00")));
        renderer2.setSeriesPaint(0, Color.red);
    
        XYPlot plot2 = new XYPlot(data2, null, new NumberAxis("Volume"), renderer2);
        plot2.setBackgroundPaint(Color.lightGray);
        plot2.setDomainGridlinePaint(Color.white);
        plot2.setRangeGridlinePaint(Color.white);
    
        CombinedDomainXYPlot cplot = new CombinedDomainXYPlot(domainAxis);
        cplot.add(plot1, 3);
        cplot.add(plot2, 2);
        cplot.setGap(8.0);
        cplot.setDomainGridlinePaint(Color.white);
        cplot.setDomainGridlinesVisible(true);
        cplot.setDomainPannable(true);
    
    
        //return the new combined chart
        JFreeChart chart = new JFreeChart("Sun Microsystems (SUNW)",
            JFreeChart.DEFAULT_TITLE_FONT, cplot, false);
    
        ChartUtilities.applyCurrentTheme(chart);
        renderer2.setShadowVisible(false);
        renderer2.setBarPainter(new StandardXYBarPainter());
    
        return chart;
    }
    
    //create a panel
    public static JPanel createDemoPanel()
    {
        JFreeChart chart = createCombinedChart();
        return new ChartPanel(chart);
    }
    
    public static void main(String[] args) {
        // TODO code application logic here
        PriceVolumeChart2 demo = new PriceVolumeChart2(
            "JFreeChart: CombinedXYPlotDemo1.java (base)");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    }
    
    //Download data from web
    }
    

    Sample data:

    20110103,41.56,42.14,41.41,41.88,3572300
    20110104,41.99,42.1,41.18,41.49,3588800
    20110105,41.28,41.73,41,41.4,3232700
    20110106,41.37,41.84,41.21,41.48,3361400
    20110107,41.52,41.8,41.04,41.62,2725900
    20110110,41.41,42.72,41.3,42.22,3145800
    20110111,42.52,43.31,42.38,42.94,3315400
    20110112,43.2,43.41,42.96,43.13,2463100
    20110113,42.88,43.23,42.87,42.97,1676400
    20110114,42.79,43.37,42.76,43.26,2215600
    20110117,43.26,43.26,43.26,43.26,0
    20110118,43.33,44.45,43.32,44.35,2982300
    20110119,44.16,44.29,42.27,42.43,4537200
    20110120,41.95,42.58,41.46,42.29,4874700
    20110121,42.5,43.26,42.03,42.11,3004500
    20110124,42.18,42.79,42.07,42.77,2067400
    20110125,42.77,43.52,42.28,42.69,3132700
    20110126,42.82,42.97,42.04,42.57,3927300
    20110127,42.77,43.09,42.37,42.45,3189600
    20110128,42.5,42.52,40.88,40.98,3629800
    20110131,41.21,41.83,40.89,41.83,3690900
    20110201,42.07,42.7,41.93,42.05,3388200
    20110202,41.75,41.91,40.82,41.23,3970700
    20110203,40.93,41.18,40.23,40.99,3522700
    20110204,41.19,43.13,40.94,42.99,5197700
    20110207,43.45,44.66,43.37,44.44,4569800
    20110208,44.65,44.71,43.83,44.17,3734500
    20110209,44.01,44.17,43.16,43.43,3779300
    20110210,43.22,44.01,42.57,44,3275700
    20110211,43.77,45.15,43.65,45.02,4436000
    20110214,45,45.42,44.72,44.79,3484400
    20110215,44.79,45,42.64,42.65,7328000
    20110216,42.91,43.6,42.7,43.57,4159800
    20110217,43.38,44,43.02,43.92,2621800
    20110218,43.78,44.29,43.65,43.92,4390200
    20110221,43.92,43.92,43.92,43.92,0
    20110222,43.21,43.75,42.39,42.49,4143600
    20110223,42.59,42.75,39.94,40.45,7074200
    20110224,40.34,41.55,40.18,41.15,4455700
    20110225,41.16,42.41,41.13,42.36,4297500
    20110228,42.43,42.65,41.36,42.08,3070200
    20110301,41.98,42.49,40.65,40.68,4091300
    

    The new code is inside asterisks.

    If you believe that there is a more elegant solution , please let me know.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to overlay two images in my app, but they seem to
I am trying to make overlay over image while dragging but its also moving
I am trying to to overlay some rotated text over my images but I
I am trying to merge two examples from the ApiDemos so one overlay over
I am trying to position an overlay over a div. I had the code
I am trying to overlay a matrix over another matrix using matplotlib imshow(). I
Using GTK, I'm trying to overlay a More prompt (but it could just as
I'm trying to overlay text over a text form field. The text should appear
I'm trying to overlay glyphs from different fonts, but it's hard to get them
I am trying to overlay a div over my entire page to show a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.