Improving the BucketObject class and introducing the Converter class

master
Nelson R. Perez 2016-12-26 21:54:44 -05:00
parent 01766c856a
commit c26687e591
6 changed files with 177 additions and 26 deletions

View File

@ -0,0 +1,112 @@
package de.bitsharesmunich.graphenej;
import java.math.BigDecimal;
import de.bitsharesmunich.graphenej.errors.IncompleteAssetError;
import de.bitsharesmunich.graphenej.models.BucketObject;
/**
* Converter class used to translate the market information contained in a BucketObject instance.
*
* Created by nelson on 12/23/16.
*/
public class Converter {
private final String TAG = this.getClass().getName();
public static final int OPEN_VALUE = 0;
public static final int CLOSE_VALUE = 1;
public static final int HIGH_VALUE = 2;
public static final int LOW_VALUE = 3;
public static final int BASE_TO_QUOTE = 100;
public static final int QUOTE_TO_BASE = 101;
private Asset base;
private Asset quote;
private BucketObject bucket;
public Converter(Asset base, Asset quote, BucketObject bucket){
this.base = base;
this.quote = quote;
this.bucket = bucket;
}
/**
* Method used to obtain the equivalence between two assets considering their precisions
* and given the specific time bucket passed in the constructor.
*
* The resulting double value will tell us how much of a given asset, a unit of
* its pair is worth.
*
* The second argument is used to specify which of the assets should
* be taken as a unit reference.
*
* For instance if used with the BASE_TO_QUOTE constant, this method will tell us how
* many of the quote asset will make up for a unit of the base asset. And the opposite
* is true for the QUOTE_TO_BASE contant.
*
* @param bucketAttribute: The desired bucket attribute to take in consideration. Can
* be any of the following: OPEN_VALUE, CLOSE_VALUE, HIGH_VALUE or
* LOW_VALUE.
* @param direction: One of two constants 'BASE_TO_QUOTE' or 'QUOTE_TO_BASE' used to specify
* which of the two assets is the one used as a unitary reference.
* @return: double value representing how much of one asset, a unit of the paired asset
* was worth at the point in time specified by the time bucket and the bucket parameter.
*/
public double getConversionRate(int bucketAttribute, int direction){
if(this.base.getPrecision() == -1 || this.quote.getPrecision() == -1){
throw new IncompleteAssetError();
}
BigDecimal baseValue;
BigDecimal quoteValue;
switch (bucketAttribute){
case OPEN_VALUE:
baseValue = bucket.open_base;
quoteValue = bucket.open_quote;
break;
case CLOSE_VALUE:
baseValue = bucket.close_base;
quoteValue = bucket.close_quote;
break;
case HIGH_VALUE:
baseValue = bucket.high_base;
quoteValue = bucket.high_quote;
break;
case LOW_VALUE:
baseValue = bucket.low_base;
quoteValue = bucket.low_quote;
break;
default:
baseValue = bucket.close_base;
quoteValue = bucket.close_quote;
}
double basePrecisionAdjusted = baseValue.divide(BigDecimal.valueOf((long) Math.pow(10, base.getPrecision()))).doubleValue();
double quotePrecisionAdjusted = quoteValue.divide(BigDecimal.valueOf((long) Math.pow(10, quote.getPrecision()))).doubleValue();
if(direction == QUOTE_TO_BASE){
return basePrecisionAdjusted / quotePrecisionAdjusted;
}else{
return quotePrecisionAdjusted / basePrecisionAdjusted;
}
}
/**
* Converts a given asset amount to the corresponding pair used when creating this class.
* @param assetAmount: The asset to convert from.
* @param bucketAttribute: The bucket attribute to use as a reference. Possible values are OPEN_VALUE,
* CLOSE_VALUE, HIGH_VALUE or LOW_VALUE.
* @return: The converted value in base units, that is the number of a unit x 10^precision
*/
public long convert(AssetAmount assetAmount, int bucketAttribute) {
double conversionRate = 0;
double precisionFactor = 0.0;
if(assetAmount.getAsset().equals(this.base)){
conversionRate = this.getConversionRate(bucketAttribute, BASE_TO_QUOTE);
precisionFactor = Math.pow(10, this.quote.getPrecision()) / Math.pow(10, this.base.getPrecision());
}else if(assetAmount.getAsset().equals(this.quote)){
conversionRate = this.getConversionRate(bucketAttribute, QUOTE_TO_BASE);
precisionFactor = Math.pow(10, this.base.getPrecision()) / Math.pow(10, this.quote.getPrecision());
}
long assetAmountValue = assetAmount.getAmount().longValue();
long convertedBaseValue = (long) (assetAmountValue * conversionRate * precisionFactor);
return convertedBaseValue;
}
}

View File

@ -22,4 +22,4 @@ public class RPC {
public static final String CALL_GET_LIMIT_ORDERS = "get_limit_orders";
public static final String CALL_GET_TRADE_HISTORY = "get_trade_history";
public static final String CALL_GET_MARKET_HISTORY = "get_market_history";
}
}

View File

