Skip to content

Commit

Permalink
feat: dynamic minimum quantity
Browse files Browse the repository at this point in the history
  • Loading branch information
Karl Ranna committed Jan 6, 2021
1 parent 6f14f0a commit 0cf6ef5
Show file tree
Hide file tree
Showing 14 changed files with 218 additions and 298 deletions.
1 change: 1 addition & 0 deletions src/arby.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export const startArby = ({
loggers.global.info('Starting. Hello, Arby.');
logConfig(config, loggers.global);
verifyMarkets(config, CEXmarkets);
store.setMarkets(CEXmarkets);
const tradeComplete$ = trade$({
config,
loggers,
Expand Down

This file was deleted.

32 changes: 16 additions & 16 deletions src/centralized/ccxt/create-order.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ describe('CCXT', () => {
},
};
const expectedSymbol = `${config.CEX_BASEASSET}/${config.CEX_QUOTEASSET}`;
const sellOrder$ = createOrder$({
const sellOrder$ = createOrder$(
config,
exchange,
side: OrderSide.SELL,
quantity: orderQuantity,
});
OrderSide.SELL,
orderQuantity
);
sellOrder$.subscribe({
next: actualOrderResponse => {
expect(actualOrderResponse).toEqual(orderResponse);
Expand Down Expand Up @@ -68,12 +68,12 @@ describe('CCXT', () => {
};
orderQuantity = new BigNumber('0.12345678');
const expectedSymbol = `${config.CEX_BASEASSET}/${config.CEX_QUOTEASSET}`;
const buyOrder$ = createOrder$({
const buyOrder$ = createOrder$(
config,
exchange,
side: OrderSide.BUY,
quantity: orderQuantity,
});
OrderSide.BUY,
orderQuantity
);
buyOrder$.subscribe({
next: actualOrderResponse => {
expect(actualOrderResponse).toEqual(orderResponse);
Expand Down Expand Up @@ -108,12 +108,12 @@ describe('CCXT', () => {
},
};
const expectedSymbol = `${config.CEX_BASEASSET}/${config.CEX_QUOTEASSET}`;
const buyOrder$ = createOrder$({
const buyOrder$ = createOrder$(
config,
exchange,
side: OrderSide.BUY,
quantity: orderQuantity,
});
OrderSide.BUY,
orderQuantity
);
buyOrder$.subscribe({
next: actualOrderResponse => {
expect(actualOrderResponse).toEqual(orderResponse);
Expand Down Expand Up @@ -150,12 +150,12 @@ describe('CCXT', () => {
},
};
const expectedSymbol = `${config.CEX_BASEASSET}/${config.CEX_QUOTEASSET}`;
const sellOrder$ = createOrder$({
const sellOrder$ = createOrder$(
config,
exchange,
side: OrderSide.SELL,
quantity: orderQuantity,
});
OrderSide.SELL,
orderQuantity
);
sellOrder$.subscribe({
next: actualOrderResponse => {
expect(actualOrderResponse).toEqual(orderResponse);
Expand Down
21 changes: 8 additions & 13 deletions src/centralized/ccxt/create-order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,12 @@ import BigNumber from 'bignumber.js';
import { Observable, from, defer } from 'rxjs';
import { Config } from '../../config';

type CreateOrderParams = {
config: Config;
exchange: Exchange;
side: OrderSide;
quantity: BigNumber;
};

const createOrder$ = ({
config,
exchange,
side,
quantity,
}: CreateOrderParams): Observable<Order> => {
const createOrder$ = (
config: Config,
exchange: Exchange,
side: OrderSide,
quantity: BigNumber
): Observable<Order> => {
return defer(() => {
const price = undefined;
const params =
Expand All @@ -33,4 +26,6 @@ const createOrder$ = ({
});
};

type CreateOrderParams = Parameters<typeof createOrder$>;

export { createOrder$, CreateOrderParams };
14 changes: 7 additions & 7 deletions src/centralized/execute-order.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ const assertExecuteCEXorder = (
return (cold(inputEvents.createOrder$) as unknown) as Observable<Order>;
};
const CEX = (null as unknown) as Exchange;
const CEXorder$ = executeCEXorder$({
const CEXorder$ = executeCEXorder$(
CEX,
config: inputEvents.config,
logger: getLoggers().centralized,
price: inputEvents.price,
order: inputEvents.order,
createOrder$,
});
inputEvents.config,
getLoggers().centralized,
inputEvents.price,
inputEvents.order,
createOrder$
);
expectObservable(CEXorder$, inputEvents.unsubscribe).toBe(expected, {
a: null,
});
Expand Down
39 changes: 11 additions & 28 deletions src/centralized/execute-order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,19 @@ import { Logger } from '../logger';
import { CreateOrderParams } from './ccxt/create-order';
import { CEXorder } from './order-builder';

type ExecuteCEXorderParams = {
CEX: Exchange;
config: Config;
logger: Logger;
price: BigNumber;
order: CEXorder;
createOrder$: ({
config,
exchange,
side,
quantity,
}: CreateOrderParams) => Observable<Order>;
};

const executeCEXorder$ = ({
CEX,
config,
logger,
price,
order,
createOrder$,
}: ExecuteCEXorderParams): Observable<null> => {
const executeCEXorder$ = (
CEX: Exchange,
config: Config,
logger: Logger,
price: BigNumber,
order: CEXorder,
createOrder$: (...args: CreateOrderParams) => Observable<Order>
): Observable<null> => {
if (!config.TEST_MODE) {
logger.info(
`Starting centralized exchange ${config.CEX_BASEASSET}/${config.CEX_QUOTEASSET} market ${order.side} order (quantity: ${order.quantity})`
);
return createOrder$({
exchange: CEX,
config,
side: order.side,
quantity: order.quantity,
}).pipe(
return createOrder$(config, CEX, order.side, order.quantity).pipe(
tap(order =>
logger.info(
`Centralized exchange order finished: ${JSON.stringify(order)}`
Expand Down Expand Up @@ -77,4 +58,6 @@ const executeCEXorder$ = ({
}
};

type ExecuteCEXorderParams = Parameters<typeof executeCEXorder$>;

export { executeCEXorder$, ExecuteCEXorderParams };
48 changes: 0 additions & 48 deletions src/centralized/minimum-order-quantity-filter.spec.ts

This file was deleted.

58 changes: 32 additions & 26 deletions src/centralized/minimum-order-quantity-filter.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
import BigNumber from 'bignumber.js';
import { errors } from '../opendex/errors';
import { BigNumber } from 'bignumber.js';
import { curry } from 'ramda';
import { EMPTY, Observable, of } from 'rxjs';
import { mergeMap, take } from 'rxjs/operators';
import { Logger } from '../logger';
import { ArbyStore } from '../store';

type MinimumCEXquantities = {
[key: string]: BigNumber;
};

const MINIMUM_ORDER_SIZE: MinimumCEXquantities = {
BTC: new BigNumber('0.001'),
ETH: new BigNumber('0.05'),
DAI: new BigNumber('15'),
USDT: new BigNumber('15'),
};

const getMinimumOrderSize = (asset: string): BigNumber => {
const minimumOrderSize = MINIMUM_ORDER_SIZE[asset];
if (!minimumOrderSize) {
throw errors.CEX_INVALID_MINIMUM_ORDER_QUANTITY(asset);
const quantityAboveMinimum = curry(
(
store: ArbyStore,
logger: Logger,
assetToTradeOnCEX: string,
minimumQuantity$: Observable<BigNumber>,
quantity: BigNumber
) => {
logger.info(
`Swap success. Accumulated ${assetToTradeOnCEX} quantity: ${quantity.toFixed()}`
);
store.resetLastOrderUpdatePrice();
return minimumQuantity$.pipe(
take(1),
mergeMap(minimumQuantity => {
if (quantity.isGreaterThanOrEqualTo(minimumQuantity)) {
return of(quantity);
}
logger.info(
`Will not execute CEX order because ${quantity.toFixed()} is below the minimum allowed CEX quantity ${minimumQuantity.toFixed()}`
);
return EMPTY;
})
);
}
return minimumOrderSize;
};

const quantityAboveMinimum = (asset: string) => {
return (quantity: BigNumber): boolean => {
return quantity.isGreaterThanOrEqualTo(getMinimumOrderSize(asset));
};
};
);

export { quantityAboveMinimum, MINIMUM_ORDER_SIZE };
export { quantityAboveMinimum };
Loading

0 comments on commit 0cf6ef5

Please sign in to comment.