Created
February 26, 2014 19:35
-
-
Save dizzarg/9236752 to your computer and use it in GitHub Desktop.
tes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package candle.stick.app; | |
import candle.stick.view.MainFrame; | |
import javax.swing.*; | |
public class Program { | |
public static void main(String[] args) { | |
try { | |
SwingUtilities.invokeAndWait(new Runnable() { | |
@Override | |
public void run() { | |
createCandlestickFrame(); | |
} | |
}); | |
} catch (Exception e) { | |
e.fillInStackTrace(); | |
} | |
} | |
private static void createCandlestickFrame() { | |
new MainFrame(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package candle.stick.model; | |
public class Candle { | |
double Open ; // цена открытия | |
double Close ; // цена закрытия | |
double High ; // наибольшая цена | |
double Low ; // наименьшая цена | |
long Time ; // время (первый тик в интервале) | |
long Volume ; // объём торгов | |
@Override | |
public String toString() { | |
return Open +";" + Close +";" + High +";" + Low +";" + Volume; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package candle.stick.model; | |
import org.jfree.data.xy.DefaultHighLowDataset; | |
import java.util.*; | |
public class CandleBuilder { | |
private final List<TradeQuote> tradeQuotes; | |
public CandleBuilder(List<TradeQuote> tradeQuotes) { | |
this.tradeQuotes = tradeQuotes; | |
} | |
// характеристика свечки, длина интервала (минута, час, 10 секунд…) | |
public HashMap<Long, Candle> builder(Long length){ | |
HashMap<Long, Candle> candleMap = new HashMap<Long, Candle>(); | |
for (TradeQuote tradeQuote: tradeQuotes){ | |
Long lengthTime = (tradeQuote.time / length) * length; | |
buildCandleMap(candleMap, tradeQuote, lengthTime); | |
} | |
return candleMap; | |
} | |
public DefaultHighLowDataset createDataset(Long length) { | |
HashMap<Long, Candle> candleMap = builder(length); | |
int serice = candleMap.size(); | |
Date[] date = new Date[serice]; | |
double[] high = new double[serice]; | |
double[] low = new double[serice]; | |
double[] open = new double[serice]; | |
double[] close = new double[serice]; | |
double[] volume = new double[serice]; | |
ArrayList<Long> list = new ArrayList<Long>(candleMap.keySet()); | |
Collections.sort(list); | |
int i = 0; | |
for (Long time: list){ | |
Candle candle = candleMap.get(time); | |
date[i] = new Date(time * 1000); | |
high[i] = candle.High; | |
low[i] = candle.Low; | |
open[i] = candle.Open; | |
close[i] = candle.Close; | |
volume[i] = candle.Volume; | |
i++; | |
} | |
return new DefaultHighLowDataset("", date, high, low, open, close, volume); | |
} | |
public DefaultHighLowDataset createEmptyDataset() { | |
int serice = 100; | |
Date[] date = new Date[serice]; | |
double[] high = new double[serice]; | |
double[] low = new double[serice]; | |
double[] open = new double[serice]; | |
double[] close = new double[serice]; | |
double[] volume = new double[serice]; | |
Calendar calendar = Calendar.getInstance(); | |
calendar.add(Calendar.DAY_OF_MONTH, -100); | |
for (int i=0; i<serice; i++){ | |
date[i] = calendar.getTime(); | |
high[i] = 0; | |
low[i] = 0; | |
open[i] = 0; | |
close[i] = 0; | |
volume[i] = 0; | |
calendar.add(Calendar.DAY_OF_MONTH, 1); | |
} | |
return new DefaultHighLowDataset("", date, high, low, open, close, volume); | |
} | |
private void buildCandleMap(HashMap<Long, Candle> candleDayMap, TradeQuote row, Long dayTime) { | |
if(candleDayMap.containsKey(dayTime)){ | |
Candle candle = candleDayMap.get(dayTime); | |
candle.Close = row.price; | |
candle.High = Math.max(candle.High, row.price); | |
candle.Low = Math.min(candle.Low, row.price); | |
candle.Volume += row.volume; | |
candle.Time = dayTime; | |
} else { | |
Candle candle = new Candle(); | |
candle.Open = row.price; | |
candle.High = row.price; | |
candle.Low = row.price; | |
candle.Volume = row.volume; | |
candle.Time = dayTime; | |
candleDayMap.put(dayTime, candle); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package candle.stick.model; | |
public class TradeQuote implements Comparable<TradeQuote>{ | |
final Long time; | |
final Long volume; | |
final double price; | |
public TradeQuote(Long time, Long volume, double price) { | |
this.time = time; | |
this.volume = volume; | |
this.price = price; | |
} | |
@Override | |
public int compareTo(TradeQuote o) { | |
return time.compareTo(o.time); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package candle.stick.model.utils; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import java.util.TimeZone; | |
public class CalendarUtils { | |
public static final Long MIN_CONST = 60L; | |
public static final Long HOUR_CONST = 60L * MIN_CONST; | |
public static final Long DAY_CONST = 24L * HOUR_CONST; | |
public static SimpleDateFormat format = new SimpleDateFormat("d MMM yyyy HH:mm"); | |
static { | |
format.setTimeZone(TimeZone.getTimeZone("GMT")); | |
} | |
public static String toString(Date date){ | |
return format.format(date); | |
} | |
private CalendarUtils() {} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package candle.stick.model.utils; | |
import candle.stick.model.Candle; | |
import candle.stick.model.TradeQuote; | |
import java.io.*; | |
import java.util.*; | |
public class FileUtils { | |
public static List<TradeQuote> readTrideQuote(File file) throws Exception{ | |
BufferedReader reader = new BufferedReader(new FileReader(file)); | |
List<TradeQuote> tradeQuotes = new ArrayList<TradeQuote>(); | |
while (reader.ready()){ | |
String line = reader.readLine(); | |
String[] cols = line.split(";"); | |
long time = Long.parseLong(cols[0]); | |
long volume = Long.parseLong(cols[2]); | |
double price = Double.parseDouble(cols[1].replace(',', '.')); | |
TradeQuote tradeQuote = new TradeQuote(time, volume, price); | |
tradeQuotes.add(tradeQuote); | |
} | |
reader.close(); | |
return tradeQuotes; | |
} | |
public static void writeTrideQuote(HashMap<Long, Candle> candleDayMap, File file) throws Exception{ | |
BufferedWriter writer = new BufferedWriter(new FileWriter(file)); | |
ArrayList<Long> list = new ArrayList<Long>(candleDayMap.keySet()); | |
Collections.sort(list); | |
for (Long time: list){ | |
Candle candle = candleDayMap.get(time); | |
String strDate = CalendarUtils.toString(new Date(time * 1000)); | |
writer.write(strDate +";"+candle.toString()); | |
writer.newLine(); | |
} | |
writer.flush(); | |
writer.close(); | |
} | |
public static void writeTrideQuote(HashMap<Long, Candle> candleDayMap, String fileName) throws Exception{ | |
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName)); | |
ArrayList<Long> list = new ArrayList<Long>(candleDayMap.keySet()); | |
Collections.sort(list); | |
for (Long time: list){ | |
Candle candle = candleDayMap.get(time); | |
String strDate = CalendarUtils.toString(new Date(time * 1000)); | |
writer.write(strDate +";"+candle.toString()); | |
writer.newLine(); | |
} | |
writer.flush(); | |
writer.close(); | |
} | |
public static List<TradeQuote> readTrideQuote(String fileName) throws Exception{ | |
BufferedReader reader = new BufferedReader(new FileReader(fileName)); | |
List<TradeQuote> tradeQuotes = new ArrayList<TradeQuote>(); | |
while (reader.ready()){ | |
String line = reader.readLine(); | |
String[] cols = line.split(";"); | |
long time = Long.parseLong(cols[0]); | |
long volume = Long.parseLong(cols[2]); | |
double price = Double.parseDouble(cols[1].replace(',', '.')); | |
TradeQuote tradeQuote = new TradeQuote(time, volume, price); | |
tradeQuotes.add(tradeQuote); | |
} | |
reader.close(); | |
return tradeQuotes; | |
} | |
private FileUtils() {} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package candle.stick.view; | |
import javax.swing.*; | |
import java.awt.*; | |
public class JStatusBar extends JPanel { | |
private static final long serialVersionUID = 1L; | |
protected JPanel leftPanel; | |
protected JPanel rightPanel; | |
public JStatusBar() { | |
createPartControl(); | |
} | |
protected void createPartControl() { | |
setLayout(new BorderLayout()); | |
setPreferredSize(new Dimension(getWidth(), 23)); | |
leftPanel = new JPanel(new FlowLayout( | |
FlowLayout.LEADING, 5, 3)); | |
leftPanel.setOpaque(false); | |
add(leftPanel, BorderLayout.WEST); | |
rightPanel = new JPanel(new FlowLayout( | |
FlowLayout.TRAILING, 5, 3)); | |
rightPanel.setOpaque(false); | |
add(rightPanel, BorderLayout.EAST); | |
} | |
public void setLeftComponent(JComponent component) { | |
leftPanel.add(component); | |
} | |
public void addRightComponent(JComponent component) { | |
JPanel panel = new JPanel(new FlowLayout( | |
FlowLayout.LEADING, 5, 0)); | |
panel.add(new SeparatorPanel(Color.GRAY, Color.WHITE)); | |
panel.add(component); | |
rightPanel.add(panel); | |
} | |
@Override | |
protected void paintComponent(Graphics g) { | |
super.paintComponent(g); | |
int y = 0; | |
g.setColor(new Color(156, 154, 140)); | |
g.drawLine(0, y, getWidth(), y); | |
y++; | |
g.setColor(new Color(196, 194, 183)); | |
g.drawLine(0, y, getWidth(), y); | |
y++; | |
g.setColor(new Color(218, 215, 201)); | |
g.drawLine(0, y, getWidth(), y); | |
y++; | |
g.setColor(new Color(233, 231, 217)); | |
g.drawLine(0, y, getWidth(), y); | |
y = getHeight() - 3; | |
g.setColor(new Color(233, 232, 218)); | |
g.drawLine(0, y, getWidth(), y); | |
y++; | |
g.setColor(new Color(233, 231, 216)); | |
g.drawLine(0, y, getWidth(), y); | |
y++; | |
g.setColor(new Color(221, 221, 220)); | |
g.drawLine(0, y, getWidth(), y); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package candle.stick.view; | |
import candle.stick.model.CandleBuilder; | |
import candle.stick.model.utils.CalendarUtils; | |
import candle.stick.model.utils.FileUtils; | |
import org.jfree.chart.ChartFactory; | |
import org.jfree.chart.ChartPanel; | |
import org.jfree.chart.JFreeChart; | |
import org.jfree.data.xy.DefaultHighLowDataset; | |
import org.jfree.ui.ExtensionFileFilter; | |
import javax.swing.*; | |
import javax.swing.filechooser.FileNameExtensionFilter; | |
import java.awt.*; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.awt.event.InputEvent; | |
import java.awt.event.KeyEvent; | |
import java.io.File; | |
public class MainFrame extends JFrame { | |
private CandleBuilder builder; | |
private final JLabel leftLabel; | |
private final JPanel centerPanel; | |
private ChartPanel chartPanel = null; | |
private JRadioButton dayBth = new JRadioButton("Дневной", true); | |
private JRadioButton hourBth = new JRadioButton("Часовой", false); | |
private JRadioButton minuteBth = new JRadioButton("Минутный", false); | |
private Long hourConst = CalendarUtils.HOUR_CONST; | |
public MainFrame() { | |
super("Candlestick demo"); | |
centerPanel = new JPanel(); | |
JStatusBar statusBar = new JStatusBar(); | |
leftLabel = new JLabel("Запуск приложения."); | |
statusBar.setLeftComponent(leftLabel); | |
dayBth.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
setHourConst(CalendarUtils.DAY_CONST); | |
} | |
}); | |
minuteBth.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
setHourConst(CalendarUtils.MIN_CONST); | |
} | |
}); | |
hourBth.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
setHourConst(CalendarUtils.HOUR_CONST); | |
} | |
}); | |
getContentPane().add(centerPanel); | |
getContentPane().add(statusBar, BorderLayout.SOUTH); | |
final GridBagConstraints gbc = new GridBagConstraints(); | |
gbc.insets = new Insets(2,2,2,2); | |
ButtonGroup group = new ButtonGroup(); | |
group.add(dayBth); | |
group.add(hourBth); | |
group.add(minuteBth); | |
final JPanel groupPane = new JPanel(); | |
groupPane.setLayout(new GridLayout(3, 1)); | |
groupPane.add(dayBth); | |
groupPane.add(hourBth); | |
groupPane.add(minuteBth); | |
final JPanel actionPane = new JPanel(); | |
actionPane.setLayout(new GridLayout(2, 1)); | |
final JButton buildBth = new JButton("Построить график"); | |
buildBth.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
DefaultHighLowDataset dataset = builder.createDataset(getHourConst()); | |
JFreeChart chart = createChart(dataset); | |
updateChart(chart); | |
centerPanel.add(chartPanel, BorderLayout.CENTER); | |
leftLabel.setText("График построен"); | |
} | |
}); | |
final JButton exportBth = new JButton("Сохранить файл"); | |
exportBth.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent event) { | |
try { | |
JFileChooser fileChooser = new JFileChooser(); | |
fileChooser.setFileFilter(new ExtensionFileFilter("Текстовый файл", "txt")); | |
fileChooser.setDialogTitle("Сохранить в файл"); | |
fileChooser.setApproveButtonText("88"); | |
fileChooser.setCurrentDirectory(new File( "." )); | |
fileChooser.showSaveDialog(null); | |
File file = fileChooser.getSelectedFile(); | |
FileUtils.writeTrideQuote(builder.builder(getHourConst()), file); | |
leftLabel.setText("Файл '"+file.getName()+"' сохранен успешно."); | |
} catch (Exception e) { | |
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. | |
} | |
} | |
}); | |
actionPane.add(buildBth); | |
actionPane.add(exportBth); | |
JMenu fileMenu = new JMenu("Файл"); | |
KeyStroke newKey = KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK); | |
fileMenu.add(createMenuItem("Открыть", newKey, new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent event) { | |
try { | |
createCandleBuilder(); | |
DefaultHighLowDataset dataset = builder.createEmptyDataset(); | |
JFreeChart chart = createChart(dataset); | |
updateChart(chart); | |
addComponents(new JLabel("Тип: "), groupPane, centerPanel, gbc); | |
addCenterComponents(actionPane, centerPanel, gbc); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
})); | |
JMenuBar menuBar = new JMenuBar(); | |
menuBar.add(fileMenu); | |
setJMenuBar(menuBar); | |
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
setPreferredSize(new Dimension(800, 600)); | |
setDefaultLookAndFeelDecorated(true); | |
setLocationRelativeTo(null); | |
setVisible(true); | |
pack(); | |
} | |
public Long getHourConst() { | |
return hourConst; | |
} | |
public void setHourConst(Long hourConst) { | |
this.hourConst = hourConst; | |
} | |
private void addComponents(JLabel label, JComponent component, JPanel p, GridBagConstraints gbc){ | |
gbc.anchor = GridBagConstraints.EAST; | |
gbc.gridwidth = GridBagConstraints.RELATIVE; | |
p.add(label, gbc); | |
gbc.anchor = GridBagConstraints.WEST; | |
gbc.gridwidth = GridBagConstraints.REMAINDER; | |
p.add(component, gbc); | |
} | |
private void addCenterComponents(JComponent component, JPanel p, GridBagConstraints gbc){ | |
gbc.anchor = GridBagConstraints.CENTER; | |
gbc.gridwidth = GridBagConstraints.REMAINDER; | |
p.add(component, gbc); | |
} | |
private void createCandleBuilder() throws Exception{ | |
JFileChooser fileChooser = new JFileChooser(); | |
fileChooser.setFileFilter(new FileNameExtensionFilter("Текстовый файл", "txt")); | |
fileChooser.setDialogTitle("Открыть файл"); | |
fileChooser.setCurrentDirectory(new File( "." )); | |
fileChooser.showOpenDialog(this); | |
File file = fileChooser.getSelectedFile(); | |
builder = new CandleBuilder(FileUtils.readTrideQuote(file)); | |
leftLabel.setText("Файл '"+file.getName()+"' прочитан успешно."); | |
} | |
private JMenuItem createMenuItem(String text,KeyStroke keyStroke, ActionListener actionListener) { | |
JMenuItem menuItem = new JMenuItem(text); | |
menuItem.addActionListener(actionListener); | |
menuItem.setAccelerator(keyStroke); | |
return menuItem; | |
} | |
private JFreeChart createChart(DefaultHighLowDataset dataset) { | |
return ChartFactory.createCandlestickChart("Свечи", "Временной ряд", "Цена", dataset, false); | |
} | |
private void updateChart(JFreeChart chart) { | |
if(chartPanel==null){ | |
chartPanel = new ChartPanel(chart); | |
} else { | |
chartPanel.setChart(chart); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package candle.stick.view; | |
import javax.swing.*; | |
import java.awt.*; | |
public class SeparatorPanel extends JPanel { | |
private static final long serialVersionUID = 1L; | |
protected Color leftColor; | |
protected Color rightColor; | |
public SeparatorPanel(Color leftColor, Color rightColor) { | |
this.leftColor = leftColor; | |
this.rightColor = rightColor; | |
setOpaque(false); | |
} | |
@Override | |
protected void paintComponent(Graphics g) { | |
g.setColor(leftColor); | |
g.drawLine(0, 0, 0, getHeight()); | |
g.setColor(rightColor); | |
g.drawLine(1, 0, 1, getHeight()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Manifest-Version: 1.0 | |
Class-Path: jcommon-1.0.17.jar jfreechart-1.0.15.jar | |
Main-Class: candle.stick.app.Program | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PK | |