Solved

OHLC chart

Posted on 2011-03-09
74
931 Views
Last Modified: 2012-05-11
Please advice how to create OHLC chart from this dataset:
public class ChartCommander {

    LineEURUSD lineEURUSD;
    List<Date> datesArrayList = new ArrayList<Date>();
    ArrayList pricesArrayList;
    DefaultHighLowDataset dhld;
    String title;
    Comparable s1;
    RegularTimePeriod period;
    java.util.Date[] avg_ts;
    java.util.Date startDate;
    long interval;

    double open;
    double high;
    double low;
    double close;
    double[] prices;
    java.util.Date dates;

    //  TimeSeries s1;

    private static ChartCommander instance = new ChartCommander();

    public static ChartCommander getInstance() {
        return instance;
    }

    public ChartCommander() {

        lineEURUSD = new LineEURUSD(title);
        datesArrayList = new ArrayList();
        pricesArrayList = new ArrayList();
        interval = 10000L;
    }

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

        if (avg[0] != 0) {
            System.out.println("CC avg[0]    = " + avg[0]);
            System.out.println("CC avg_ts[0]    = " + avg_ts[0]);

            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);

            lineEURUSD.doEvent_avg(avg);
        }
        if (outY[0] != 0) {
            System.out.println("CC outY[0]    = " + outY[0]);

            lineEURUSD.doEvent_outY(outY);
        }
    }

Open in new window

0
Comment
Question by:gbcbr
  • 38
  • 35
74 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 35086861
So it still complains when running this constructor?
0
 

Author Comment

by:gbcbr
ID: 35086868
no, now it's OK
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35086883
Well, that's great!

You mentioned that you were printing graphs with JFreeChart before.
Can you post the corresponding code?

I believe it should be similar.
0
 

Author Comment

by:gbcbr
ID: 35086977
This is simple line chart, but I suppose for candlestick chart we have to use something like this
http://www.roseindia.net/chartgraphs/candle-stick-chart.shtml
package charts;

import java.awt.Color;

import java.text.SimpleDateFormat;

import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.FixedMillisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.RectangleInsets;

public class LineEURUSD extends JFrame {

    public LineEURUSD(final String title) {
        super(title);
        setContentPane(chartPanel);
        setSize(800, 600);
        setVisible(true);
    }

    TimeSeries s1 = new TimeSeries("s1");
    TimeSeries s2 = new TimeSeries("s2");

    int sc = 0;

    public void doEvent_avg(double[] avg) {
        sc++;

        s1.addOrUpdate(new FixedMillisecond(new Date().getTime()),
                       new Float(avg[0]));

        if (sc > 100) {
            s1.delete(0, 0);
        }
    }

    public void doEvent_outY(double[] outX) {
        sc++;

        s2.addOrUpdate(new FixedMillisecond(new Date().getTime() + 5000),
                       new Float(outX[0]));

        if (sc > 100) {
            s2.delete(0, 0);
        }
    }

    private JFreeChart createChart(XYDataset dataset) {

        JFreeChart chart =
            ChartFactory.createTimeSeriesChart("EUR/USD - 3", // title
                "Time", // x-axis label
                "inX[0]", // y-axis label
                dataset, // data
                true, // create legend?
                true, // generate tooltips?
                false) // generate URLs?
        ;
        XYPlot plot = (XYPlot)chart.getPlot();
        XYDataset dataset2 = createDataset2();
        plot.setDataset(0, dataset);
        plot.setDataset(1, dataset2);
        DateAxis axis = (DateAxis)plot.getDomainAxis();
        axis.setDateFormatOverride(new SimpleDateFormat("hh:mm:ss"));
        XYItemRenderer r = plot.getRenderer();
        XYItemRenderer renderer2 = new XYLineAndShapeRenderer();
        r.setSeriesPaint(0, Color.blue);
        r.setSeriesPaint(1, Color.red);
        renderer2.setSeriesPaint(0, Color.red);
        plot.setRenderer(1, renderer2);
        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);
        plot.setAxisOffset(new RectangleInsets(1.0, 1.0, 1.0, 1.0));
        NumberAxis axis2 = new NumberAxis("outX[0]");
        axis2.setAutoRangeIncludesZero(false);
        axis2.setLabelPaint(Color.red);
        axis2.setTickLabelPaint(Color.green);
        plot.setRangeAxis(1, axis2);
        plot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_RIGHT);
        plot.mapDatasetToRangeAxis(1, 1);

        return chart;
    }

    private XYDataset createDataset() {

        TimeSeriesCollection dataset = new TimeSeriesCollection();

        dataset.addSeries(s1);

        return dataset;
    }

    private XYDataset createDataset2() {

        TimeSeriesCollection dataset2 = new TimeSeriesCollection();

        dataset2.addSeries(s2);

        return dataset2;
    }

    public JPanel createDemoPanel() {
        JFreeChart chart = createChart(createDataset());
        ChartPanel panel = new ChartPanel(chart);
        panel.setFillZoomRectangle(true);
        panel.setMouseWheelEnabled(true);
     
        return panel;
    }
    ChartPanel chartPanel = (ChartPanel)createDemoPanel();
}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35087056
I aslo believe this way - just try use this our new dataset whuich we created
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35087079
I'll now respond slower for some time, as I have to do some other stuff for a little while
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35087243
There's an example of a dynamic candlestick here http://technojeeves.com/tech/dynachart-cand-fat.jar
Run as
java -jar dynachart-cand-fat.jar

