Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save evan-coygo/b59c2f6c256009730f698068bd309408 to your computer and use it in GitHub Desktop.
Save evan-coygo/b59c2f6c256009730f698068bd309408 to your computer and use it in GitHub Desktop.
/*
Determine if orders should be submitted or cancelled
*/
let shouldSubmitBuyOrder = false;
let shouldCancelBuyOrder = false;
let shouldSubmitSellOrder = false;
let shouldCancelSellOrder = false;
// determine if it's time to submit or cancel the buy order
if (!buyOrder.get()) {
// if no current buy order exists it's time to submit one
shouldSubmitBuyOrder = true;
coygo.log('## No current buy order found. Signalling to submit a new buy order');
} else {
coygo.log('## Current buy order:');
const buyOrderObj = buyOrder.get();
let buyOrderTableLog =
`| Property | Value |
| ---- | ---- |
| side | ${buyOrderObj.side} |
| status | ${buyOrderObj.status} |
| rate | ${buyOrderObj.rate} |
| amount | ${buyOrderObj.amount} |
| filled | ${buyOrderObj.filled} |
`;
coygo.log(buyOrderTableLog);
// if a current buy order exists we need to check its status and rate to determine if we need
// to submit a new order or cancel it
if (isWaitingForBuyToCancel.get() === true) {
coygo.log('Waiting for the buy order to be canceled. Buy order=', buyOrder.get());
if (buyOrder.get().status === coygo.orderStatuses.canceled) {
// we were waiting for the buy order to confirm being canceled and now it's done. time to submit new order
shouldSubmitBuyOrder = true;
isWaitingForBuyToCancel.set(false);
coygo.log('Buy order finished being canceled. Signaling to submit a new buy order');
}
} else if (buyOrder.get().isOpen === false) {
// the existing buy order is no longer open, time to re-submit a new one
shouldSubmitBuyOrder = true;
coygo.log('Current buy order is no longer open. New order status: ' + buyOrder.get().status + '. Signalling to submit a new buy order.');
buyOrder.set(null);
} else if (buyOrder.get().isOpen === true) {
// current buy order found and is open. check if it needs to be cancelled by calculating difference between this order's rate and current ask
// example: "ask" is 100, order "rate" is 95. difference = ((100 - 95) / 100) * 100 = 5. "rate" of 95 is 5% from "ask" of 100
const differenceBetweenRateAndAsk = ((orderBook.ask - buyOrder.get().rate) / orderBook.ask) * 100;
coygo.log('An existing buy order was found, determining if it should be cancelled');
if (differenceBetweenRateAndAsk > cancelOrderThresholdInput) {
shouldCancelBuyOrder = true;
coygo.log('The buy order was found to be too far from current ask. Signaling to be cancelled', {
ask: orderBook.ask,
'order rate': buyOrder.get().rate,
'difference between rate and ask': differenceBetweenRateAndAsk.toFixed(4) + ' %',
'cancel order threshold': cancelOrderThresholdInput + ' %'
});
} else {
coygo.log('The buy order was found to be within the proper threshold and will not be cancelled', {
ask: orderBook.ask,
'order rate': buyOrder.get().rate,
'difference between rate and ask': differenceBetweenRateAndAsk.toFixed(4) + ' %',
'cancel order threshold': cancelOrderThresholdInput + ' %'
})
}
}
}
coygo.log('Finished determining next actions for buy order', {
'is waiting for buy to cancel?': isWaitingForBuyToCancel.get(),
'should submit buy order?': shouldSubmitBuyOrder,
'should cancel buy order?': shouldCancelBuyOrder
});
// determine if it's time to submit or cancel the sell order
if (!sellOrder.get()) {
// if no current sell order exists it's time to submit one
shouldSubmitSellOrder = true;
coygo.log('## No current sell order found. Signalling to submit a new sell order');
} else {
coygo.log('## Current sell order:');
const sellOrderObj = sellOrder.get();
let sellOrderLog =
`| Property | Value |
| ---- | ---- |
| side | ${sellOrderObj.side} |
| status | ${sellOrderObj.status} |
| rate | ${sellOrderObj.rate} |
| amount | ${sellOrderObj.amount} |
| filled | ${sellOrderObj.filled} |
`;
coygo.log(sellOrderLog);
// if a current sell order exists we need to check its status and rate to determine if we need
// to submit a new order or cancel it
if (isWaitingForSellToCancel.get() === true) {
coygo.log('Waiting for the sell order to be canceled. Sell order=', sellOrder.get());
if (sellOrder.get().status === coygo.orderStatuses.canceled) {
// we were waiting for the ellrder to confirm being canceled and now it's done. time to submit new order
shouldSubmitSellOrder = true;
isWaitingForSellToCancel.set(false);
coygo.log('Sell order finished being canceled. Signaling to submit a new sell order');
}
} else if (sellOrder.get().isOpen === false) {
// the existing sell order is no longer open, time to re-submit a new one
shouldSubmitSellOrder = true;
coygo.log('Current sell order is no longer open. New order status: ' + sellOrder.get().status + '. Signalling to submit a new sell order.');
sellOrder.set(null);
} else if (sellOrder.get().isOpen === true) {
// current sell order found and is open. check if it needs to be cancelled by calculating difference between this order's rate and current bid
// example: "bid" is 100, order "rate" is 105. difference = ((105 - 100) / 100) * 100 = 5. "rate" of 105 is 5% from "bid" of 100
const differenceBetweenRateAndBid = ((sellOrder.get().rate - orderBook.bid) / orderBook.bid) * 100;
coygo.log('An existing sell order was found, determining if it should be cancelled');
if (differenceBetweenRateAndBid > cancelOrderThresholdInput) {
shouldCancelSellOrder = true;
coygo.log('The sell order was found to be too far from current bid. Signaling to be cancelled', {
bid: orderBook.bid,
'order rate': sellOrder.get().rate,
'difference between rate and bid': differenceBetweenRateAndBid.toFixed(4) + ' %',
'cancel order threshold': cancelOrderThresholdInput + ' %'
});
} else {
coygo.log('The sell order was found to be within the proper threshold and will not be cancelled', {
bid: orderBook.bid,
'order rate': sellOrder.get().rate,
'difference between rate and bid': differenceBetweenRateAndBid.toFixed(4) + ' %',
'cancel order threshold': cancelOrderThresholdInput + ' %'
});
}
}
}
coygo.log('Finished determining next actions for sell order', {
'is waiting for sell to cancel?': isWaitingForSellToCancel.get(),
'should submit sell order?': shouldSubmitSellOrder,
'should cancel sell order?': shouldCancelSellOrder
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment