Last active
August 17, 2022 17:36
-
-
Save evan-coygo/ce06c55e1f66ecb5843533986235234f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Submit or cancel orders when appropriate | |
*/ | |
// handle submiting or canceling the buy order when instructed | |
if (shouldSubmitBuyOrder) { | |
// calculate the buy order rate using configured inputs | |
// example: if "ask" is 100 and "buyRatePercentBelowAskInput" is 2, buyOrderRate = 100 * (1 - (2 / 100) = 98 | |
let buyOrderRate = orderBook.ask * (1 - (buyRatePercentBelowAskInput / 100)); | |
if (marketToUse && marketToUse.quoteSymbolDecimalCount) { | |
// apply decimal count limit for this exchange | |
buyOrderRate = parseFloat(buyOrderRate.toFixed(marketToUse.quoteSymbolDecimalCount)); | |
} | |
// there is no buy order, submit one | |
const buyOrderConfig = { | |
market: marketToUse, | |
type: coygo.orderTypes.limit, | |
side: coygo.orderSides.buy, | |
amount: orderAmount, | |
rate: buyOrderRate | |
}; | |
coygo.log('## Attempting to submit the following buy order:', { | |
ask: orderBook.ask, | |
'buy rate % below ask input': buyRatePercentBelowAskInput, | |
'buy order rate': buyOrderRate, | |
'order config': buyOrderConfig | |
}) | |
const canSubmitCheck = coygo.canSubmitOrder(buyOrderConfig); | |
if (canSubmitCheck.canSubmit === true) { | |
// order can be submitted | |
const submittedBuyOrder = coygo.submitOrder(buyOrderConfig); | |
coygo.log('Submitted buy order!'); | |
// record the submitted buy order so it can be accessed later to check on its status | |
buyOrder.set(submittedBuyOrder); | |
} else if (canSubmitCheck.canSubmit === false) { | |
// order cannot be submitted. see 'canSubmitCheck.reason' for more details | |
coygo.log('## Unable to submit buy order', canSubmitCheck); | |
} | |
} else if (shouldCancelBuyOrder) { | |
coygo.log('## Canceling buy order'); | |
// handle canceling the buy order | |
coygo.cancelOpenOrder({ | |
order: buyOrder.get() | |
}); | |
// record that we're now waiting for the buy order to cancel | |
// this takes a variable amount of time so we don't know exactly when it will be complete | |
isWaitingForBuyToCancel.set(true); | |
} | |
// handle submiting or canceling the sell order when instructed | |
if (shouldSubmitSellOrder) { | |
// calculate the buy order rate using configured inputs | |
// example: if "bid" is 100 and "sellRatePercentAboveBidInput" is 2, sellOrderRate = 100 * (1 + (2 / 100) = 102 | |
let sellOrderRate = orderBook.bid * (1 + (sellRatePercentAboveBidInput / 100)); | |
if (marketToUse && marketToUse.quoteSymbolDecimalCount) { | |
// apply decimal count limit for this exchange | |
sellOrderRate = parseFloat(sellOrderRate.toFixed(marketToUse.quoteSymbolDecimalCount)); | |
} | |
// there is no buy order, submit one | |
const sellOrderConfig = { | |
market: marketToUse, | |
type: coygo.orderTypes.limit, | |
side: coygo.orderSides.sell, | |
amount: orderAmount, | |
rate: sellOrderRate | |
}; | |
coygo.log('## Attempting to submit the following sell order:', { | |
bid: orderBook.bid, | |
'sell rate % above bid input': sellRatePercentAboveBidInput, | |
'sell order rate': sellOrderRate, | |
'order config': sellOrderConfig | |
}) | |
const canSubmitCheck = coygo.canSubmitOrder(sellOrderConfig); | |
if (canSubmitCheck.canSubmit === true) { | |
// order can be submitted | |
const submittedSellOrder = coygo.submitOrder(sellOrderConfig); | |
coygo.log('Submitted sell order!'); | |
// record the submitted sell order so it can be accessed later to check on its status | |
sellOrder.set(submittedSellOrder); | |
} else if (canSubmitCheck.canSubmit === false) { | |
// order cannot be submitted. see 'canSubmitCheck.reason' for more details | |
coygo.log('## Unable to submit sell order', canSubmitCheck); | |
} | |
} else if (shouldCancelSellOrder) { | |
coygo.log('## Canceling sell order'); | |
// handle canceling the sell order | |
coygo.cancelOpenOrder({ | |
order: sellOrder.get() | |
}); | |
// record that we're now waiting for the sell order to cancel | |
// this takes a variable amount of time so we don't know exactly when it will be complete | |
isWaitingForSellToCancel.set(true); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment