Skip to content

Commit

Permalink
Updated MACD-STOP-CHASER
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Lee committed Aug 17, 2020
1 parent 8aaf477 commit 40e2c3b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
23 changes: 20 additions & 3 deletions app/jobs/common/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,15 +247,32 @@ const placeStopLossLimitOrder = async (
indicators,
stopLossLimitInfo
) => {
const { symbol } = symbolInfo;
const lastBuyPrice = +(cache.get(`last-buy-price-${symbol}`) || 0);
const lastCandleClose = +indicators.lastCandle.close;

if (lastCandleClose <= lastBuyPrice) {
logger.error({ lastCandleClose, lastBuyPrice }, `Last buy price is lower than current price. Do not place order.`);
return {
result: false,
message: `Last buy price is lower than current price. Do not place order.`,
lastCandleClose,
lastBuyPrice
};
}

const basePrice = +indicators.lastCandle.close;
const balance = balanceInfo.freeBalance;
const lotPrecision = symbolInfo.filterLotSize.stepSize.indexOf(1) - 1;
const orderPrecision = symbolInfo.filterPrice.tickSize.indexOf(1) - 1;

logger.info({ basePrice, balance, orderPrecision, stopLossLimitInfo }, 'Prepare params');
logger.info(
{ lastBuyPrice, lastCandleClose, basePrice, balance, orderPrecision, stopLossLimitInfo },
'Prepare params'
);

const stopPrice = roundDown(basePrice * stopLossLimitInfo.stopPercentage, orderPrecision);
const price = roundDown(basePrice * stopLossLimitInfo.limitPercentage, orderPrecision);
const stopPrice = roundDown(basePrice * +stopLossLimitInfo.stopPercentage, orderPrecision);
const price = roundDown(basePrice * +stopLossLimitInfo.limitPercentage, orderPrecision);

// Calculate quantity - commission
const quantity = +(balance - balance * (0.1 / 100)).toFixed(lotPrecision);
Expand Down
9 changes: 5 additions & 4 deletions app/jobs/macdStopChaser.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ const execute = async logger => {
`);
orderResult = await macdStopChaserHelper.placeOrder(logger, 'buy', 100, indicators);
} else if (tradeActionResult.action === 'sell') {
slack.sendMessage(`Signal: *SELL*
- Action Result: \`\`\`${JSON.stringify(tradeActionResult, undefined, 2)}\`\`\`
`);
orderResult = await macdStopChaserHelper.placeOrder(logger, 'sell', 100, indicators);
logger.info(`Got sell signal, but do nothing`);
// slack.sendMessage(`Signal: *SELL*
// - Action Result: \`\`\`${JSON.stringify(tradeActionResult, undefined, 2)}\`\`\`
// `);
// orderResult = await macdStopChaserHelper.placeOrder(logger, 'sell', 100, indicators);
} else {
orderResult = await macdStopChaserHelper.chaseStopLossLimitOrder(logger, indicators);
}
Expand Down
10 changes: 6 additions & 4 deletions app/jobs/macdStopChaser/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const determineAction = (logger, indicators) => {

let allRising = true;
let allFalling = true;
for (let i = -3; i < 0; i += 1) {
for (let i = -2; i < 0; i += 1) {
let macdValue = 0;
let macdSignal = 0;
if (i !== -1) {
Expand Down Expand Up @@ -194,7 +194,7 @@ const placeOrder = async (logger, side, percentage, indicators) => {

// 5. Place order
const orderParams = {
symbol: config.get('jobs.macdStopChaser.symbol'),
symbol,
side,
type: 'LIMIT',
quantity: orderQuantityInfo.orderQuantity,
Expand All @@ -213,6 +213,8 @@ const placeOrder = async (logger, side, percentage, indicators) => {

logger.info({ orderResult }, 'Order result');

cache.set(`last-buy-price-${symbol}`, orderPriceInfo.orderPriceInfo);

await slack.sendMessage(
`Action Result: *${side}*
- Order Result: \`\`\`${JSON.stringify(orderResult, undefined, 2)}\`\`\``
Expand Down Expand Up @@ -268,10 +270,10 @@ const chaseStopLossLimitOrder = async (logger, indicators) => {
const basePrice = +indicators.lastCandle.close;
// 3. If the order is not stop loss limit order, then do nothing
if (order.type !== 'STOP_LOSS_LIMIT') {
logger.info({ order }, 'Order is not STOP_LOSS_LIMIT, do nothing.');
logger.info({ order }, 'Order is not STOP_LOSS_LIMIT, Do nothing.');
return {
result: false,
message: 'Order is not STOP_LOSS_LIMIT, do nothing.'
message: 'Order is not STOP_LOSS_LIMIT, Do nothing.'
};
}

Expand Down
12 changes: 6 additions & 6 deletions config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,22 @@
"macdStopChaser": {
"enabled": true,
"cronTime": "* * * * * *",
"symbol": "BTCUSDT",
"symbol": "BNBUSDT",
"macd": {
"shortPeriod": 12,
"longPeriod": 26,
"signalPeriod": 9
},
"min": {
"period": 200
"period": 500
},
"candles": {
"interval": "1m",
"limit": 200
"interval": "3m",
"limit": 500
},
"stopLossLimit": {
"stopPercentage": 0.99,
"limitPercentage": 0.98
"stopPercentage": 0.998,
"limitPercentage": 0.985
}
}
}
Expand Down

0 comments on commit 40e2c3b

Please sign in to comment.