Skip to main content

A set of tools to pull and analyze stock data.

Project description

Stonks

(C) 2020 Mark M. Bailey, PhD

About

This is a set of tools I built to pull and analyze stock prices. Stock prices can be pulled from the World Trading Data API (www.worldtradingdata.com, key required), or they can be acquired through other means. This set of tools can also be used to generate synthetic price lists in order to test out the analytical features (or for anything else you can come up with). The "analyze()" class will generate an analysis object from the input price list, which can be analyzed using either a stochastic oscillator or a relative strength index indicator (or both). An output of "Hold", "Sell", or "Buy" signal can be extracted from the analysis based on the most recent indicator values.

Disclaimer: I do this mostly for fun (I like to code, I'm a data science nerd and I like stocks; but I'm just a math professor and not qualified to offer financial advice).

If you want to make this better, let's collaborate!

Installation

pip install stonk

Sample usage

Initial import

import stonk

Access historic stock data

Note that this requires an API key from World Trading Data (www.worldtradingdata.com).

stock = stonks.stock_API(API_KEY)
Parameters:

  • API_KEY = World Trading Data API Key (str).

daily_price_dictionary = stock.daily(TKR, time_delta, beta=True, EX='^IXIC')
Parameters:

  • TKR = Ticker symbol (str).
  • time_delta = Specifies the number of days into the past (from today) to pull historic closing prices (int).
  • beta = Boolean to include beta calculation in output dictionary (default=True).
  • EX = Exchange on which to base beta calculation (default = '^IXIC')
  • output = dictionary

intraday_price_dictionary = intraday(TKR, range_=1, interval=60, beta=True, EX='^IXIC')
Parameters:

  • TKR = Ticker symbol (str).
  • range_ = Specifies the number of days you want to return data for (int or str).
  • interval = Specifies the interval (in minutes) of time between the data (int or str).
  • interval = Boolean to include beta calculation in output dictionary (default=True).
  • EX = Exchange on which to base beta calculation (default = '^IXIC')
  • output = dictionary

Generate synthetic stock price data

If you don't have API access or other real stock data and want to mess around with simulations, this class will generate synthetic price data (either bullish random price or oscillating random price). Note that these generators are imperfect and will sometimes generate negative prices (if you'd like to contribute to this project and make this function better, please feel free!).

synthetic_price_generator = stonks.syn_stock()
synthetic_prices = synthetic_price_generator.oscillator(n, scale=1)
synthetic_prices = synthetic_price_generator.bullish_random(n, scale=1)
Parameters:

  • n = Number of prices to simulate.
  • scale = Standard deviation (spread or “width”) of the distribution. Must be non-negative.

Calculate momentum indicators

This class will calculate either a stochastic oscillator or relative strength index indicator for a list of stock prices. Sell or buy signals are also generated.

stock = analyze(input_, closing_prices='closing_prices')
Parameters:

  • input_ = Price list or dictionary (generated from daily_price_dictionary).
  • closing_prices = Key for prices dictionary (default is 'closing_prices').

stock_object = stock.stochastic_oscillator(n, c=3)
output = stock_object.stochastic_output
signal = stock_object.stochastic_assessment
Parameters:

  • n = Period size (int).
  • c = Moving average size (default is 3).
  • output = JSON output (Within the 'assessment' list, a value of -1 means the stock is oversold, and 1 indicates that it is overbought. A value of 0 means neither).
  • signal = Buy, Sell, or Hold signal based on most recent stochastic output.

stock_object = stock.RSI(n)
output = stock_object.RSI_output
signal = stock_object.RSI_assessment
Parameters:

  • n = Period size (int).
  • output = JSON output (Within the 'assessment' list, a value of -1 means the stock is oversold, and 1 indicates that it is overbought. A value of 0 means neither).
  • signal = Buy, Sell, or Hold signal based on most recent stochastic output.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

stonk-0.0.1.tar.gz (5.7 kB view hashes)

Uploaded Source

Built Distribution

stonk-0.0.1-py3-none-any.whl (6.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page