Solved

CandleStick chart limitation

Posted on 2011-03-11
5
500 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 500 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now