|
From: Alex L. <dj...@gm...> - 2010-11-17 10:17:15
|
Hi Viorel!
Thank you so much for the help. I've tried to do this on my own and came up
with something similar.
I now only have 2 questions:
1. I think that the more correct code would be:
if (emaDataset.getDataItem(i) != null && emaDataset.getDataItem(i-1) != null
&& macdDataset.getDataItem(i) != null &&
macdDataset.getDataItem(i-1) != null)
{
if (emaDataset.getCloseAt(i)
> emaDataset.getCloseAt(i-1)
&& macdDataset.getCloseAt(i)
> macdDataset.getCloseAt(i-1) )
g.setColor(Color.GREEN);
else if (emaDataset.getCloseAt(i)
< emaDataset.getCloseAt(i-1)
&& macdDataset.getCloseAt(i)
< macdDataset.getCloseAt(i-1))
g.setColor(Color.RED);
else
g.setColor(Color.BLUE);
}
2. Should it just compile? I had a problem at home with compiling this
module for some reason...
On Wed, Nov 17, 2010 at 11:24 AM, Viorel Gheba <vio...@gm...> wrote:
> Hi Alex
>
> I tried to make a chart as you described.
> This is the code that i used:
>
>
> package org.chartsy.testmodule;
>
> import java.awt.Color;
> import java.awt.Graphics2D;
> import java.awt.Rectangle;
> import org.chartsy.ema.EMA;
> import org.chartsy.ema.OverlayProperties;
> import org.chartsy.macd.IndicatorProperties;
> import org.chartsy.macd.MACD;
> import org.chartsy.main.ChartFrame;
> import org.chartsy.main.ChartProperties;
> import org.chartsy.main.chart.Chart;
> import org.chartsy.main.data.ChartData;
> import org.chartsy.main.data.Dataset;
> import org.chartsy.main.utils.CoordCalc;
> import org.chartsy.main.utils.Range;
> import org.chartsy.main.utils.SerialVersion;
> import org.chartsy.main.utils.StrokeGenerator;
>
> /**
> *
> * @author Viorel
> */
> public class TestChart extends Chart
> {
>
> private static final long serialVersionUID = SerialVersion.APPVERSION;
> private EMA ema;
> private MACD macd;
>
> private Dataset mainDataset;
>
> public TestChart()
> {
> ema = new EMA();
> OverlayProperties emaProps =
> ema.getNode().getLookup().lookup(OverlayProperties.class);
> emaProps.setPeriod(13);
> macd = new MACD();
> IndicatorProperties macdProps =
> macd.getNode().getLookup().lookup(IndicatorProperties.class);
> macdProps.setFast(12);
> macdProps.setSlow(26);
> macdProps.setSmooth(9);
> }
>
> private void setDataset(Dataset dataset)
> {
> if (mainDataset == null)
> {
> mainDataset = dataset;
> calculateDatasets();
> }
> else if (!mainDataset.equals(dataset))
> {
> mainDataset = dataset;
> calculateDatasets();
> }
> }
>
> private void calculateDatasets()
> {
> ema.setDataset(mainDataset);
> ema.calculate();
> macd.setDataset(mainDataset);
> macd.calculate();
> }
>
> @Override public String getName()
> {
> return "Test Chart";
> }
>
> @Override public void paint(Graphics2D g, ChartFrame cf)
> {
> setDataset(cf.getChartData().getDataset());
>
> ChartData cd = cf.getChartData();
> ChartProperties cp = cf.getChartProperties();
> Rectangle rect = cf.getSplitPanel().getChartPanel().getBounds();
> rect.grow(-2, -2);
> Range range = cf.getSplitPanel().getChartPanel().getRange();
>
> Dataset dataset = cd.getVisible();
> Dataset emaDataset = ema.visibleDataset(cf, EMA.EMA);
> Dataset macdDataset = macd.visibleDataset(cf, MACD.HISTOGRAM);
>
> if (dataset != null && emaDataset != null && macdDataset != null)
> {
> g.setStroke(StrokeGenerator.getStroke(3));
>
> int count = emaDataset.getItemsCount();
> for (int i = 0; i < count; i++)
> {
> double open = dataset.getOpenAt(i);
> double close = dataset.getCloseAt(i);
> double high = dataset.getHighAt(i);
> double low = dataset.getLowAt(i);
>
> double x = cd.getX(i, rect);
> double yOpen = cd.getY(open, rect, range);
> double yClose = cd.getY(close, rect, range);
> double yHigh = cd.getY(high, rect, range);
> double yLow = cd.getY(low, rect, range);
>
> double candleWidth = cp.getBarWidth();
>
> if (emaDataset.getDataItem(i) != null
> && macdDataset.getDataItem(i) != null)
> {
> if (emaDataset.getCloseAt(i) > close &&
> macdDataset.getCloseAt(i) > 0)
> g.setColor(Color.GREEN);
> else if (emaDataset.getCloseAt(i) < close &&
> macdDataset.getCloseAt(i) < 0)
> g.setColor(Color.RED);
> else
> g.setColor(Color.BLUE);
> }
> else
> {
> g.setColor(Color.BLUE);
> }
>
> g.draw(CoordCalc.line(x, yLow, x, yHigh));
> g.draw(CoordCalc.line(x, yOpen, x - candleWidth/2, yOpen));
> g.draw(CoordCalc.line(x, yClose, x + candleWidth/2,
> yClose));
> }
> }
> }
>
> }
>
> You ca see that I copied the OHLC code.
> Note:
> - Make sure that EMA and MACD packages are included in your project
> libraries.
> - Make EMA and MACD public packages.
>
> For more questions, please contact us.
> Best regards,
> Viorel
>
>
> ------------------------------------------------------------------------------
> Beautiful is writing same markup. Internet Explorer 9 supports
> standards for HTML5, CSS3, SVG 1.1, ECMAScript5, and DOM L2 & L3.
> Spend less time writing and rewriting code and more time creating great
> experiences on the web. Be a part of the beta today
> http://p.sf.net/sfu/msIE9-sfdev2dev
> _______________________________________________
> Chartsy-devel mailing list
> Cha...@li...
> https://lists.sourceforge.net/lists/listinfo/chartsy-devel
>
>
--
Alex Lourie
|