How to Schedule Your Python Scripts with Cron Jobs (with Examples)

Cron jobs have been the most important and most used functions on Python. It’s definitely a function that I could not live without.

A cron job is a task scheduled by script or command to run automatically at certain intervals. Think of it as a schedule of when to run your programs. If you want to read more about cron jobs, this is a great beginner’s guide to cron jobs.

How I Leverage Cron for Trading?

Here are the primary purposes for cron jobs when it comes to my trading program:

  • Update all my datasets: I store a lot of data for machine learning and stock analysis. It can range from news data, price data, financial data, SEC filings and much more. All these updates are stored in different functions and I have a midnight cron job that updates all these dataset
  • Update my portfolio: I have custom built reporting that reports on my portfolio at the end of the day. I have functions built to pull my portfolio results end of day to update all my reporting.
  • Email alerts: I have custom notification functions that store all the alerts that need to go out and send it at the beginning of every hour. This helps keep my email clean and not be spammed by own stock and news alerts.

How to Implement Cron Jobs for Your Project?

If you’re using python, it’s extremely easy to implement cron jobs. All you need is the python library apscheduler.

Advanced Python Scheduler (APScheduler) is a Python library that lets you schedule your Python code to be executed later, either just once or periodically. You can add new jobs or remove old ones on the fly as you please. If you store your jobs in a database, they will also survive scheduler restarts and maintain their state. When the scheduler is restarted, it will then run all the jobs it should have run while it was offline 

To install the library:

pip install apscheduler

Here’s a snippet of my code currently to run my functions.

  • update_price function is scheduled to run between mon-fri every 15 minutes
  • update_eod function is scheduled to run between tues-sat every 3 hours
  • balance_cash function is scheduled to run between mon-fri at 9:31 am
from apscheduler.schedulers.blocking import BlockingScheduler

scheduler = BlockingScheduler()

""" TRADEBOT CRON JOBS """
scheduler.add_job(Tradebot.update_price, 'cron', day_of_week='mon-fri', minute='*/15')
scheduler.add_job(Tradebot.update_eod, 'cron', day_of_week='tue-sat', hour=3)
scheduler.add_job(Tradebot.balance_cash, 'cron', day_of_week='mon-fri', hour=9, minute=31)
scheduler.start()

For more details, read the documentation here.

Running Cron Jobs in the Background

If you don’t want the cron to run within the python terminal and run on the background, you can leverage windows task scheduler. Task Scheduler is a component of Microsoft Windows that provides the ability to schedule the launch of programs or scripts at pre-defined times or after specified time intervals: job scheduling (task scheduling).

Here’s a video I would recommend to watch to get started.

Related