Open in new window

0
 

Author Comment

by:gbcbr
ID: 35087343
yes, this is well, just advice how to make CandleStickEURUSD class with this dataset
DefaultHighLowDataset dataset =
                OHLCData(myDates, myPrices, myDates[0], interval);

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35087445

 immediately after;

   DeafaultHighLowDataset dataset = OHLCD....

    try this:

       JFreeChart chart = createChart(dataset);
         ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(600, 350));
      ApplicationFrame af = new ApplicationFrame("Chart");
  af. setContentPane(chartPanel);
     af.setVisible(true);

0
 

Author Comment

by:gbcbr
ID: 35087486
JFreeChart chart = createChart(dataset);
Method 'createChart' not found
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 35087562
yes, this is my mistake - try this:

    ApplicationFrame af = new ApplicationFrame("Chart");
  JFreeChart chart = af.createChart(dataset);
         ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(600, 350));
       af. setContentPane(chartPanel);
     af.setVisible(true);


0
 

Author Comment

by:gbcbr
ID: 35087584
the same Method 'createChart' not found
0
 

Author Comment

by:gbcbr
ID: 35087641
I try to create separate class, but I don't understand how to set there our dataset
package charts;

import java.util.Calendar;
import java.util.Date;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;

import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickMarkPosition;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.data.time.RegularTimePeriod;
import org.jfree.data.xy.DefaultHighLowDataset;
import org.jfree.ui.ApplicationFrame;

public class CandleStickEURUSD extends ApplicationFrame {

    transient RegularTimePeriod period;
    java.util.Date[] dates;
    java.util.Date startDate;
    long interval;
    double[] avg; // = new double[10];
    java.util.Date[] avg_ts; // = new java.util.Date[10];
    double[] outY; // = new double[10];
    java.util.Date[] myDates;
    double[] myPrices;


    public CandleStickEURUSD() {

        ChartCommander.getInstance().chartEURUSD(avg, avg_ts, outY);
        
        final DefaultHighLowDataset dataset = createDataset();     
        final JFreeChart chart = createChart(dataset);
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(800, 600));
        setContentPane(chartPanel);
        chartPanel.setVisible(true);
    }

    private DefaultHighLowDataset createDataset() {
      DefaultHighLowDataset dataset = OHLCData(myDates, myPrices, myDates[0], interval);
        return dataset;
    }

    private JFreeChart createChart(final DefaultHighLowDataset dataset) {

        final JFreeChart chart =
            ChartFactory.createHighLowChart("OHLC EURUSD bot", "Time", "Value",
                                            dataset, true);

        final DateAxis axis = (DateAxis)chart.getXYPlot().getDomainAxis();
        axis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);

        final XYPlot plot = (XYPlot)chart.getPlot();
        plot.setRenderer(1, new StandardXYItemRenderer());

        return chart;

    }
}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35087645

yes, add this method to your class (kjust as it is inside your class - not inside
any methods - in parallel to OHLCData method):

---------------------------------------------------------------
  private JFreeChart createChart(final DefaultHighLowDataset dataset) {
        final JFreeChart chart = ChartFactory.createCandlestickChart(
                "Candlestick Demo", "Time", "Price", dataset, false);
        return chart;
    }
