Code Monkey home page Code Monkey logo

algorithmic-trading's Introduction

Algorithmic Trading Workshop

In this workshop, participants will learn how to load and store financial data on AWS from AWS Data Exchange and other external data sources and how to build and backtest algorithmic trading strategies with Amazon SageMaker that use technical indicators and advanced machine learning models.

chart

Time Commitment Expectation: This workshop was created to be completed in approximately 1h 30 min.

Regions

This workshop has been tested in us-east-1.

Considerations for Each Role

As the team lead on this lean team of one, you'll need to wear multiple hats. Below are some things we'll cover from the perspective of each role:

  • Data Engineer - You'll modify scripts to load external market data to AWS.
  • Data Scientist - You'll need to load the data into your machine learning development environment. Once loaded, you'll understand the data, use a machine learning algorithm to train the model and do predictions.
  • Trader - You will use different trading strategies based on data to maximize Profit & Loss while attributing to Risk.

Goals

At minimum, at the end of this workshop, you will have an understanding how to load historical price data from external market data sources like AWS Data Exchange into S3. You get familiar how to store price data in S3 and expose it via Glue Data Catalog and Athena, how to backtested trading strategies using Amazon SageMaker, and how to train machine learning models that are used in trading strategies. You also get a basic understand how trading strategies using trend following and machine learning are developed with Python and can be optimized for better performance.

Architecture

chart

License

This library is licensed under the MIT-0 License. See the LICENSE file.

External Dependencies

This package depends on and may retrieve a number of third-party software packages (such as open source packages) from third-party servers at install-time or build-time ("External Dependencies"). The External Dependencies are subject to license terms that you must accept in order to use this package. If you do not accept all of the applicable license terms, you should not use this package. We recommend that you consult your company’s open source approval policy before proceeding.

Provided below is a list of the External Dependencies and the applicable license terms as indicated by the documentation associated with the External Dependencies as of Amazon's most recent review of such documentation. THIS INFORMATION IS PROVIDED FOR CONVENIENCE ONLY. AMAZON DOES NOT PROMISE THAT THE LIST OR THE APPLICABLE TERMS AND CONDITIONS ARE COMPLETE, ACCURATE, OR UP-TO-DATE, AND AMAZON WILL HAVE NO LIABILITY FOR ANY INACCURACIES. YOU SHOULD CONSULT THE DOWNLOAD SITES FOR THE EXTERNAL DEPENDENCIES FOR THE MOST COMPLETE AND UP-TO-DATE LICENSING INFORMATION. YOUR USE OF THE EXTERNAL DEPENDENCIES IS AT YOUR SOLE RISK. IN NO EVENT WILL AMAZON BE LIABLE FOR ANY DAMAGES, INCLUDING WITHOUT LIMITATION ANY DIRECT, INDIRECT, CONSEQUENTIAL, SPECIAL, INCIDENTAL, OR PUNITIVE DAMAGES (INCLUDING FOR ANY LOSS OF GOODWILL, BUSINESS INTERRUPTION, LOST PROFITS OR DATA, OR COMPUTER FAILURE OR MALFUNCTION) ARISING FROM OR RELATING TO THE EXTERNAL DEPENDENCIES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, EVEN IF AMAZON HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS AND DISCLAIMERS APPLY EXCEPT TO THE EXTENT PROHIBITED BY APPLICABLE LAW.

** backtrader; version 1.9.74.123 -- https://www.backtrader.com/

References


Instructions using SageMaker Studio

A newer version of this workshop has been developed for SageMaker Studio and is available in folder 5_SageMakerStudio.

  1. Setup SageMaker Studio with sufficient permissions.
  2. Run Jupyter Notebook: 1_Setup.ipynb: This will setup S3 bucket, Glue Data Catalog Schema, Athena Workgroup. For SageMaker Studio, a custom kernel is built and attached.
  3. Run Jupyter Notebook: 2_Load_Data.ipynb: This will load daily stock price data into the S3 bucket.
  4. Run Jupyter Notebook: 3_Backtest_Strategy.ipynb: Backtest strategy in SageMaker Studio and find optimal parameters.

Instructions using SageMaker Notebooks

Step 0: Set up environment

  1. Create a new unique S3 bucket that starts with "algotrading-" (e.g. "algotrading-YYYY-MM-DD-XYZ") that we use for storing external price data.
  2. For the base infrastructure components (SageMaker Notebook, Athena, Glue Tables), deploy the following CloudFormation template. Go to CloudFormation and upload the downloaded CF template. For the S3 bucket specify the previously created bucket name. Verify that stackName is algotrading before creating the stack and acknowledge that IAM changes will be made.

This step will take ca. 5 minutes.

Step 1: Load Historical Price Data

Here are a few data source options for this workshop. The daily datasets can be downloaded and generated in a few minutes, for the intraday dataset, please plan for at least 15 mins.

  1. Sample Daily EOD Stock Price Data (from public data source or AWS Data Exchange)

Option 1a: Sample Daily EOD Stock Price Data (from public data source)

If you are not able to use AWS Data Exchange in your account, you can run instead the following Jupyter notebook that generates some sample EOD price data from a public data souce. Run all the cells in 1_Data/Load_Hist_Data_Daily_Public.ipynb.

Option 1b: Sample Daily EOD Stock Price Data (via AWS Data Exchange)

If you want to use AWS Data Exchange, you can download the following dataset for example. There are multiple options available, and we picked this for demonstration purposes.

