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

Posted ecoflex

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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.

 

以上是关于[Machine Learning for Trading] {ud501} Lesson 3: 01-02 Working with multiple stocks的主要内容,如果未能解决你的问题,请参考以下文章

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

机器学习- 吴恩达Andrew Ng 编程作业技巧 for Week6 Advice for Applying Machine Learning

Advice for students of machine learning

I'm back for Machine Learning

SAP热招DevOps Engineer for Machine Learning(Shanghai)

CS224W摘要01.Introduction; Machine Learning for Graphs