------------------------------------------------

and then modify the line from above

replace this:
  JFreeChart chart = af.createChart(dataset);
back to this:
 JFreeChart chart = createChart(dataset);





0
 
LVL 47

Expert Comment

by:for_yan
ID: 35087660
Please, don't make any arbitrary changes - please follow instructions
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35087675
It would also be good if you still find the printout after creation
of mydates and myPrices array and post it - I'm sure we'll need it
0
 

Author Comment

by:gbcbr
ID: 35087744
It's starts to create for each signal very small chart window and printout is this:
interval: 10000
CC outY[0]    = -0.1877336
CC outY[0]    = -0.1877336
CC outY[0]    = -0.18773669
CC avg[0]    = 1.3904550000000002
CC avg_ts[0]    = Wed Mar 09 22:57:17 EET 2011
Dates: 
Wed Mar 09 22:56:55 EET 2011
Wed Mar 09 22:56:55 EET 2011
Wed Mar 09 22:57:03 EET 2011
Wed Mar 09 22:57:06 EET 2011
Wed Mar 09 22:57:06 EET 2011
Wed Mar 09 22:57:08 EET 2011
Wed Mar 09 22:57:15 EET 2011
Wed Mar 09 22:57:17 EET 2011
Prices: 
1.390475
1.390475
1.3904550000000002
1.3904649999999998
1.390485
1.3904550000000002
1.390475
1.3904550000000002
interval: 10000
CC outY[0]    = -0.18773669
CC outY[0]    = -0.18773669
CC outY[0]    = -0.18773669
CC outY[0]    = -0.18773669
CC outY[0]    = -0.18773669
CC outY[0]    = -0.18773682

Open in new window

0
 

Author Comment

by:gbcbr
ID: 35087775
maybe really better to move it to separate class, because when I forgot to comment
lineEURUSD = new LineEURUSD(title);
and it create empty window of LineEURUSD class with proper size, just without chart, because data are commented.
0
 

Author Comment

by:gbcbr
ID: 35087864
Anyway we will need to do this because:
1. It has to be 10 different charts;
2. In the next step we have to use OHLCSeriesCollection and place in one chart window two charts;
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 500 total points
ID: 35087873

Put this line

ApplicationFrame af;

after
long interval;
in the very beginning of your class

put this line:

af = new ApplicationFrame("Chart");

into constructor after interval = 10000L;


remove this line
    ApplicationFrame af = new ApplicationFrame("Chart");
from its' present location altogether.

after  af.setVisible(true);
put the line
        chartPanel.repaint();

Let's see what happens







0
 

Author Comment

by:gbcbr
ID: 35087877
I just don't understand how to call our dataset from
private DefaultHighLowDataset createDataset() {
      DefaultHighLowDataset dataset = ??????????
        return dataset;
    }

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35087937
You don't need that - we already created dataset, so this functionality is already
accomplished - we have data - we need to create chart and all visual stuff around it
0
 

Author Comment

by:gbcbr
ID: 35087939
We have one very small chart window, after manual expanding looks like this CS1.tiff
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35088001
I believe it looks good - now it requires adjustment of scale
and other details. I need to learn more about JFreeChart
to advise you about it. I need to do some other stuff for now.
If you can try to read how to change scale etc.
maybe someone else would help you. Otherwise,
I'll need to install JFreeChart and try to run it myself - perhaps
I can do it overnight
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35088348
Try replacing  the line

  final JFreeChart chart = ChartFactory.createCandlestickChart(
                "Candlestick Demo", "Time", "Price", dataset, false);

with this line:

final JFreeChart chart = ChartFactory.createHighLowChart(
            "OHLC Demo 2",
            "Time",
            "Value",
            dataset,
            true
        );
0
 

Author Comment

by:gbcbr
ID: 35088417
the same
0
 

Author Comment

by:gbcbr
ID: 35088442
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35088625
I already used that - I still don't understadn why it does not scale correctly
They always make it all so complex and invent lots of terms

Well, try immeditaley after:
-------------------
final JFreeChart chart = ChartFactory.createHighLowChart(
            "OHLC Demo 2",
            "Time",
            "Value",
            dataset,
            true
        );
-------------------
  add:
-----------------
ValueAxis ax = chart.getXYPlot().getRangeAxis();
ax.setRange(1.39, 1.41);
--------
Probably would not work either - it needs to be studied



 
0
 

