Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating library to be compatible with latest pandas #18

Merged
merged 3 commits into from
Nov 28, 2023

Conversation

FilipBolt
Copy link
Contributor

@FilipBolt FilipBolt commented Nov 27, 2023

  1. Updating to ffill beforehand on entire dataset, instead of fetching due to pandas interface change.
  2. No longer slicing using sets, but casting to list3
  3. Minor typos

This should help resolve issues:

  1. 'https://etfdailynews.com/etf/spy/' this page is invalid now #15 (comment)
  2. How to use it on gogle colab? #14

Confirming that the following dataset downloaded with the yfinance framework now works with this library backtesting examples:

import yfinance as yf


def download_stock_data(
    ticker, start_date, end_date, granularity
):
    # Download daily OHLC, volume, dividends, stock splits, and other data
    stock_data = yf.download(
        ticker, start=start_date, end=end_date, actions=True,
        interval=granularity
    )

    # Calculate the 5-day moving average
    stock_data['MA5'] = stock_data['Close'].rolling(window=5).mean()

    # Keep the relevant columns
    stock_data = stock_data[[
        'Open', 'High', 'Low', 'Close',
        'Volume', 'Dividends', 'Stock Splits', 'MA5'
    ]]

    # Extract ex-dividend date from the dividends column
    stock_data['Ex-Dividend'] = stock_data['Dividends'].shift(1)

    # Extract split ratio from the stock splits column
    stock_data['Split Ratio'] = stock_data['Stock Splits'].apply(
        lambda x: 1 / x if x != 0 else 1
    )

    # Drop rows with NaN values
    stock_data = stock_data.dropna()

    # Add a column for the asset (use the ticker symbol as an example)
    stock_data['Asset'] = ticker

    # rename columns to lowercase
    stock_data.columns = stock_data.columns.str.lower()
    # rename to snake case
    stock_data.columns = stock_data.columns.str.replace(' ', '_')

    stock_data = stock_data[[
        'open', 'high', 'low', 'close',
        'volume', 'ex-dividend', 'split_ratio', 'ma5',
        'asset'
    ]]
    stock_data = stock_data.resample(granularity.upper()).ffill()
    stock_data['date'] = stock_data.index
    return stock_data

@Heerozh
Copy link
Owner

Heerozh commented Nov 28, 2023

great works
index.get_indexer([start], 'bfill')[0]
would be better?
so the start, end date can be a holiday

@FilipBolt
Copy link
Contributor Author

Sure, index.get_indexer works as well.

Confirming that I've tried with specifying both start and end date as holidays.

@Heerozh Heerozh merged commit 334c09e into Heerozh:master Nov 28, 2023
@Heerozh
Copy link
Owner

Heerozh commented Nov 28, 2023

confirmed, thanks for your pull request.

About YahooDownloader class, it would indeed be better to change to yfinance. your code above is a good start, if you want, you can just rewrite that class.

#15 I don’t know where to get a free ticker list now, this may be a problem, or just remove it, let the user pass in the tickers list.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

'https://etfdailynews.com/etf/spy/' this page is invalid now How to use it on gogle colab?
2 participants