psemdel / py-trading-bot

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

General Workflow #5

Closed ben1628 closed 1 year ago

ben1628 commented 1 year ago

I appreciate if more details are given to the following features. Like how someone can go about doing that using the Python Trading Bot

psemdel commented 1 year ago

Sorry, that it not clear enough. It is clear to me that this code is still in alpha version. There is still a lot to do in the code and in the documentation.

1) Performing live automatically orders using the tested strategies and interactive brokers, thanks to ib_insync

Strategies need to be defined in core/strat.py I use aggregates from classical strategies (MA+RSI let's say) or use preselection of actions, which make the whole less readable. If you only need let's say RSI, you can simplify the code or use directly the indicators. Result of all those strategies is "entries, exits, entries_short and exits_short". Maybe you can tell me what you want to implement and I can try to give an example. Then in reporting/models daily_report() will run those simple strategies. At line 332-335, you can put the strategy you want (st.strategy_you_want()). It will calculate the entry/exit for it and all actions in a stock exchange. To apply this strategy on an action, you should make this action eligible. Go in admin/ strat candidates and then normal. Select the action you want to trade with this strategy. It will then perform orders entry/exit on this action.

If you want the order to be performed automatically in IB, you need to put several settings correctly in settings.py: PERFORM_ORDER=True DIC_PERFORM_ORDER={ "normal":True, }

self.define_ent_ex() will go to entry_order() which will then execute it.

2) Send Telegram messages when performing an order

Normally a telegram message should be sent by any order (manual, so without IB, or with IB). entry_order() return True or False. It will append the symbol to self.ent_symbols. In reporting/telegram.py there is the function send_order() which will then read self.ent_symbols and send the message accordingly. It would have been easier to put a "send_msg" in the entry_order function, but the issue is that the Telegram bot is included in the Scheduler (to produce alert) and is therefore asynchronous from the main python code of the bot.

3) Send Telegram alerts when the action price variation exceeds a certain threshold The telegram function and the Scheduler are closely related and put in reporting/telegram.py. When starting the bot, different jobs are started. The alerting one is performed every TIME_INTERVAL_CHECK minutes as defined in settings.py. From there, it will check the actions in the "PF"s and owned in IB. It basically check the price variation in the day. And send an alert if the threshold ALERT_THRESHOLD is exceeded. It proved to work many times. If you are more interested in checking a variation from your buying price, the function check_sl is done for that. However, this part is insufficiently tested as I never use stop loss.

4) Writing at regular interval reports on the market, using Django and Celery with Redis

It is the same the telegram function and the scheduler are together in reporting/telegram.py. The time of report writing is set at row 70 and 71. Then at those time, self.daily_report_17h and self.daily_report_22h are triggered. It should be equivalent to clicking on "Write report 17h" and "Write report 22h" on the home page.

ben1628 commented 1 year ago

Please don't apologize.

I'm grateful that you are sharing the code here, no matter the state of this code, it's very amazing that you make this works. I always want to do something similar but wasn't able to do it. The concept is great.

This is especially true with VectorBT, it is a great tool to do backtesting, and now you bring this to another level and actually implement the strategies in trading.

I will continue exploring...