Author Comment

by:gbcbr
ID: 35088686
the same, I can't understand this also, before I make another 10 different changes, no reaction at  all, looks like it blind.
I tried to do this in constructor also, the same result:
small, just initial chart window, axis from 0 and some horizontal bars
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35088712
Well, remove these changes which I suggested before - and
mmake sure you remove all yourr ten changes either.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35088772
I think you'll be better off opening new question - write something - "need help in JFReeChart - how to scale the plot" -
and attach the picture
then objects or someone who is or has specialists in JFreeChart -should help with it.
Don't know if anyone follows it now.I didn't use JFreeChart myself,
and  I'm too busy today to install all that stuff and try to understand it.
0
 

Author Comment

by:gbcbr
ID: 35089101
I think that is better to move all graphic into separate class and leave hear only dataset preparation.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35089169
But then you'll have to instantiate a new class, and then you'll say that you mind is burning as you don't understand
how to do it; remember, how I wanted to have calculations in a separate class, and that was quite logical.

Frankly, I don't think any of that matters as long as it works. How to scale it and to make it show
reasonable picture - that is important. Anyway all this drawing stuff will be happening in JFreeChart classes.



0
 
LVL 47

Expert Comment

by:for_yan
ID: 35089244

What is strange is that I see here 22:56 time,
and in the figure I see times around 23:21 -was it at another time?

Maybe there is some place where we still happen to have price equal to zero ?
that would explain why it  doesn't want to scale it automatically.


Dates:
Wed Mar 09 22:56:55 EET 2011
Wed Mar 09 22:56:55 EET 2011
Wed Mar 09 22:57:03 EET 2011
Wed Mar 09 22:57:06 EET 2011
Wed Mar 09 22:57:06 EET 2011
Wed Mar 09 22:57:08 EET 2011
Wed Mar 09 22:57:15 EET 2011
Wed Mar 09 22:57:17 EET 2011
Prices:
1.390475
1.390475
1.3904550000000002
1.3904649999999998
1.390485
1.3904550000000002
1.390475
1.3904550000000002
interval: 10000
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 500 total points
ID: 35089309
after
-------------------
final JFreeChart chart = ChartFactory.createHighLowChart(
            "OHLC Demo 2",
            "Time",
            "Value",
            dataset,
            true
        );
----------------------

Try to add this

-------------------------------

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

yaxis.setAutoRangeIncludesZero(false);
----------------------------------------------

Make sure you return everything else to our original situation
without arbitrary changes


0
 

Author Comment

by:gbcbr
ID: 35089402
I'm already at home, let's speak tomorrow.
Just think how we can get this dataset from specific class CandleStickEURUSD and manage it there, I suppose that they developed this JFreeChart classes especially, because they have some specific properties for charts drawing. But we try to mix standard class with JFreeChart, most probably this is the reason why it doesn't react on any commands.
You already cool my brain when you solve this bloody data sorting, to plot this chart is only 1% of loading, it's hundreds samples published in Internet, so this is easy question.
For me only one action important, how to get this dataset
private DefaultHighLowDataset createDataset() {
      DefaultHighLowDataset dataset = ??????????OHLCData(myDates, myPrices, myDates[0], interval);
        return dataset;
    }

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35089442
No, let's keep it as it is for now.
Return everything to how it was and then add these too lines, which I posted before.
We are not mixing anything - it is just a library with very many options and with those usually come some
complexitities. we do not know the details of its usage, therefore we don't know how to modify it.
Just add these two lines tomorrow and let's see what is going on.
You don't need this method createDataset - we already created dataset. Atleast for now it seems to be reasonable.
We need to know how to manage the drawing, scaling, etc., the inner working of JFreeChart.
We should concentrate on these tasks and minimize any other changes, otherwise the errors get piled one over another.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 47

Expert Comment

by:for_yan
ID: 35089451

thsi is the source where I found last recommendation - just not to lose it jsut in case:
http://rishisoftwareblog.wordpress.com/2009/05/13/setting-custom-range-for-your-plot-within-jfreechart/
0
 

Author Comment

by:gbcbr
ID: 35093221
what is the problem happened last week, sometimes I see only message from EE 2-3 hours
0
 

Author Comment

