Skip to content

Commit

Permalink
add some convenience methods to place orders
Browse files Browse the repository at this point in the history
  • Loading branch information
chochos committed Dec 30, 2024
1 parent 00836a9 commit 10132e3
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/main/java/com/bitso/Bitso.java
Original file line number Diff line number Diff line change
Expand Up @@ -390,9 +390,69 @@ public BitsoOrder[] lookupOrders(String... ordersId)
return orders;
}

/** Place a market order to sell the specified amount.
* @param book The trading pair for this order.
* @param amount The amount to sell, in major currency.
* @return The generated order ID.
*/
public String placeMarketSellOrder(String book, BigDecimal amount)
throws BitsoAPIException, BitsoPayloadException, BitsoServerException {
return placeOrder(book, BitsoOrder.SIDE.SELL, BitsoOrder.TYPE.MARKET, amount, null, null, null);
}

/** Place a market order to buy the specified value.
* @param book The trading pair for this order.
* @param value The value to buy, in minor currency.
*/
public String placeMarketBuyOrder(String book, BigDecimal value)
throws BitsoAPIException, BitsoPayloadException, BitsoServerException {
return placeOrder(book, BitsoOrder.SIDE.SELL, BitsoOrder.TYPE.MARKET, null, value, null, null);
}

/** Place a limit order.
* @param book The trading pair for this order.
* @param side Buy or sell
* @param major The amount to buy or sell, in major currency.
* @param minor The value to buy or sell, in minor currency.
* @param price The maximum price at which to buy, or minimum price at which to sell,
* expressed in minor currency.
* @param tif The time-in-force attribute.
*/
public String placeLimitOrder(String book, BitsoOrder.SIDE side, BigDecimal major, BigDecimal minor,
BigDecimal price, BitsoOrder.TIME_IN_FORCE tif)
throws BitsoAPIException, BitsoPayloadException, BitsoServerException {
return placeOrder(book, side, BitsoOrder.TYPE.LIMIT, major, minor, price, tif);
}

/** Place an order, using GOODTILLCANCELLED.
* @param book The trading pair for the order.
* @param side Whether it's a buy or sell order.
* @param type A limit or market order.
* @param major The amount of the order, in major currency.
* @param minor The value of the order, in minor currency.
* @param price The price of the order, in minor currency. Use null for market orders.
*/
public String placeOrder(String book, BitsoOrder.SIDE side, BitsoOrder.TYPE type, BigDecimal major,
BigDecimal minor, BigDecimal price)
throws BitsoAPIException, BitsoPayloadException, BitsoServerException {
return placeOrder(book, side, type, major, minor, price, BitsoOrder.TIME_IN_FORCE.GOODTILLCANCELLED);
}

/** Place an order, using the specified parameters.
* Only one of the major (amount) or the minor (value) must be specified.
*
* @param book The trading pair for the order.
* @param side Whether it's a buy or sell order.
* @param type A limit or market order.
* @param major The amount of the order, in major currency.
* @param minor The value of the order, in minor currency.
* @param price The price of the order, in minor currency. Use null for market orders.
* @param tif The time-in-force attribute, for limit orders.
* @return The order ID generated by the system.
*/
public String placeOrder(String book, BitsoOrder.SIDE side, BitsoOrder.TYPE type, BigDecimal major,
BigDecimal minor, BigDecimal price, BitsoOrder.TIME_IN_FORCE tif)
throws BitsoAPIException, BitsoPayloadException, BitsoServerException {
String request = "/api/v3/orders";

JSONObject parameters = new JSONObject();
Expand All @@ -414,6 +474,9 @@ public String placeOrder(String book, BitsoOrder.SIDE side, BitsoOrder.TYPE type

if (type.equals(BitsoOrder.TYPE.LIMIT) && (price != null)) {
parameters.put("price", price.toString());
if (tif != null) {
parameters.put("time_in_force", tif.name().toLowerCase());
}
}

if (major != null) {
Expand Down

0 comments on commit 10132e3

Please sign in to comment.