Send SMS alerts from your trading algorithms in Python

Occasionally there may be some important alerts that you want your trading algorithms to send to you that require immediate attention. One that I use frequently is daily account drawdown. Here’s how you can set up Twilio to send you a text message when your account enters a certain percentage of drawdown.

Head over to Twilio

You do need to purchase a number to text from. It costs $1/month. You can find that here.

You’ll now need to go to your general settings page to retrieve your live credentials.

The next step is to install Twilio on your operating system. I use Ubuntu this can be done with the following command

sudo pip3 install twilio

Great now you’re ready to code.

The first thing you want to do is write a simple Python script to test your message sending capabilities. In the below code you’ll want to change a few variables:

account_sid – Change with your Twilio information found on your account page.

Auth_token – Change with your Twilio information found on your account page.

Using the format +19998887777 change to_num to your cell

Using the format +19998887777 change from_num to your Twilio phone number

def send_sms(to_num,from_num,body_message):
    import os
    from twilio.rest import Client

    account_sid = 'Enter your account sid here'
    auth_token = 'Enter your account token here'
    client = Client(account_sid, auth_token)

    try:
        message = client.messages.create(
            to=to_num,
            from_=from_num,
            body=body_message
        )

        print(message.sid)
    except Exception as e:
        print(e)

if __name__ == '__main__':
    send_sms(+14809999999,+17149999999,'test')

Now that we have a function that can send text messages we just need to incorporate this into our code.

Setting up Python algos to run on Ubuntu startup using systemd

If you’re running your trading algorithms on Linux the best way to get them to start on bootup is to use a service called systems. Here are the steps on setting that up.

Go to:

/etc/systemd/system


Then create a new service as root:



sudo nano trading_algo_23.service


Here is a sample of what the file should look like. You can find your account name by using the command whoami in linux



[Unit]
Description=This algo is designed to hedge...



User=your username
WorkingDirectory=/directory/of/algo
ExecStart=/usr/bin/python3  /directory/of/algo/algo_23.py
Restart=always



[Install]
WantedBy=multi-user.target

Now reload systems

sudo systemctl daemon-reload


Enable the service

sudo systemctl enable

Start your script:

sudo systemctl start trading_algo_23.service


Check the status:

sudo systemctl status trading_algo_23.service

Setting up Python algos to run on Ubuntu startup using crontab

If you’re using Ubuntu or some other flavor of Linux you more than likely want your algorithm to start with the server. This is a pretty straightforward process.

The first thing you’ll want to do is make sure cron is installed

sudo apt-get update
sudo apt-get install cron

Next run crontab -e select nano when it asks which editor. The code below can be used as a template The first two lines are just writing the current date to the log files on reboot so you know when the server was restarted.

@reboot date >> /home/me_jeremywhittaker_com/AAII/log.txt
@reboot date >> /home/me_jeremywhittaker_com/AAII/errors.txt

    

I actually am running two scripts for this particular strategy. Each one gets its own line of code. This basically tells it to run on on startup @reboot then the path to the script to execute,

@reboot /home/me_jeremywhittaker_com/queen_of_stonks/queen_of_stonks_monitor.py >> /home/me_jeremywhittaker_com/queen_of_stonks/logs.txt 2>> /home/me_jeremywhittaker_com/queen_of_stonks/errors.txt 

@reboot /home/me_jeremywhittaker_com/queen_of_stonks/queen_of_stonks_trade.py >> /home/me_jeremywhittaker_com/queen_of_stonks/logs.txt 2>> /home/me_jeremywhittaker_com/queen_of_stonks/errors.txt

That’s it. We can now do a sudo reboot and our scripts should start with our server and save to the log files indicated.

Earn 7.12% on money sitting in your checking or savings account and hedge against inflation, risk-free, here’s how.

If you’re like me you probably have money sitting in your checking, savings, or money-market account. Paying practically zero interest and getting eaten alive by inflation. Here’s what you can do to earn 7.12% on that money and get inflation-adjusted returns on your excess cash.

Yield on savings and checking accounts are horrible

If you’re like me you’re probably trying to move your liquid funds around into these high-yield savings accounts which aren’t exactly keeping up with inflation. In fact, to date, the highest I can find is LendingClub at 0.60%

Inflation is a regressive tax on the poor and middle class

The annual inflation rate in the US has surged to 6.2% in October to 2021 according to the US Bureau of Labor and Statistics. This means your buying power is dropping significantly if you are holding on to cash. You of course know this if you have bought anything recently.

Use I bonds for risk-free inflation-adjusted return on your cash

To get the 7.12% yield you can buy I bonds which are backed by the United States government. They are inflation-adjusted. Meaning, you’re not technically building any wealth but you’re also not allowing the government to steal it through inflation. You can read more about how the rate is calculated here.

  • I bonds are an excellent investment rather than holding cash
  • They are exempt from state and local taxes
  • Subject to federal taxes.
  • Tax-deferred until you redeem them.
  • Mature in 30-years or until you redeem them
  • Redeemable after the first year of purchase
  • Early redemption is penalized with 3-months worth of interest
  • Interest is compounded every 6-months
  • Your money is locked up for a duration of 1-year

Historical I bond rates

Period when you bought
your I bond
Composite rate for your six-month earning period starting during November 2021 – April 2022
(See “When does my bond change rates?”)
FromThrough
Nov. 2021Apr. 20227.12%
May 2021Oct. 20217.12%
Nov. 2020Apr. 20217.12%
May 2020Oct. 20207.12%
Nov. 2019Apr. 20207.33%
May 2019Oct. 20197.64%
Nov. 2018Apr. 20197.64%
May 2018Oct. 20187.43%
Nov. 2017Apr. 20187.22%
May 2017Oct. 20177.12%
Nov. 2016Apr. 20177.12%
May 2016Oct. 20167.22%
Nov. 2015Apr. 20167.22%
May 2015Oct. 20157.12%
Nov. 2014Apr. 20157.12%
May 2014Oct. 20147.22%
Nov. 2013Apr. 20147.33%
May 2013Oct. 20137.12%
Nov. 2012Apr. 20137.12%
May 2012Oct. 20127.12%
Nov. 2011Apr. 20127.12%
May 2011Oct. 20117.12%
Nov. 2010Apr. 20117.12%
May 2010Oct. 20107.33%
Nov. 2009Apr. 20107.43%
May 2009Oct. 20097.22%
Nov. 2008Apr. 20097.84%
May 2008Oct. 20087.12%
Nov. 2007Apr. 20088.36%
May 2007Oct. 20078.47%
Nov. 2006Apr. 20078.57%
May 2006Oct. 20068.57%
Nov. 2005Apr. 20068.16%
May 2005Oct. 20058.36%
Nov. 2004Apr. 20058.16%
May 2004Oct. 20048.16%
Nov. 2003Apr. 20048.26%
May 2003Oct. 20038.26%
Nov. 2002Apr. 20038.78%
May 2002Oct. 20029.19%
Nov. 2001Apr. 20029.19%
May 2001Oct. 200110.23%
Nov. 2000Apr. 200110.64%
May 2000Oct. 200010.85%
Nov. 1999Apr. 200010.64%
May 1999Oct. 199910.54%
Nov. 1998Apr. 199910.54%
Sept. 1998Oct. 199810.64%

Here’s how you can buy I bonds

The first step is to sign up for a TreasuryDirect account which can be done here.

Once your account is set up you can log in here. You should have received your account number via email confirmation.

Once logged in you’ll see this screen.

Click on the BuyDirect option at the top. You’ll want to select series I bonds.

The most you can buy per social security number is $10,000/year

That’s it. Click submit.

And finally your confirmation page. Pretty simple and straightforward process.

Have kids?

If you have kids you can gift $10,000/year to them as well in I bonds. Read more on gifting I bonds here.

Want to purchase $5,000 more in I bonds every year?

You are also allowed to buy an additional $5,000 worth of I bonds every year with your tax return. That is of course if you have a tax return. You can do this by overpaying your taxes intentionally to the IRS via their website, here. I wouldn’t overpay by exactly $5,000 but probably an amount over. You can read more of the details on that process here. They are going to send you paper I bonds so you will want to convert them and attach them to your online account for ease of management. You buy I bonds with your tax return by submitting tax form 8888 with your taxes

Converting your I bonds to treasury direct electronic format

Directions on this process can be found on the treasury direct website, here.

You will need to create a conversion account one time.

You then need to create a registration list that matches the paper bond ownership that is printed on the paper bonds.