Skip to content

Commit

Permalink
Add support for timestamp field for Balance Candlesticks
Browse files Browse the repository at this point in the history
  • Loading branch information
Daan Kooij committed Jul 16, 2018
1 parent db5e9e6 commit 023d9e7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/main/java/nl/utwente/ing/misc/date/IntervalHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class IntervalHelper {
*
* @param intervalPeriod The size of the intervals to be generated.
* @param amount The amount of intervals to be generated.
* @return An array containing (amount
* @return An array containing LocalDateTime objects representing the requested intervals.
*/
public static LocalDateTime[] getIntervals(IntervalPeriod intervalPeriod, int amount) {
LocalDateTime[] intervals = new LocalDateTime[amount + 1];
Expand Down
20 changes: 16 additions & 4 deletions src/main/java/nl/utwente/ing/model/bean/BalanceCandlestick.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,23 @@
public class BalanceCandlestick {

private float open, close, high, low, volume;
private long timestamp;

/**
* The constructor of BalanceCandlestick.
* Initially, only the opening balance is specified, after which this balance can be modified by using the mutate
* method. Along the way, this bean keeps track of the opening balance, the closing balance, the highest balance,
* the lowest balance and the volume of all the mutations combined.
* Initially, only the opening balance and the opening timestamp are specified, after which this balance can be
* modified by using the mutate method. Along the way, this bean keeps track of the opening balance, the closing
* balance, the highest balance, the lowest balance and the volume of all the mutations combined.
*
* @param open The opening balance of this BalanceCandlestick.
*/
public BalanceCandlestick(float open) {
public BalanceCandlestick(float open, long timestamp) {
this.open = open;
this.close = open;
this.high = open;
this.low = open;
this.volume = 0;
this.timestamp = timestamp;
}

/**
Expand Down Expand Up @@ -71,6 +73,16 @@ public float getVolume() {
return volume;
}

/**
* Method used to retrieve the unix (seconds) timestamp indicating the beginning of the period of the
* BalanceCandlestick.
*
* @return The unix (seconds) timestamp indicating the beginning of the period of the BalanceCandlestick.
*/
public long getTimestamp() {
return timestamp;
}

/**
* Method used to indicate a mutation of the balance of BalanceCandlestick.
* Along the way, this method updates the closing balance, the highest balance, the lowest balance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.sql.Connection;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.UUID;

Expand Down Expand Up @@ -466,10 +467,12 @@ public ArrayList<BalanceCandlestick> getBalanceHistory(String sessionID, Interva
ArrayList<BalanceCandlestick> candlesticks = new ArrayList<>();
int index = 0;
for (int i = 1; i <= amount; i++) {
LocalDateTime interval = intervals[i];
BalanceCandlestick candlestick = new BalanceCandlestick(balance);
LocalDateTime startInterval = intervals[i - 1];
LocalDateTime endInterval = intervals[i];
long startUnixTime = startInterval.toEpochSecond(ZoneOffset.UTC); // Convert start of interval to UNIX time
BalanceCandlestick candlestick = new BalanceCandlestick(balance, startUnixTime);
while (index < transactions.size() &&
!IntervalHelper.isSmallerThan(interval, transactions.get(index).getDate())) {
!IntervalHelper.isSmallerThan(endInterval, transactions.get(index).getDate())) {
if (transactions.get(index).getType().equals("deposit")) {
candlestick.mutation(transactions.get(index).getAmount());
} else {
Expand Down

0 comments on commit 023d9e7

Please sign in to comment.