Step-by-step guide to running a simple trading algorithm in the cloud using PythonAnywhere

samcha
Automation Generation
3 min readFeb 14, 2019

Using PythonAnywhere When Running an Algorithm Live in the Cloud

After first getting into quant trading and writing algos on your local machine, the biggest area of resistance for newbie quant traders is running an algorithm live in the cloud.

There are many cloud services on the market, including AWS, Google Cloud, Heroku, Digital Ocean, and PythonAnywhere. This walkthrough focuses on setting up PythonAnywhere and running a sample algorithm in Python and the Alpaca Trade API.

PythonAnywhere has a free tier of accounts, but only with access to whitelisted URLs. To access the Alpaca endpoints, you will need a paid account ($5/month).

PythonAnywhere is a nice option because it gives you some manner of GUI for accessing consoles, files, notebooks, and even scheduling tasks that automatically restart if they go down (excellent for algo trading).

Let’s jump in with a fresh account.

Step 1.) Setting up PythonAnywhere.

Step A.) Make a new PythonAnywhere account.

https://www.pythonanywhere.com/

You’re welcomed with a dashboard like below.

Step B.) Install alpaca-trade-api.

Under “Consoles”, open a new Bash console.

Run the following:

pip3 install --user alpaca-trade-api

Step 2.) Set up algo.

Our sample algo for this walkthrough is a simple 5/20 period ema (exponential moving average) crossover on the 5-minute timeframe, checking every 1 minute for signals.

Upload the algo as a file, or go to “Open another file” on the dashboard.

Enter it as /home/yourusername/algoname.py

You can run this right from the terminal editor, and see if it works. If you’re missing any dependencies, install them in the Bash console.

If the algo runs, you can leave it up and check results in the log file.

Step 3.) Set as task.

Go to “Tasks”, and go down to “Always On Tasks”.

You can schedule your algo to automatically restart if it crashes by entering “python3.6 /home/yourusername/algoname.py”.

This works especially well with our sample algo, which dynamically finds the amount of time to sleep until next market open.

Checking our Alpaca account, using the below as an example, we can see it’s generating signals about every minute:

Please note that this example is for illustrative purposes only. Past performance is not indicative of future results.

Technology and services are offered by AlpacaDB, Inc. Brokerage services are provided by Alpaca Securities LLC (alpaca.markets), member FINRA/SIPC. Alpaca Securities LLC is a wholly-owned subsidiary of AlpacaDB, Inc.

You can find us @AlpacaHQ, if you use twitter.

Follow Automation Generation, a Medium’s publication created for developers/makers in trading and fintech.

--

--

samcha
Automation Generation

Python, trading, data viz. Get access to samchaaa++ for ready-to-implement algorithms and quantitative studies: https://samchaaa.substack.com/