psemdel / py-trading-bot

Trading-bot in python using django, vertorbt lib and interactive-brokers
MIT License
141 stars 34 forks source link

bot not found #19

Closed ben1628 closed 1 year ago

ben1628 commented 1 year ago

Here is my start_bot.sh

cd bot

 redis-server & 
 #python3 manage.py runserver & 
 sleep 20
 #xdg-open http://localhost:8000/start_bot
 celery -A trading_bot worker -l info
 && fg

That gives me this output

(.vbt) usr@MainPC:~/py-trading-bot/py-trading-bot$ code .
(.vbt) usr@MainPC:~/py-trading-bot/py-trading-bot$ . start_bot.sh
-bash: cd: bot: No such file or directory
626:C 15 Feb 2023 14:12:28.138 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
626:C 15 Feb 2023 14:12:28.138 # Redis version=6.0.16, bits=64, commit=00000000, modified=0, pid=626, just started
626:C 15 Feb 2023 14:12:28.138 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
626:M 15 Feb 2023 14:12:28.138 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 6.0.16 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 626
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

626:M 15 Feb 2023 14:12:28.140 # Server initialized
626:M 15 Feb 2023 14:12:28.140 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
626:M 15 Feb 2023 14:12:28.140 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo madvise > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled (set to 'madvise' or 'never').
626:M 15 Feb 2023 14:12:28.141 * Ready to accept connections
Telegram else

 -------------- celery@MainPC v5.2.7 (dawn-chorus)
--- ***** -----
-- ******* ---- Linux-5.15.79.1-microsoft-standard-WSL2-x86_64-with-glibc2.35 2023-02-15 14:12:50
- *** --- * ---
- ** ---------- [config]
- ** ---------- .> app:         trading_bot:0x7f40bb0ed540
- ** ---------- .> transport:   redis://localhost:6379/0
- ** ---------- .> results:     disabled://
- *** --- * --- .> concurrency: 8 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** -----
 -------------- [queues]
                .> trading-bot-worker exchange=trading-bot-worker(direct) key=trading-bot-worker

[tasks]
  . reporting.telegram.async_sched
  . trading_bot.celery.debug_task

[2023-02-15 14:12:52,571: INFO/MainProcess] Connected to redis://localhost:6379/0
[2023-02-15 14:12:52,576: INFO/MainProcess] mingle: searching for neighbors
[2023-02-15 14:12:53,585: INFO/MainProcess] mingle: all alone
[2023-02-15 14:12:53,619: WARNING/MainProcess] /home/usr/.vbt/lib/python3.10/site-packages/celery/fixups/django.py:203: UserWarning: Using settings.DEBUG leads to a memory
            leak, never use this setting in production environments!
  warnings.warn('''Using settings.DEBUG leads to a memory

[2023-02-15 14:12:53,620: INFO/MainProcess] celery@MainPC ready.
[2023-02-15 14:17:04,332: INFO/MainProcess] Task reporting.telegram.async_sched[780291ab-aad0-4180-a246-35d51247b5ce] received
[2023-02-15 14:17:04,334: WARNING/ForkPoolWorker-7] Telegram
[2023-02-15 14:17:05,066: INFO/ForkPoolWorker-7] Application started

then I started vscode and do run server in debug mode

I then access the 127.0.0.1/8000 clicke start_bot and got the following error

[2023-02-15 14:17:05,780: ERROR/ForkPoolWorker-7] Task reporting.telegram.async_sched[780291ab-aad0-4180-a246-35d51247b5ce] raised unexpected: BadRequest('Chat not found')
Traceback (most recent call last):
  File "/home/usr/py-trading-bot/py-trading-bot/reporting/telegram_sub.py", line 785, in start
    raise exc
  File "/home/usr/py-trading-bot/py-trading-bot/reporting/telegram_sub.py", line 778, in start
    self.loop.run_until_complete(self.post_start_callback())
  File "/usr/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete
    return future.result()
  File "/home/usr/py-trading-bot/py-trading-bot/reporting/telegram_sub.py", line 654, in post_start_callback
    await self.send_message_to_all("I'm back to life!")
  File "/home/usr/py-trading-bot/py-trading-bot/reporting/telegram_sub.py", line 632, in send_message_to_all
    await self.send_to_all("message", text, *args, log_msg=log_msg, **kwargs)
  File "/home/usr/py-trading-bot/py-trading-bot/reporting/telegram_sub.py", line 622, in send_to_all
    await self.send(kind, chat_id, *args, **kwargs)
  File "/home/usr/py-trading-bot/py-trading-bot/reporting/telegram_sub.py", line 604, in send
    await getattr(self.application.bot, "send_" + kind)(chat_id, *args, **kwargs)
  File "/home/usr/.vbt/lib/python3.10/site-packages/telegram/ext/_extbot.py", line 2598, in send_message
    return await super().send_message(
  File "/home/usr/.vbt/lib/python3.10/site-packages/telegram/_bot.py", line 331, in decorator
    result = await func(*args, **kwargs)  # skipcq: PYL-E1102
  File "/home/usr/.vbt/lib/python3.10/site-packages/telegram/_bot.py", line 760, in send_message
    return await self._send_message(
  File "/home/usr/.vbt/lib/python3.10/site-packages/telegram/ext/_extbot.py", line 488, in _send_message
    result = await super()._send_message(
  File "/home/usr/.vbt/lib/python3.10/site-packages/telegram/_bot.py", line 512, in _send_message
    result = await self._post(
  File "/home/usr/.vbt/lib/python3.10/site-packages/telegram/_bot.py", line 419, in _post
    return await self._do_post(
  File "/home/usr/.vbt/lib/python3.10/site-packages/telegram/ext/_extbot.py", line 306, in _do_post
    return await super()._do_post(
  File "/home/usr/.vbt/lib/python3.10/site-packages/telegram/_bot.py", line 450, in _do_post
    return await request.post(
  File "/home/usr/.vbt/lib/python3.10/site-packages/telegram/request/_baserequest.py", line 165, in post
    result = await self._request_wrapper(
  File "/home/usr/.vbt/lib/python3.10/site-packages/telegram/request/_baserequest.py", line 328, in _request_wrapper
    raise BadRequest(message)
telegram.error.BadRequest: Chat not found

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/usr/.vbt/lib/python3.10/site-packages/celery/app/trace.py", line 451, in trace_task
    R = retval = fun(*args, **kwargs)
  File "/home/usr/.vbt/lib/python3.10/site-packages/celery/app/trace.py", line 734, in __protected_call__
    return self.run(*args, **kwargs)
  File "/home/usr/py-trading-bot/py-trading-bot/reporting/telegram.py", line 63, in async_sched
    bot.start(in_background=True)
  File "/home/usr/py-trading-bot/py-trading-bot/reporting/telegram_sub.py", line 788, in start
    self.stop(close_loop=close_loop)
  File "/home/usr/py-trading-bot/py-trading-bot/reporting/telegram_sub.py", line 795, in stop
    self.loop.run_until_complete(self.pre_stop_callback())
  File "/usr/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete
    return future.result()
  File "/home/usr/py-trading-bot/py-trading-bot/reporting/telegram_sub.py", line 658, in pre_stop_callback
    await self.send_message_to_all("Bye!")
  File "/home/usr/py-trading-bot/py-trading-bot/reporting/telegram_sub.py", line 632, in send_message_to_all
    await self.send_to_all("message", text, *args, log_msg=log_msg, **kwargs)
  File "/home/usr/py-trading-bot/py-trading-bot/reporting/telegram_sub.py", line 622, in send_to_all
    await self.send(kind, chat_id, *args, **kwargs)
  File "/home/usr/py-trading-bot/py-trading-bot/reporting/telegram_sub.py", line 604, in send
    await getattr(self.application.bot, "send_" + kind)(chat_id, *args, **kwargs)
  File "/home/usr/.vbt/lib/python3.10/site-packages/telegram/ext/_extbot.py", line 2598, in send_message
    return await super().send_message(
  File "/home/usr/.vbt/lib/python3.10/site-packages/telegram/_bot.py", line 331, in decorator
    result = await func(*args, **kwargs)  # skipcq: PYL-E1102
  File "/home/usr/.vbt/lib/python3.10/site-packages/telegram/_bot.py", line 760, in send_message
    return await self._send_message(
  File "/home/usr/.vbt/lib/python3.10/site-packages/telegram/ext/_extbot.py", line 488, in _send_message
    result = await super()._send_message(
  File "/home/usr/.vbt/lib/python3.10/site-packages/telegram/_bot.py", line 512, in _send_message
    result = await self._post(
  File "/home/usr/.vbt/lib/python3.10/site-packages/telegram/_bot.py", line 419, in _post
    return await self._do_post(
  File "/home/usr/.vbt/lib/python3.10/site-packages/telegram/ext/_extbot.py", line 306, in _do_post
    return await super()._do_post(
  File "/home/usr/.vbt/lib/python3.10/site-packages/telegram/_bot.py", line 450, in _do_post
    return await request.post(
  File "/home/usr/.vbt/lib/python3.10/site-packages/telegram/request/_baserequest.py", line 165, in post
    result = await self._request_wrapper(
  File "/home/usr/.vbt/lib/python3.10/site-packages/telegram/request/_baserequest.py", line 328, in _request_wrapper
    raise BadRequest(message)
telegram.error.BadRequest: Chat not found
^A^C
worker: Hitting Ctrl+C again will terminate all running tasks!

worker: Warm shutdown (MainProcess)
-bash: start_bot.sh: line 9: syntax error near unexpected token `&&'
-bash: start_bot.sh: line 9: ` && fg'
(.vbt) usr@MainPC:~/py-trading

What did I do wrong? Any ideas? may be you can share your output so I can find out.

Thanks

psemdel commented 1 year ago

DId you start your bot with /start ?

ben1628 commented 1 year ago

Oh, I did that yesterday and it telegram was able to send me message when I do some test.

I just did a /start again, and it seems to be working

bot started, task id 39ec2d6d-fdcc-4b3e-8672-b2bacdaca25f
bot_start ok

How often do I have to do /start ?

I guess the next step is for me to start using IB

ben1628 commented 1 year ago

is the bot supposed to send a message? like heartbeat?

psemdel commented 1 year ago

How often do I have to do /start ? Normally, once is enough, but it proved to be the solution when "nothing happens" in 99% of the cases.

Yes, to test you can set heartbeat to true, and it will send messages. Otherwise, it should at least send you a message "I am back online" when starting. Moreover when you make /start or anything from telegram, it should be also displayed in your shell.