To download this dataset, complete a subscription request first where you provide the required information for Company Name, Name, Email Address, and Intended Use Case. Once the provider confirms the subscription, you can navigate to AWS Data Exchange/My subscriptions/Entitled data. Then choose the latest revision for this subscription, select all assets, and click on Export to Amazon S3. In the new window select the root folder of the S3 bucket that starts with "algotrading-data-". Then click on Export and wait until your export job is completed.

In order to use this dataset for algorithmic trading, we want to standardize it to a CSV file with the following columns: dt, sym, open, high, low, close, vol. Once you have successfully exported the dataset, please run the the following Jupyter notebook to format the dataset and store it in the hist_data_daily folder of your S3 bucket. Go to Amazon SageMaker/Notebook/Notebook instances, then click on Open Jupyter for the provisioned notebook instance. Run all the cells in 1_Data/Load_Hist_Data_Daily.ipynb.

Step 2: Backtest a trend following strategy (or move directly to Step 3)

In this module, we backtest a trend following strategy on daily price data with Amazon SageMaker. For these notebooks, please ensure that you have daily price data loaded.

You can choose between the following trading strategies:

  1. Simple Moving Average Strategy: 2_Strategies/Strategy SMA.ipynb

  2. Daily Breakout Strategy: 2_Strategies/Strategy_Breakout.ipynb

Select the Jupyter Notebook for backtesting the strategy in the folder 2_Strategies for your selected strategy and run it from your Amazon SageMaker Notebook instance. In the instructions, there is guidance on how to optimize the strategy.

Step 3: Backtest a machine-learning based strategy

In this module, we backtest a machine-learning strategy with Amazon SageMaker on daily or intraday price data. Please ensure that you have daily or intraday price data loaded before running the corresponding notebooks.

Usually you will have two parts, one for training the machine learning model, and one for backtesting the strategy. You can run both notebooks or skip the training of the model as a trained model is already available in the repository:

ML Long/Short Prediction Strategy

  • Model Training (Daily Price Data) (Optional): 3_Models/Train_Model_Forecast.ipynb
  • Strategy Backtesting (Daily Price Data): 2_Strategies/Strategy_Forecast.ipynb

Instructions using Amazon FinSpace

  1. Setup Amazon FinSpace
  2. Run the following notebook: 2_Strategies/Strategy_WhatIfScenarios.ipynb in Amazon FinSpace

Congratulations! You have completed the workshop. Don't forget to cleanup the resources if you use your own account in this workshop.

algorithmic-trading's People

Contributors

amazon-auto avatar colombatto avatar kedikala avatar osteffmann avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

algorithmic-trading's Issues

setting up environment

I'm setting up the environment and running into challenges

the first issue is that the template file (algo-reference.yaml) creates an S3 bucket parameter but the instructions don't specify how this bucket parameter should be named

when the 'Create Stack' process is executed the S3 bucket parameter expects a value (it is blank by default) but, again, the instructions don't specify this parameter value

the name of the bucket matters because the 'Click on Load_Hist_Data_Daily.ipynb' script is looking for an S3 bucket that includes 'algotrading' as part of its name

if the bucket isn't found the 'Click on Load_Hist_Data_Daily.ipynb' script fails on step 2

if the bucket naming issue is resolved, step 4 fails with "fatal error: An error occurred (403) when calling the HeadObject operation: Forbidden"

I'm assuming that this issue is occurring because the principal executing the 'Click on Load_Hist_Data_Daily.ipynb' script doesn't have needed privileges on the S3 bucket

my next step in resolving these issues will be to grant Put, Get, and Delete privileges on the S3 bucket - I'll have to figure out the appropriate principle to grant these privileges to

I'm concerned that I am running into issues at the very start of the setup process - what am I doing wrong?

Unable to deploy CloudFormation Teamplate

I have tested some other Sample AWS CF Templates, and they deploy without issue.

When I attempt to deploy this one, I receive, "Template format error: At least one Resources member must be defined." in the AWS CloudFormation Web GUI.

I cannot find an issue with the Template itself; however, when using the AWS YAML Template Extension for Visual Studio Code, it indicates the following error:

Line 235: SageMakerExecutionRole: "unacceptable node kind for !<!Sub> tag; it should be "scalar", not "sequence" and "incomplete explicit mapping pair; a key node is missed"

I cannot determine the issue - is it talking about the previously line, where !Sub is used?

Line 231:

Resource:
- !Sub
- arn:aws:s3:::${S3Bucket}/*
- S3Bucket: !Ref 'S3Bucket'

Instructions for Docker Contianer and Python Version Management in Sagemaker

I am going through the repo and on the SMA strategy I am getting error when trying to run Step 4) Backtest locally It seems we need to have a docker container running locally for this to work? There's no instructuons so hard to tell. Also, I am using Sagemaker kernel conda_mxnet_latest_p37 and for some reason getting an error saying i need to update from 3.6 to 3.7. I checked in the terminal and both pip and python versions good... pip 22.0.4 from /home/ec2-user/anaconda3/envs/JupyterSystemEnv/lib/python3.7/site-packages/pip (python 3.7), Python 3.7.12

Problem with Host_Strategy_Live_Simulation

So, When I try to choose one of the strategies and run the code, regardless of what strategy I choose, there is an error of hosting the end point, Plus this one for deploying lambda function

An error occurred (ResourceNotFoundException) when calling the UpdateFunctionCode operation: Function not found: arn:aws:lambda:eu-west-1:619056185714:function:algo_market_data

An error occurred (ResourceNotFoundException) when calling the UpdateFunctionConfiguration operation: Function not found: arn:aws:lambda:eu-west-1:619056185714:function:algo_market_data

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.