Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

CandleStick chart limitation

Posted on 2011-03-11
5
Medium Priority
?
513 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
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

971 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