Machine Learning for Trading

share ›
‹ links

Below are the top discussions from Reddit that mention this online Udacity course.

Implement machine learning based strategies to make trading decisions using real-world data.

Reddacity may receive an affiliate commission if you enroll in a paid course after using these buttons to visit Udacity. Thank you for using these buttons to support Reddacity.

Reddit Posts and Comments

1 posts • 49 mentions • top 25 shown below

r/stocks • post
206 points • ATribeCalledM
[How-To] Technical Trading Using Python and Machine Learning

I’ve had numerous requests about building a predictive model for stocks so here’s a walk through to jump start your journey. This guide will take you through the process of testing and training a model using technical indicators. This guide will utilize Bollinger Bands and a 50 day moving average to make price predictions for Tesla. This only an example to get started and shouldn’t be treated as a holy grail for trading. It will be up to you to improve on the model with your inputs and assumptions and make it your own. While making accurate predictions might be complex, I will try to explain the process and concepts in layman’s terms without getting too technical (no pun intended).  

Tools:

  • Python (2.7 or 3.X)
  • Pandas
  • Numpy
  • Ta-Lib
  • Pandas_DataReader
  • SkLearn
  • Jupyter Notebook (optional)  

Most of these packages come installed with the Conda build of Python. I would suggest installing that. Then you would only have to install pandas_datareader and ta-lib. I use Jupyter Notebook as the IDE but feel free to use any program you like for development.  

I’m not going to go through the process how to install and troubleshoot the installation of Python and packages. Hopefully, you will be able to remedy any problems encountered with the help of Google.  

Step 1: Import Packages

 

If you are new to Python or haven’t programmed before, this is just a step to make sure all the functions you need will be available when called.
 

import pandas as pd
import numpy as np
import talib
from pandas_datareader import data
import matplotlib.pyplot as plt
from sklearn.tree import DecisionTreeRegressor
%matplotlib inline

 

Step 2: Gather historical financial data to build model

 

Utilizing panda_datareader, we will pull historical stock information from Yahoo to build a historical dataset. You will pass in the stock symbol, website(in this case yahoo), and the beginning date that you want. This will return the open, high, low, close, adj close, and volume for each trading day from the beginning date to present.  

#Import open, high, low, close, and volume data from Yahoo using DataReader

TSLA = data.DataReader(TSLA,'yahoo', '2009-01-01') #Import historical stock data for training


#Convert Volume from Int to Float

TSLA.Volume = TSLA.Volume.astype(float)

Tip: If you want to aggregate the data into weekly, monthly, yearly, etc. Look into the asfreq function in the Pandas documentation

Step 3: Select Features

 

In machine learning, the features are anything that describe the data that you’re trying to predict. In this case, this will be historical price data and technical indicators. We will add Bollinger Bands and 50-Day moving average as features using the TA-Lib function.  

##Update Technical Indicators data

##Overlap Indicators

TSLA['MA50'] = talib.MA(TSLA['Close'].values, timeperiod=50, matype=0)
TSLA['UPPERBAND'], TSLA['MIDDLEBAND'], TSLA['LOWERBAND'] = talib.BBANDS(TSLA[‘Close’].values, timeperiod=20, nbdevup=2,          nbdevdn=2, matype=0)

Step 4: Select Target

 

In machine learning, the target is the value you’re trying to predict. Since we are trying to predict a continuous value from labeled data, this is considered a supervised learning regression model. If we were trying to predict a label or categorical data, it would be considered classification.  

In this example, we are going to use the shift function in Pandas to create forward looking columns. Since we are using daily data, shifting the values forward one will give the actual closing price of the next day. We will use the historical prices to try to predict future data. If you want to predict further into the future, just change your shift value to the corresponding time period you’re trying to forecast.  

#Create forward looking columns using shift.


TSLA['NextDayPrice'] = TSLA['Close'].shift(-1)

Step 5: Clean Data

 

This is really the most important part. This where you will use your judgement to normalize, remove, and/or alter any data based on your assumptions. The biases your bring with you will be reflected in your model.  

Bad data + bad assumptions = Bad Model  

Bad data + good assumptions = Bad Model  

Good data + bad assumptions = Bad Model  

Good data + good assumptions = Good Model  

For this example, we are only dropping data that have no values, but there is much more you can do during this stage. Since the technical indicators are lagging (50 day moving average needs 50 data points first) there will be data points without any values. In order for the model to properly learn the effects of each feature on the target, we will need to drop those data points.  

#Copy dataframe and clean data

TSLA_cleanData = TSLA.copy()
TSLA_cleanData.dropna(inplace=True)

Step 6: Split Data into Training and Testing Set

 

To train the model, we will first need to separate the features and targets into separate datasets. We will then use cross validation to split the data into training and testing sets using a 70/30 split (70 percent of the data will be used to train the model and the rest will be used to validate the effectiveness of the model). Cross validation is important because you want to make sure your model is robust. If you train your model on all the data, then you have no idea on how well it works on data that it has not seen. Using splicing, we will separate the features from the target into individual data sets.  

X_all = TSLA_cleanData.ix[:, TSLA_cleanData.columns != NextDayPrice]  # feature values for all days
y_all = TSLA_cleanData[‘NextDayPrice’]  # corresponding targets/labels
print (X_all.head())  # print the first 5 rows

#Split the data into training and testing sets using the given feature as the target
X_train, X_test, y_train, y_test = train_test_split(X_all, y_all, test_size=0.30, random_state=42)

Step 7: Train Model

 

We will use a linear regression model to train the model. There are many models you can use and many parameters you can tune, but for simplicity, none of this is shown.

 from sklearn.linear_model import LinearRegression


#Create a decision tree regressor and fit it to the training set
 regressor = LinearRegression()

 regressor.fit(X_train,y_train)

 print ("Training set: {} samples".format(X_train.shape[0]))
 print ("Test set: {} samples".format(X_test.shape[0]))

Step 8: Evaluate Model

 

Next, we will evaluate the performance of our model. The metrics you use is up to you. Accuracy and Mean Squared Error are shown below.  

from sklearn import cross_validation

