Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Buy grid stuck after last buy order is not removed from mongodb #560

Closed
uhliksk opened this issue Dec 30, 2022 · 30 comments · Fixed by #562
Closed

Buy grid stuck after last buy order is not removed from mongodb #560

uhliksk opened this issue Dec 30, 2022 · 30 comments · Fixed by #562
Assignees
Labels
bug Something isn't working

Comments

@uhliksk
Copy link
Contributor

uhliksk commented Dec 30, 2022

Version

v0.0.96

Description

Sometimes after buy order is filled it is not removed from trailing-trade-grid-trade-orders which will cause another buy order will create secondary SYMBOL-grid-trade-last-buy-order record. After buy order is filled, then one of those records is deleted and one will remain in database. This will cause next buy order is not executed because of There is a last gird trade buy order. Wait.. After that the symbol trading is stuck. Not even stop-loss is working as there are buy orders not filled. This can cause significant loss in bear market.

To Reproduce

Random error. I tried to log in detail but didn't found the cause why is the SYMBOL-grid-trade-last-buy-order record not removed from mongodb sometimes.

Edit: From what I've seen I think this issue is occuring when buy order is filled very shortly after new buy order is made. In normal slow market this error will not occur as there is plenty of time to process new order before it's filled but in fast market the price can spike so fast it'll trigger buy order filled right after new buy order is created and this will make inconsistent state as websocket is processed while previous job in queue is still running. The probable cause of issue is making changes in database out of order. Changes in database should be part of the job in queue or otherwise we shouldn't use the queue at all.

Expected Behaviours

It should always remove the SYMBOL-grid-trade-last-buy-order record from mongodb when buy order is filled.

Screenshots

image

Additional context

I tried to log all "deleteOne" transactions which reveals previous SYMBOL-grid-trade-last-buy-order was never asked to be removed from database. I didn't found why ensure-grid-trade-order-executed.js will skip this. I'm currently trying to log in detail how the buy order is processed line by line, but it's hard to debug as I have to wait until the bug will occur again.

@uhliksk uhliksk added the bug Something isn't working label Dec 30, 2022
@chrisleekr
Copy link
Owner

Hmm, strange.

Are you able to export logs when it happens again?

@uhliksk
Copy link
Contributor Author

uhliksk commented Dec 30, 2022

It just happend even after buy order cancellation:

image

Now stuck at Grid Trade #1:

image

I can't export logs as it is freezing my browser when I click Export logs. I'll try to resolve the issue.

@uhliksk
Copy link
Contributor Author

uhliksk commented Dec 30, 2022

It looks like BUY - FILLED has been executed before BUY - NEW has been finished. Isn't queue concurrency set to 1 to prevent this? How is it possible it was not waiting in the queue? 🤔

@uhliksk
Copy link
Contributor Author

uhliksk commented Dec 30, 2022

I tried to log every job start and finish in queue and it is very strage what I found.

image

Jobs are running as expected, but logged events are overlapping and running before jobs with corresponding IDs were running in queue.

image

I thought everything is running in the quere but from what I see there are some processes running in parallel before the job is queued?

@uhliksk
Copy link
Contributor Author

uhliksk commented Dec 30, 2022

Now I see those unknown events are actually inserting jobs into queue and then just first job is processed as buy filled and other one is silently skipped thru trailing trade steps. It's a bit confusing but I think I understand the logic now.

I thought when new buy order is received and then buy order fill is received then in queue will be both events processed. It looks like only the last event is processed in first running job in queue. Second job is practically useless and doing nothing as there is no change since the first queued job has been finished.

So it looks like if server is lagging then it can somehow receive next buy order before the job for buy filled is finished? I'm little bit confused about the logic of queue.

@uhliksk
Copy link
Contributor Author

uhliksk commented Dec 30, 2022

I tried to change max to 100 as the bot was also very laggy missing some spikes in price. Since then I don't see the error yet. I'll keep testing over night.

const queue = new Queue(symbol, REDIS_URL, {
prefix: `bull`,
limiter: {
max: 1,
duration: 10000, // 10 seconds

@uhliksk
Copy link
Contributor Author

uhliksk commented Dec 31, 2022

Doesn't helped. It's There is a last gird trade buy order. Wait. again.

image

@uhliksk
Copy link
Contributor Author

uhliksk commented Dec 31, 2022

Interesting thing is it can fix this automatically if bot is restarted by docker restart binance-bot. I'll do restart every 15 minutes with cron as a workaround but it's very annoying bug causing bad losses.

@uhliksk
Copy link
Contributor Author

uhliksk commented Dec 31, 2022

It happened again. Log from bot:

image

@uhliksk
Copy link
Contributor Author

uhliksk commented Dec 31, 2022

My detailed log about running steps indicated the trailing trade at time of buy order filled on Binance (10:04:41) has been running longer than usual. It looks like a problem is when Binance fill buy order while trailing trade is processing a job. Time on Binance is local, time in log is UTC.

image

..."level":50,"msg":"381e1d81-0586-4bf9-94a5-a0f66f4ff471 Job started.","time":"2022-12-31T10:04:38.902Z","v":0}
..."jobName":"trailingTrade","correlationId":"381e1d81-0586-4bf9-94a5-a0f66f4ff471","symbol":"BTCBUSD","level":50,"msg":"Step - get-symbol-configuration","time":"2022-12-31T10:04:38.904Z","v":0}
..."jobName":"trailingTrade","correlationId":"381e1d81-0586-4bf9-94a5-a0f66f4ff471","symbol":"BTCBUSD","level":50,"msg":"Step - get-symbol-info","time":"2022-12-31T10:04:38.909Z","v":0}
..."jobName":"trailingTrade","correlationId":"381e1d81-0586-4bf9-94a5-a0f66f4ff471","symbol":"BTCBUSD","level":50,"msg":"Step - ensure-manual-order","time":"2022-12-31T10:04:38.913Z","v":0}
..."jobName":"trailingTrade","correlationId":"381e1d81-0586-4bf9-94a5-a0f66f4ff471","symbol":"BTCBUSD","level":50,"msg":"Step - ensure-grid-trade-order-executed","time":"2022-12-31T10:04:38.917Z","v":0}
..."jobName":"trailingTrade","correlationId":"381e1d81-0586-4bf9-94a5-a0f66f4ff471","symbol":"BTCBUSD","level":50,"msg":"Step - get-balances","time":"2022-12-31T10:04:38.922Z","v":0}
..."jobName":"trailingTrade","correlationId":"381e1d81-0586-4bf9-94a5-a0f66f4ff471","symbol":"BTCBUSD","level":50,"msg":"Step - get-open-orders","time":"2022-12-31T10:04:38.925Z","v":0}
..."jobName":"trailingTrade","correlationId":"381e1d81-0586-4bf9-94a5-a0f66f4ff471","symbol":"BTCBUSD","level":50,"msg":"Step - get-indicators","time":"2022-12-31T10:04:38.929Z","v":0}
..."jobName":"trailingTrade","correlationId":"381e1d81-0586-4bf9-94a5-a0f66f4ff471","symbol":"BTCBUSD","level":50,"msg":"Step - get-override-action","time":"2022-12-31T10:04:38.937Z","v":0}
..."jobName":"trailingTrade","correlationId":"381e1d81-0586-4bf9-94a5-a0f66f4ff471","symbol":"BTCBUSD","level":50,"msg":"Step - handle-open-orders","time":"2022-12-31T10:04:38.943Z","v":0}
..."jobName":"trailingTrade","correlationId":"381e1d81-0586-4bf9-94a5-a0f66f4ff471","symbol":"BTCBUSD","level":50,"msg":"Step - get-balances","time":"2022-12-31T10:04:38.946Z","v":0}
..."jobName":"trailingTrade","correlationId":"381e1d81-0586-4bf9-94a5-a0f66f4ff471","symbol":"BTCBUSD","level":50,"msg":"Step - determine-action","time":"2022-12-31T10:04:38.950Z","v":0}
..."jobName":"trailingTrade","correlationId":"381e1d81-0586-4bf9-94a5-a0f66f4ff471","symbol":"BTCBUSD","level":50,"msg":"Step - place-manual-order","time":"2022-12-31T10:04:38.953Z","v":0}
..."jobName":"trailingTrade","correlationId":"381e1d81-0586-4bf9-94a5-a0f66f4ff471","symbol":"BTCBUSD","level":50,"msg":"Step - cancel-order","time":"2022-12-31T10:04:38.957Z","v":0}
..."jobName":"trailingTrade","correlationId":"381e1d81-0586-4bf9-94a5-a0f66f4ff471","symbol":"BTCBUSD","level":50,"msg":"Step - place-buy-order","time":"2022-12-31T10:04:38.960Z","v":0}
..."jobName":"trailingTrade","correlationId":"381e1d81-0586-4bf9-94a5-a0f66f4ff471","symbol":"BTCBUSD","level":50,"msg":"Step - place-sell-order","time":"2022-12-31T10:04:38.963Z","v":0}
..."jobName":"trailingTrade","correlationId":"381e1d81-0586-4bf9-94a5-a0f66f4ff471","symbol":"BTCBUSD","level":50,"msg":"Step - place-sell-stop-loss-order","time":"2022-12-31T10:04:38.966Z","v":0}
..."jobName":"trailingTrade","correlationId":"381e1d81-0586-4bf9-94a5-a0f66f4ff471","symbol":"BTCBUSD","level":50,"msg":"Step - get-balances","time":"2022-12-31T10:04:38.969Z","v":0}
..."jobName":"trailingTrade","correlationId":"381e1d81-0586-4bf9-94a5-a0f66f4ff471","symbol":"BTCBUSD","level":50,"msg":"Step - remove-last-buy-price","time":"2022-12-31T10:04:38.972Z","v":0}
..."jobName":"trailingTrade","correlationId":"381e1d81-0586-4bf9-94a5-a0f66f4ff471","symbol":"BTCBUSD","level":50,"msg":"Step - save-data-to-cache","time":"2022-12-31T10:04:38.981Z","v":0}
..."level":50,"msg":"381e1d81-0586-4bf9-94a5-a0f66f4ff471 Job finished.","time":"2022-12-31T10:04:38.994Z","v":0}
..."level":50,"msg":"a56c59fc-5559-4601-bdf8-af3da2781733 Job started.","time":"2022-12-31T10:04:39.909Z","v":0}
..."jobName":"trailingTrade","correlationId":"a56c59fc-5559-4601-bdf8-af3da2781733","symbol":"BTCBUSD","level":50,"msg":"Step - get-symbol-configuration","time":"2022-12-31T10:04:39.911Z","v":0}
..."jobName":"trailingTrade","correlationId":"a56c59fc-5559-4601-bdf8-af3da2781733","symbol":"BTCBUSD","level":50,"msg":"Step - get-symbol-info","time":"2022-12-31T10:04:39.916Z","v":0}
..."jobName":"trailingTrade","correlationId":"a56c59fc-5559-4601-bdf8-af3da2781733","symbol":"BTCBUSD","level":50,"msg":"Step - ensure-manual-order","time":"2022-12-31T10:04:39.920Z","v":0}
..."jobName":"trailingTrade","correlationId":"a56c59fc-5559-4601-bdf8-af3da2781733","symbol":"BTCBUSD","level":50,"msg":"Step - ensure-grid-trade-order-executed","time":"2022-12-31T10:04:39.924Z","v":0}
..."jobName":"trailingTrade","correlationId":"a56c59fc-5559-4601-bdf8-af3da2781733","symbol":"BTCBUSD","level":50,"msg":"Step - get-balances","time":"2022-12-31T10:04:39.929Z","v":0}
..."jobName":"trailingTrade","correlationId":"a56c59fc-5559-4601-bdf8-af3da2781733","symbol":"BTCBUSD","level":50,"msg":"Step - get-open-orders","time":"2022-12-31T10:04:39.932Z","v":0}
..."jobName":"trailingTrade","correlationId":"a56c59fc-5559-4601-bdf8-af3da2781733","symbol":"BTCBUSD","level":50,"msg":"Step - get-indicators","time":"2022-12-31T10:04:39.936Z","v":0}
..."jobName":"trailingTrade","correlationId":"a56c59fc-5559-4601-bdf8-af3da2781733","symbol":"BTCBUSD","level":50,"msg":"Step - get-override-action","time":"2022-12-31T10:04:39.943Z","v":0}
..."jobName":"trailingTrade","correlationId":"a56c59fc-5559-4601-bdf8-af3da2781733","symbol":"BTCBUSD","level":50,"msg":"Step - handle-open-orders","time":"2022-12-31T10:04:39.947Z","v":0}
..."jobName":"trailingTrade","correlationId":"a56c59fc-5559-4601-bdf8-af3da2781733","symbol":"BTCBUSD","level":50,"msg":"Step - get-balances","time":"2022-12-31T10:04:39.950Z","v":0}
..."jobName":"trailingTrade","correlationId":"a56c59fc-5559-4601-bdf8-af3da2781733","symbol":"BTCBUSD","level":50,"msg":"Step - determine-action","time":"2022-12-31T10:04:39.954Z","v":0}
..."jobName":"trailingTrade","correlationId":"a56c59fc-5559-4601-bdf8-af3da2781733","symbol":"BTCBUSD","level":50,"msg":"Step - place-manual-order","time":"2022-12-31T10:04:39.957Z","v":0}
..."jobName":"trailingTrade","correlationId":"a56c59fc-5559-4601-bdf8-af3da2781733","symbol":"BTCBUSD","level":50,"msg":"Step - cancel-order","time":"2022-12-31T10:04:39.960Z","v":0}
..."jobName":"trailingTrade","correlationId":"a56c59fc-5559-4601-bdf8-af3da2781733","symbol":"BTCBUSD","level":50,"msg":"Step - place-buy-order","time":"2022-12-31T10:04:39.963Z","v":0}
..."jobName":"trailingTrade","correlationId":"a56c59fc-5559-4601-bdf8-af3da2781733","symbol":"BTCBUSD","level":50,"msg":"Step - place-sell-order","time":"2022-12-31T10:04:39.967Z","v":0}
..."jobName":"trailingTrade","correlationId":"a56c59fc-5559-4601-bdf8-af3da2781733","symbol":"BTCBUSD","level":50,"msg":"Step - place-sell-stop-loss-order","time":"2022-12-31T10:04:39.970Z","v":0}
..."jobName":"trailingTrade","correlationId":"a56c59fc-5559-4601-bdf8-af3da2781733","symbol":"BTCBUSD","level":50,"msg":"Step - get-balances","time":"2022-12-31T10:04:39.973Z","v":0}
..."jobName":"trailingTrade","correlationId":"a56c59fc-5559-4601-bdf8-af3da2781733","symbol":"BTCBUSD","level":50,"msg":"Step - remove-last-buy-price","time":"2022-12-31T10:04:39.976Z","v":0}
..."jobName":"trailingTrade","correlationId":"a56c59fc-5559-4601-bdf8-af3da2781733","symbol":"BTCBUSD","level":50,"msg":"Step - save-data-to-cache","time":"2022-12-31T10:04:39.986Z","v":0}
..."level":50,"msg":"a56c59fc-5559-4601-bdf8-af3da2781733 Job finished.","time":"2022-12-31T10:04:39.997Z","v":0}
..."level":50,"msg":"50c6465d-bfca-456e-8a5a-fdde57a4342a Job started.","time":"2022-12-31T10:04:40.915Z","v":0}
..."jobName":"trailingTrade","correlationId":"50c6465d-bfca-456e-8a5a-fdde57a4342a","symbol":"BTCBUSD","level":50,"msg":"Step - get-symbol-configuration","time":"2022-12-31T10:04:40.916Z","v":0}
..."jobName":"trailingTrade","correlationId":"50c6465d-bfca-456e-8a5a-fdde57a4342a","symbol":"BTCBUSD","level":50,"msg":"Step - get-symbol-info","time":"2022-12-31T10:04:40.921Z","v":0}
..."jobName":"trailingTrade","correlationId":"50c6465d-bfca-456e-8a5a-fdde57a4342a","symbol":"BTCBUSD","level":50,"msg":"Step - ensure-manual-order","time":"2022-12-31T10:04:40.925Z","v":0}
..."jobName":"trailingTrade","correlationId":"50c6465d-bfca-456e-8a5a-fdde57a4342a","symbol":"BTCBUSD","level":50,"msg":"Step - ensure-grid-trade-order-executed","time":"2022-12-31T10:04:40.932Z","v":0}
..."jobName":"trailingTrade","correlationId":"50c6465d-bfca-456e-8a5a-fdde57a4342a","symbol":"BTCBUSD","level":50,"msg":"Step - get-balances","time":"2022-12-31T10:04:40.938Z","v":0}
..."jobName":"trailingTrade","correlationId":"50c6465d-bfca-456e-8a5a-fdde57a4342a","symbol":"BTCBUSD","level":50,"msg":"Step - get-open-orders","time":"2022-12-31T10:04:40.941Z","v":0}
..."jobName":"trailingTrade","correlationId":"50c6465d-bfca-456e-8a5a-fdde57a4342a","symbol":"BTCBUSD","level":50,"msg":"Step - get-indicators","time":"2022-12-31T10:04:40.945Z","v":0}
..."jobName":"trailingTrade","correlationId":"50c6465d-bfca-456e-8a5a-fdde57a4342a","symbol":"BTCBUSD","level":50,"msg":"Step - get-override-action","time":"2022-12-31T10:04:40.952Z","v":0}
..."jobName":"trailingTrade","correlationId":"50c6465d-bfca-456e-8a5a-fdde57a4342a","symbol":"BTCBUSD","level":50,"msg":"Step - handle-open-orders","time":"2022-12-31T10:04:40.956Z","v":0}
..."jobName":"trailingTrade","correlationId":"50c6465d-bfca-456e-8a5a-fdde57a4342a","symbol":"BTCBUSD","level":50,"msg":"Step - get-balances","time":"2022-12-31T10:04:41.479Z","v":0}
..."jobName":"trailingTrade","correlationId":"50c6465d-bfca-456e-8a5a-fdde57a4342a","symbol":"BTCBUSD","level":50,"msg":"Step - determine-action","time":"2022-12-31T10:04:41.482Z","v":0}
..."jobName":"trailingTrade","correlationId":"50c6465d-bfca-456e-8a5a-fdde57a4342a","symbol":"BTCBUSD","level":50,"msg":"Step - place-manual-order","time":"2022-12-31T10:04:41.486Z","v":0}
..."jobName":"trailingTrade","correlationId":"50c6465d-bfca-456e-8a5a-fdde57a4342a","symbol":"BTCBUSD","level":50,"msg":"Step - cancel-order","time":"2022-12-31T10:04:41.489Z","v":0}
..."jobName":"trailingTrade","correlationId":"50c6465d-bfca-456e-8a5a-fdde57a4342a","symbol":"BTCBUSD","level":50,"msg":"Step - place-buy-order","time":"2022-12-31T10:04:41.493Z","v":0}
..."jobName":"trailingTrade","correlationId":"50c6465d-bfca-456e-8a5a-fdde57a4342a","symbol":"BTCBUSD","level":50,"msg":"Step - place-sell-order","time":"2022-12-31T10:04:42.226Z","v":0}
..."jobName":"trailingTrade","correlationId":"50c6465d-bfca-456e-8a5a-fdde57a4342a","symbol":"BTCBUSD","level":50,"msg":"Step - place-sell-stop-loss-order","time":"2022-12-31T10:04:42.231Z","v":0}
..."jobName":"trailingTrade","correlationId":"50c6465d-bfca-456e-8a5a-fdde57a4342a","symbol":"BTCBUSD","level":50,"msg":"Step - get-balances","time":"2022-12-31T10:04:42.234Z","v":0}
..."jobName":"trailingTrade","correlationId":"50c6465d-bfca-456e-8a5a-fdde57a4342a","symbol":"BTCBUSD","level":50,"msg":"Step - remove-last-buy-price","time":"2022-12-31T10:04:42.237Z","v":0}
..."jobName":"trailingTrade","correlationId":"50c6465d-bfca-456e-8a5a-fdde57a4342a","symbol":"BTCBUSD","level":50,"msg":"Step - save-data-to-cache","time":"2022-12-31T10:04:42.240Z","v":0}
..."level":50,"msg":"50c6465d-bfca-456e-8a5a-fdde57a4342a Job finished.","time":"2022-12-31T10:04:42.255Z","v":0}
..."level":50,"msg":"ed29884f-f4f1-46f5-ae8a-0b59e6c0b300 Job started.","time":"2022-12-31T10:04:42.256Z","v":0}
..."jobName":"trailingTrade","correlationId":"ed29884f-f4f1-46f5-ae8a-0b59e6c0b300","symbol":"BTCBUSD","level":50,"msg":"Step - get-symbol-configuration","time":"2022-12-31T10:04:42.258Z","v":0}
..."jobName":"trailingTrade","correlationId":"ed29884f-f4f1-46f5-ae8a-0b59e6c0b300","symbol":"BTCBUSD","level":50,"msg":"Step - get-symbol-info","time":"2022-12-31T10:04:42.262Z","v":0}
..."jobName":"trailingTrade","correlationId":"ed29884f-f4f1-46f5-ae8a-0b59e6c0b300","symbol":"BTCBUSD","level":50,"msg":"Step - ensure-manual-order","time":"2022-12-31T10:04:42.266Z","v":0}
..."jobName":"trailingTrade","correlationId":"ed29884f-f4f1-46f5-ae8a-0b59e6c0b300","symbol":"BTCBUSD","level":50,"msg":"Step - ensure-grid-trade-order-executed","time":"2022-12-31T10:04:42.270Z","v":0}
..."jobName":"trailingTrade","correlationId":"ed29884f-f4f1-46f5-ae8a-0b59e6c0b300","symbol":"BTCBUSD","level":50,"msg":"Step - get-balances","time":"2022-12-31T10:04:42.275Z","v":0}
..."jobName":"trailingTrade","correlationId":"ed29884f-f4f1-46f5-ae8a-0b59e6c0b300","symbol":"BTCBUSD","level":50,"msg":"Step - get-open-orders","time":"2022-12-31T10:04:42.278Z","v":0}
..."jobName":"trailingTrade","correlationId":"ed29884f-f4f1-46f5-ae8a-0b59e6c0b300","symbol":"BTCBUSD","level":50,"msg":"Step - get-indicators","time":"2022-12-31T10:04:42.282Z","v":0}
..."jobName":"trailingTrade","correlationId":"ed29884f-f4f1-46f5-ae8a-0b59e6c0b300","symbol":"BTCBUSD","level":50,"msg":"Step - get-override-action","time":"2022-12-31T10:04:42.289Z","v":0}
..."jobName":"trailingTrade","correlationId":"ed29884f-f4f1-46f5-ae8a-0b59e6c0b300","symbol":"BTCBUSD","level":50,"msg":"Step - handle-open-orders","time":"2022-12-31T10:04:42.293Z","v":0}
..."jobName":"trailingTrade","correlationId":"ed29884f-f4f1-46f5-ae8a-0b59e6c0b300","symbol":"BTCBUSD","level":50,"msg":"Step - get-balances","time":"2022-12-31T10:04:42.296Z","v":0}
..."jobName":"trailingTrade","correlationId":"ed29884f-f4f1-46f5-ae8a-0b59e6c0b300","symbol":"BTCBUSD","level":50,"msg":"Step - determine-action","time":"2022-12-31T10:04:42.300Z","v":0}
..."jobName":"trailingTrade","correlationId":"ed29884f-f4f1-46f5-ae8a-0b59e6c0b300","symbol":"BTCBUSD","level":50,"msg":"Step - place-manual-order","time":"2022-12-31T10:04:42.303Z","v":0}
..."jobName":"trailingTrade","correlationId":"ed29884f-f4f1-46f5-ae8a-0b59e6c0b300","symbol":"BTCBUSD","level":50,"msg":"Step - cancel-order","time":"2022-12-31T10:04:42.307Z","v":0}
..."jobName":"trailingTrade","correlationId":"ed29884f-f4f1-46f5-ae8a-0b59e6c0b300","symbol":"BTCBUSD","level":50,"msg":"Step - place-buy-order","time":"2022-12-31T10:04:42.310Z","v":0}
..."jobName":"trailingTrade","correlationId":"ed29884f-f4f1-46f5-ae8a-0b59e6c0b300","symbol":"BTCBUSD","level":50,"msg":"Step - place-sell-order","time":"2022-12-31T10:04:42.313Z","v":0}
..."jobName":"trailingTrade","correlationId":"ed29884f-f4f1-46f5-ae8a-0b59e6c0b300","symbol":"BTCBUSD","level":50,"msg":"Step - place-sell-stop-loss-order","time":"2022-12-31T10:04:42.319Z","v":0}
..."jobName":"trailingTrade","correlationId":"ed29884f-f4f1-46f5-ae8a-0b59e6c0b300","symbol":"BTCBUSD","level":50,"msg":"Step - get-balances","time":"2022-12-31T10:04:42.322Z","v":0}
..."jobName":"trailingTrade","correlationId":"ed29884f-f4f1-46f5-ae8a-0b59e6c0b300","symbol":"BTCBUSD","level":50,"msg":"Step - remove-last-buy-price","time":"2022-12-31T10:04:42.325Z","v":0}
..."jobName":"trailingTrade","correlationId":"ed29884f-f4f1-46f5-ae8a-0b59e6c0b300","symbol":"BTCBUSD","level":50,"msg":"Step - save-data-to-cache","time":"2022-12-31T10:04:42.336Z","v":0}
..."level":50,"msg":"ed29884f-f4f1-46f5-ae8a-0b59e6c0b300 Job finished.","time":"2022-12-31T10:04:42.346Z","v":0}

@uhliksk
Copy link
Contributor Author

uhliksk commented Dec 31, 2022

I think the problem is we are doing changes in mongodb while another job in queue is still running. This can cause some unexpected results.

For example if new buy order is still processing in queue when websocket received buy order fill it can make inconsistent state in mongodb removing order before the order has been processed yet in queue.

const deleteLastBuyPrice = async (logger, ws, symbol) => {
await mongo.deleteOne(logger, 'trailing-trade-symbols', {
key: `${symbol}-last-buy-price`
});
queue.executeFor(logger, symbol, {
correlationId: _.get(logger, 'fields.correlationId', '')
});

We should do all changes at once in queue or don't use queue at all. Or as a workaround we should not make any changes in mongodb for symbol while symbol is processed in queue. Any changes should be made after the current job in queue is finished to prevent intermediate changes in database state during the job in queue is still running. Or am I wrong?

@uhliksk
Copy link
Contributor Author

uhliksk commented Dec 31, 2022

@chrisleekr What do you think about it? Can changes in mongodb made by websocket while previous job in queue is still running make an inconsistent state which will leave buy order stuck in database if buy order is filled very shortly after new buy order is made?

@uhliksk
Copy link
Contributor Author

uhliksk commented Dec 31, 2022

Current websocket process is:

  1. Make changes to mongodb.
  2. Add job in queue.

I think we should implement those steps for websocket:

  1. Mark queue as paused.
  2. If any job is still running, wait until the job is finished.
  3. Make changes to mongodb.
  4. Add job in queue.
  5. Mark queue as resumed.

This will prevent any changes to mongodb while job is running like you can see in this log:

..."helper":"queue","level":50,"symbol":"BTCBUSD","msg":"Added job for BTCBUSD","time":"2022-12-31T13:00:16.490Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"9df0ab6d-a004-4ac3-b219-76fd7a699bfd","symbol":"BTCBUSD","level":50,"msg":"TrailingTrade: Start process...","time":"2022-12-31T13:00:16.510Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"9df0ab6d-a004-4ac3-b219-76fd7a699bfd","symbol":"BTCBUSD","level":50,"msg":"Step - get-symbol-configuration","time":"2022-12-31T13:00:16.513Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"9df0ab6d-a004-4ac3-b219-76fd7a699bfd","symbol":"BTCBUSD","level":50,"msg":"Step - get-symbol-info","time":"2022-12-31T13:00:16.520Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"9df0ab6d-a004-4ac3-b219-76fd7a699bfd","symbol":"BTCBUSD","level":50,"msg":"Step - ensure-manual-order","time":"2022-12-31T13:00:16.526Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"9df0ab6d-a004-4ac3-b219-76fd7a699bfd","symbol":"BTCBUSD","level":50,"msg":"Step - ensure-grid-trade-order-executed","time":"2022-12-31T13:00:16.533Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"9df0ab6d-a004-4ac3-b219-76fd7a699bfd","symbol":"BTCBUSD","level":50,"msg":"Step - get-balances","time":"2022-12-31T13:00:17.119Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"9df0ab6d-a004-4ac3-b219-76fd7a699bfd","symbol":"BTCBUSD","level":50,"msg":"Step - get-open-orders","time":"2022-12-31T13:00:17.124Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"9df0ab6d-a004-4ac3-b219-76fd7a699bfd","symbol":"BTCBUSD","level":50,"msg":"Step - get-indicators","time":"2022-12-31T13:00:17.131Z","v":0}
..."helper":"queue","level":50,"symbol":"BTCBUSD","msg":"Added job for BTCBUSD","time":"2022-12-31T13:00:17.139Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"9df0ab6d-a004-4ac3-b219-76fd7a699bfd","symbol":"BTCBUSD","level":50,"msg":"Step - get-override-action","time":"2022-12-31T13:00:17.159Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"9df0ab6d-a004-4ac3-b219-76fd7a699bfd","symbol":"BTCBUSD","level":50,"msg":"Step - handle-open-orders","time":"2022-12-31T13:00:17.166Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"9df0ab6d-a004-4ac3-b219-76fd7a699bfd","symbol":"BTCBUSD","level":50,"msg":"Step - get-balances","time":"2022-12-31T13:00:17.170Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"9df0ab6d-a004-4ac3-b219-76fd7a699bfd","symbol":"BTCBUSD","level":50,"msg":"Step - determine-action","time":"2022-12-31T13:00:17.175Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"9df0ab6d-a004-4ac3-b219-76fd7a699bfd","symbol":"BTCBUSD","level":50,"msg":"Step - place-manual-order","time":"2022-12-31T13:00:17.180Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"9df0ab6d-a004-4ac3-b219-76fd7a699bfd","symbol":"BTCBUSD","level":50,"msg":"Step - cancel-order","time":"2022-12-31T13:00:17.185Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"9df0ab6d-a004-4ac3-b219-76fd7a699bfd","symbol":"BTCBUSD","level":50,"msg":"Step - place-buy-order","time":"2022-12-31T13:00:17.190Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"9df0ab6d-a004-4ac3-b219-76fd7a699bfd","symbol":"BTCBUSD","level":50,"msg":"Step - place-sell-order","time":"2022-12-31T13:00:17.194Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"9df0ab6d-a004-4ac3-b219-76fd7a699bfd","symbol":"BTCBUSD","level":50,"msg":"Step - place-sell-stop-loss-order","time":"2022-12-31T13:00:17.199Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"9df0ab6d-a004-4ac3-b219-76fd7a699bfd","symbol":"BTCBUSD","level":50,"msg":"Step - get-balances","time":"2022-12-31T13:00:17.203Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"9df0ab6d-a004-4ac3-b219-76fd7a699bfd","symbol":"BTCBUSD","level":50,"msg":"Step - remove-last-buy-price","time":"2022-12-31T13:00:17.207Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"9df0ab6d-a004-4ac3-b219-76fd7a699bfd","symbol":"BTCBUSD","level":50,"msg":"Step - save-data-to-cache","time":"2022-12-31T13:00:17.212Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"9df0ab6d-a004-4ac3-b219-76fd7a699bfd","symbol":"BTCBUSD","level":50,"msg":"TrailingTrade: Finish process...","time":"2022-12-31T13:00:17.247Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"6b546ec2-eef1-49d7-b61e-7332a46e4bbb","symbol":"BTCBUSD","level":50,"msg":"TrailingTrade: Start process...","time":"2022-12-31T13:00:17.258Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"6b546ec2-eef1-49d7-b61e-7332a46e4bbb","symbol":"BTCBUSD","level":50,"msg":"Step - get-symbol-configuration","time":"2022-12-31T13:00:17.259Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"6b546ec2-eef1-49d7-b61e-7332a46e4bbb","symbol":"BTCBUSD","level":50,"msg":"Step - get-symbol-info","time":"2022-12-31T13:00:17.265Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"6b546ec2-eef1-49d7-b61e-7332a46e4bbb","symbol":"BTCBUSD","level":50,"msg":"Step - ensure-manual-order","time":"2022-12-31T13:00:17.272Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"6b546ec2-eef1-49d7-b61e-7332a46e4bbb","symbol":"BTCBUSD","level":50,"msg":"Step - ensure-grid-trade-order-executed","time":"2022-12-31T13:00:17.282Z","v":0}
..."correlationId":"da3f69b7-6fd3-415b-868a-548d35bcd08c","symbol":"BTCBUSD","helper":"queue","level":50,"msg":"Added job for BTCBUSD","time":"2022-12-31T13:00:17.622Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"6b546ec2-eef1-49d7-b61e-7332a46e4bbb","symbol":"BTCBUSD","level":50,"msg":"Step - get-balances","time":"2022-12-31T13:00:17.820Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"6b546ec2-eef1-49d7-b61e-7332a46e4bbb","symbol":"BTCBUSD","level":50,"msg":"Step - get-open-orders","time":"2022-12-31T13:00:17.824Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"6b546ec2-eef1-49d7-b61e-7332a46e4bbb","symbol":"BTCBUSD","level":50,"msg":"Step - get-indicators","time":"2022-12-31T13:00:17.829Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"6b546ec2-eef1-49d7-b61e-7332a46e4bbb","symbol":"BTCBUSD","level":50,"msg":"Step - get-override-action","time":"2022-12-31T13:00:17.842Z","v":0}
..."helper":"queue","level":50,"symbol":"BTCBUSD","msg":"Added job for BTCBUSD","time":"2022-12-31T13:00:17.844Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"6b546ec2-eef1-49d7-b61e-7332a46e4bbb","symbol":"BTCBUSD","level":50,"msg":"Step - handle-open-orders","time":"2022-12-31T13:00:17.849Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"6b546ec2-eef1-49d7-b61e-7332a46e4bbb","symbol":"BTCBUSD","level":50,"msg":"Step - get-balances","time":"2022-12-31T13:00:17.853Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"6b546ec2-eef1-49d7-b61e-7332a46e4bbb","symbol":"BTCBUSD","level":50,"msg":"Step - determine-action","time":"2022-12-31T13:00:17.857Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"6b546ec2-eef1-49d7-b61e-7332a46e4bbb","symbol":"BTCBUSD","level":50,"msg":"Step - place-manual-order","time":"2022-12-31T13:00:17.868Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"6b546ec2-eef1-49d7-b61e-7332a46e4bbb","symbol":"BTCBUSD","level":50,"msg":"Step - cancel-order","time":"2022-12-31T13:00:17.872Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"6b546ec2-eef1-49d7-b61e-7332a46e4bbb","symbol":"BTCBUSD","level":50,"msg":"Step - place-buy-order","time":"2022-12-31T13:00:17.876Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"6b546ec2-eef1-49d7-b61e-7332a46e4bbb","symbol":"BTCBUSD","level":50,"msg":"Step - place-sell-order","time":"2022-12-31T13:00:17.880Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"6b546ec2-eef1-49d7-b61e-7332a46e4bbb","symbol":"BTCBUSD","level":50,"msg":"Step - place-sell-stop-loss-order","time":"2022-12-31T13:00:17.884Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"6b546ec2-eef1-49d7-b61e-7332a46e4bbb","symbol":"BTCBUSD","level":50,"msg":"Step - get-balances","time":"2022-12-31T13:00:17.888Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"6b546ec2-eef1-49d7-b61e-7332a46e4bbb","symbol":"BTCBUSD","level":50,"msg":"Step - remove-last-buy-price","time":"2022-12-31T13:00:17.893Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"6b546ec2-eef1-49d7-b61e-7332a46e4bbb","symbol":"BTCBUSD","level":50,"msg":"Step - save-data-to-cache","time":"2022-12-31T13:00:17.898Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"6b546ec2-eef1-49d7-b61e-7332a46e4bbb","symbol":"BTCBUSD","level":50,"msg":"TrailingTrade: Finish process...","time":"2022-12-31T13:00:17.922Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"da3f69b7-6fd3-415b-868a-548d35bcd08c","symbol":"BTCBUSD","level":50,"msg":"TrailingTrade: Start process...","time":"2022-12-31T13:00:17.924Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"da3f69b7-6fd3-415b-868a-548d35bcd08c","symbol":"BTCBUSD","level":50,"msg":"Step - get-symbol-configuration","time":"2022-12-31T13:00:17.925Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"da3f69b7-6fd3-415b-868a-548d35bcd08c","symbol":"BTCBUSD","level":50,"msg":"Step - get-symbol-info","time":"2022-12-31T13:00:17.969Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"da3f69b7-6fd3-415b-868a-548d35bcd08c","symbol":"BTCBUSD","level":50,"msg":"Step - ensure-manual-order","time":"2022-12-31T13:00:17.976Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"da3f69b7-6fd3-415b-868a-548d35bcd08c","symbol":"BTCBUSD","level":50,"msg":"Step - ensure-grid-trade-order-executed","time":"2022-12-31T13:00:17.980Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"da3f69b7-6fd3-415b-868a-548d35bcd08c","symbol":"BTCBUSD","level":50,"msg":"Step - get-balances","time":"2022-12-31T13:00:17.986Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"da3f69b7-6fd3-415b-868a-548d35bcd08c","symbol":"BTCBUSD","level":50,"msg":"Step - get-open-orders","time":"2022-12-31T13:00:17.990Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"da3f69b7-6fd3-415b-868a-548d35bcd08c","symbol":"BTCBUSD","level":50,"msg":"Step - get-indicators","time":"2022-12-31T13:00:17.996Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"da3f69b7-6fd3-415b-868a-548d35bcd08c","symbol":"BTCBUSD","level":50,"msg":"Step - get-override-action","time":"2022-12-31T13:00:18.006Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"da3f69b7-6fd3-415b-868a-548d35bcd08c","symbol":"BTCBUSD","level":50,"msg":"Step - handle-open-orders","time":"2022-12-31T13:00:18.015Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"da3f69b7-6fd3-415b-868a-548d35bcd08c","symbol":"BTCBUSD","level":50,"msg":"Step - get-balances","time":"2022-12-31T13:00:18.020Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"da3f69b7-6fd3-415b-868a-548d35bcd08c","symbol":"BTCBUSD","level":50,"msg":"Step - determine-action","time":"2022-12-31T13:00:18.024Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"da3f69b7-6fd3-415b-868a-548d35bcd08c","symbol":"BTCBUSD","level":50,"msg":"Step - place-manual-order","time":"2022-12-31T13:00:18.032Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"da3f69b7-6fd3-415b-868a-548d35bcd08c","symbol":"BTCBUSD","level":50,"msg":"Step - cancel-order","time":"2022-12-31T13:00:18.036Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"da3f69b7-6fd3-415b-868a-548d35bcd08c","symbol":"BTCBUSD","level":50,"msg":"Step - place-buy-order","time":"2022-12-31T13:00:18.041Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"da3f69b7-6fd3-415b-868a-548d35bcd08c","symbol":"BTCBUSD","level":50,"msg":"Step - place-sell-order","time":"2022-12-31T13:00:18.045Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"da3f69b7-6fd3-415b-868a-548d35bcd08c","symbol":"BTCBUSD","level":50,"msg":"Step - place-sell-stop-loss-order","time":"2022-12-31T13:00:18.049Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"da3f69b7-6fd3-415b-868a-548d35bcd08c","symbol":"BTCBUSD","level":50,"msg":"Step - get-balances","time":"2022-12-31T13:00:18.053Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"da3f69b7-6fd3-415b-868a-548d35bcd08c","symbol":"BTCBUSD","level":50,"msg":"Step - remove-last-buy-price","time":"2022-12-31T13:00:18.057Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"da3f69b7-6fd3-415b-868a-548d35bcd08c","symbol":"BTCBUSD","level":50,"msg":"Step - save-data-to-cache","time":"2022-12-31T13:00:18.061Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"da3f69b7-6fd3-415b-868a-548d35bcd08c","symbol":"BTCBUSD","level":50,"msg":"TrailingTrade: Finish process...","time":"2022-12-31T13:00:18.074Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"7d47a04a-de0f-4326-8a74-be7cfaf21ec8","symbol":"BTCBUSD","level":50,"msg":"TrailingTrade: Start process...","time":"2022-12-31T13:00:18.217Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"7d47a04a-de0f-4326-8a74-be7cfaf21ec8","symbol":"BTCBUSD","level":50,"msg":"Step - get-symbol-configuration","time":"2022-12-31T13:00:18.218Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"7d47a04a-de0f-4326-8a74-be7cfaf21ec8","symbol":"BTCBUSD","level":50,"msg":"Step - get-symbol-info","time":"2022-12-31T13:00:18.224Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"7d47a04a-de0f-4326-8a74-be7cfaf21ec8","symbol":"BTCBUSD","level":50,"msg":"Step - ensure-manual-order","time":"2022-12-31T13:00:18.229Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"7d47a04a-de0f-4326-8a74-be7cfaf21ec8","symbol":"BTCBUSD","level":50,"msg":"Step - ensure-grid-trade-order-executed","time":"2022-12-31T13:00:18.234Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"7d47a04a-de0f-4326-8a74-be7cfaf21ec8","symbol":"BTCBUSD","level":50,"msg":"Step - get-balances","time":"2022-12-31T13:00:18.240Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"7d47a04a-de0f-4326-8a74-be7cfaf21ec8","symbol":"BTCBUSD","level":50,"msg":"Step - get-open-orders","time":"2022-12-31T13:00:18.244Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"7d47a04a-de0f-4326-8a74-be7cfaf21ec8","symbol":"BTCBUSD","level":50,"msg":"Step - get-indicators","time":"2022-12-31T13:00:18.248Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"7d47a04a-de0f-4326-8a74-be7cfaf21ec8","symbol":"BTCBUSD","level":50,"msg":"Step - get-override-action","time":"2022-12-31T13:00:18.256Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"7d47a04a-de0f-4326-8a74-be7cfaf21ec8","symbol":"BTCBUSD","level":50,"msg":"Step - handle-open-orders","time":"2022-12-31T13:00:18.261Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"7d47a04a-de0f-4326-8a74-be7cfaf21ec8","symbol":"BTCBUSD","level":50,"msg":"Step - get-balances","time":"2022-12-31T13:00:18.266Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"7d47a04a-de0f-4326-8a74-be7cfaf21ec8","symbol":"BTCBUSD","level":50,"msg":"Step - determine-action","time":"2022-12-31T13:00:18.270Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"7d47a04a-de0f-4326-8a74-be7cfaf21ec8","symbol":"BTCBUSD","level":50,"msg":"Step - place-manual-order","time":"2022-12-31T13:00:18.277Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"7d47a04a-de0f-4326-8a74-be7cfaf21ec8","symbol":"BTCBUSD","level":50,"msg":"Step - cancel-order","time":"2022-12-31T13:00:18.282Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"7d47a04a-de0f-4326-8a74-be7cfaf21ec8","symbol":"BTCBUSD","level":50,"msg":"Step - place-buy-order","time":"2022-12-31T13:00:18.286Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"7d47a04a-de0f-4326-8a74-be7cfaf21ec8","symbol":"BTCBUSD","level":50,"msg":"Step - place-sell-order","time":"2022-12-31T13:00:18.290Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"7d47a04a-de0f-4326-8a74-be7cfaf21ec8","symbol":"BTCBUSD","level":50,"msg":"Step - place-sell-stop-loss-order","time":"2022-12-31T13:00:18.294Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"7d47a04a-de0f-4326-8a74-be7cfaf21ec8","symbol":"BTCBUSD","level":50,"msg":"Step - get-balances","time":"2022-12-31T13:00:18.298Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"7d47a04a-de0f-4326-8a74-be7cfaf21ec8","symbol":"BTCBUSD","level":50,"msg":"Step - remove-last-buy-price","time":"2022-12-31T13:00:18.302Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"7d47a04a-de0f-4326-8a74-be7cfaf21ec8","symbol":"BTCBUSD","level":50,"msg":"Step - save-data-to-cache","time":"2022-12-31T13:00:18.306Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"7d47a04a-de0f-4326-8a74-be7cfaf21ec8","symbol":"BTCBUSD","level":50,"msg":"TrailingTrade: Finish process...","time":"2022-12-31T13:00:18.318Z","v":0}

@uhliksk
Copy link
Contributor Author

uhliksk commented Dec 31, 2022

For example:

const deleteLastBuyPrice = async (logger, ws, symbol) => {
  await mongo.deleteOne(logger, 'trailing-trade-symbols', {
    key: `${symbol}-last-buy-price`
  });


  queue.executeFor(logger, symbol, {
    correlationId: _.get(logger, 'fields.correlationId', '')
  });

Should be:

const deleteLastBuyPrice = async (logger, ws, symbol) => {
  await queue.pauseQueue(logger, symbol);
  await queue.waitForCurrentJobToFinish(logger, symbol);

  await mongo.deleteOne(logger, 'trailing-trade-symbols', {
    key: `${symbol}-last-buy-price`
  });

  queue.executeFor(logger, symbol, {
    correlationId: _.get(logger, 'fields.correlationId', '')
  });

  queue.resumeQueue(logger, symbol);

@uhliksk
Copy link
Contributor Author

uhliksk commented Dec 31, 2022

We also should limit number of jobs queued since there is nothing to do after actual state is processed. We should have only one actually running job and one queued job. All other jobs in queue are useless if there is no new change in data to process then.

image

@uhliksk
Copy link
Contributor Author

uhliksk commented Dec 31, 2022

I added pause, resume and waitForJob functions to queue.js to test if my idea will be working. I'll test over night.

const pause = async (funcLogger, symbol) => {
  const logger = funcLogger.child({ helper: 'queue' });

  await queues[symbol].pause();

  logger.info({ symbol }, `Queue ${symbol} paused`);
};

const resume = async (funcLogger, symbol) => {
  const logger = funcLogger.child({ helper: 'queue' });

  await queues[symbol].resume();

  logger.info({ symbol }, `Queue ${symbol} resumed`);
};

const waitForJob = async (funcLogger, symbol) => {
  const logger = funcLogger.child({ helper: 'queue' });

  while (await queue[symbol].getActiveCount() > 0) {
    await new Promise(resolve => setTimeout(resolve, 10));
  }

  logger.info({ symbol }, `Queue ${symbol} inactive`);
};

@uhliksk
Copy link
Contributor Author

uhliksk commented Dec 31, 2022

I changed the code little bit and now it's working as expected.


..."helper":"queue","jobName":"trailingTrade","correlationId":"13555c69-71a9-4e92-b548-a856529d26bc","symbol":"BTCBUSD","level":50,"msg":"TrailingTrade: Start process...","time":"2022-12-31T21:55:02.353Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"13555c69-71a9-4e92-b548-a856529d26bc","symbol":"BTCBUSD","level":50,"msg":"Step - get-symbol-configuration","time":"2022-12-31T21:55:02.354Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"13555c69-71a9-4e92-b548-a856529d26bc","symbol":"BTCBUSD","level":50,"msg":"Step - get-symbol-info","time":"2022-12-31T21:55:02.360Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"13555c69-71a9-4e92-b548-a856529d26bc","symbol":"BTCBUSD","level":50,"msg":"Step - ensure-manual-order","time":"2022-12-31T21:55:02.366Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"13555c69-71a9-4e92-b548-a856529d26bc","symbol":"BTCBUSD","level":50,"msg":"Step - ensure-grid-trade-order-executed","time":"2022-12-31T21:55:02.371Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"13555c69-71a9-4e92-b548-a856529d26bc","symbol":"BTCBUSD","level":50,"msg":"Step - get-balances","time":"2022-12-31T21:55:02.375Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"13555c69-71a9-4e92-b548-a856529d26bc","symbol":"BTCBUSD","level":50,"msg":"Step - get-open-orders","time":"2022-12-31T21:55:02.379Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"13555c69-71a9-4e92-b548-a856529d26bc","symbol":"BTCBUSD","level":50,"msg":"Step - get-indicators","time":"2022-12-31T21:55:02.383Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"13555c69-71a9-4e92-b548-a856529d26bc","symbol":"BTCBUSD","level":50,"msg":"Step - get-override-action","time":"2022-12-31T21:55:02.390Z","v":0}
..."correlationId":"f2847e24-3474-4fc1-8de7-8aeba92185cf","symbol":"BTCBUSD","helper":"queue","level":50,"msg":"Queue BTCBUSD paused","time":"2022-12-31T21:55:02.391Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"13555c69-71a9-4e92-b548-a856529d26bc","symbol":"BTCBUSD","level":50,"msg":"Step - handle-open-orders","time":"2022-12-31T21:55:02.395Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"13555c69-71a9-4e92-b548-a856529d26bc","symbol":"BTCBUSD","level":50,"msg":"Step - get-balances","time":"2022-12-31T21:55:02.399Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"13555c69-71a9-4e92-b548-a856529d26bc","symbol":"BTCBUSD","level":50,"msg":"Step - determine-action","time":"2022-12-31T21:55:02.403Z","v":0}
..."correlationId":"f2847e24-3474-4fc1-8de7-8aeba92185cf","symbol":"BTCBUSD","helper":"queue","level":50,"msg":"Queue BTCBUSD active","time":"2022-12-31T21:55:02.404Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"13555c69-71a9-4e92-b548-a856529d26bc","symbol":"BTCBUSD","level":50,"msg":"Step - place-manual-order","time":"2022-12-31T21:55:02.409Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"13555c69-71a9-4e92-b548-a856529d26bc","symbol":"BTCBUSD","level":50,"msg":"Step - cancel-order","time":"2022-12-31T21:55:02.414Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"13555c69-71a9-4e92-b548-a856529d26bc","symbol":"BTCBUSD","level":50,"msg":"Step - place-buy-order","time":"2022-12-31T21:55:02.418Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"13555c69-71a9-4e92-b548-a856529d26bc","symbol":"BTCBUSD","level":50,"msg":"Step - place-sell-order","time":"2022-12-31T21:55:02.421Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"13555c69-71a9-4e92-b548-a856529d26bc","symbol":"BTCBUSD","level":50,"msg":"Step - place-sell-stop-loss-order","time":"2022-12-31T21:55:02.425Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"13555c69-71a9-4e92-b548-a856529d26bc","symbol":"BTCBUSD","level":50,"msg":"Step - get-balances","time":"2022-12-31T21:55:02.428Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"13555c69-71a9-4e92-b548-a856529d26bc","symbol":"BTCBUSD","level":50,"msg":"Step - remove-last-buy-price","time":"2022-12-31T21:55:02.432Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"13555c69-71a9-4e92-b548-a856529d26bc","symbol":"BTCBUSD","level":50,"msg":"Step - save-data-to-cache","time":"2022-12-31T21:55:02.435Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"13555c69-71a9-4e92-b548-a856529d26bc","symbol":"BTCBUSD","level":50,"msg":"TrailingTrade: Finish process...","time":"2022-12-31T21:55:02.445Z","v":0}
..."helper":"queue","level":50,"symbol":"BTCBUSD","msg":"Queue BTCBUSD inactive","time":"2022-12-31T21:55:02.451Z","v":0}
..."correlationId":"f2847e24-3474-4fc1-8de7-8aeba92185cf","symbol":"BTCBUSD","helper":"queue","level":50,"msg":"Queue BTCBUSD inactive","time":"2022-12-31T21:55:02.451Z","v":0}
..."correlationId":"f2847e24-3474-4fc1-8de7-8aeba92185cf","symbol":"BTCBUSD","helper":"queue","level":50,"msg":"Added job for BTCBUSD","time":"2022-12-31T21:55:02.452Z","v":0}
..."correlationId":"f2847e24-3474-4fc1-8de7-8aeba92185cf","symbol":"BTCBUSD","helper":"queue","level":50,"msg":"Queue BTCBUSD resumed","time":"2022-12-31T21:55:02.452Z","v":0}
..."helper":"queue","jobName":"trailingTrade","correlationId":"c6dd6639-c741-4b45-b468-85a0ec2d3bad","symbol":"BTCBUSD","level":50,"msg":"TrailingTrade: Start process...","time":"2022-12-31T21:55:02.454Z","v":0}

@uhliksk
Copy link
Contributor Author

uhliksk commented Jan 1, 2023

It looks finally stable. No locks or missing buy/sell orders since queue pause/resume implemented and duplicated queued jobs removed. I'll prepare PR today. Happy new year 2023. 🥳

@chrisleekr
Copy link
Owner

@uhliksk Sorry for not getting back to you sooner. I couldn't make time to be in front of the computer.

If you push the PR, I will take a look.

@uhliksk
Copy link
Contributor Author

uhliksk commented Jan 1, 2023

While debugging I found there is also error when Stop price would trigger immediately. which is causing trailing trade is not finished correctly. See 5th line of this log. Then there is no finish process log as it crashed. I can include fix into this PR also.

{..."helper":"queue","jobName":"trailingTrade","correlationId":"e0703def-1b15-432c-bcd2-274aeb5990f6","symbol":"BTCBUSD","level":50,"msg":"▶ TrailingTrade: Start process...","time":"2023-01-01T19:21:51.346Z","v":0}
{..."helper":"queue","jobName":"trailingTrade","correlationId":"e0703def-1b15-432c-bcd2-274aeb5990f6","symbol":"BTCBUSD","level":50,"msg":"TrailingTrade: Finish process...","time":"2023-01-01T19:21:51.396Z","v":0}
{..."correlationId":"4a9aea3d-37d3-482b-8537-88b142cb4639","symbol":"BTCBUSD","helper":"queue","level":50,"msg":"Added job for BTCBUSD","time":"2023-01-01T19:21:52.352Z","v":0}
{..."helper":"queue","jobName":"trailingTrade","correlationId":"4a9aea3d-37d3-482b-8537-88b142cb4639","symbol":"BTCBUSD","level":50,"msg":"▶ TrailingTrade: Start process...","time":"2023-01-01T19:21:52.354Z","v":0}
{..."helper":"queue","jobName":"trailingTrade","correlationId":"4a9aea3d-37d3-482b-8537-88b142cb4639","symbol":"BTCBUSD","level":50,"err":{"message":"Stop price would trigger immediately.","name":"Error","stack":"Error: Stop price would trigger immediately.\n    at /srv/node_modules/binance-api-node/dist/http-client.js:100:17\n    at runMicrotasks (<anonymous>)\n    at processTicksAndRejections (internal/process/task_queues.js:95:5)\n    at async execute (/srv/dist/server.js:1:50586)\n    at async /srv/dist/server.js:1:21389\n    at async errorHandlerWrapper (/srv/dist/server.js:1:102373)\n    at async execute (/srv/dist/server.js:1:20060)\n    at async Queue.<anonymous> (/srv/dist/server.js:1:88417)","code":-2010},"errorCode":-2010,"debug":true,"saveLog":true,"msg":"⚠ Execution failed.","time":"2023-01-01T19:21:54.412Z","v":0}
{..."correlationId":"310983ec-8e80-4ae8-978a-5b1395d62fbb","symbol":"BTCBUSD","helper":"queue","level":50,"msg":"Added job for BTCBUSD","time":"2023-01-01T19:21:54.421Z","v":0}
{..."correlationId":"62a0eaa9-c48e-4e1a-9b35-5b616535fc17","symbol":"BTCBUSD","helper":"queue","level":50,"msg":"Already waiting 1 job(s) in BTCBUSD","time":"2023-01-01T19:21:54.425Z","v":0}
{..."correlationId":"716b4da5-4c9b-4df0-b33f-a003221ffc7a","symbol":"BTCBUSD","helper":"queue","level":50,"msg":"Already waiting 1 job(s) in BTCBUSD","time":"2023-01-01T19:21:54.435Z","v":0}
{..."helper":"queue","jobName":"trailingTrade","correlationId":"310983ec-8e80-4ae8-978a-5b1395d62fbb","symbol":"BTCBUSD","level":50,"msg":"▶ TrailingTrade: Start process...","time":"2023-01-01T19:21:54.438Z","v":0}
{..."helper":"queue","jobName":"trailingTrade","correlationId":"310983ec-8e80-4ae8-978a-5b1395d62fbb","symbol":"BTCBUSD","level":50,"msg":"TrailingTrade: Finish process...","time":"2023-01-01T19:21:56.365Z","v":0}