by:gbcbr
ID: 35093247
I put these lines and I see normal chart as a OHLC, as a CandleStick.
Two questions:
Chart window still appear in top-left screen corner and has only initial size, so to see the chart I have to pull corner of chart window and extend it manually.
Another sizing question is to fix width of  candle, because fist candle appear on half of screen.
0
 

Author Comment

by:gbcbr
ID: 35093263
0
 

Author Comment

by:gbcbr
ID: 35093353
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 500 total points
ID: 35094109
Great success!
I even didn't think it would look so beautiful.
Add lines

af.setSize(700,400);
af.move(200,200);

right before af.setVisible(true);


0
 
LVL 47

Expert Comment

by:for_yan
ID: 35094136
What is it about the width of the candle?
I look at the picture you posted - it all looks normal to me?
0
 

Author Comment

by:gbcbr
ID: 35094140
Yes, it sets size and position, now we to setCandleWidth and question be solved
0
 

Author Comment

by:gbcbr
ID: 35094167
Because it's late picture, this is start picture
CS3.tiff
0
 

Author Comment

by:gbcbr
ID: 35094187
And what is the limit of candles, how I can set as in line chart, only 100 last.
There is simple
public void doEvent_outY(double[] outX) {
        sc++;

        s2.addOrUpdate(new FixedMillisecond(new Date().getTime() + 5000),
                       new Float(outX[0]));

        if (sc > 100) {
            s2.delete(0, 0);
        }

Open in new window

But in candle maybe another way?
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 500 total points
ID: 35094356
I think to set the limit - you can filter by time - remember arraylists to which we add stuff from the very beginning and to which we add points as they arrive - after we add new point but before we make arrays of them we may filter them by time - sdomething like that:
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);
}

In the following code where you create arrays from araylists - use these lists which end with ones instead of original arraylists - thenm we'll have only most recent times and candles
But be careful - don't break what we have
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35094402
About one fat candle in the beginnoing - ikm not sure what to do - franlly on tdameritrade software I see the same thing in the beginning of the trading day - I don't think it is a big deal - it scales them to the whole screen. I'll think about it after I come to work in about 4 or 5 hours from now - if we have any ideas, we'll think
0
 

Author Comment

by:gbcbr
ID: 35094426
I  just need your advise how to use this class methods in our class
>>method setCandleWidth
http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/renderer/xy/CandlestickRenderer.html
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35094572
OK, I'll browse through API when I come to work. I think in general you should create CandleStickRenderer(double width) with constructor - providing width - don't know in what units , and then we should find to what class to setCandleStickRenderer - is it CandleStickChart ?
We need to find in API.
0
 

Author Comment

by:gbcbr
ID: 35094667
OK, during your way to your work think about third question about OHLC charts.
When we finish with this I'l open new question about OHLCSeriesCollection.
I extract this chapter from Developer Guide.
OHLCSeriesCollection.docx
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35094690
What is the third question ?
0
 

Author Comment

by:gbcbr
ID: 35094737
How to place in one chart plot two OHLC datasets with different range of axis Y and color them in similar palette (red/green), but little bit different colors, for easy recognizing.
I'll try to create second dataset, similar for yours.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35095445
Try first to put them in a straightforward way - just make another label, say "Series 2" - you can
add this as an additional String argument to our same method. And let's see what happens.

Is your difference in Y ranges dramatic - e.g. you have tens and dozens instead of one and two - in the Y range?
0
 

Author Comment

by:gbcbr
ID: 35095506
Let's finish with setCandleWidth and close this question.
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 500 total points
ID: 35095685

Try put these three lines immediately after those two magic lines
about autoinclude zero which solved our problems:

CandleStickRenderer csr = new CandleStickRenderer();
csr.setAutoWidthMethod(CandleStickRenderer.WIDTHMETHOD_SMALLEST);
chart.getXYPlot().setRenderer(csr);

Let's see what happens with the first candle.
If this does not help we can try other methods in this CandleSticRenderer which deals
with widths - ther are plenty of them


0
 

Author Comment

by:gbcbr
ID: 35095815
it asks about class CandleStickRenderer
0
 

Author Comment

by:gbcbr
ID: 35095903
I found the error:
not CandleStickRenderer
CandlestickRenderer
But anyway it still fat
0
 

Author Comment

