As an Amazon Associate I earn from qualifying purchases.
Last week we talked about how we can send messages through slack and by email. Today, let’s see how we can use this to our benefit and see how to monitor P/L for your algorithmic trading bot by sending stats directly to our computer or phone via slack or email.
For this post I will be using Slack to send my notifications using the method send_notification
.
Creating the stats schedule
In trader.py
start by creating a new method and name it send_stats
with no arguments:
def send_stats():
For your stats, you will want to know information about the open trades. Use the method postions_get()
created in this post and assign it to a new variable named open_positions
:
def send_stats():
open_positions = positions_get()
If there are no open positions, you can do one of two things (depending on your preference). Either, send no message or send a message to say that there are no open positions. This is totally up to you which path you wish to take. For me, I decided to post the message “No trades are currently open.”
Add a condition to check if the open_positions
data frame is empty or not and depending on your preference add a return
statement or send a message.
def send_stats():
open_positions = positions_get()
if(open_positions.empty):
send_notification("No trades are currently open.")
So what happens if you do have open positions? You will remember that our open positions data frame will look like the following:
ticket time type magic identifier reason volume price_open sl tp price_current swap profit symbol comment
0 548297723 2020-03-18 15:00:55 1 0 548297723 3 0.01 1.09301 1.11490 1.06236 1.10104 -0.10 -8.03 EURUSD
1 548655158 2020-03-18 20:31:26 0 0 548655158 3 0.01 1.08676 1.06107 1.12446 1.10099 -0.08 14.23 EURUSD
2 548663803 2020-03-18 20:40:04 0 0 548663803 3 0.01 1.08640 1.06351 1.11833 1.10099 -0.08 14.59 EURUSD
3 548847168 2020-03-19 01:10:05 0 0 548847168 3 0.01 1.09545 1.05524 1.15122 1.10099 -0.06 5.54 EURUSD
4 548847194 2020-03-19 01:10:07 0 0 548847194 3 0.02 1.09536 1.04478 1.16587 1.10099 -0.08 11.26 EURUSD
For the notification, you will need to extract the time
, symbol
and proft
of each trade. Start by creating the else condition (for when the data frame is not empty) and reassign open_positions
to a subset of the data frame:
else:
open_positions = open_positions[['time', 'symbol', 'profit']]
The last thing you need to do is convert the data frame to text by using the built-in to_markdown()
function in pandas.DataFrame
. More information can be found here about to_markdown(). After converting to markdown, you can send the message using send_notification
:
else:
open_positions = open_positions[open_positions['time', 'symbol', 'profit']]
send_notification(open_positions.to_markdown())
Creating the schedule
Now that you have your stats alert setup, you will want to call this on an interval using the schedule (Similar to what we used Creating an algotrader/trading bot with Python – Part 1). I recommend scheduling this stats notification every 4 hours but again, it is totally down to personal preference.
Navigate to the live_trading
method in trader.py
and find the following code:
def live_trading(strategy):
schedule.every().hour.at(":00").do(run_trader, mt5.TIMEFRAME_M15, strategy)
schedule.every().hour.at(":15").do(run_trader, mt5.TIMEFRAME_M15, strategy)
schedule.every().hour.at(":30").do(run_trader, mt5.TIMEFRAME_M15, strategy)
schedule.every().hour.at(":45").do(run_trader, mt5.TIMEFRAME_M15, strategy)
After the last line, add the following code:
schedule.every(4).hours.at(":00").do(send_stats)
This is telling us to run the send_stats
method every 4 hours at the top of the hour.
Testing the code
To test this functionality, I will be opening 3 trades manually and waiting 4 hours to see if I get a stats notification on slack


As you can see after 3 hours I have the real time information about 3 of my open trades. Now you can monitor P/L for your algorithmic trading bot!
If you are interested in learning more about algo trading and trading systems, I highly recommend reading this book. I have taken some of my own trading ideas and strategies from this book. It also provided me a great insight into effective back testing. Check it out here.
That’s all for now! Check back on Wednesday to see how we can extend this functionality giving you daily reports and bot health checks which is important for the reliablity of your bot. As always, if you have any questions or comments please feel free to post them below. Additionally, if you run into any issues please let me know.