Skip to content

Instantly share code, notes, and snippets.

@hitbtc-com
Created September 29, 2015 13:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hitbtc-com/2765a1431a2384975c01 to your computer and use it in GitHub Desktop.
Save hitbtc-com/2765a1431a2384975c01 to your computer and use it in GitHub Desktop.
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
public class TestHitBtc {
public static void main(String[] args) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
//Insert your keys
String secretKey = "";
String publicKey = "";
executeOrder(publicKey, secretKey, 'BTCUSD', "sell", 1, 250.1));
}
public static void executeOrder(String publicKey, String symbol, String secretKey, String side, double quantity, double price) {
Date date = new Date();
String message = "/api/1/trading/new_order?nonce=" + date.getTime() + "&apikey=" + publicKey + "";
HttpClient client = HttpClientBuilder.create().build();
try {
List<NameValuePair> postData = new ArrayList<NameValuePair>();
postData.add(new BasicNameValuePair("clientOrderId", UUID.randomUUID().toString().substring(0, 30))); //generating a random id here
postData.add(new BasicNameValuePair("symbol", symbol));
postData.add(new BasicNameValuePair("side", side));
postData.add(new BasicNameValuePair("type", "limit"));
postData.add(new BasicNameValuePair("timeInForce", "GTC"));
postData.add(new BasicNameValuePair("quantity", Double.toString(quantity)));
postData.add(new BasicNameValuePair("price", formatNumber(price, 8)));
HttpPost post = new HttpPost("https://api.hitbtc.com" + message);
UrlEncodedFormEntity postEntity = new UrlEncodedFormEntity(postData);
BufferedReader postReader = new BufferedReader(new InputStreamReader(postEntity.getContent()));
String postLine;
while ((postLine = postReader.readLine()) != null) {
System.out.println(postLine);
message = message + postLine;
}
post.addHeader("X-Signature", hmacDigest(message, secretKey));
post.setEntity(postEntity);
HttpResponse response = client.execute(post);
System.out.println("Response Code : " + response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuilder result = new StringBuilder();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(result);
} catch (IOException e) {
e.printStackTrace();
}
}
public static String hmacDigest(String message, String secretKey) {
String digest = null;
String algo = "HmacSHA512";
try {
SecretKeySpec key = new SecretKeySpec((secretKey).getBytes("UTF-8"), algo);
Mac mac = Mac.getInstance(algo);
mac.init(key);
byte[] bytes = mac.doFinal(message.getBytes("UTF-8"));
StringBuilder hash = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(0xFF & bytes[i]);
if (hex.length() == 1) {
hash.append('0');
}
hash.append(hex);
}
digest = hash.toString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return digest;
}
//Format numbers in decimal notation with custom digits
public static String formatNumber(double number, int digits) {
DecimalFormat df = new DecimalFormat("0");
df.setMaximumFractionDigits(digits);
return df.format(number);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment