OpenSeSim/src/sesim/OHLCData.java

202 lines
5.4 KiB
Java
Raw Normal View History

2017-01-08 13:16:00 +01:00
/*
* Copyright (c) 2017, 7u83 <7u83@mail.ru>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
2017-01-22 07:03:16 +01:00
package sesim;
2017-01-08 13:16:00 +01:00
import java.util.*;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
2017-02-18 23:32:03 +01:00
public class OHLCData {
private float max = 0;
private float min = 0;
private int frame_size = 60000;
int max_size = 100;
public OHLCData() {
2017-01-22 07:03:16 +01:00
}
2017-02-18 23:32:03 +01:00
2017-03-27 01:31:55 +02:00
/**
* Create an OHLCData object that stores OHLCDataItems
*
* @param frame_size Time frame stored in one OHLCDataItem
*/
2017-02-18 23:32:03 +01:00
public OHLCData(int frame_size) {
this.frame_size = frame_size;
2017-01-22 07:03:16 +01:00
}
2017-01-09 08:25:29 +01:00
public float getMax() {
2017-01-08 13:16:00 +01:00
return max;
}
2017-02-18 23:32:03 +01:00
public float getMin() {
2017-01-22 07:03:16 +01:00
return min;
}
2017-02-18 23:32:03 +01:00
public int size() {
2017-01-22 07:03:16 +01:00
return data.size();
}
2017-02-18 23:32:03 +01:00
2017-03-27 01:31:55 +02:00
/**
*
* @return Time frame of OHLCDataItem
*/
2017-02-18 23:32:03 +01:00
public int getFrameSize() {
2017-01-22 07:03:16 +01:00
return this.frame_size;
}
2017-02-18 23:32:03 +01:00
2017-03-27 01:31:55 +02:00
/**
* Get the minimum and maximum value between two OHLCDataItems
* @param first Position of first OHLCDataItem
* @param last Position of last OHCLDataItem
* @return MinMax object containing the calculated values
*/
2017-02-18 23:32:03 +01:00
public MinMax getMinMax(int first, int last) {
2017-10-02 16:15:33 +02:00
if (data.isEmpty())
return new MinMax(0,0);
2017-02-18 23:32:03 +01:00
if (first >= data.size()) {
OHLCDataItem di = data.get(data.size() - 1);
return new MinMax(di.low, di.high);
}
2017-01-23 18:56:09 +01:00
OHLCDataItem di = data.get(first);
2017-02-18 23:32:03 +01:00
MinMax minmax = new MinMax(di.low, di.high);
for (int i = first + 1; i < last && i < data.size(); i++) {
2017-01-23 18:56:09 +01:00
di = data.get(i);
2017-02-18 23:32:03 +01:00
if (di.low < minmax.min) {
minmax.min = di.low;
}
if (di.high > minmax.max) {
minmax.max = di.high;
}
}
return minmax;
}
2017-10-08 00:16:39 +02:00
/**
* Get minimum an maximum from volume
* @param first
* @param last
* @return MinMax found
*/
2017-02-18 23:32:03 +01:00
public MinMax getVolMinMax(int first, int last) {
if (first >= data.size()) {
OHLCDataItem di = data.get(data.size() - 1);
return new MinMax(di.volume, di.volume);
}
OHLCDataItem di = data.get(first);
MinMax minmax = new MinMax(di.volume, di.volume);
for (int i = first + 1; i < last && i < data.size(); i++) {
di = data.get(i);
if (di.volume < minmax.min) {
minmax.min = di.volume;
}
if (di.volume > minmax.max) {
minmax.max = di.volume;
2017-01-23 18:56:09 +01:00
}
}
return minmax;
}
2017-01-09 08:25:29 +01:00
2017-01-22 07:03:16 +01:00
long getFrameStart(long time) {
2017-01-09 08:25:29 +01:00
2017-01-22 07:03:16 +01:00
long rt = time / frame_size;
return rt * frame_size;
2017-01-09 08:25:29 +01:00
2017-01-08 13:16:00 +01:00
}
2017-01-09 08:25:29 +01:00
2017-01-22 07:03:16 +01:00
public ArrayList<OHLCDataItem> data = new ArrayList<>();
2017-02-18 23:32:03 +01:00
public OHLCDataItem get(int n) {
2017-01-22 07:03:16 +01:00
return data.get(n);
}
public void set(int n, OHLCDataItem item){
data.set(n,item);
}
2017-02-18 23:32:03 +01:00
2017-11-17 23:09:06 +01:00
public void add(OHLCDataItem item){
data.add(item);
}
2017-02-18 23:32:03 +01:00
private void updateMinMax(float price) {
if (price > max) {
2017-01-12 23:16:37 +01:00
max = price;
}
2017-02-18 23:32:03 +01:00
if (price < min) {
2017-01-12 23:16:37 +01:00
min = price;
}
2017-01-09 08:25:29 +01:00
}
2017-02-18 23:32:03 +01:00
public Iterator<OHLCDataItem> iterator() {
2017-01-22 07:03:16 +01:00
return data.iterator();
2017-02-18 23:32:03 +01:00
}
2017-01-09 08:25:29 +01:00
2017-01-22 07:03:16 +01:00
// Start and end of current frame
private long current_frame_end = 0;
2017-02-18 23:32:03 +01:00
private long current_frame_start = 0;
2017-01-09 08:25:29 +01:00
2017-01-22 07:03:16 +01:00
public boolean realTimeAdd(long time, float price, float volume) {
2017-02-18 23:32:03 +01:00
2017-01-22 07:03:16 +01:00
if (time >= current_frame_end) {
2017-02-18 23:32:03 +01:00
if (current_frame_end == 0) {
this.min = price;
this.max = price;
2017-01-12 23:16:37 +01:00
}
2017-02-18 23:32:03 +01:00
long last_frame_start = current_frame_start;
2017-01-22 07:03:16 +01:00
current_frame_start = getFrameStart(time);
2017-02-18 23:32:03 +01:00
2017-01-22 07:03:16 +01:00
current_frame_end = current_frame_start + frame_size;
2017-02-18 23:32:03 +01:00
//System.out.printf("TA %d TE %d\n",this.current_frame_start,this.current_frame_end);
2017-01-22 07:03:16 +01:00
data.add(new OHLCDataItem(this.current_frame_start, price, volume));
2017-01-12 23:16:37 +01:00
this.updateMinMax(price);
2017-01-09 08:25:29 +01:00
return true;
}
2017-01-12 23:16:37 +01:00
this.updateMinMax(price);
2017-02-18 23:32:03 +01:00
2017-01-22 07:03:16 +01:00
OHLCDataItem d = data.get(data.size() - 1);
2017-01-09 08:25:29 +01:00
boolean rc = d.update(price, volume);
return rc;
}
2017-01-08 13:16:00 +01:00
}