How to Create Stock Alert System using Python and Windows Task Scheduler

Amir Nejad
QuantJam
6 min readMay 2, 2020

--

Photo by Malvestida Magazine on Unsplash

There are a plethora of stock alert systems available for free or for a nominal price. However, I found most of these alert system too simplistic (e.g. daily price movement, magnitude, etc.). Maybe I was not lucky enough to find better alert system so I decided to write an alert system on my own using Python and Windows Task Scheduler. The idea is to create a sophisticated alert system that triggers every day at market close (or open depending on your applications), finds stocks that fit your criteria, and send an email including stock symbols.

Code Structure

  • Trigger code at specific times of the day using Windows Task Scheduler (this will be explained later) [Windows]
  • Import watch list of stocks of interest [Python]
  • Download historical stock data of all symbols in the watch list [Python]
  • Find which stock fit the alert criteria [Python]
  • Send email alert containing the stock symbols discovered in the previous step [Python]

Python Libraries

There are two main libraries needed to accomplish this task:

  • pandas_datareader: This library enables downloading time series of stock data from different sources.
  • smtplib: This is a SMTP protocol client for sending emails. This library is needed to send daily email alerts.
  • Other standard libraries include: pandas, numpy, and datetime.

Alert Discovery Code:

Let’s write the python code that everyday (could be every week or month) downloads historical data and alerts us if any stock symbol fits our criteria. First, we need to import our watch list stored locally in a csv file:

Next, using pandas_datareader library, time series of stock data can be downloaded and alerts determined. For the purpose of this study, I am going to set up an alert if the value of close of the day is below 100 day moving average. AlertFinder function contains the alert function. This function can be easily changed to fit your own needs. The following block shows AlertFinder function:

After finding stock symbols that fit our criteria, the next task would be setting up an email system so we can get notified on when stock alerts are triggered. Sending emails via Python is very easy using smtplib library. First, we need to declare email credentials in the code (you can also encrypt your username and password instead of explicitly declaring those values. for more information you can refer to here). Then smtplib can login into your account and send emails:

I primarily use Gmail as my main emailing account. In order to use a gmail account, we have make sure Gmail allows less secure apps to interact with your account. In order to do this you need to:

  1. Sign in to Gmail
  2. Click here to access Less Secure App Access in My Account.
  3. Next, to “Allow less secure apps: OFF,” select the toggle switch to turn ON.

Setting up Windows Task Scheduler

The final part of this tutorial is on how to set up a windows task scheduler to repeatedly run every day. We need to write a bat file containing the path of the Python location and our code location. Simply create a text file on your windows machine, then rename text file to run.bat. Then, open the bat file using notepad and write following commands in the bat file:

The first line of the code activates the Anaconda environment so we can use pre-installed Python libraries. The second line of the code simply runs Python and main.py which contains the alert code.

The last thing we need to do is simply add a new task in windows task scheduler to run the bat file daily using the following process. First, search “Windows Task Scheduler” from the windows search dialog box on the lower left part of your windows machine, and click on Windows Task Scheduler. Then, follow each step from the list and screenshots below:

  1. Create a basic Task from the upper right side of the window
  2. Specify the name of this task. Press next.
  3. Specify the frequency that this task should run. In this scenario I have selected daily frequency. Press next.
  4. Specify the starting date and hour of the task. In this case I have specified the task to run every day at 3:35 pm from the beginning of Janaury 10, 2020. Press next.
  5. Select Running a Program in the next window and press next.
  6. In the following window, click on browse and select the bat file that we created in the previous section. Press next.
  7. Finally click on the “open the properties” check box and press finish.
  8. In the setting windows of the task scheduler we would like to ensure this task runs even if the computer has shut down. In the conditions window of the task, make sure the “Wake the computer to run this task” is checked.
  9. In the settings window, makes sure “Stop the task if it runs longer than” is set to one hour or less. This will ensure that if there is a problem with the task, it gets restarted before the start of the next day.
1- Select a Basic Task
2- Create a basic task
3- Frequency of the task
4- Select start date and time
5- Select an action type
6- Select run.bat file and press next
7- Finish the basic task
8- Select the created task setting: make sure to wake up computer for running this task ( if computer is off)
9- Stop task after 1 hour in case if code runs into a problem

That’s it, guys. You now know how to create alert systems using Python and Windows to notify you what happens in the market daily. Obviously the code that I have shown you is simple and can be improved significantly (e.g. weekly report on performance of stocks, sophisticated stock selection criteriaetc.). However, the possibilities are endless and you can create your own very sophisticated alert system.

Stay tuned and follow us to get notified on the future episodes.

Codes:

All codes are published on the Stock Alchemy GitHub [Link]. For more finance and Python related articles visit http://stockalchemy.com/.

Thanks for reading! My name is Amir Nejad,PhD. I’m a data scientist and editor of QuantJam , and I love share my ideas and to collaborate with other fellow data scientists. You can connect with me on Github, Twitter, and LinkedIn.

QuantJam:

You can see my other writings at:

About QuantJam

Quantjam is a medium publishing company aims at sharing reproducible research focused on finance and stock market data.

Follow us to stay up-to-date on all articles and activities: https://medium.com/quantjam

Disclaimer

QuantJam writings do not include any investment advice. Past performance is no guarantee of future results. Please consult with your financial advisor before making any investment decision. Investing involves risk and you may incur a profit or loss regardless of strategy selected, including diversification and asset allocation. Investments mentioned may not be suitable for all investors.

All images have been produced by the author, except where stated otherwise.

--

--

Amir Nejad
QuantJam

PhD. Engineer | Data Scientist | Problem Solver | Solution Oriented (twitter: @Dr_Nejad)