scores = cross_validation.cross_val_score(regressor, X_test, y_test, cv=10)
print ("Accuracy: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() / 2))

from sklearn.metrics import mean_squared_error

mse = mean_squared_error(y_test, regressor.predict(X_test))
print("MSE: %.4f" % mse)

Step 9: Predict

 

Once you are happy with your model, you can now start using it to predict future prices. We will take the last row from the data set and predict the price of the next data.

X=TSLA[-1:]
print(regressor.predict(X))

 

Congrats, you have now built a predictive model using stock data. Below are documentation and resources to help you deeper understand the functions used and their applications.

Resources

 

r/algotrading • post
41 points • shortpaleugly
CS 7646: Machine Learning for Trading free at Udacity
r/algotrading • post
114 points • quantor_algotrading
List of online courses to Learn AlgoTrading & Quantitative Finance

Here is the list of free online courses for learning algo trading, investing and quantitative finance:

Machine Learning for Trading |Udacity

https://www.udacity.com/course/machine-learning-for-trading--ud501

Investments | Massachusetts Institute of Technology

http://ocw.mit.edu/courses/sloan-school-of-management/15-433-investments-spring-2003/

Analytics of Finance | Massachusetts Institute of Technology

http://ocw.mit.edu/courses/sloan-school-of-management/15-450-analytics-of-finance-fall-2010/

Topics in Mathematics with Applications in Finance | MIT

http://ocw.mit.edu/courses/mathematics/18-s096-topics-in-mathematics-with-applications-in-finance-fall-2013/

Model a Quantitative Trading Strategy in R | QuantInsti | DataCamp

https://www.datacamp.com/courses/model-a-quantitative-trading-strategy-in-r/

The R Trader Blog

http://www.thertrader.com/

r/FinancialCareers • post
16 points • TonyDarko
How to get into Business Analytics / Financial Engineering (As a CS Student)?

Hey /r/FinancialCareers, I'm a Computer Engineering + Computer Science double major/grad student looking to learn about business analytics and financial engineering then eventually apply to internships/full-time positions.

I have a limited background in economics (basic macro/micro), so I was thinking about trying to snag the Computational Finance course on Udemy when there's a coupon.

This course from Udacity also looks promising.

I'm need help finding resources on the following:

  • Basic investments and capital market knowledge
  • General Finance
  • Portfolio analytics
  • Risk and Quantitative analysis
  • Manipulating financial data in MATLAB/Python
  • Computational Investing

Advice from people in these types of jobs would also be very helpful. Thanks!

r/investing • comment
11 points • ardme

Yes I hope so! I am working on trying to identify better entry points to avoid choppiness now. This weekend I spent some time on tweaking and I am now trying understand if indicators like RSI, ADX and moving average percent difference at time of entry are related the outcome of the trade. I am also trying to build a machine learning model against these features but currently struggling with labeling enough data to fit the model against.

Recently, I discovered there is a Udacity subject on this very topic - https://www.udacity.com/course/machine-learning-for-trading--ud501. There is just never enough time to do all these things!

r/algotrading • comment
18 points • GORILLA_FACE

Watch the videos here https://www.udacity.com/course/machine-learning-for-trading--ud501

Do the assignments here https://quantsoftware.gatech.edu/CS7646_Spring_2018

Oops, this assumes you were interested in machine learning as well, but by the end you do implement a rule based trader and a back tester.

r/algotrading • comment
11 points • sch77

Here is something that I'm doing right now. It's FREE

https://www.udacity.com/course/machine-learning-for-trading--ud501

r/algotrading • post
9 points • need-a-username
Which Tucker Balch course do you think is better for starters and advice for starter

Udacity and Coursera each have a course by Dr. Balch. Which one do you think is best for someone who is new to algo trading?

https://www.udacity.com/course/machine-learning-for-trading--ud501

https://www.coursera.org/learn/computational-investing

I haven't programmed in a couple years. I used to be somewhat skilled in programming with Python. Also I have knowledge of finance but this is more of value investing and valuation. I have taken up to multivar calc.

I've been messing around on Quantopian for a while just to get my feet wet. I have actually tested some ideas of mine but they are not trading ideas.

Any other resources that you think I should look at? I am going to have a lot of free time for the next year and half. I would like to be able to backtest and form ideas by that time.

Yes, I have looked at the side bar.

Anyone want to layout a frame work to get me started?

Steps that I think I need to cover: 1. Refresh python some 2. Learn how to use all these finance libraries by...?

r/algotrading • comment
2 points • okaw

Is this the Georgia course you mentioned?

https://www.udacity.com/course/machine-learning-for-trading--ud501

r/algotrading • comment
1 points • pulifrici

i think you're referencing this class: https://www.udacity.com/course/machine-learning-for-trading--ud501

r/algotrading • comment
1 points • jaco6y

Not a textbook, but I thought Tucker Balch's course was pretty good. (Not sure how much of it you will have seen before)

It teaches you the basics of how to apply ML to actual financial data, do stuff like portfolio optimization, use different regressors to predict returns, reinforcement learning, etc. I think it does a good job at laying things out such that you'll know where to start with experimenting on your own. (Although IMO the course ends too soon!)

r/algotrading • comment
1 points • 42codex

Looks like there's a few of us in the same boat. It is indeed a fun project. I've been coding bits and pieces to search for inefficiencies. Then I've been trying out smaller manual trades to see how it works out.

My strategy is to build up from a kind of expert advisor to something that I will eventually let loose unattended. I'm almost ... there but still working on scaling things up. A lot of unexpected stuff can happen, slippage, flash crashes, de-leveraging ...

I can vouch for the advice from mementix. Just getting into backtrader and ccxt, wish I knew about them earlier. Just hope when the big boys from CME come in, they don't mess up all our strategies :)

PS. I got started with this course which bridged some of the initial finance gaps for me. Although you might already know what you need.

Good luck!

r/algotrading • comment
1 points • pharmerino

https://www.udacity.com/course/machine-learning-for-trading--ud501

Most of this course I just did manually in python but honestly this covers the fundamentals and you can pick whatever framework you want.

r/wallstreetbets • comment
1 points • rlql

ML part is easy. hard part is finding indicators that will actually give you an edge. and getting enough data if you want intra-day data. you can get daily data on yahoo.

I did this during a class using SMAs and bollinger bands. Sometimes it worked and sometimes it didn't. I wouldn't trade it.

r/neuralnetworks • comment
1 points • anon35202

Stop being a child, imagining what might be, and ask your parents for some money to sign up for a class to teach you how to do it.

https://www.udacity.com/course/machine-learning-for-trading--ud501

r/cscareerquestions • comment
1 points • themadstork13

https://www.udacity.com/course/machine-learning-for-trading--ud501

r/learnpython • comment
1 points • rnolan7

before diving into machine learning, make sure you have a basic understanding of statistics and are comfortable working with arrays in python. Udacity has a course called "machine learning for trading" that covers some basic concepts (random/decision trees, linear regression, and q-learning) and applies them to stock market trade decisions which I found really interesting.

r/OSUOnlineCS • comment
1 points • EfficientMasochist
r/algotrading • comment
1 points • _x13

When talking about algo-trading, are you talking about this course: https://www.udacity.com/course/machine-learning-for-trading--ud501?

r/investimentos • comment
1 points • ahcsilva90

Na realidade é uma especialização (https://www.coursera.org/specializations/machine-learning-trading) com 3 cursos:

1) Introduction to Trading, Machine Learning & GCP 2) Using Machine Learning in Trading and Finance 3) Trading Strategies as a Reinforcement Learning (RL) Problem

