我将库存数据保存在mySql表@ frame 1分钟内,这样1行就是1分钟的数据。保存的数据如下
由于市场交易区间从0900到1730,我每天有511行。
我需要将这些日内数据压缩(聚合)为1个每日数据(仅限一行),以便
每日开放 - >第一个日内开盘价(0900开盘价)
每日高 - >所有511个日内值行的最高值
每日低点 - >所有511个日内值行的最低值
每日关闭 - >最新的日内交易价值(1730收盘价)
每日交易量 - >所有511个日内体积值行的总和
我需要尽可能快地执行此压缩,因为我会绘制数百个每日数据。
任何帮助都非常感谢:这是我的完整代码
import importexportdata.DataItem;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
public class AggregateByDay {
String dbTabName = "";
String sTimeFrom = "";
String sTimeTo = "";
short iTotBar = 0;
boolean volumeFieldIsOnTable;
LinkedList<DataItem> data_aggregated = null;
Dividends dividendsHdl = null;
Connection con = null;
ResultSet rs = null;
Statement st = null;
PreparedStatement selectReview=null;
Statement stmtPrepareInitRow=null;
ResultSet rsSelectPrepareInitRow = null;
public AggregateByDay(String dbTabName, String timeFrom, String timeTo, short iTotBar, boolean volumeFieldIsOnTable, Dividends dividendsHdl){
this.dbTabName = dbTabName;
this.sTimeFrom = timeFrom;
this.sTimeTo = timeTo;
this.iTotBar = iTotBar;
this.volumeFieldIsOnTable = volumeFieldIsOnTable;
this.dividendsHdl = dividendsHdl;
}
private void connectToDb(){
try {
Class.forName("com.mysql.jdbc.Driver");
//String connectionUrl = "jdbc:mysql://localhost:3306/borsaitalia?" +
// "user="+_dBUserName+"&password="+_dBPassword;
String connectionUrl = DbConnect.getConnectionUrl();
con = DriverManager.getConnection(connectionUrl);
} //end try Class.forName("com.mysql.jdbc.Driver");
catch (SQLException ex) {
System.out.println("Error "+ ex.getMessage ()+" "+ex.toString());
} catch (ClassNotFoundException cE) {
System.out.println("Class Not Found Exception: "+ cE.toString());
}
}
private String getLastFirstDayLower(String _date, String _DbTabName){
String lastDate = "";
try {
st = con.createStatement();
if ( "".equals(_date)) {
//Get the most recent date
rs = st.executeQuery("SELECT date FROM "+_DbTabName+" ORDER BY date DESC, time DESC LIMIT 1;");
}
else{
rs = st.executeQuery("SELECT date FROM "+_DbTabName+" WHERE date < "+_date+" ORDER BY date DESC, time DESC LIMIT 1;");
}
while (rs.next()){
lastDate = rs.getString("date");
}
if (st != null) {
st.close();
st = null;
}
} //end try Class.forName("com.mysql.jdbc.Driver");
catch (SQLException ex) {
System.out.println("Error "+ ex.getMessage ()+" "+ex.toString());
}
return lastDate;
}
private String getLastFirstDayLowerNext(String _date, String _DbTabName){
String lastDate = "";
try {
if (rsSelectPrepareInitRow.next()){
lastDate = rsSelectPrepareInitRow.getString("date");
}
//System.out.println("lastDate "+lastDate);
} //end try Class.forName("com.mysql.jdbc.Driver");
catch (SQLException ex) {
System.out.println("Error "+ ex.getMessage ()+" "+ex.toString());
}
return lastDate;
}
private LinkedList<DataItem> getDbOneMinuteDataForDay(String _date, String _sTimeFrom, String _sTimeTo){
String sDate = "", sTime = "";
double dOpen = 0d, dHigh = 0d, dLow = 0d, dClose = 0d;
int iVolume = 0;
float dividendsValue = 0;
LinkedList<DataItem> init_one_minute_data = new LinkedList<>();
try {
selectReview.setString(1, _date);
selectReview.setString(2, _sTimeFrom);
selectReview.setString(3, _sTimeTo);
rs = selectReview.executeQuery();
while (rs.next()){
sDate = rs.getString("date");
sTime = rs.getString("time");
dOpen = rs.getDouble("open");
dHigh = rs.getDouble("high");
dLow = rs.getDouble("low");
dClose = rs.getDouble("close");
if (volumeFieldIsOnTable)
iVolume = rs.getInt("volume");
else
iVolume = 0;
DataItem dataItem = new DataItem();
java.util.Date date = null;
try {
date = new SimpleDateFormat("yyyyMMdd HHmm").parse(sDate+" "+sTime);
} catch (ParseException ex) {
Logger.getLogger(AggregateByDay.class.getName()).log(Level.SEVERE, null, ex);
}
dataItem.setDate(date);
if (dividendsHdl != null) {
dividendsValue = dividendsHdl.getTotalDividendsFromDate(sDate);
}
dataItem.setOpen(dOpen-dividendsValue);
dataItem.setHigh(dHigh-dividendsValue);
dataItem.setLow(dLow-dividendsValue);
dataItem.setClose(dClose-dividendsValue);
dataItem.setVolume(iVolume);
init_one_minute_data.add(dataItem);
}
} //end try Class.forName("com.mysql.jdbc.Driver");
catch (SQLException ex) {
System.out.println("Error "+ ex.getMessage ()+" "+ex.toString());
}/* catch (ClassNotFoundException cE) {
System.out.println("Class Not Found Exception: "+ cE.toString());
}*/
return init_one_minute_data;
}
public void getAggregateValue(ArrayList<DataItem> externalDataList) {
String sDate;
short count_min;
Calendar calendarCurrWeek = null;
int i, n;
boolean end;
LinkedList<DataItem> one_minute_data = null;
LinkedList<DataItem> data_one_day_aggregated = new LinkedList<>();
java.util.Date dDateTimeRgp = null;
int iVolumeRaggruppato = 0;
double dOpenRgp = 0d, dHighRgp = 0d, dLowRgp = 0d, dCloseRgp = 0d;
data_aggregated = new LinkedList<>();
connectToDb();
sDate = getLastFirstDayLower("", dbTabName);
if ( "".equals(sDate) ) {
return;
}
end = false;
try {
//selectPrepareInitRow = con.prepareStatement("SELECT date FROM "+DbTabName+" WHERE date < ? ORDER BY date DESC, time DESC LIMIT 1;");
stmtPrepareInitRow = con.createStatement();
rsSelectPrepareInitRow = stmtPrepareInitRow.executeQuery("SELECT DISTINCT date FROM "+dbTabName+" WHERE date < "+sDate+" ORDER BY date DESC, time DESC;");
} catch (SQLException ex) {
Logger.getLogger(AggregateByDay.class.getName()).log(Level.SEVERE, null, ex);
}
if (volumeFieldIsOnTable) {
try {
selectReview = con.prepareStatement("SELECT open, high, low, close, volume, date, time FROM "+dbTabName+" WHERE date = ? AND time >= ? AND time <= ? ORDER BY time ASC;");
} catch (SQLException ex) {
Logger.getLogger(AggregateByDay.class.getName()).log(Level.SEVERE, null, ex);
}
}
else {
try {
selectReview = con.prepareStatement("SELECT open, high, low, close, date, time FROM "+dbTabName+" WHERE date = ? AND time >= ? AND time <= ? ORDER BY time ASC;");
} catch (SQLException ex) {
Logger.getLogger(AggregateByDay.class.getName()).log(Level.SEVERE, null, ex);
}
}
do {
count_min = 1;
iVolumeRaggruppato = -1;
i = 0;
DataItem one_minute_data_item;
one_minute_data = getDbOneMinuteDataForDay(sDate, sTimeFrom, sTimeTo);
n = one_minute_data.size();
while ((i < n)&&(!end)){
one_minute_data_item = one_minute_data.get(i);
if (count_min == 1){
iVolumeRaggruppato = one_minute_data_item.getVolume();
dOpenRgp = one_minute_data_item.getOpen();
dHighRgp = one_minute_data_item.getHigh();
dLowRgp = one_minute_data_item.getLow();
dDateTimeRgp = one_minute_data_item.getDate();
}
else{
iVolumeRaggruppato += one_minute_data_item.getVolume();
if (dHighRgp < one_minute_data_item.getHigh()) {
dHighRgp = one_minute_data_item.getHigh();
}
if (dLowRgp > one_minute_data_item.getLow()) {
dLowRgp = one_minute_data_item.getLow();
}
}
dCloseRgp = one_minute_data_item.getClose();
if (i == (n-1)){
DataItem dataItemRgp = new DataItem();
dataItemRgp.setDate(dDateTimeRgp);
dataItemRgp.setOpen(dOpenRgp);
dataItemRgp.setHigh(dHighRgp);
dataItemRgp.setLow(dLowRgp);
dataItemRgp.setClose(dCloseRgp);
dataItemRgp.setVolume(iVolumeRaggruppato);
data_one_day_aggregated.add(dataItemRgp);
count_min = 1;
}
else {
count_min++;
}
i++;
}//end while ((i < n)&&(!end))
one_minute_data.clear();
//fine raggruppamento di 1 intero giorno, inserisco i dati
if (data_one_day_aggregated.size() > 0){
if (data_aggregated.size() >= iTotBar) {
end = true;
}
else {
data_aggregated.addFirst(data_one_day_aggregated.get(0));
}
}
data_one_day_aggregated.clear();
if (!end){
sDate = getLastFirstDayLowerNext(sDate, dbTabName);
if (sDate.equals("")) {
end = true;
}
}
} while (!end);
if (stmtPrepareInitRow != null) {
try {
stmtPrepareInitRow.close();
stmtPrepareInitRow = null;
} catch (SQLException ex) {
Logger.getLogger(AggregateByDay.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (selectReview != null) {
try {
selectReview.close();
selectReview = null;
} catch (SQLException ex) {
Logger.getLogger(AggregateByDay.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (con != null) {
try {
con.close();
con = null;
} catch (SQLException ex) {
Logger.getLogger(AggregateByDay.class.getName()).log(Level.SEVERE, null, ex);
}
}
one_minute_data.clear();
data_one_day_aggregated.clear();
if ((data_aggregated != null)&&(data_aggregated.size() > 0)) {
externalDataList.ensureCapacity(data_aggregated.size());
for (DataItem item : data_aggregated) {
externalDataList.add(item);
}
}
data_aggregated.clear();
}
}