Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

CandleStick chart limitation

Posted on 2011-03-11
5
Medium Priority
?
511 Views
Last Modified: 2012-06-27
Please advise how to limit length of CandleStick chart up to maximum10 minutes
public EURUSD_CandleStick_Market() {

        datesArrayList = new ArrayList();
        pricesArrayList = new ArrayList();

        interval = 60000L;
        af = new ApplicationFrame("Chart Market");
        csr = new CandlestickRenderer();

    }

    public void aflagc(boolean flag0c) {
        mflag0c = flag0c;
    }

    public void dsControl(boolean ds0) {
        mds = ds0;
    }

    public void eurusd_CandleStick_Market(double[] avg,
                                          java.util.Date[] avg_ts) {

        if (avg[0] != 0 && mds == true) {
            System.out.println("EUR/USD Market avg[0]    = " + avg[0]);
            System.out.println("EUR/USD Market avg_ts[0]    = " + avg_ts[0]);
            System.out.println("EUR/USD Market mds    = " + mds);
            System.out.println("EUR/USD Market mflag0c    = " + mflag0c);

            datesArrayList.add(avg_ts[0]);
            pricesArrayList.add(new Double(avg[0]));

            java.util.Date[] myDates =
                new java.util.Date[datesArrayList.size()];

            for (int j = 0; j < myDates.length; j++) {
                myDates[j] = (java.util.Date)datesArrayList.get(j);
            }

            double[] myPrices = new double[pricesArrayList.size()];
            for (int j = 0; j < myPrices.length; j++) {
                myPrices[j] = ((Double)pricesArrayList.get(j)).doubleValue();
            }

            //            System.out.println("Dates: ");
            for (int j = 0; j < myDates.length; j++) {
                //                System.out.println(myDates[j].toString());
            }
            //            System.out.println("Prices: ");

            for (int j = 0; j < myPrices.length; j++) {
                //                System.out.println(myPrices[j]);
            }
            System.out.println("interval: " + interval);

            DefaultHighLowDataset dataset =
                OHLCData(myDates, myPrices, myDates[0], interval);


            JFreeChart chart = createChart(dataset);
            ChartPanel chartPanel = new ChartPanel(chart);

            af.setContentPane(chartPanel);
            af.setSize(800, 500);
            af.setLocation(1600, 800);
            if (mflag0c == true) {
                af.setVisible(true);
                chartPanel.repaint();
            } else {
                af.setVisible(false);
            }
        }
    }

    public DefaultHighLowDataset OHLCData(java.util.Date[] dates,
                                          double[] prices,
                                          java.util.Date startDate,
                                          long interval) {
        java.util.Date[] finalDates;
        double[] highs;
        double[] opens;
        double[] closes;
        double[] lows;
        double[] volumes;

        ArrayList anchors = new ArrayList();
        HashMap h = new HashMap();

        long start = startDate.getTime();

        for (int j = 0; j < dates.length; j++) {
            long cur = dates[j].getTime();
            long num = ((cur - start) / interval);
            Long anchor = new Long(start + num * interval + interval / 2);
            if (h.get(anchor) == null) {
                Moment mm = new Moment(dates[j], prices[j]);
                h.put(anchor, mm);
                anchors.add(anchor);
            } else {
                Moment mm = (Moment)h.get(anchor);
                mm.update(dates[j], prices[j]);
                h.put(anchor, mm);
            }
        }

        Collections.sort(anchors);

        finalDates = new java.util.Date[anchors.size()];
        opens = new double[anchors.size()];
        highs = new double[anchors.size()];
        lows = new double[anchors.size()];
        closes = new double[anchors.size()];
        volumes = new double[anchors.size()];

        for (int j = 0; j < anchors.size(); j++) {
            Long ttime = (Long)anchors.get(j);
            Moment mm = (Moment)h.get(ttime);
            finalDates[j] = new java.util.Date(ttime.longValue());
            opens[j] = mm.getOpen();
            closes[j] = mm.getClose();
            highs[j] = mm.getHigh();
            lows[j] = mm.getLow();
            volumes[j] = 0.0;
        }

        DefaultHighLowDataset dataset =
            new DefaultHighLowDataset("Series 1", finalDates, highs, lows,
                                      opens, closes, volumes);
        return dataset;
    }

    private JFreeChart createChart(final DefaultHighLowDataset dataset) {
        final JFreeChart chart =
            ChartFactory.createCandlestickChart("Candlestick EUR/USD Market",
                                                "Time", "Price", dataset,
                                                false);

        NumberAxis yaxis = (NumberAxis)chart.getXYPlot().getRangeAxis();
        yaxis.setAutoRangeIncludesZero(false);

        csr.setCandleWidth(10);
        chart.getXYPlot().setRenderer(csr);

        return chart;
    }

}

Open in new window

0
Comment
Question by:gbcbr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 35110964
About that you already know my suggestion.
I think it would be the  quickest way just to follow it.
0
 

Author Comment

by:gbcbr
ID: 35110973
Can you please insert your suggestion in this code?
0
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 35111051

You just replace lines 32-42 in the above with the following code:


ArrayList datesArrayList1 = new ArrayList();
ArrayList pricesArrayList1 = new ArrayList();
for (int j=0; j< datesArrayList.size(); j++){
java.util.Date dd = (java.util.Date) datesArrayList.get(j);
Double dpr = (Double) priceArrayList.get(j);
if ((dd.getTime() - avg_ts[0].getTime())<-1000000L) continue;
datesArrayList1.add(dd);
pricesArrayList1.add(dpr);
}

          java.util.Date[] myDates =
                new java.util.Date[datesArrayList1.size()];

            for (int j = 0; j < myDates.length; j++) {
                myDates[j] = (java.util.Date)datesArrayList1.get(j);
            }

            double[] myPrices = new double[pricesArrayList1.size()];
            for (int j = 0; j < myPrices.length; j++) {
                myPrices[j] = ((Double)pricesArrayList1.get(j)).doubleValue();
            }



0
 

Author Comment

by:gbcbr
ID: 35111209
Thank you, works very well, I put 12000 only and it keeps only two candles all the time.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35111222
Great!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This video teaches viewers about errors in exception handling.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question