Python Day Trading Algorithm on AWS

Python Day Trading on AWS

Ever wanted to become a stock trader? Its been something we’ve wanted to master for years. We’ve tried everything from trading futures, arbitrage, forex, crypto, you name it we’ve tried it. Out of all these different markets the toughest to play is day trading. Picture yourself sitting down in front of a large number of screens with charts and indicators, analysing trends to pick a buy and sell, limits, stop losses, arghhh! It sends your brain crazy. The biggest challenge recently was spent focusing on trading futures with algorithms. Did you know 60% of the day trading market now is run by algorithms? That means as humans, our brains are competing against computers to analyse stock data.

Who do you think will win?

Computers think and calculate much faster meaning when programmed correctly, computers are a better trader. One of the biggest advantages, NO HUMAN EMOTION. This is the biggest money loser in all stock trading.

The Emotion Effect on Day Trading

Why does emotion play such a big role in day trading? No matter the level of experience, even if you are making millions and not losing a penny, there is emotion tied to any buy or sell. If we lose money, you feel like S**T, if you make money you walk away telling yourself “See I got this, this is easy”, and we feel all good inside. Now try and entire trading where those emotions repeat back and forth as you make trades. You may make 7 trades in a row that make you money, then 1 trade to wipe all the profits out again. This is where emotion gets in the way. If it was a computer, it would be 7 profitable trades, and only 1 losing trade that wipes the profit from 1 winning trade.

WHY? Because a computer goes purely on rules and enforces them, as a human, we tend to break these rules when we are losing money, causing us to lose much bigger than any profitable trade. This also applies when a trade is positive, when we see green, we take it and run, when we could make a lot more by holding the trade longer. No matter how long you have been trading, there will always be times where you break your own trading rules because of human emotion. Even the best traders are guilty of it.

tp://flusharcade.com.au/wp-content/uploads/gbpjpy_bearfish.gif”> How does your stomach feel watching your money go up and down?

[/caption]What is Algorithm Trading?

Algorithm trading is software designed to automate a trading strategy. In the example we are about to show is how you can built a day trading algorithm using python, market data and AWS to host the back end.

Finding a Datasource

This was difficult to find at first. Since we’ve done all the hard work trialing different API sources, we’ve chose Alpha Vantage as the best and cheapest intraday data. At first we began using a free API key but there is a limit “up to 5 API requests per minute and 500 requests per day” . This wasn’t enough, because we currently run 2 day trading algorithms, one for stocks to buy, and another for stocks to short, we need almost 3312 requests per hour, meaning a total of 26496 per day. That means we only need a premium key for a total of $99 per month for 300 requests per minute. Thats super cheap for that load of requests to pull the latest intraday stock data.

Testing the Datasource

This is something we must confirm at the beginning. We had a little trouble before the algorithms were running consistently. At first we used data from TD Ameritrade and iextrading, but the data feeds were not intraday or they were too high cost for the amount of requests required. It wasn’t until the algorithms were running for 8 straight hours with 3312 requests for a few weeks did we determine the consistency of the data.

Step 1: Coding the Algorithm

arcade.com.au/wp-content/uploads/Screen-Shot-2018-11-29-at-11.27.45.png”> Visual Studio Code is perfect for Python development.

[/caption]When it comes to Python development, we would recommend using Visual Studio Code. They offer great extensions for Python syntax, compiling, debugging and you can set the Python version to Python 2 or Python 3.

Note

We recommend using the latest version of Python. If you aren’t familiar with Python3, there have been quite a few syntax and library changes, i.e. urlparse -> urllib.parse. We can also run the python application on AWS’s Elastic Beanstalk which can support up to Python 3.7.

Coding Indicators

When it comes to the actual programming behind the algorithm, we have to program indicators from the original candle stick data taken from Alpha Vantage. We also have the option to call the indicators from the their API, but this would increase our API requests by 5-6 times as we require a number of indicators to line up to direct a buy/sell order. We recommend you code the indicators yourself – have a look at FMLabs for the maths behind each indicator, i.e. here is the maths behind calculating an EMA.

Containerising our Python Applications using Docker

Since we want this project to be portable, we have the option to deploy the python app inside a container, host the applications on Docker Hub and using a simple JSON config file for AWS Elastic beanstalk, it has the ability to retrieve the docker container directly from Docker Hub and run a containerised application publicly all through the JSON config file. Here is what this file looks like:



