Skip to content

Commit

Permalink
refactor TickerValue to use CurrencyValue instead BigDecimal, fix Exc…
Browse files Browse the repository at this point in the history
…hangeClient accordingly
  • Loading branch information
ebirn committed Feb 24, 2014
1 parent 2130c3e commit 51578b7
Show file tree
Hide file tree
Showing 21 changed files with 193 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ public BigDecimal getQuote(Currency base, Currency quote) {
TickerValue ticker = getTicker(asset);

if(asset.getBase() == base) {
rate = ticker.getBid();
rate = ticker.getBid().getValue();
}
else {
rate = BigDecimal.ONE.divide(ticker.getAsk());
rate = BigDecimal.ONE.divide(ticker.getAsk().getValue());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,17 @@
*/
public class CurrencyValue implements Cloneable, Comparable<CurrencyValue> {

public static final MathContext CURRENCY_MATH_CONTEXT = new MathContext(7, RoundingMode.HALF_UP);
public static final MathContext CURRENCY_MATH_CONTEXT = new MathContext(8, RoundingMode.HALF_UP);

public static final CurrencyValue EMPTY = new CurrencyValue((Currency) null);

public static BigDecimal createZero() {
return new BigDecimal(0L, CURRENCY_MATH_CONTEXT);
}

private void scaleValue(BigDecimal value) {
value.setScale(CURRENCY_MATH_CONTEXT.getPrecision(), CURRENCY_MATH_CONTEXT.getRoundingMode());
}

private BigDecimal value;
private Currency currency;
Expand All @@ -40,7 +50,7 @@ public CurrencyValue(long value, Currency curr) {


public CurrencyValue(BigDecimal value, Currency curr) {
value.setScale(CURRENCY_MATH_CONTEXT.getPrecision(), CURRENCY_MATH_CONTEXT.getRoundingMode());
scaleValue(value);
this.value = value;
this.currency = curr;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package at.outdated.bitcoin.exchange.api.market;

import at.outdated.bitcoin.exchange.api.currency.Currency;
import at.outdated.bitcoin.exchange.api.currency.CurrencyValue;

import java.math.BigDecimal;
import java.util.Date;

Expand All @@ -12,23 +15,29 @@
*/
public class TickerValue extends TimedValue<double[]> {

private BigDecimal last, bid, ask, high, low, volume;
private CurrencyValue last, bid, ask, high, low, volume;

public static final int DIMENSIONS = 4;

private AssetPair asset = null;

public TickerValue() {
this.timestamp = new Date();
}

public TickerValue(AssetPair asset) {
this.timestamp = new Date();
this.asset = asset;
last = new CurrencyValue(asset.getQuote());
bid = new CurrencyValue(asset.getQuote());
ask = new CurrencyValue(asset.getQuote());
high = new CurrencyValue(asset.getQuote());
low = new CurrencyValue(asset.getQuote());

volume = new CurrencyValue(asset.getBase());
}

public static final TickerValue createNanInstance(AssetPair curr) {
TickerValue ticker = new TickerValue(null, BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, curr);
public static final TickerValue createNanInstance(AssetPair asset) {

Currency quote = asset.getQuote();

TickerValue ticker = new TickerValue(null, new CurrencyValue(quote), new CurrencyValue(quote), new CurrencyValue(quote), new CurrencyValue(quote), new CurrencyValue(quote), new CurrencyValue(quote), asset);
return ticker;
}

Expand All @@ -48,7 +57,7 @@ public TickerValue(TickerValue other) {
this.asset = other.asset;
}

public TickerValue(Date timestamp, BigDecimal last, BigDecimal bid, BigDecimal ask, BigDecimal volume, BigDecimal high, BigDecimal low, AssetPair asset) {
public TickerValue(Date timestamp, CurrencyValue last, CurrencyValue bid, CurrencyValue ask, CurrencyValue volume, CurrencyValue high, CurrencyValue low, AssetPair asset) {
this.timestamp = timestamp;
this.last = last;
this.bid = bid;
Expand All @@ -71,51 +80,51 @@ public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}

public BigDecimal getLast() {
public CurrencyValue getLast() {
return last;
}

public void setLast(BigDecimal last) {
public void setLast(CurrencyValue last) {
this.last = last;
}

public BigDecimal getBid() {
public CurrencyValue getBid() {
return bid;
}

public void setBid(BigDecimal bid) {
public void setBid(CurrencyValue bid) {
this.bid = bid;
}

public BigDecimal getAsk() {
public CurrencyValue getAsk() {
return ask;
}

public void setAsk(BigDecimal ask) {
public void setAsk(CurrencyValue ask) {
this.ask = ask;
}

public BigDecimal getHigh() {
public CurrencyValue getHigh() {
return high;
}

public void setHigh(BigDecimal high) {
public void setHigh(CurrencyValue high) {
this.high = high;
}

public BigDecimal getLow() {
public CurrencyValue getLow() {
return low;
}

public void setLow(BigDecimal low) {
public void setLow(CurrencyValue low) {
this.low = low;
}

public BigDecimal getVolume() {
public CurrencyValue getVolume() {
return volume;
}

public void setVolume(BigDecimal volume) {
public void setVolume(CurrencyValue volume) {
this.volume = volume;
}

Expand All @@ -127,22 +136,18 @@ public AssetPair getAsset() {
return asset;
}

public BigDecimal getBidAskSpread() {
public CurrencyValue getBidAskSpread() {
return bid.subtract(ask);
}

public BigDecimal getMiddle() {
return bid.add(ask).divide(new BigDecimal(2.0));
public CurrencyValue getMiddle() {
CurrencyValue middle = new CurrencyValue(bid);

return middle.add(ask).divide(new BigDecimal(2.0));
}

@Override
public String toString() {

String quote = "";
if(asset != null) {
quote = asset.getQuote().name();
}

return "Ticker: " + getMiddle() + " " + quote;
return "Ticker("+asset.getBase()+"): " + getMiddle();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ public TickerTrackCollection() {
for(TrackInterval interval : TrackInterval.values()) {
tracks.put(interval, new TickerValueTrack(interval.numSamples()));
}

latest = new TickerValue();
latest.setTimestamp(new Date(0L));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,7 @@ public TickerValue getTicker(AssetPair asset) {

if(bTicker == null) return null;

TickerValue ticker = bTicker.getTickerValue();
ticker.setAsset(asset);
return ticker; //To change body of implemented methods use File | Settings | File Templates.
return bTicker.getTickerValue(asset);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package at.outdated.bitcoin.exchange.bitcurex.jaxb;

import at.outdated.bitcoin.exchange.api.currency.Currency;
import at.outdated.bitcoin.exchange.api.currency.CurrencyValue;
import at.outdated.bitcoin.exchange.api.market.AssetPair;
import at.outdated.bitcoin.exchange.api.market.TickerValue;

import javax.xml.bind.annotation.XmlAccessType;
Expand Down Expand Up @@ -54,19 +57,22 @@ public class BitcurexTickerValue {
long unixTime;


public TickerValue getTickerValue() {
TickerValue ticker = new TickerValue();
public TickerValue getTickerValue(AssetPair asset) {

Currency quote = asset.getQuote();

TickerValue ticker = new TickerValue(asset);

ticker.setTimestamp(new Date(unixTime * 1000L));
ticker.setLast(last);
ticker.setLast(new CurrencyValue(last, quote));

ticker.setHigh(high);
ticker.setLow(low);
ticker.setHigh(new CurrencyValue(high, quote));
ticker.setLow(new CurrencyValue(low, quote));

ticker.setAsk(sell);
ticker.setBid(buy);
ticker.setAsk(new CurrencyValue(sell, quote));
ticker.setBid(new CurrencyValue(buy, quote));

ticker.setVolume(vol);
ticker.setVolume(new CurrencyValue(vol, asset.getBase()));

return ticker;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ public TickerValue getTicker(AssetPair asset) {

TickerValue value = null;
if(response != null) {
value = response.getTickerValue();
value.setAsset(asset);
value = response.getTickerValue(asset);
}

return value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package at.outdated.bitcoin.exchange.bitkonan;

import at.outdated.bitcoin.exchange.api.currency.Currency;
import at.outdated.bitcoin.exchange.api.currency.CurrencyValue;
import at.outdated.bitcoin.exchange.api.market.AssetPair;
import at.outdated.bitcoin.exchange.api.market.TickerValue;

import javax.xml.bind.annotation.XmlAccessType;
Expand Down Expand Up @@ -40,16 +43,19 @@ public class BitkonanTickerValue {
double open;


public TickerValue getTickerValue() {
public TickerValue getTickerValue(AssetPair asset) {

TickerValue ticker = new TickerValue();
Currency quote = asset.getQuote();
TickerValue ticker = new TickerValue(asset);

ticker.setLast(new CurrencyValue(last, quote));
ticker.setBid(new CurrencyValue(bid, quote));
ticker.setAsk(new CurrencyValue(ask, quote));
ticker.setHigh(new CurrencyValue(high, quote));
ticker.setLow(new CurrencyValue(low, quote));

ticker.setVolume(new CurrencyValue(volume, asset.getBase()));

ticker.setLast(last);
ticker.setVolume(volume);
ticker.setBid(bid);
ticker.setAsk(ask);
ticker.setHigh(high);
ticker.setLow(low);

return ticker;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,7 @@ public TickerValue getTicker(AssetPair asset) {

TickerValue ticker = null;
if(bticker != null) {
ticker = bticker.getTickerValue();
ticker.setAsset(asset);
ticker = bticker.getTickerValue(asset);
}

return ticker;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package at.outdated.bitcoin.exchange.bitstamp;

import at.outdated.bitcoin.exchange.api.currency.Currency;
import at.outdated.bitcoin.exchange.api.currency.CurrencyValue;
import at.outdated.bitcoin.exchange.api.jaxb.StringBigDecimalAdapter;
import at.outdated.bitcoin.exchange.api.market.AssetPair;
import at.outdated.bitcoin.exchange.api.market.TickerValue;

import javax.xml.bind.annotation.XmlAccessType;
Expand Down Expand Up @@ -46,19 +49,20 @@ public class BitstampTickerValue {

Date timeStamp = new Date();

public TickerValue getTickerValue() {
public TickerValue getTickerValue(AssetPair asset) {

TickerValue ticker = new TickerValue();
Currency quote = asset.getQuote();
TickerValue ticker = new TickerValue(asset);
ticker.setTimestamp(timeStamp);

ticker.setLast(last);
ticker.setHigh(high);
ticker.setLow(low);
ticker.setLast(new CurrencyValue(last, quote));
ticker.setHigh(new CurrencyValue(high, quote));
ticker.setLow(new CurrencyValue(low, quote));

ticker.setAsk(ask);
ticker.setBid(bid);
ticker.setAsk(new CurrencyValue(ask, quote));
ticker.setBid(new CurrencyValue(bid, quote));

ticker.setVolume(volume);
ticker.setVolume(new CurrencyValue(volume, asset.getBase()));

return ticker;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ public TickerValue getTicker(AssetPair asset) {

BtcETickerValue btcETickerValue = response.getTicker();

TickerValue value = btcETickerValue.getTickerValue();
value.setAsset(asset);
TickerValue value = btcETickerValue.getTickerValue(asset);


return value;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package at.outdated.bitcoin.exchange.btce;

import at.outdated.bitcoin.exchange.api.currency.Currency;
import at.outdated.bitcoin.exchange.api.currency.CurrencyValue;
import at.outdated.bitcoin.exchange.api.market.AssetPair;
import at.outdated.bitcoin.exchange.api.market.TickerValue;

import javax.xml.bind.annotation.XmlAccessType;
Expand Down Expand Up @@ -60,19 +63,21 @@ public class BtcETickerValue {
@XmlElement
protected long updated;

public TickerValue getTickerValue() {
public TickerValue getTickerValue(AssetPair asset) {

TickerValue ticker = new TickerValue();
Currency quote = asset.getQuote();

TickerValue ticker = new TickerValue(asset);

ticker.setTimestamp(new Date(updated*1000));
ticker.setLast(last);
ticker.setVolume(vol_cur);
ticker.setBid(sell);
ticker.setAsk(buy);
ticker.setHigh(high);
ticker.setLow(low);
ticker.setLast(new CurrencyValue(last, quote));

ticker.setBid(new CurrencyValue(sell, quote));
ticker.setAsk(new CurrencyValue(buy, quote));
ticker.setHigh(new CurrencyValue(high, quote));
ticker.setLow(new CurrencyValue(low, quote));

ticker.setVolume(new CurrencyValue(vol_cur, asset.getBase()));

return ticker;
}
Expand Down
Loading

0 comments on commit 51578b7

Please sign in to comment.