📜 ⬆️ ⬇️

My java library implementation for the BTC-e exchange

After starting trading on BTC-e, I noticed a pretty good API. His opportunities can be sent to a good cause. Namely - the creation of trading bots and mobile clients. The bots are clear, but the Android client was needed pretty soon, but they were not found on the market then. And when the library was added (yes, yes, there is little code in it, but it turned out to be a long-term construction), although the applications appeared, there were still some not so. So, if you are interested to find out what happened to me and how much it can be useful to you - I ask for cat.

Before writing, I set myself a number of tasks, namely:

GitHub was chosen for hosting, links can be found at the end of the article.

Reflections on how to put it all together led to the following class hierarchy:

image
')
The two main groups of classes are PrivateBaseClass and PublicBaseClass, for the private and public api sections, respectively. PrivateBaseClass is divided into methods that return iterable results and are non-iterable. They are based on CommonClass, containing common to all things. Private Network is used to work with keys when sending private requests, as well as encryption in 3DES, for storage on the device. StringCrypter helps him with this. And completes the composition class TradeApi, which absorbed all their capabilities and serves as a point of interaction with the user. However, no one forbids the use of classes like Ticker or Info separately, if you do not need anything more than their functionality.
For work with json Jackson library has been chosen, and for all remaining Apache Commons Codec.
And now with an example, what came of all this and how to deal with it using it.
The algorithm will be approximately as follows: choose from the official documentation the method we need, set the parameters for it, run it, get the results.
The first thing you might want to do is find out the prices of your favorite couples. And it will be quite simple to do this:

TradeApi tradeApi = new TradeApi(); tradeApi.ticker.addPair("btc_usd");//  tradeApi.ticker.addPair("ltc_usd"); tradeApi.ticker.addPair("nmc_usd"); tradeApi.ticker.runMethod();//  while (tradeApi.ticker.hasNextPair()) {//    tradeApi.ticker.switchNextPair();//    System.out.println(tradeApi.ticker.getCurrentLast());//   } 

The result will be:
368.98
4.216
1.02

The next in line we will have is getting the balance, you can take the keys in your personal account (these, of course, are not valid):

 String aKey = "TK8HD88A-ENLZZBCW-P4L5JYVR-18K9NZZP-CYQ7WKKH";//API- String aSecret = "b7738a9f4d62da1b6ebdcd7ff2d7b5ddb93de88b71f017ae600b7ab3ed5b7015";//SECRET TradeApi tradeApi = new TradeApi(aKey,aSecret);//   tradeApi.getInfo.runMethod();// ,     System.out.println(tradeApi.getInfo.getBalance("usd"));//     

The result will be:
0.25

In case you don’t want to enter keys in your final implementation every time, but you don’t want to store them in the clear either, it is possible to use built-in encryption. The code will be the same except for handing over the keys. In this case, the constructor will need to send an encrypted api-key, a secret, as well as a password to decrypt them:

 String aKey = "TK8HD88A-ENLZZBCW-P4L5JYVR-18K9NZZP-CYQ7WKKH";//API- String aSecret = "b7738a9f4d62da1b6ebdcd7ff2d7b5ddb93de88b71f017ae600b7ab3ed5b7015";//SECRET String password = "12345";//  String encryptedKey = tradeApi.encodeString(aKey, password);//   String encryptedSecret = tradeApi.encodeString(aSecret, password);//   tradeApi.setKeys(encryptedKey, encryptedSecret, password);//   ,       

If the password is incorrect, an exception will be thrown. If a valid key and secret were installed, but after an attempt was made to set up encrypted keys with the wrong password, the exception will also be thrown away, but the old keys will continue to work further.

And now, perhaps, the most interesting: additional features created using the official API.
Let's start with the trade. In addition to the native Trade method described in the documentation, I added a slightly smarter method. It uses two methods at once: Trade and Info. It automatically rounds the price to acceptable limits and throws exceptions with different messages to quickly diagnose the cause of the error:

 String aKey = "TK8HD88A-ENLZZBCW-P4L5JYVR-18K9NZZP-CYQ7WKKH";//API- String aSecret = "b7738a9f4d62da1b6ebdcd7ff2d7b5ddb93de88b71f017ae600b7ab3ed5b7015";//SECRET TradeApi tradeApi = new TradeApi(aKey,aSecret);//   Trade trade = tradeApi.extendedTrade("btc_usd", false, "600", "0.01");//false - , true -  :) if (trade.isSuccess()){ System.out.println(trade.getReceived());//  } 

Result: 0
that is, we didn’t get anything, because we couldn’t sell 0.01 Bitcoin for $ 600 at the time of this writing.

The next in turn, the priceDifference method, pleasant for use in all alarm clocks:

 TradeApi tradeApi = new TradeApi(); for (int i = 0; i < 3; i++) { System.out.println(tradeApi.priceDifference("btc-usd", 600.0, 10000)); } 

Result:
233.39
233.39
233.103

The method takes as input the name of the pair and the price limit with which the current price should be compared, as well as the delay time in milliseconds, this can be useful if the method is used in a cycle, for example, to build alarms.

And finally, tryMaximumBuy (String pairName, long reuseAgeMillis) and tryMaximumSell (String pairName) remain. The most interesting of these two is the tryMaximumBuy method. He is trying to make the highest possible purchase of currency on the selected pair. The second parameter is the time after which the data used in the method are considered obsolete and are subject to update. Unfortunately, I cannot show an example of work, due to the fact that it will not be free for me.
Of course, the possibilities of this library are not limited to this and, perhaps, you will find in it some other useful things for yourself, for example, rounding numbers to the selected decimal place, useful when working with quantities and prices when buying. With the library, you can also download Javadoc, which describes many methods.

In conclusion, I hope that this library will be useful to you and will serve the further development of the cryptocurrency community, as well as remove the problem of maintaining your set of classes for working with the Exchange API up to date. I will do this.

Promised at the beginning of the article links


Link to the library repository: github.com/alexandersjn/btc_e_assist_api
Reference to the documentation on the private part of the API: btc-e.com/tapi/docs#Trade
Link to the documentation for the public part of the API: btc-e.com/api/3/docs
Also, as an example of usage, you can find a link to my android client for btc-e in the library repository description. You will also find its source code in my repository.

Thank you for attention.

Source: https://habr.com/ru/post/239073/


All Articles