{
  "AWSEBDockerrunVersion": "1",
  "Image": {
    "Name": "flusharcade/{app-name}",
    "Update": "true"
  },
  "Ports": [
    {
      "ContainerPort": "5000"
    }
  ],
  "Logging": "/var/log/nginx"
}


Looking more closely above, the ports we specify are opened publicly. The first part of the JSON file specifies the image name and location specified according to Docker Hub. With a public Python application we must open port 5000. We also specify the logging destination for the application – “/var/log/nginx”. This is a common ubuntu location for logging purposes.

Deploying a Container to AWS Elastic Beanstalk

This is the easy part, lets get the Python application running live. Jump into Elastic beanstalk through the AWS console and select a new Web Server environment as we are going to be calling this app as an API from AWS Lambda.

/Screen-Shot-2018-11-29-at-12.54.34.png”> We must select a Web Server environment as we are going to be calling this app as an API from AWS Lambda.

[/caption]On the next screen, we must select Docker as the platform and in the Upload your code section, we simply upload our JSON config file generated earlier.

een-Shot-2018-11-29-at-12.54.53.png”> It’s as easy as setting the platform to Docker and uploading our config file generated earlier.

[/caption]Done and Done, give it a few minutes and the application will be running live.

Using Lambda to Generate Stock actions

In this project we use AWS Lambda to run a timed event to check 138 stocks on the US stock exchange. The Lambda function is written in Python. It simply makes a request to our Elastic Beanstalk application with a JSON payload for stock code and action (buy/sell). Our Elastic Beanstalk application with take the stock code, do the analysis using the indicators, and return a boolean whether to buy/sell according to the action specified.



phase1 = '{elatic beanstalk url}'
stocks = [{stocks to check}]


def lambda_handler(event, context):
    longStocks = ''

    for stock in stocks:
        url = phase1

        longReply = requests.post(url, data={'stock': stock, 'interval': '5min', 'action': 'buy'}, headers={})
        longResponse = longReply.text.encode()
        isBuy = str(longResponse.decode())
          
        # check stock is a buy and add to long stocks string
        if isBuy == 'True':
            longStocks = '{}{}, '.format(longStocks, stock)
        
    response = ses.send_email(
        Source = '{source email address}',
        Destination={
            'ToAddresses': [
		'{email addresses to receive stocks}',
            ],
            'CcAddresses': [
            ]
        },
        Message={
            'Subject': {
                'Data': 'Stocks to BUY'
            },
            'Body': {
                'Text': {
                    'Data': longStocks
                }
            }
        }
    )

Let’s look at the code above. The function lambda_handler is used in all Lambda functions using Python, this is what is called when our Lambda executes. All we are doing is taking the array of stocks, looping through each one, and checking via our Python application if the stock currently presents a buy setup.

Using SES to Send Emails

If you have never used SES its a reliable service for sending emails from a source email address. We use SES through the AWS SDK to send an email with a list of stocks to buy/sell at the end of each Lambda execution. Before we can send an email through SES, we must first verify both source and send emails through SES

All email addresses must be verified to be used with SES

To verify an email address, we simply select the Verify a New Email Address button and add in the required email address to verify.

Enter the email address to verify.

Once we select Verify New Email Address an email will be sent to that address. The user must click the verify link in the email. SES can now use this email address as a source or send email address for our Lambda Function.

Using Cloud Watch to Trigger Timed Events for Lambda

Now for the final piece of the puzzle with Cloud Watch. With any day trading algorithm, as the stock data changes, we must run a buy/sell check at certain intervals. This occurs throughout the entire trading day. In this example, we are using Cloud Watch to trigger a lambda execution every 5 mins throughout the entire market day. In order to do this, jump into Cloud Watch through the AWS Console and select Rules from the left panel.

The rules section is where we created timed triggers for Lambda functions.

Here we specify a Cron Expression to specify date / time / interval.

For more information on Cron Expressions please see this AWS Documentation. Its best to try a dummy Lambda function which uses SES to send an email. This will ensure the end to end flow works correctly.

Congratulations, we are now running a day trading algorithm on AWS. Here is the final architecture:

Posted in Learn