 |
Financial style for OHLC
Source code name: "xhart_ohic.java"
Programming language: Java
Topic: Finance/Plots
DMelt Version 3. Last modified: 04/05/1977. License: Pro
https://datamelt.org/code/cache/xhart_ohic_4114.java
To run this script using the DMelt IDE,
copy the above URL link to the menu [File]→[Read script from URL] of the DMelt IDE.
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.knowm.xchart.*;
import org.knowm.xchart.style.Styler;
/**
* Demonstrates the following:
*
*
* - Tooltips
*
- LegendPosition.OutsideS *
*
- default OHLCSeriesRenderStyle.Candle
*/
public class xchart_ohic {
public static void main(String[] args) {
OHLCChart chart = getChart();
new SwingWrapper<>(chart).displayChart();
}
public static void populateData(
List xData,
List openData,
List highData,
List lowData,
List closeData) {
// generate data
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = sdf.parse("2017-01-01");
populateData(date, 5000.0, 20, xData, openData, highData, lowData, closeData);
} catch (ParseException e) {
e.printStackTrace();
}
}
// TODO move this? It's shared by all the OHLC classes
public static void populateData(
Date startDate,
double startPrice,
int count,
List xData,
List openData,
List highData,
List lowData,
List closeData) {
Calendar cal = Calendar.getInstance();
cal.setTime(startDate);
double data = startPrice;
for (int i = 1; i <= count; i++) {
// add 1 day
// startDate = new Date(startDate.getTime() + (1 * 1000 * 60 * 60 * 24));
// xData.add(startDate);
cal.add(Calendar.DATE, 1);
xData.add(cal.getTime());
double previous = data;
data = getNewClose(data, startPrice);
openData.add(previous);
highData.add(getHigh(Math.max(previous, data), startPrice));
lowData.add(getLow(Math.min(previous, data), startPrice));
closeData.add(data);
}
}
private static double getHigh(double close, double orig) {
return close + (orig * Math.random() * 0.02);
}
private static double getLow(double close, double orig) {
return close - (orig * Math.random() * 0.02);
}
private static double getNewClose(double close, double orig) {
return close + (orig * (Math.random() - 0.5) * 0.1);
}
static public OHLCChart getChart() {
// Create Chart
OHLCChart chart = new OHLCChartBuilder().width(600).height(400).title("OHLCChart01").build();
// Customize Chart
chart.getStyler().setLegendPosition(Styler.LegendPosition.OutsideS);
chart.getStyler().setLegendLayout(Styler.LegendLayout.Horizontal);
List xData = new ArrayList<>();
List openData = new ArrayList<>();
List highData = new ArrayList<>();
List lowData = new ArrayList<>();
List closeData = new ArrayList<>();
populateData(xData, openData, highData, lowData, closeData);
xData = null;
chart.addSeries("Series", xData, openData, highData, lowData, closeData);
chart.getStyler().setToolTipsEnabled(true);
return chart;
}
}
You see the box below because you did not login.