@chrisleekr chrisleekr linked a pull request Jan 2, 2023 that will close this issue
@uhliksk
Copy link
Contributor Author

uhliksk commented Jan 3, 2023

There is another glitch when buy order is rapidly filled by multiple partial buys. The order of processing is as expected so I think there is another problem, but I have to research. It looks like fill order has been processed faster than new order so we should apply the hold much sooner than what I applied in fix to prevent out of order processing. If that's true then holdAndExecute() will not be applicable.

Edit: It looks like holdAndExecute() is fine where hold() is currently applied. We just need to add hold() and resume() to parts where database is updated, like in place-buy-order.js to not be overtaken.

image

image

image

@uhliksk
Copy link
Contributor Author

uhliksk commented Jan 3, 2023

@chrisleekr This whole block should be inside hold() and resume() pattern? Maybe holdAndResume() function can be created and put that block inside. Same for sale or other long running operations.

logger.info(
{
function: 'order',
orderParams,
calculationParams,
saveLog: true
},
`The grid trade #${humanisedGridTradeIndex} buy order will be placed.`
);
const orderResult = await binance.client.order(orderParams);
logger.info(
{ orderResult, saveLog: true },
`The grid trade #${humanisedGridTradeIndex} buy order has been placed.`
);
// Set last buy grid order to be checked until it is executed
await saveGridTradeOrder(logger, `${symbol}-grid-trade-last-buy-order`, {
...orderResult,
currentGridTradeIndex
});
// Save number of open orders
await saveOrderStats(logger, symbols);
// FIXME: If you change this comment, please refactor to use common.js:refreshOpenOrdersAndAccountInfo
// Get open orders and update cache
data.openOrders = await getAndCacheOpenOrdersForSymbol(logger, symbol);
data.buy.openOrders = data.openOrders.filter(
o => o.side.toLowerCase() === 'buy'
);
// Refresh account info
data.accountInfo = await getAccountInfoFromAPI(logger);
if (notifyDebug || notifyOrderConfirm)
slack.sendMessage(
`*${symbol}* Buy Action Grid Trade #${humanisedGridTradeIndex} Result: *STOP_LOSS_LIMIT*\n` +
`- Order Result: \`\`\`${JSON.stringify(
orderResult,
undefined,
2
)}\`\`\``,
{ symbol, apiLimit: getAPILimit(logger) }
);
return setMessage(
logger,
data,
`Placed new stop loss limit order for buying of grid trade #${humanisedGridTradeIndex}.`
);
)

