Alexander Chepurnoy

The Web of Mind

Scala Clients for BTC-e Trade and Public Data APIs (My First Opensource Released)

| Comments

I just released my first open-source component, Scala Client for BTC-e Trade and Public Data APIs! BTC-e.com is broker for Bitcoin/Litecoin/Namecoin/other cryprocurrencies trading. This post is about some choices made during development and how to use the clients.

Usage Details and Examples

  • Implement ClientCredentials trait to connect to the Trade API :

      object MyCredentials extends ClientCredentials {
          val Key = "my key"
          val Secret = "my secret"
      }
    
  • Initialize Trade API client as

      val tradeClient = new DefaultTradeApiClient(MyCredentials)
    
  • Get free funds info with

      client.getInfo.map(println(_)) 
    
    It will print something like
      USD: 4.7 RUR: 2399 EUR: 0.0 BTC: 10 LTC: 19.99 NMC: 0.0 NVC: 0.0 TRC: 0.0 PPC: 0.0
    
  • Cancel all open orders with following code

      tradeClient.orderList.getOrElse(List()).foreach{order=>
          tradeClient.cancelOrder(order.orderId)
      } 
    
  • Create order to sell 200 litecoins for $4.99 each

      tradeClient.trade(Currency.LTC, Currency.USD, Direction.Sell, 4.99, 200.00)
    
  • Close connections at the end

      tradeClient.releaseConnections 
    
  • Initialize Public Data API as

      val pubClient = new DefaultMarketDataApiClient
    
  • Get last deal price from ticker data for BTC/USD and print it:

      pubClient.ticker(Currency.BTC, Currency.USD).map{td=>
          println(td.last)
      }
    

See MarketDataApiClient and TradeApiClient classes for more functions.

Implementation and Customization Details

  1. Common functions located in btce.scala, Trade API client in btce-trade.scala, Public Data API client in btce-marketdata.scala, Specs2 tests in BtceSpec.scala

  2. There are many HTTP layer implementations. I implemented http requests/responses using WS framework from PlayFramework 2(Scala wrapper for Ning framework). If your project doesn’t use PlayFramework and/or already uses another HTTP framework(e.g. Apache HttpClient), make own implementation of HttpApiClient trait. Override functions getRequest(url: String): String (simple get request, it’s used by Public Data API), signedPostRequest(url: String, key: String, Secret: String, postBody: String): String (post request with already signed postBody, used by Trade API ), releaseConnections (shutdown connections pool here, if needed). Then define own Trade API client with code like class MyTradeApiClient(credentials: ClientCredentials) extends TradeApiClient(credentials) with MyHttpApiClient, class MyMarketDataApiClient extends MarketDataApiClient with MyHttpApiClient for Public Data client.

  3. Enumerations chosen over sealed case classes hierarchy, e.g.

     object Direction extends Enumeration {
         type Direction = Value
         val Sell = Value("sell")
         val Buy = Value("buy")
     }
    

    It could be not the best choice in case of having in mind to build trading DSL over it. But I have no plans for trading DSL now.

  4. No logging implemented in the released version to avoid extra dependency. If you incorporate a client into your software, add logging where needed(catch clauses, None results) with a logging framework project uses.

Again, the URL is https://github.com/kushti/btce-scala

Comments