package net.germes.charts.jfreechart;

import net.germes.core.entities.ResourceManager;
import net.germes.core.entities.QuoteHistory;
import net.germes.core.entities.constants.Quotes;
import net.germes.core.entities.price.Bar;
import net.germes.core.entities.price.TimeFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.data.xy.DefaultOHLCDataset;
import org.jfree.data.xy.OHLCDataItem;
import org.jfree.data.xy.OHLCDataset;

import java.util.List;

/**
 * Created by Almaz
 * Date: 07.10.2015 19:39
 */
public class CandlestickChart {

    public static void main(String[] args) throws Exception{
        QuoteHistory quoteHistory = ResourceManager.getQuoteHistory(Quotes.XAUUSD, TimeFrame.Day1);

        OHLCDataItem[] items = convertToOHLCItems(quoteHistory.getHistory());

        OHLCDataset dataset = new DefaultOHLCDataset("Gold", items);
        JFreeChart chart = ChartFactory.createCandlestickChart(
                "",
                "",
                "",
                dataset,
                false
        );
        ChartFrame frame = new ChartFrame("Candlestck frame", chart);
        frame.pack();
        frame.setVisible(true);
    }

    private static OHLCDataItem[] convertToOHLCItems(List<Bar> history) {
        OHLCDataItem[] result = new OHLCDataItem[history.size()];

        for (int i = 0; i < history.size(); i++) {
            Bar bar = history.get(i);
            result[i] = new OHLCDataItem(bar.getDate(),
                    bar.getOpen(),
                    bar.getHigh(),
                    bar.getLow(),
                    bar.getClose(),
                    0);
                    //bar.getValue()/ 100);
        }

        return result;
    }
}