@uhliksk
Copy link
Contributor Author

uhliksk commented Jan 3, 2023

@chrisleekr Should I create another issue for ^^^ or will we solve everything at once in #562?

@habibalkhabbaz
Copy link
Contributor

Hello @uhliksk

Kindly check my proposed solution in your PR #562
It may cover this issue.

@uhliksk
Copy link
Contributor Author

uhliksk commented Jan 3, 2023

Hello @habibalkhabbaz

thank you, it looks very promising. It would certainly cover all types of interference we are dealing with when they will be processed in queue.

@uhliksk
Copy link
Contributor Author

uhliksk commented Jan 3, 2023

@habibalkhabbaz

Sorry, I was wrong. I misunderstood the log because event is logged in bot before it is actually processed. In screenshot of docker log you can see event *652 has been processed after trailing trade has finished so my fix is working as expected. Maybe mongodb itself is processing writes out of order? Should we require acknowledgement of mongodb writes?

@uhliksk
Copy link
Contributor Author

uhliksk commented Jan 3, 2023

There is a new update is when event is received from Binance and The last order has been update is when it was actually processed which show queue is working well, but I'm confused why there is 3f3758380bf3 as The last order has been updated only. And also 3a106d8b45dc and some others are missing completely. It looks like even logging is missing writes into database. There is clearly something wrong with mongodb when too many writes are done in short period of time. This is not a queue issue anymore after I applied pause/resume solution.

