Skip to content

Commit

Permalink
1Bse Added.
Browse files Browse the repository at this point in the history
OkCoin updated to handle futures.
  • Loading branch information
andrefbsantos committed Oct 17, 2014
1 parent 2917046 commit cf54f7f
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 10 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ The main goal of libdynticker is to get the traded pairs dynamically. That way i
## Supported exchanges

### Bitcoin and altcoins to FIAT
* [1Bse](https://www.1bse.com/)
* [ANX](https://anxpro.com)
* [BitBay](https://bitbay.pl)
* [Bitcoin.de](https://www.bitcoin.de)
* [Bitcurex](http://bitcurex.com)
* [Bitfinex](https://www.bitfinex.com)
* [BitMarket.pl](https://www.bitmarket.pl)
* [Bitstamp](https://www.bitstamp.net)
* [BTC100](https://www.btc100.com/)
* [BtcTrade](https://www.btctrade.com)
* [BTC China](https://btcchina.com)
* [BTC Markets](https://btcmarkets.net)
* [BTC-E](https://btc-e.com)
Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>mobi.boilr.libdynticker</groupId>
<artifactId>libdynticker</artifactId>
<version>0.3.0</version>
<version>0.4.0</version>
<packaging>jar</packaging>
<name>libdynticker</name>
<description>Java library to retrieve trading pairs and last value from cryptocurrency and bullion exchanges.</description>
Expand All @@ -24,8 +24,8 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<jdk.version>1.6</jdk.version>
<release.description>
\+ Added exchanges: Atomic Trade, BitBay, Bitcurex, BitMarket.pl, C-CEX, CCEDK, CoinMkt, Coins-E, Crypto-Trade, Cryptonit, Justcoin, Paymium and Vircurex.
\* Corrected bugs in several exchanges which were listing inactive pairs.
\+ Added exchanges: BTC100, BtcTrade and 1Bse.
\+ Added Okcoin futures.
</release.description>
</properties>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
import mobi.boilr.libdynticker.core.Pair;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

public class OKCoinExchange extends Exchange {
private static final List<Pair> pairs;
Expand All @@ -20,6 +23,17 @@ public class OKCoinExchange extends Exchange {
tempPairs.add(new Pair("LTC", "USD"));
tempPairs.add(new Pair("BTC", "CNY"));
tempPairs.add(new Pair("LTC", "CNY"));

tempPairs.add(new Pair("BTC Futures (this week)", "USD"));
tempPairs.add(new Pair("BTC Futures (next week)", "USD"));
tempPairs.add(new Pair("BTC Futures (month)", "USD"));
tempPairs.add(new Pair("BTC Futures (quarter)", "USD"));

tempPairs.add(new Pair("LTC Futures (this week)", "USD"));
tempPairs.add(new Pair("LTC Futures (next week)", "USD"));
tempPairs.add(new Pair("LTC Futures (month)", "USD"));
tempPairs.add(new Pair("LTC Futures (quarter)", "USD"));

pairs = Collections.unmodifiableList(tempPairs);
}

Expand All @@ -37,20 +51,49 @@ protected String getTicker(Pair pair) throws IOException {
if(!pairs.contains(pair))
throw new IOException("Invalid pair.");
String url = "";
if(pair.getExchange().equals("USD")) {

if(pair.getCoin().contains("Futures")) {
String contractTtpe = null;
if(pair.getCoin().contains("this week")) {
contractTtpe = "this_week";
}
else if(pair.getCoin().contains("next week")) {
contractTtpe = "this_week";
}
else if(pair.getCoin().contains("month")) {
contractTtpe = "this_week";
}
else if(pair.getCoin().contains("quarter")) {
contractTtpe = "quarter";
}
else {
throw new IOException();
}
url = "https://www.okcoin.com/api/future_ticker.do?symbol=" + pair.getCoin().substring(0, 3).toLowerCase() + "_"
+ pair.getExchange().toLowerCase() + "&" + "contractType=" + contractTtpe;
JsonNode node = new ObjectMapper().readTree(new URL(url));
return parseJSONFuture(node, pair);
}
else if(pair.getExchange().equals("USD")) {
// https://www.okcoin.com/api/ticker.do?symbol=ltc_usd&ok=1
url = "https://www.okcoin.com/api/ticker.do?symbol=" + pair.getCoin().toLowerCase() + "_" + pair.getExchange().toLowerCase() + "&ok=1";
} else if(pair.getExchange().equals("CNY")) {
}
else if(pair.getExchange().equals("CNY")) {
url = "https://www.okcoin.cn/api/ticker.do?symbol=" + pair.getCoin().toLowerCase() + "_" + pair.getExchange().toLowerCase();
} else {

}
JsonNode node = (new ObjectMapper()).readTree(new URL(url));
return parseJSON(node, pair);
}

protected String parseJSONFuture(JsonNode node, Pair pair) throws JsonParseException, JsonMappingException, IOException {
TypeReference<JsonNode[]> typeReference = new TypeReference<JsonNode[]>() {
};
JsonNode[] values = new ObjectMapper().readValue(node.get("ticker"), typeReference);
return values[0].get("last").toString();
}

@Override
public String parseJSON(JsonNode node, Pair pair) {
return node.get("ticker").get("last").getTextValue();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package mobi.boilr.libdynticker.exchanges;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import mobi.boilr.libdynticker.core.Exchange;
import mobi.boilr.libdynticker.core.Pair;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;

public class OneBseExchange extends Exchange {

public OneBseExchange(long experiedPeriod) {
super("1Bce", experiedPeriod);
}

@Override
protected List<Pair> getPairsFromAPI() throws IOException {
String url = "https://www.1bse.com/allprices.json";
List<Pair> pairs = new ArrayList<Pair>();
JsonNode node = new ObjectMapper().readTree(new URL(url));

if(node.get("success").getTextValue().equals("1")) {
JsonNode ret = node.get("return");
Iterator<String> fieldNames = ret.getFieldNames();
String[] split;
while(fieldNames.hasNext()) {
split = fieldNames.next().split("_");
pairs.add(new Pair(split[0].toUpperCase(), split[1].toUpperCase()));
}
}
else {
throw new IOException("Could not retreive pairs from " + getName() + ".");
}
return pairs;
}

@Override
protected String getTicker(Pair pair) throws IOException {
String url = "https://www.1bse.com/allprices.json";
JsonNode node = new ObjectMapper().readTree(new URL(url));
if(node.get("success").getTextValue().equals("1"))
return parseJSON(node, pair);
else
throw new MalformedURLException();
}

@Override
public String parseJSON(JsonNode node, Pair pair) throws IOException {
return node.get("return").get(pair.getCoin().toLowerCase() + "_" + pair.getExchange().toLowerCase()).get("price").getTextValue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public void testParseJson() {
JsonNode node = (new ObjectMapper().readTree(new File("src/test/json/okcoin-ticker.json")));
String lastValue = testExchange.parseJSON(node, pair);
Assert.assertEquals("3652.0", lastValue);
} catch (IOException e) {
}
catch(IOException e) {
e.printStackTrace();
Assert.fail();
}
Expand All @@ -48,8 +49,24 @@ public void testGetPairs() {
Assert.assertTrue(pairs.contains(new Pair("BTC", "CNY")));
Assert.assertTrue(pairs.contains(new Pair("LTC", "USD")));
Assert.assertFalse(pairs.contains(new Pair("InvalidCoin", "BTC")));
} catch (IOException e) {
}
catch(IOException e) {
Assert.fail();
}
}

@Test
public void testParseJSONFuture() {
try {
Pair pair = new Pair("BTC", "CNY");
JsonNode node = (new ObjectMapper().readTree(new File("src/test/json/okcoin-future-ticker.json")));
String lastValue = ((OKCoinExchange) testExchange).parseJSONFuture(node, pair);
Assert.assertEquals("374.1", lastValue);
}
catch(IOException e) {
e.printStackTrace();
Assert.fail();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package mobi.boilr.libdynticker.exchanges;

import java.io.File;
import java.io.IOException;
import java.util.List;

import mobi.boilr.libdynticker.core.ExchangeTest;
import mobi.boilr.libdynticker.core.Pair;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class OneBseExchangeTest extends ExchangeTest {

@Override
@Before
public void setUp() throws Exception {
testExchange = new OneBseExchange(1000);
}

@Override
@After
public void tearDown() throws Exception {
}

@Test
public void testParseJson() {
try {
Pair pair = new Pair("BTC", "USD");
JsonNode node = (new ObjectMapper().readTree(new File("src/test/json/1bse-ticker.json")));
String lastValue = testExchange.parseJSON(node, pair);
Assert.assertEquals("373.4329", lastValue);
}
catch(IOException e) {
e.printStackTrace();
Assert.fail();
}
}

@Test
public void testGetPairs() {
List<Pair> pairs;
try {
pairs = testExchange.getPairs();
Assert.assertTrue(pairs.contains(new Pair("BTC", "USD")));
Assert.assertTrue(pairs.contains(new Pair("BTC", "EUR")));
Assert.assertFalse(pairs.contains(new Pair("InvalidCoin", "BTC")));
}
catch(IOException e) {
e.printStackTrace();
Assert.fail();
}
}
}
1 change: 1 addition & 0 deletions src/test/json/1bse-ticker.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"success":"1","return":{"btc_hkd":{"price":"2912.7759","status":"buy","volume":"41.15219"},"btc_usd":{"price":"373.4329","status":"buy","volume":"44.4239"},"btc_eur":{"price":"298.7423","status":"buy","volume":"43.99374"},"ltc_hkd":{"price":"30.3347","status":"sell","volume":"904.9875"},"ltc_usd":{"price":"3.8841","status":"sell","volume":"1187.86805"},"ltc_eur":{"price":"3.1063","status":"sell","volume":"1203.87942"},"ltc_btc":{"price":"0.01048","status":"sell","volume":"68.34239"},"btc_cny":{"price":"2315.2877","status":"buy","volume":"47.31296"},"btc_sgd":{"price":"485.4665","status":"buy","volume":"51.3111"},"ltc_cny":{"price":"24.1093","status":"sell","volume":"1080.84824"},"ltc_sgd":{"price":"5.0528","status":"sell","volume":"1175.59023"},"doge_hkd":{"price":"0.00198027","status":"buy","volume":"2671322.11903"},"doge_usd":{"price":"0.00025415","status":"buy","volume":"2971556.8088"},"doge_cny":{"price":"0.00157478","status":"buy","volume":"2614673.78244"},"doge_btc":{"price":"0.00000068","status":"unch","volume":"564027.06652"},"doge_ltc":{"price":"0.00006477","status":"unch","volume":"257682.21237"},"drk_hkd":{"price":"24.07645","status":"buy","volume":"3795.68269"},"drk_usd":{"price":"3.09151","status":"buy","volume":"3318.7443"},"drk_eur":{"price":"2.47095","status":"buy","volume":"3277.11109"},"drk_cny":{"price":"19.13641","status":"buy","volume":"3267.15913"},"drk_btc":{"price":"0.0068868","status":"unch","volume":"50.37416"},"drk_ltc":{"price":"0.79593986","status":"buy","volume":"40.67421"}}}
1 change: 1 addition & 0 deletions src/test/json/okcoin-future-ticker.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"ticker":[{"buy":374.1,"contractId":20141024012,"high":386.51,"last":374.1,"low":372.35,"sell":374.66,"unitAmount":100.0,"volume":78810.0}]}

0 comments on commit cf54f7f

Please sign in to comment.