Skip to content

Commit

Permalink
fix: closed trades timezone consistency (#454)
Browse files Browse the repository at this point in the history
  • Loading branch information
uhliksk authored Aug 8, 2022
1 parent a9319ad commit 8160e25
Show file tree
Hide file tree
Showing 28 changed files with 165 additions and 95 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

- Fixed timezone inconsistency. Thanks [@uhliksk](https://github.com/uhliksk) - [#454](https://github.com/chrisleekr/binance-trading-bot/pull/454)
- Improved UI. Thanks [@uhliksk](https://github.com/uhliksk) - [#452](https://github.com/chrisleekr/binance-trading-bot/pull/452)
- Improved error handler and stability. Thanks [@habibalkhabbaz](https://github.com/habibalkhabbaz) - [#448](https://github.com/chrisleekr/binance-trading-bot/pull/448)

Expand Down
26 changes: 13 additions & 13 deletions app/cronjob/trailingTrade/step/__tests__/determine-action.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1588,7 +1588,7 @@ describe('determine-action.js', () => {
},
tradingView: {
result: {
time: moment().format(),
time: moment().toISOString(),
summary: {
RECOMMENDATION: 'SELL'
}
Expand Down Expand Up @@ -1638,7 +1638,7 @@ describe('determine-action.js', () => {
},
tradingView: {
result: {
time: moment().format(),
time: moment().toISOString(),
summary: {
RECOMMENDATION: undefined
}
Expand All @@ -1663,7 +1663,7 @@ describe('determine-action.js', () => {
},
tradingView: {
result: {
time: moment().subtract('6', 'minutes').format(),
time: moment().subtract('6', 'minutes').toISOString(),
summary: {
RECOMMENDATION: 'SELL'
}
Expand Down Expand Up @@ -1691,7 +1691,7 @@ describe('determine-action.js', () => {
},
tradingView: {
result: {
time: moment().format(),
time: moment().toISOString(),
summary: {
RECOMMENDATION: 'SELL'
}
Expand Down Expand Up @@ -1723,7 +1723,7 @@ describe('determine-action.js', () => {
},
tradingView: {
result: {
time: moment().format(),
time: moment().toISOString(),
summary: {
RECOMMENDATION: 'SELL'
}
Expand Down Expand Up @@ -1758,7 +1758,7 @@ describe('determine-action.js', () => {
},
tradingView: {
result: {
time: moment().format(),
time: moment().toISOString(),
summary: {
RECOMMENDATION: 'SELL'
}
Expand Down Expand Up @@ -1805,7 +1805,7 @@ describe('determine-action.js', () => {
},
tradingView: {
result: {
time: moment().format(),
time: moment().toISOString(),
summary: {
RECOMMENDATION: 'SELL'
}
Expand Down Expand Up @@ -1841,7 +1841,7 @@ describe('determine-action.js', () => {
},
tradingView: {
result: {
time: moment().format(),
time: moment().toISOString(),
summary: {
RECOMMENDATION: 'NEUTRAL'
}
Expand Down Expand Up @@ -1880,7 +1880,7 @@ describe('determine-action.js', () => {
},
tradingView: {
result: {
time: moment().format(),
time: moment().toISOString(),
summary: {
RECOMMENDATION: 'NEUTRAL'
}
Expand Down Expand Up @@ -1916,7 +1916,7 @@ describe('determine-action.js', () => {
},
tradingView: {
result: {
time: moment().format(),
time: moment().toISOString(),
summary: {
RECOMMENDATION: 'SELL'
}
Expand Down Expand Up @@ -1952,7 +1952,7 @@ describe('determine-action.js', () => {
},
tradingView: {
result: {
time: moment().format(),
time: moment().toISOString(),
summary: {
RECOMMENDATION: 'STRONG_SELL'
}
Expand Down Expand Up @@ -1988,7 +1988,7 @@ describe('determine-action.js', () => {
},
tradingView: {
result: {
time: moment().format(),
time: moment().toISOString(),
summary: {
RECOMMENDATION: 'SELL'
}
Expand Down Expand Up @@ -2027,7 +2027,7 @@ describe('determine-action.js', () => {
},
tradingView: {
result: {
time: moment().format(),
time: moment().toISOString(),
summary: {
RECOMMENDATION: 'STRONG_SELL'
}
Expand Down
4 changes: 2 additions & 2 deletions app/cronjob/trailingTrade/step/determine-action.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,8 @@ const shouldForceSellByTradingViewRecommendation = (logger, data) => {
if (tradingViewUpdatedAt.isBefore(currentTime)) {
logger.info(
{
tradingViewUpdatedAt: tradingViewUpdatedAt.format(),
currentTime: currentTime.format()
tradingViewUpdatedAt: tradingViewUpdatedAt.toISOString(),
currentTime: currentTime.toISOString()
},
`TradingView data is older than ${tradingViewUseOnlyWithin} minutes. Ignore TradingView recommendation.`
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ const execute = async (logger, rawData) => {
);
} else {
logger.info(
{ lastBuyOrder, currentTime: moment() },
{ lastBuyOrder, currentTime: moment().format() },
'Skip checking the grid trade last buy order'
);
}
Expand Down Expand Up @@ -328,7 +328,7 @@ const execute = async (logger, rawData) => {
);
} else {
logger.info(
{ lastSellOrder, currentTime: moment() },
{ lastSellOrder, currentTime: moment().format() },
'Skip checking the grid trade last sell order'
);
}
Expand Down
5 changes: 4 additions & 1 deletion app/cronjob/trailingTrade/step/ensure-manual-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,10 @@ const execute = async (logger, rawData) => {

slackMessageOrderDeleted(logger, symbol, order.side, order);
} else {
logger.info({ order, currentTime: moment() }, 'Skip checking the order');
logger.info(
{ order, currentTime: moment().format() },
'Skip checking the order'
);
}
}

Expand Down
4 changes: 3 additions & 1 deletion app/cronjob/trailingTrade/step/get-balances.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ const execute = async (logger, rawData) => {
data.baseAssetBalance = baseAssetBalance;
data.baseAssetBalance.total = baseAssetTotalBalance;
data.baseAssetBalance.estimatedValue = baseAssetEstimatedValue;
data.baseAssetBalance.updatedAt = moment(accountInfo.updateTime).toDate();
data.baseAssetBalance.updatedAt = moment(accountInfo.updateTime)
.utc()
.toDate();

data.quoteAssetBalance = quoteAssetBalance;

Expand Down
2 changes: 1 addition & 1 deletion app/cronjob/trailingTrade/step/get-override-action.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ const execute = async (logger, rawData) => {
...overrideData,
actionAt: moment()
.add(autoTriggerBuyTriggerAfter, 'minutes')
.format()
.toISOString()
},
rescheduleReason
);
Expand Down
6 changes: 3 additions & 3 deletions app/cronjob/trailingTrade/step/handle-open-orders.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ const execute = async (logger, rawData) => {
symbol,
{
action: 'buy',
actionAt: moment().format(),
actionAt: moment().toISOString(),
triggeredBy: 'buy-cancelled',
notify: false,
checkTradingView: true
Expand Down Expand Up @@ -153,7 +153,7 @@ const execute = async (logger, rawData) => {
data.accountInfo = await updateAccountInfo(
logger,
balances,
moment().utc().format()
moment().toISOString()
);
}
} else {
Expand Down Expand Up @@ -219,7 +219,7 @@ const execute = async (logger, rawData) => {
data.accountInfo = await updateAccountInfo(
logger,
balances,
moment().utc().format()
moment().toISOString()
);
}
} else {
Expand Down
12 changes: 6 additions & 6 deletions app/cronjob/trailingTrade/step/place-buy-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ const isAllowedTradingViewRecommendation = (logger, data) => {
if (tradingViewIfExpires === 'do-not-buy') {
logger.info(
{
tradingViewUpdatedAt: tradingViewUpdatedAt.format(),
currentTime: currentTime.format()
tradingViewUpdatedAt: tradingViewUpdatedAt.toISOString(),
currentTime: currentTime.toISOString()
},
`TradingView data is older than ${tradingViewUseOnlyWithin} minutes. Do not buy.`
);
Expand All @@ -93,8 +93,8 @@ const isAllowedTradingViewRecommendation = (logger, data) => {

logger.info(
{
tradingViewUpdatedAt: tradingViewUpdatedAt.format(),
currentTime: currentTime.format()
tradingViewUpdatedAt: tradingViewUpdatedAt.toISOString(),
currentTime: currentTime.toISOString()
},
`TradingView data is older than ${tradingViewUseOnlyWithin} minutes. Ignore TradingView recommendation.`
);
Expand Down Expand Up @@ -218,7 +218,7 @@ const execute = async (logger, rawData) => {
symbol,
{
action: 'buy',
actionAt: moment().add(1, 'minutes').format(),
actionAt: moment().add(1, 'minutes').toISOString(),
triggeredBy: 'buy-order-trading-view',
notify: false,
checkTradingView: true
Expand Down Expand Up @@ -462,7 +462,7 @@ const execute = async (logger, rawData) => {
data.accountInfo = await updateAccountInfo(
logger,
balances,
moment().utc().format()
moment().toISOString()
);

slack.sendMessage(
Expand Down
4 changes: 3 additions & 1 deletion app/cronjob/trailingTrade/step/remove-last-buy-price.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ const removeLastBuyPrice = async (
symbol,
{
action: 'buy',
actionAt: moment().add(autoTriggerBuyTriggerAfter, 'minutes').format(),
actionAt: moment()
.add(autoTriggerBuyTriggerAfter, 'minutes')
.toISOString(),
triggeredBy: 'auto-trigger',
notify: true,
checkTradingView: true
Expand Down
4 changes: 2 additions & 2 deletions app/cronjob/trailingTradeHelper/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,12 +451,12 @@ const archiveSymbolGridTrade = async (logger, symbol = null) => {
..._.pick(symbolInfo, ['baseAsset', 'quoteAsset']),
...gridProfit,
..._.omit(symbolGridTrade, 'key', '_id'),
archivedAt: moment().format()
archivedAt: moment().toISOString()
};

await saveSymbolGridTradeArchive(
logger,
`${symbol}-${moment().format()}`,
`${symbol}-${moment().toISOString()}`,
archivedGridTrade
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,29 @@ describe('get-closed-trades.js', () => {
describe('when mongo.aggregate returns value', () => {
[
{
selectedPeriod: undefined
selectedPeriod: undefined,
selectedPeriodTZ: 'UTC',
selectedPeriodLC: 'us'
},
{
selectedPeriod: 'd'
selectedPeriod: 'd',
selectedPeriodTZ: 'UTC',
selectedPeriodLC: 'us'
},
{
selectedPeriod: 'w'
selectedPeriod: 'w',
selectedPeriodTZ: 'UTC',
selectedPeriodLC: 'us'
},
{
selectedPeriod: 'm'
selectedPeriod: 'm',
selectedPeriodTZ: 'UTC',
selectedPeriodLC: 'us'
},
{
selectedPeriod: 'a'
selectedPeriod: 'a',
selectedPeriodTZ: 'UTC',
selectedPeriodLC: 'us'
}
].forEach(t => {
describe(`selectedPeriod: ${t.selectedPeriod}`, () => {
Expand All @@ -46,7 +56,9 @@ describe('get-closed-trades.js', () => {

cacheMock.hget = jest.fn().mockResolvedValue(
JSON.stringify({
selectedPeriod: t.selectedPeriod
selectedPeriod: t.selectedPeriod,
selectedPeriodTZ: t.selectedPeriodTZ,
selectedPeriodLC: t.selectedPeriodLC
})
);

Expand All @@ -55,27 +67,31 @@ describe('get-closed-trades.js', () => {
start = null;
end = null;

const momentLocale = moment
.tz(t.selectedPeriodTZ)
.locale(t.selectedPeriodLC);

switch (t.selectedPeriod) {
case 'd':
start = moment().startOf('day').toISOString();
end = moment().endOf('day').toISOString();
start = momentLocale.startOf('day').toISOString();
end = momentLocale.endOf('day').toISOString();
break;
case 'w':
start = moment().startOf('week').toISOString();
end = moment().endOf('week').toISOString();
start = momentLocale.startOf('week').toISOString();
end = momentLocale.endOf('week').toISOString();
break;
case 'm':
start = moment().startOf('month').toISOString();
end = moment().endOf('month').toISOString();
start = momentLocale.startOf('month').toISOString();
end = momentLocale.endOf('month').toISOString();
break;
case 'a':
default:
}

if (start || end) {
match.archivedAt = {
...(start ? { $gte: moment(start).toISOString() } : {}),
...(end ? { $lte: moment(end).toISOString() } : {})
...(start ? { $gte: start } : {}),
...(end ? { $lte: end } : {})
};
}

Expand Down Expand Up @@ -176,7 +192,11 @@ describe('get-closed-trades.js', () => {
'closed-trades',
JSON.stringify({
selectedPeriod: t.selectedPeriod,
loadedPeriod: t.selectedPeriod || 'a'
selectedPeriodTZ: t.selectedPeriodTZ,
selectedPeriodLC: t.selectedPeriodLC,
loadedPeriod: t.selectedPeriod || 'a',
loadedPeriodTZ: t.selectedPeriodTZ,
loadedPeriodLC: t.selectedPeriodLC
})
);
});
Expand Down Expand Up @@ -298,7 +318,9 @@ describe('get-closed-trades.js', () => {
'trailing-trade-common',
'closed-trades',
JSON.stringify({
loadedPeriod: 'a'
loadedPeriod: 'a',
loadedPeriodTZ: 'UTC',
loadedPeriodLC: 'us'
})
);
});
Expand Down
Loading

0 comments on commit 8160e25

Please sign in to comment.