@ -986,7 +986,17 @@ public class Test {
List<BucketObject> bucketList = (List<BucketObject>) response.result;
if(bucketList.size() > 0){
BucketObject bucket = bucketList.get(0);
System.out.println(String.format("bucket. high_base: %d, high_quote: %d", bucket.high_base, bucket.high_quote));
Asset base = bucket.key.base;
Asset quote = bucket.key.quote;
base.setPrecision(5);
quote.setPrecision(4);
System.out.println(String.format("Base. symbol: %s, precision: %d", base.getObjectId(), base.getPrecision()));
System.out.println(String.format("Quote. symbol: %s, precision: %d", quote.getObjectId(), quote.getPrecision()));
Converter converter = new Converter(base, quote, bucket);
double rate = converter.getConversionRate(Converter.CLOSE_VALUE, Converter.BASE_TO_QUOTE);
System.out.println(String.format("Conversion rate is 1 base -> %f quote", rate));
double rate2 = converter.getConversionRate(Converter.CLOSE_VALUE, Converter.QUOTE_TO_BASE);
System.out.println(String.format("Conversion rate is 1 quote -> %f base", rate2));
}
}
@ -1011,11 +1021,11 @@ public class Test {
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);
Asset BTS = new Asset("1.3.0");
Asset USD = new Asset("1.3.121");
Asset USD = new Asset("1.3.121", "USD", 4);
Asset BTS = new Asset("1.3.0", "BTS", 5);
long bucket = 3600;
mWebSocket.addListener(new GetMarketHistory(USD, BTS, bucket, cal.getTime(), cal.getTime(), listener));
mWebSocket.addListener(new GetMarketHistory(BTS, USD, bucket, cal.getTime(), cal.getTime(), listener));
mWebSocket.connect();
} catch (NoSuchAlgorithmException e) {
System.out.println("NoSuchAlgorithmException. Msg: " + e.getMessage());

View File

@ -0,0 +1,10 @@
package de.bitsharesmunich.graphenej.errors;
/**
* Created by nelson on 12/25/16.
*/
public class IncompleteAssetError extends RuntimeException{
public IncompleteAssetError(){
super("The asset used in this method is probably incomplete, Assets instances can be created with just its id information but this context requires more information");
}
}

View File

@ -4,7 +4,7 @@ import com.google.gson.*;
import de.bitsharesmunich.graphenej.Asset;
import java.lang.reflect.Type;
import java.math.BigInteger;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
@ -31,16 +31,16 @@ public class BucketObject {
public String id;
public Key key;
public BigInteger high_base;
public BigInteger high_quote;
public BigInteger low_base;
public BigInteger low_quote;
public BigInteger open_base;
public BigInteger open_quote;
public BigInteger close_base;
public BigInteger close_quote;
public BigInteger base_volume;
public BigInteger quote_volume;
public BigDecimal high_base;
public BigDecimal high_quote;
public BigDecimal low_base;
public BigDecimal low_quote;
public BigDecimal open_base;
public BigDecimal open_quote;
public BigDecimal close_base;
public BigDecimal close_quote;
public BigDecimal base_volume;
public BigDecimal quote_volume;
public static class Key {
public Asset base;
@ -55,16 +55,16 @@ public class BucketObject {
public BucketObject deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject jsonBucket = json.getAsJsonObject();
BucketObject bucket = new BucketObject();
bucket.high_base = jsonBucket.get(KEY_HIGH_BASE).getAsBigInteger();
bucket.high_quote = jsonBucket.get(KEY_HIGH_QUOTE).getAsBigInteger();
bucket.low_base = jsonBucket.get(KEY_LOW_BASE).getAsBigInteger();
bucket.low_quote = jsonBucket.get(KEY_LOW_QUOTE).getAsBigInteger();
bucket.open_base = jsonBucket.get(KEY_OPEN_BASE).getAsBigInteger();
bucket.open_quote = jsonBucket.get(KEY_OPEN_QUOTE).getAsBigInteger();
bucket.close_base = jsonBucket.get(KEY_CLOSE_BASE).getAsBigInteger();
bucket.close_quote = jsonBucket.get(KEY_CLOSE_QUOTE).getAsBigInteger();
bucket.base_volume = jsonBucket.get(KEY_BASE_VOLUME).getAsBigInteger();
bucket.quote_volume = jsonBucket.get(KEY_QUOTE_VOLUME).getAsBigInteger();
bucket.high_base = jsonBucket.get(KEY_HIGH_BASE).getAsBigDecimal();
bucket.high_quote = jsonBucket.get(KEY_HIGH_QUOTE).getAsBigDecimal();
bucket.low_base = jsonBucket.get(KEY_LOW_BASE).getAsBigDecimal();
bucket.low_quote = jsonBucket.get(KEY_LOW_QUOTE).getAsBigDecimal();
bucket.open_base = jsonBucket.get(KEY_OPEN_BASE).getAsBigDecimal();
bucket.open_quote = jsonBucket.get(KEY_OPEN_QUOTE).getAsBigDecimal();
bucket.close_base = jsonBucket.get(KEY_CLOSE_BASE).getAsBigDecimal();
bucket.close_quote = jsonBucket.get(KEY_CLOSE_QUOTE).getAsBigDecimal();
bucket.base_volume = jsonBucket.get(KEY_BASE_VOLUME).getAsBigDecimal();
bucket.quote_volume = jsonBucket.get(KEY_QUOTE_VOLUME).getAsBigDecimal();
bucket.key = new Key();
String baseId = jsonBucket.get(KEY_KEY).getAsJsonObject().get(KEY_BASE).getAsString();
String quoteId = jsonBucket.get(KEY_KEY).getAsJsonObject().get(KEY_QUOTE).getAsString();

View File

@ -0,0 +1,19 @@
package de.bitsharesmunich.graphenej;
import org.junit.*;
import static org.junit.Assert.*;
/**
* Created by nelson on 12/24/16.
*/
public class AssetTest {
@org.junit.Test
public void equals() throws Exception {
Asset bts = new Asset("1.3.0");
Asset bitUSD = new Asset("1.3.121");
assertNotEquals("Different assets should not be equal", bts, bitUSD);
}
}