Git Product home page Git Product logo

Comments (3)

BadRookiDev avatar BadRookiDev commented on June 16, 2024

doubles are not stored exactly some times if i am correct.
you need to round it.
I solved this on a really dirty way:

`public double determineRoundingPrice(String tickSize, double desired){
String decimalformat = "";
String out = "";
char splitter = '"';
String splt = "" + splitter;
String[] ssSplitted = tickSize.split(splt);
System.out.println("tickSize: "+tickSize);
String firstchar = ""+ssSplitted[1].charAt(0);
String secondchar = ""+ssSplitted[1].charAt(1);

    if (Objects.equals(firstchar, "0")) {
        System.out.println("ts: " + ssSplitted[1]);
        System.out.println("desired: " + desired);
        String regex = "\\.";
        String[] decimalsSplit = ssSplitted[1].split(regex);
        for (int i = 0; i < decimalsSplit[0].length(); i++) {
            decimalformat += "#";
        }
        decimalformat += ".";

        int amountofhastags = 0;
        for (int i = 0; i < decimalsSplit[1].length(); i++) {
            String one = ""+decimalsSplit[1].charAt(i);
            if (Objects.equals(one, "1")) {
                amountofhastags=i+1;
                i = ssSplitted[1].length();
            }
        }

        for (int i = 0; i < amountofhastags; i++) {
            decimalformat += "#";
        }

        System.out.println(desired);
        DecimalFormat df = new DecimalFormat(decimalformat);
        df.setRoundingMode(RoundingMode.FLOOR);
        String resultString = df.format(desired);
        System.out.println(resultString);
        String[] resultarray = resultString.split(",");
        if (resultarray.length>1) {
            out = resultarray[0] + "." + resultarray[1];
        }
        else if (resultarray.length==1){
            out = resultarray[0];
        }
    }
    else if (Objects.equals(firstchar, "1")){
        int whole = (int) desired;
        System.out.println(whole);
        out = String.valueOf(whole);
        if (Objects.equals(secondchar, "0")){
            String temp = "";
            for (int i = 0; i<out.length()-1; i++){
                temp+=""+out.charAt(i);
            }
            out=temp;
            System.out.println(out);
        }
    }

    double result = Double.parseDouble(out);
    System.out.println(result);
    return result;
}

public String getPriceFilter(String desiredSymbol) {
System.out.println(desiredSymbol);
String result = "";
BinanceExchangeInfo binanceExchangeInfo = null;
try {
binanceExchangeInfo = new BinanceApi().exchangeInfo();
List symbols = binanceExchangeInfo.getSymbols();
for (int i = 0; i<symbols.size(); i++) {
if (desiredSymbol.equals(symbols.get(i).getBaseAsset() + symbols.get(i).getQuoteAsset())) {
result = symbols.get(i).getPriceFilter().get("tickSize").toString();
System.out.println(symbols.get(i).getPriceFilter().toString());
System.out.println();
i=symbols.size();
}
}
} catch (BinanceApiException e) {
e.printStackTrace();
String method = " getPriceFilter";
errors.add( new Error(e.toString()+method, LocalDateTime.now()));
}
return result;
}`

Don't mind the variable names and the print lines!
double desired is the inserted price, String tickSize in the first method. is actually the outcome of the method below AKA Price_filter (sorry about that), So thats why i split it up a couple of times.

Keep in mind its a messy solution! (was in a hurry)

from java-binance-api.

ben-arnao avatar ben-arnao commented on June 16, 2024

For those still have the same issue here's how i solved it...

double validatePrice(double price) {
	if (price < minTradePrice)
		return 0;
	price -= (price - minTradePrice) % tickSize;
	Double toBeTruncated = new Double(price);
	price = BigDecimal.valueOf(toBeTruncated).setScale(assetPrecision, RoundingMode.HALF_UP).doubleValue();
	return price;
}
double validateAmount(double amount, double price) {
	amount -= (amount - minTradeQty) % stepSize;
	if (amount < minTradeQty || amount * price <= minNotional)
		return 0;
	return amount;
}

from java-binance-api.

luxel1103 avatar luxel1103 commented on June 16, 2024

How do you get the assetPrecision?

if i use:
int assetPrecision = client.getExchangeInfo().getSymbolInfo(symbol).getBaseAssetPrecision();
it always returns 8 for every coin.

from java-binance-api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.