image

@chrisleekr
Copy link
Owner

@uhliksk

This whole block should be inside hold() and resume() pattern? Maybe holdAndResume() function can be created and put that block inside. Same for sale or other long running operations.

Interesting. So those block should trigger hold/resume? Then should put inside of the holdAndResume() except the return part.

Should I create another issue for ^^^ or will we solve everything at once in #562?

Update #562 to apply the changes.

Should we require acknowledgement of mongodb writes?

Two scenarios.

  1. Like order related CRUD should be acked MongoDB writes.
  2. Like log related CRUD can be ignored acked.

There is clearly something wrong with mongodb when too many writes are done in short period of time. This is not a queue issue anymore after I applied pause/resume solution.

Yeah, I think you are right. Within 1 second, there were too many writes.
I think we should think about separating the log database out of MongoDB. It's too much pressure for MongoDB.

@uhliksk
Copy link
Contributor Author

uhliksk commented Jan 5, 2023

@chrisleekr

Interesting. So those block should trigger hold/resume? Then should put inside of the holdAndResume() except the return part.

Sorry, I misinterpretted log. This is not an issue. Please ignore. This block is already in queue so there is no need to hold() and resume().

@uhliksk
Copy link
Contributor Author

uhliksk commented Jan 5, 2023

Two scenarios.

  1. Like order related CRUD should be acked MongoDB writes.
  2. Like log related CRUD can be ignored acked.

I agree. Order related CRUD is important and should be acked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants