码迷,mamicode.com
首页 > 系统相关 > 详细

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

时间:2019-06-04 09:47:39      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:img   into   most   problems   dia   tps   div   ros   operation   

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

标签:img   into   most   problems   dia   tps   div   ros   operation   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!