Eu cheguei a fazer um curso similar na plataforma Udacity (https://www.udacity.com/course/machine-learning-for-trading--ud501) e gostei bastante. Você consegue visualizar gratuitamente. Só paga se quiser o certificado. Foi muito proveitoso para mim, pois meu Background é de Machine Learning, porém tinha zero conhecimento relacionado ao mercado financeiro. Conseguir unir bem os dois mundos e hoje invisto totalmente via recomendações baseadas em modelos de Machine Learning + Otimização.

r/Forex • comment
1 points • Epictrader1

You can give this a try: https://www.udacity.com/course/machine-learning-for-trading--ud501

and learn whatever math and statistics that does not make sense to you along the way.

r/wallstreetbets • comment
0 points • anon35201

> TA works, just really poorly

The lack of mathematical precision in each of your sentences mean you are destined for failure in this space, and your two cents here are your subconscious grasping at the very large holes in your understanding here.

If you want to to invest in yourself so that you stop sounding like an idiot, I suggest you pay to take this class: https://www.udacity.com/course/machine-learning-for-trading--ud501 from this guy; https://www.cc.gatech.edu/~tucker

He goes over all the stuff you're getting wrong here.

The professor there is a guy who is a living proof that your paragraphs are incorrect, since I worked under him and he divulged the code and the principles behind it that made him wealthy at Lucena Research. The code he gave was 100% technical, it no longer works now, or in the future, granting the professor the liberty to divulge it against the advice of his fellow directors, because the concepts there are long-since cat-out-of-the-bag, and using that code is bringing an old weapon to a new battlefield. It won't work because your adversaries in the market have already subsumed it, returning the market opportunity to equilibrium like digging a hole in the ocean.

There is enough information in only technical analysis to win every single trade, the problem is, your AI needs to subsume all the other AI's. Some companies do this, and extract trillions from the market.

r/learnprogramming • comment
2 points • my_password_is______

Discrete Math and Algorithms and Data Structures

there are just as many resources for Machine Learning as their are for Algorithms and Data Structures

https://www.udacity.com/course/machine-learning--ud262

https://www.udacity.com/course/intro-to-machine-learning--ud120

https://www.udacity.com/course/machine-learning-for-trading--ud501

^ those are free

these are free, but have a pay option for graded work (also have to pay to have lifetime access)

https://www.edx.org/v2/course/machine-learning

https://www.edx.org/v2/course/machine-learning-fundamentals-3

https://www.edx.org/v2/course/data-science-machine-learning-2

https://www.edx.org/v2/course/principles-of-machine-learning-python-edition-3

so you an learn Machine Learning on your own time (assuming you have already taken Calculus and Linear Algebra -- if you haven't then take them as electives now)

r/learnpython • comment
1 points • foxillian

Checkout Georgia Techs ML for Trading class (https://quantsoftware.gatech.edu/Machine_Learning_for_Trading_Course, https://www.udacity.com/course/machine-learning-for-trading--ud501) which gives an overview of trading, technical indicators, and structure for a trading bot program. The last set of assignments looks at ML based traders, especially around RL and Q-learning.

API for data collection https://iexcloud.io/docs/api/

I'll echo the general sentiment for the rest of the thread. The first piece of advice in the above course is to avoid using anything you write in the course for actual trading unless you are fully prepared to instantly lose all of your money.

r/CryptoCurrency • comment
1 points • Teaotic

For the plain mechanics, this tutorial was actually quite helpful:

How to create a trading bot: https://www.youtube.com/playlist?list=PLqT5wYj8Tan2gOhG3fKRAdplu4aMweyUK

Cryptocurrency arbitrage bot: https://www.youtube.com/playlist?list=PLqT5wYj8Tan3xGeG98xM7evaBcSnpcTFG

And a few useful and interesting links and quant blogs:

https://quantocracy.com/

https://www.udacity.com/course/machine-learning-for-trading--ud501

http://www.financial-hacker.com/

https://qoppac.blogspot.com/

There are 3 parts to building a trading system-

  1. Building a backtester.

  2. R&D: Finding a theoretically profitable algo (this is the hard part, and dont forget to account for fees and slippage, which can destroy an otherwise profitable strat), and then testing it more throughly on say history from different exchanges, and artificially created datasets, etc, then making changes and adjusting config, etc.

Some statistics would probably be helpful in distinguishing significance from random correlation. I'm sure there are free MOOCs for that, I ought to take one myself.

  1. Then integrating it with the API of an exchange (they will have some documentation on this, but it's usually pretty opaque even for a programmer).

You have to be able to code at least a little to do this, but it's not that hard if you are modestly competent. Many "libraries" are prebuilt, you dont have to reinvent the wheel, altho there will be problems to solve.

There are bots you can buy, and open source like Gekko, but I didnt see any open source bots that could go both long or short, so I made my own.

And if you buy a bot, and you want to change a feature they dont offer, you're out of luck unless you can do it yourself.

Then there are all kinds of little engineering problems that crop up, like whether to place trade at last price or a more favorable limit away, and try again at last price if the better price doesnt go thru, or some more sophisticated strategy.