Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions futures_api_example_java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,36 @@
<compilerArgument>-Xlint:unchecked</compilerArgument>
</configuration>
</plugin>
<!-- Build jar -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<outputDirectory>
${project.basedir}/build
</outputDirectory>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>
com.bitrue.futures.sdk.client.Main
</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
26 changes: 8 additions & 18 deletions futures_api_example_java/readme.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
## Getting started

#### Public API
#### How to run this project

* junit
run MarketTest
* To create a new self executable jar
* run the command on the terminal > mvn clean install
* run self executable jar > java -jar <jarname> e.g. bitrue-future-client-0.9.0-SNAPSHOT-jar-with-dependencies.jar
* run unit test > mvn test
* skip unit test > mvn clean install -DskipTests


#### Trade & Account API

``` java

public class PrivateConfig {

public static final String API_KEY = "XXXXXX";
public static final String SECRET_KEY = "XXXXXX";

}

# junit:
TradeTest
AccountTest
```
#### API key and signature
* See the class PrivateConfig.java, add your Bittrue APIKey and Signature


Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.bitrue.futures.sdk.client;

public class PrivateConfig {

public static final String API_KEY = "yyy";
public static final String SECRET_KEY = "xxx";
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bitrue.futures.sdk.client;

import com.bitrue.futures.sdk.client.constant.FuturesApiConstants;
import com.bitrue.futures.sdk.client.exception.BitrueApiException;

import java.net.URL;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.bitrue.futures.sdk.client;
package com.bitrue.futures.sdk.client.constant;

import org.apache.commons.lang.builder.ToStringStyle;

Expand Down Expand Up @@ -38,4 +38,4 @@ public class FuturesApiConstants {
* ToStringStyle.JSON_STYLE
*/
public static ToStringStyle TO_STRING_BUILDER_STYLE = ToStringStyle.SHORT_PREFIX_STYLE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@

public class BitrueApiException extends RuntimeException {

/**
*
*/
private static final long serialVersionUID = 4360108982268949009L;
public static final String RUNTIME_ERROR = "RuntimeError";
public static final String INPUT_ERROR = "InputError";
public static final String KEY_MISSING = "KeyMissing";
public static final String SYS_ERROR = "SysError";
public static final String SUBSCRIPTION_ERROR = "SubscriptionError";
public static final String ENV_ERROR = "EnvironmentError";
public static final String EXEC_ERROR = "ExecuteError";
private final String errCode;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.bitrue.futures.sdk.client.impl;

import com.bitrue.futures.sdk.client.FuturesApiConstants;
import com.bitrue.futures.sdk.client.exception.BitrueApiException;
import com.bitrue.futures.sdk.client.utils.UrlParamsBuilder;
import org.apache.commons.codec.binary.Hex;
Expand All @@ -12,8 +11,6 @@

class ApiSignature {

// static final String op = "op";
// static final String opValue = "auth";
private static final String signatureMethodValue = "HmacSHA256";
public static final String signatureVersionValue = "2";

Expand All @@ -23,13 +20,6 @@ String createSignature(String ts, String path, String accessKey, String secretKe
throw new BitrueApiException(BitrueApiException.KEY_MISSING, "API key and secret key are required");
}

// if(builder.getMethod().equals("GET")){
// builder.putToUrl("recvWindow", Long.toString(FuturesApiConstants.DEFAULT_RECEIVING_WINDOW));
// }
// else if(builder.getMethod().equals("POST")){
// builder.putToPost("recvWindow",Long.toString(FuturesApiConstants.DEFAULT_RECEIVING_WINDOW));
// }

Mac hmacSha256;
try {
hmacSha256 = Mac.getInstance(signatureMethodValue);
Expand All @@ -49,5 +39,4 @@ String createSignature(String ts, String path, String accessKey, String secretKe
return actualSign;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ public SyncRequestClient createSyncRequestInverseClient(String apiKey, String se
InverseRestApiRquestImpl inverseRequestImpl = new InverseRestApiRquestImpl(apiKey, secKey, options);
return new SyncRequestImpl(inverseRequestImpl);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ RestApiRequest<ServerTime> getServerTime(){
RestApiRequest<ServerTime> request = new RestApiRequest<>();
UrlParamsBuilder builder = UrlParamsBuilder.build();
request.request = createRequestByGet("/dapi/v1/time", builder);
request.jsonParser = (jsonWrapper -> {
return ServerTime.builder().serverMillis(jsonWrapper.getLong("serverTime")).timeZone(jsonWrapper.getString("timezone")).build();
});
request.jsonParser = (jsonWrapper -> ServerTime.builder().serverMillis(jsonWrapper.getLong("serverTime")).timeZone(jsonWrapper.getString("timezone")).build());
return request;
}

Expand Down Expand Up @@ -69,7 +67,7 @@ RestApiRequest<List<ContractInfo>> getContractList() {

@Override
RestApiRequest<OrderBook> getOrderBook(String contractName, Integer limit) {
RestApiRequest<OrderBook> request = new RestApiRequest<OrderBook>();
RestApiRequest<OrderBook> request = new RestApiRequest<>();
UrlParamsBuilder builder = UrlParamsBuilder.build()
.putToUrl("contractName", contractName)
.putToUrl("limit", limit);
Expand Down Expand Up @@ -112,17 +110,14 @@ RestApiRequest<PriceChangeTicker> get24HrTickerPriceChange(String contractName){

request.request = createRequestByGet("/dapi/v1/ticker", builder);

request.jsonParser = (jsonWrapper ->{
PriceChangeTicker result = PriceChangeTicker.builder()
.high(jsonWrapper.getBigDecimal("high"))
.low(jsonWrapper.getBigDecimal("low"))
.last(jsonWrapper.getBigDecimal("last"))
.vol(jsonWrapper.getBigDecimal("vol"))
.rose(jsonWrapper.getBigDecimal("rose"))
.time(jsonWrapper.getLong("time"))
.build();
return result;
});
request.jsonParser = (jsonWrapper -> PriceChangeTicker.builder()
.high(jsonWrapper.getBigDecimal("high"))
.low(jsonWrapper.getBigDecimal("low"))
.last(jsonWrapper.getBigDecimal("last"))
.vol(jsonWrapper.getBigDecimal("vol"))
.rose(jsonWrapper.getBigDecimal("rose"))
.time(jsonWrapper.getLong("time"))
.build());
return request;
}

Expand Down Expand Up @@ -171,14 +166,11 @@ RestApiRequest<Order> postOrder(String contractName, String price, String volume

request.request = createRequestByPostWithSignature("/dapi/v1/order", builder);

request.jsonParser = (jsonWrapper -> {
Order result = Order.builder()
.clientOrdId(clientOrdId).orderId(jsonWrapper.getLong("orderId")).price(new BigDecimal(price))
.orgiQty(new BigDecimal(volume)).type(orderType.name()).contractName(contractName).side(side.name())
.postionAction(action.name()).timeInForce(timeInForce.name())
.build();
return result;
});
request.jsonParser = (jsonWrapper -> Order.builder()
.clientOrdId(clientOrdId).orderId(jsonWrapper.getLong("orderId")).price(new BigDecimal(price))
.orgiQty(new BigDecimal(volume)).type(orderType.name()).contractName(contractName).side(side.name())
.postionAction(action.name()).timeInForce(timeInForce.name())
.build());

return request;
}
Expand All @@ -198,14 +190,11 @@ RestApiRequest<Order> marketOrder(String contractName, String volume, OrderSide

request.request = createRequestByPostWithSignature("/dapi/v1/order", builder);

request.jsonParser = (jsonWrapper -> {
Order result = Order.builder()
.clientOrdId(clientOrdId).orderId(jsonWrapper.getLong("orderId"))
.orgiQty(new BigDecimal(volume)).type(OrderType.MARKET.name()).contractName(contractName).side(side.name())
.postionAction(action.name()).timeInForce(OrderType.MARKET.name())
.build();
return result;
});
request.jsonParser = (jsonWrapper -> Order.builder()
.clientOrdId(clientOrdId).orderId(jsonWrapper.getLong("orderId"))
.orgiQty(new BigDecimal(volume)).type(OrderType.MARKET.name()).contractName(contractName).side(side.name())
.postionAction(action.name()).timeInForce(OrderType.MARKET.name())
.build());

return request;
}
Expand All @@ -219,12 +208,9 @@ public RestApiRequest<Order> cancelOrder(String contractName, Long orderId, Stri
.putToPost("clientOrderId", clientOrdId);
request.request = createRequestByPostWithSignature("/dapi/v1/cancel", builder);

request.jsonParser = (jsonWrapper -> {
Order result = Order.builder()
.clientOrdId(clientOrdId).orderId(jsonWrapper.getLong("orderId")).contractName(contractName)
.build();
return result;
});
request.jsonParser = (jsonWrapper -> Order.builder()
.clientOrdId(clientOrdId).orderId(jsonWrapper.getLong("orderId")).contractName(contractName)
.build());
return request;
}

Expand Down Expand Up @@ -260,14 +246,12 @@ public RestApiRequest<Order> queryOrder(String contractName, Long orderId){
builder.putToUrl("orderId", String.valueOf(orderId));

request.request = createRequestByGetWithSignature("/dapi/v1/order", builder);
request.jsonParser = (wrapper -> {
return Order.builder().orderId(wrapper.getLong("orderId")).side(wrapper.getString("side"))
.executeQty(wrapper.getBigDecimalOrDefault("executedQty", BigDecimal.ZERO))
.price(wrapper.getBigDecimal("price")).orgiQty(wrapper.getBigDecimal("origQty"))
.avgPrice(wrapper.getBigDecimalOrDefault("avgPrice", BigDecimal.ZERO))
.type(wrapper.getString("type")).status(wrapper.getString("status"))
.postionAction(wrapper.getString("action")).ctime(wrapper.getLong("transactTime")).build();
});
request.jsonParser = (wrapper -> Order.builder().orderId(wrapper.getLong("orderId")).side(wrapper.getString("side"))
.executeQty(wrapper.getBigDecimalOrDefault("executedQty", BigDecimal.ZERO))
.price(wrapper.getBigDecimal("price")).orgiQty(wrapper.getBigDecimal("origQty"))
.avgPrice(wrapper.getBigDecimalOrDefault("avgPrice", BigDecimal.ZERO))
.type(wrapper.getString("type")).status(wrapper.getString("status"))
.postionAction(wrapper.getString("action")).ctime(wrapper.getLong("transactTime")).build());
return request;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.slf4j.LoggerFactory;

abstract class RestApiInvoker {

private static final Logger log = LoggerFactory.getLogger(RestApiInvoker.class);
private static final OkHttpClient client = new OkHttpClient();

Expand Down Expand Up @@ -66,5 +65,4 @@ static <T> T callSync(RestApiRequest<T> request) {
"[Invoking] Unexpected error: " + e.getMessage());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import okhttp3.Request;

public class RestApiRequest <T>{

public Request request;

RestApiJsonParser<T> jsonParser;
}
Loading