Skip to content

Commit 2ccf461

Browse files
committed
feat: adding new flag to trade all possible quantity for buy/sell
Signed-off-by: rsercano <[email protected]>
1 parent 82e16a5 commit 2ccf461

File tree

8 files changed

+339
-247
lines changed

8 files changed

+339
-247
lines changed

docs/api.md

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/cli/placeorder.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import { checkDecimalPlaces } from '../utils/utils';
77
export const placeOrderBuilder = (argv: Argv, side: OrderSide) => {
88
const command = side === OrderSide.BUY ? 'buy' : 'sell';
99
argv.positional('quantity', {
10-
type: 'number',
11-
describe: 'the quantity to trade',
10+
type: 'string',
11+
describe: 'the quantity to trade, `all` trades everything',
1212
})
1313
.positional('pair_id', {
1414
type: 'string',
@@ -39,6 +39,8 @@ export const placeOrderBuilder = (argv: Argv, side: OrderSide) => {
3939
describe: 'immediate-or-cancel',
4040
})
4141
.example(`$0 ${command} 5 LTC/BTC .01 1337`, `place a limit order to ${command} 5 LTC @ 0.01 BTC with local order id 1337`)
42+
.example(`$0 ${command} all LTC/BTC .01`, `place a limit order to ${command} all LTC @ 0.01 BTC`)
43+
.example(`$0 ${command} all BTC/USDT mkt`, `place a market order to ${command} all BTC for USDT`)
4244
.example(`$0 ${command} 3 BTC/USDT mkt`, `place a market order to ${command} 3 BTC for USDT`)
4345
.example(`$0 ${command} 1 BTC/USDT market`, `place a market order to ${command} 1 BTC for USDT`);
4446
};
@@ -48,9 +50,17 @@ export const placeOrderHandler = async (argv: Arguments<any>, side: OrderSide) =
4850

4951
const numericPrice = Number(argv.price);
5052
const priceStr = argv.price.toLowerCase();
53+
const isAll = argv.quantity === 'all';
5154

52-
const quantity = coinsToSats(argv.quantity);
53-
request.setQuantity(quantity);
55+
if (isAll) {
56+
request.setAll(true);
57+
} else {
58+
if (isNaN(argv.quantity)) {
59+
console.error('quantity is not a valid number');
60+
process.exit(1);
61+
}
62+
request.setQuantity(coinsToSats(parseFloat(argv.quantity)));
63+
}
5464
request.setSide(side);
5565
request.setPairId(argv.pair_id.toUpperCase());
5666
request.setImmediateOrCancel(argv.ioc);
@@ -81,7 +91,7 @@ export const placeOrderHandler = async (argv: Arguments<any>, side: OrderSide) =
8191
} else {
8292
const subscription = client.placeOrder(request);
8393
let noMatches = true;
84-
let remainingQuantity = quantity;
94+
let remainingQuantity = isAll ? 0 : coinsToSats(parseFloat(argv.quantity));
8595
subscription.on('data', (response: PlaceOrderEvent) => {
8696
if (argv.json) {
8797
console.log(JSON.stringify(response.toObject(), undefined, 2));
@@ -113,6 +123,8 @@ export const placeOrderHandler = async (argv: Arguments<any>, side: OrderSide) =
113123
subscription.on('end', () => {
114124
if (noMatches) {
115125
console.log('no matches found');
126+
} else if (isAll) {
127+
console.log('no more matches found');
116128
} else if (remainingQuantity > 0) {
117129
console.log(`no more matches found, ${satsToCoinsStr(remainingQuantity)} qty will be discarded`);
118130
}

lib/proto/xudrpc.swagger.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/proto/xudrpc_pb.d.ts

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/proto/xudrpc_pb.js

Lines changed: 30 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/service/Service.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -610,19 +610,46 @@ class Service {
610610
* If price is zero or unspecified a market order will get added.
611611
*/
612612
public placeOrder = async (
613-
args: { pairId: string, price: number, quantity: number, orderId: string, side: number,
614-
replaceOrderId: string, immediateOrCancel: boolean },
613+
args: { pairId: string, price: number, quantity?: number, orderId: string, side: number,
614+
replaceOrderId: string, immediateOrCancel: boolean, all?: boolean },
615615
callback?: (e: ServicePlaceOrderEvent) => void,
616616
) => {
617617
argChecks.PRICE_NON_NEGATIVE(args);
618618
argChecks.PRICE_MAX_DECIMAL_PLACES(args);
619619
argChecks.HAS_PAIR_ID(args);
620-
const { pairId, price, quantity, orderId, side, replaceOrderId, immediateOrCancel } = args;
620+
const { pairId, price, quantity, orderId, side, replaceOrderId, immediateOrCancel, all } = args;
621+
622+
let calculatedQuantity: number;
623+
624+
if (all) {
625+
calculatedQuantity = 0;
626+
627+
this.listOrders({ pairId, owner: Owner.Both, limit: 0, includeAliases: false }).forEach((orderArrays, _) => {
628+
function iterateOrdersAndAddQuantity(orderArray: ServiceOrder[]) {
629+
for (const order of orderArray) {
630+
if (order.quantity) {
631+
if (!price || order.price === price) {
632+
calculatedQuantity += order.quantity;
633+
}
634+
}
635+
}
636+
}
637+
if (side === OrderSide.Buy) {
638+
iterateOrdersAndAddQuantity(orderArrays.sellArray);
639+
} else if (side === OrderSide.Sell) {
640+
iterateOrdersAndAddQuantity(orderArrays.buyArray);
641+
}
642+
});
643+
644+
this.logger.debug(`all flag is true to place order, calculated quantity from orderbook is ${calculatedQuantity}`);
645+
} else {
646+
calculatedQuantity = quantity || 0;
647+
}
621648

622649
const order: OwnMarketOrder | OwnLimitOrder = {
623650
pairId,
624651
price,
625-
quantity,
652+
quantity: calculatedQuantity,
626653
isBuy: side === OrderSide.Buy,
627654
localId: orderId || replaceOrderId,
628655
};

proto/xudrpc.proto

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,9 @@ message PlaceOrderRequest {
688688
string replace_order_id = 6 [json_name = "replace_order_id"];
689689
// Whether the order must be filled immediately and not allowed to enter the order book.
690690
bool immediate_or_cancel = 7 [json_name = "immediate_or_cancel"];
691+
// Whether to trade all available funds.
692+
// If true, the quantity field is ignored.
693+
bool all = 8 [json_name = "all"];
691694
}
692695
message PlaceOrderResponse {
693696
// A list of own orders (or portions thereof) that matched the newly placed order.

0 commit comments

Comments
 (0)