[Machine Learning for Trading] {ud501} Lesson 3: 01-02 Working with multiple stocks

Lesson outline

Lesson outline

Here‘s an overview of what you‘ll learn to do in this lesson. Documentation links are for reference.

Read in multiple stocks:

Manipulate stock data:

Hit Next to continue.



Pandas dataframe recap

Problems to solve

NYSE trading days

See if you can find the number of trading days here: www.nyse.com/markets/hours-calendars

Note that we are interested in the number of trading days (i.e. days the market conducted trading) for US Equities during 2014

Building a dataframe

S&P 500 is a stock market index based on 500 large American companies listed on the NYSE or NASDAQ. Think of it as a weighted mean of the stock prices of the companies, where the number of shares are used as weights (with some adjustment for any events that may affect apparent stock value, such as splits).

SPDR® S&P 500 is an ETF (or exchange-traded fund) that tracks the S&P 500 index, and is itself listed on NYSE under the ticker symbol "SPY".

"Joining" dataframes

Create an empty data frame

Join SPY data

NaN because SPY.cvs uses integers as index

Documentation: pandas.DataFrame.join

Look for the how parameter.

Read in more stocks

what‘s wrong?

Utility functions for reading data

"""Utility functions"""

import os
import pandas as pd

def symbol_to_path(symbol, base_dir="data"):
    """Return CSV file path given ticker symbol."""
    return os.path.join(base_dir, "{}.csv".format(str(symbol)))

def get_data(symbols, dates):
    """Read stock data (adjusted close) for given symbols from CSV files."""
    df = pd.DataFrame(index=dates)
    if ‘SPY‘ not in symbols:  # add SPY for reference, if absent
        symbols.insert(0, ‘SPY‘)

    for symbol in symbols:
        # TODO: Read and join data for each symbol
        df2 = pd.read_csv(symbol_to_path(symbol),index_col="Date",parse_dates=True,usecols=[‘Date‘,‘Adj Close‘],na_values=[‘nan‘])
        df2 = df2.rename(columns={‘Adj Close‘: symbol})
        df = df.join(df2)

    return df.dropna()

def test_run():
    # Define a date range
    dates = pd.date_range(‘2010-01-22‘, ‘2010-01-26‘)

    # Choose stock symbols to read
    symbols = [‘GOOG‘, ‘IBM‘, ‘GLD‘]

    # Get stock data
    df = get_data(symbols, dates)
    print df

if __name__ == "__main__":
    test_run()

Documentation:

Obtaining a slice of data

More slicing

Problems with plotting

Plotting multiple stocks

Carly Fiorina was named "the most powerful woman in business" by Forbes in 1998, while at AT&T/Lucent. She was the CEO of HP from 1999-2005, and has held several leadership positions at technology firms and business institutes.

Listen to her talk about The Importance of Selective Information as part of Stanford‘s Entrepreneurial Thought Leaders Lecture series [full podcast].

Some of her popular quotes can be found here.

Normalizing

Lesson summary

To read multiple stocks into a single dataframe, you need to:

  • Specify a set of dates using pandas.date_range
  • Create an empty dataframe with dates as index
    • This helps align stock data and orders it by trading date
  • Read in a reference stock (here SPY) and drop non-trading days using pandas.DataFrame.dropna
  • Incrementally join dataframes using pandas.DataFrame.join

Once you have multiple stocks, you can:

  • Select a subset of stocks by ticker symbols
  • Slice by row (dates) and column (symbols)
  • Plot multiple stocks at once (still using pandas.DataFrame.plot)
  • Carry out arithmetic operations across stocks, e.g. normalize by the first day‘s price

Hit Next to continue.

原文地址:https://www.cnblogs.com/ecoflex/p/10971500.html

时间: 2024-08-28 05:49:38

[Machine Learning for Trading] {ud501} Lesson 3: 01-02 Working with multiple stocks的相关文章

[Machine Learning for Trading] {ud501} Lesson 19: 02-09 The Fundamental Law of active portfolio management | Lesson 20: 02-10 Portfolio optimization and the efficient frontier

this lesson => Buffet said two things => (1) investor skill => (2) breadth / the number of investments Grinold's Fundamental Law breadth => more opportunities to applying that skill => eg. how many stocks you invest in IC => information

[Machine Learning for Trading] {ud501} Lesson 7: 01-06 Histograms and scatter plots

A closer look at daily returns Histogram of daily returns gaussian => kurtosis = 0 How to plot a histogram Computing histogram statistics Select the option that best describes the relationship between XYZ and SPY. Note: These are histograms of daily

[Machine Learning for Trading] {ud501} Lesson 9: 01-08 Optimizers: Building a parameterized model | Lesson 10: 01-09 Optimizers: How to optimize a portfolio

What is an optimizer? Minimization example How to defeat a minimizer Convex problems Building a parameterized model Minimizer finds coefficients What is portfolio optimization? The difference optimization can make Which criteria is easiest to solve f

[Machine Learning for Trading] {ud501} Lesson 25: 03-05 Reinforcement learning | Lesson 26: 03-06 Q-Learning | Lesson 27: 03-07 Dyna

原文地址:https://www.cnblogs.com/ecoflex/p/10977470.html

[Machine Learning for Trading] {ud501} Lesson 21: 03-01 How Machine Learning is used at a hedge fund | Lesson 22: 03-02 Regression

原文地址:https://www.cnblogs.com/ecoflex/p/10977432.html

[Machine Learning for Trading] {ud501} Lesson 23: 03-03 Assessing a learning algorithm | Lesson 24: 03-04 Ensemble learners, bagging and boosting

A closer look at KNN solutions What happens as K varies What happens as D varies Metric 1 RMS Error In Sample vs out of sample Which is worse? Cross validation 5-fold cross validation Roll forward cross validation Metric 2: correlation Correlation an

【转载】Machine Learning CMSC 422 Spring 2013

Machine LearningCMSC 422Spring 2013 Schedule: MWF 4:00pm-4:50pm Location: CSIC 2117 Instructor: Hal Daume III:  Office Hours: AVW 3227; Fri 2:45-3:45 or by appointment Piazza: UMD/cs422 TAs: Phil Dasler (office hours: Thr 2:00-3:00 in TA room)   Josh

[C5] Andrew Ng - Structuring Machine Learning Projects

About this Course You will learn how to build a successful machine learning project. If you aspire to be a technical leader in AI, and know how to set direction for your team's work, this course will show you how. Much of this content has never been

Introduction to Machine Learning

Chapter 1 Introduction 1.1 What Is Machine Learning? To solve a problem on a computer, we need an algorithm. An algorithm is a sequence of instructions that should be carried out to transform the input to output. For example, one can devise an algori