by:gbcbr
ID: 35096073
private double width;
    
    private JFreeChart createChart(final DefaultHighLowDataset dataset) {
        final JFreeChart chart =
            ChartFactory.createCandlestickChart("Candlestick Demo", "Time",
                                                "Price", dataset, false);
        width = 10;
        NumberAxis yaxis = (NumberAxis)chart.getXYPlot().getRangeAxis();
        yaxis.setAutoRangeIncludesZero(false);
        CandlestickRenderer csr = new CandlestickRenderer();
        csr.setCandleWidth(width);
        chart.getXYPlot().setRenderer(csr);
        
        return chart;
    }

Open in new window

CS4.tiff
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35096103
Well, that seems OK.
What happens when you have just one candle?
Is it still fat?
0
 

Author Closing Comment

by:gbcbr
ID: 35096106
Very elegant solution without creating additional classes.
Excellent work!!!
0
 

Author Comment

by:gbcbr
ID: 35096150
no, I fix size in 10 pixels, so now all the time they in the same size.
For me it was critical, how to call this class correct. Now I can do all exercises with any methods of it.
Thanks again.

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35096472
You are welcome.
0
 

Author Comment

by:gbcbr
ID: 35096705
let's start with third question
0
 

Author Comment

by:gbcbr
ID: 35107863
You confuse me with
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);
}
This has to be another array list or this just mistyping?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35108882
No this was not mistyping - I suggested to create at this point another pair of  ArrayList's which would contain only the most recent data, and do further manipulatioins with this changed list. This would ensure that number of cndles which we see will grow only to some point and after that point will become constant. This was in reponse to your concern that the number of candles becomes too big
0
 

Author Comment

by:gbcbr
ID: 35109563
In this edition it doesn't control QTY.
I put 1000L for easy control, but it continue work already 5 minutes
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]));


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

            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);
            }
        }
    }

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35109724
Well, of course it does not - after you creae your lists with "1" in the end - you should use these
new lists in what follows instead of your original lists - these new lists
have lees data in termes of time - and you continue using your old lists in the code below, so
you just created filtered lists but you are not using them to
create dataset; that's waht I had written - create filtered lists and in the code below use them instead of the original lists - then you'll get only
recent candles, and deop the old ones after some time.
Use a little bit longer interval -1000L is just a second - how many candles would you get in that period?
0
 

Author Comment

by:gbcbr
ID: 35109862
I put 12000L this is two candles exactly.
Please explain more detailed about these two arrays.
ArrayList< let's say zero> get data from external source, convert it into MyDates and MyPrices which after go to the Moment class for calculation and return back as dataset.
I don't understand how works ArrayList1 and how it control number of candles.
For me understadable formula is:
        if (sc > 100) {
            s1.delete(0, 0);
Why we can't do something like this and be sure that these extra data really deleted and free heap memory?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35109986
We just intercept between the lists without onese and myDates and myPrices
and remove the older points before we creayte myDates and myPrices - and in this way
these arrays should contain only limited number of points - most recent ones.

this is not the device to reduce memory - bu this should not affect memory siginfiocantly

Don't understand about these s1.delete, but I suggest to follow these changes
with "1" arrays and see how it waorks.
0
 

Author Comment

by:gbcbr
ID: 35110055
This is from line chart:  
public void doEvent_avg(double[] avg) {
        sc++;

        s1.addOrUpdate(new FixedMillisecond(new Date().getTime()),
                       new Float(avg[0]));

        if (sc > 100) {
            s1.delete(0, 0);
        }
    }

Open in new window

Why we can't do like this, it has to be simpler that this interception which I still can't understand.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35110248
I don't know about this sc.delete, etc. - if you believe it should work - please go ahead and try - just remember how to retrun back and do
it another way if that does not work.
I usually prefer to do exactly like I know even though it may require a few more lines of code.
It is always faster than to rely more on some API which was cretaed by other people, and they had some
other thaoughts in mind. It is only whan there is no choice, that I use these API's or when I already tried
them in certain way and know they work in this way.

Your issues with memory were most probably related with some graphics situation - it was creating too many windows
in memory or something like that - it is hard to believe that your amount of actual data )in the arryLists etc.)
could be comparable with 1 Gb of memory you are using.

0
 

Author Comment

by:gbcbr
ID: 35110348
>>with 1 Gb of memory you are using.
now 5GB
Ok, let's help me with candle limitation in new question and also I'll open question about memory.
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

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

747 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

13 Experts available now in Live!

Get 1:1 Help Now