A Python framework for building stock scanners/screeners.
Project description
Stock-scanner
Stock-scanner is a Python3 package aimed at facilitating the rapid development of custom stock scanners/screeners. The goal is for it to be a sort of framework which easily lets users swap different modules at their own discretion.
Example code
Using a pre-written Condition:
from stock_screener import Stock
from stock_screener.scanner import BasicScanner
from stock_screener.condition.Above150And200SMA import Above150And200SMA
from stock_screener.data_fetcher import YahooDataFetcher
universe = 'nasdaq'
path = f'./{universe}'
print("Looking for stocks above the 150 and 200 day SMA.")
data_fetcher = YahooDataFetcher(universe, path)
conditions = [ Above150And200SMA ]
candidates = (
BasicScanner(conditions, data_fetcher)
.loadData()
.getCandidates()
)
print(list(map(lambda x: x.getTicker(), candidates)))
Writing a custom Condition:
from stock_screener import Stock
from stock_screener.condition import Condition
from stock_screener.scanner.BasicScanner import BasicScanner
from stock_screener.data_fetcher.YahooDataFetcher import YahooDataFetcher
universe = 'nasdaq'
path = f'./{universe}'
# Has the stock consolidated?
class Consolidating(Condition):
def __init__(self, stock: Stock) -> None:
"""
Always call super in the constructor.
"""
super().__init__(stock)
# We will look from the last close to 10 days back
window = 10
try:
# Find the max and min closes in this window
self.max_close = stock.getClose()[-window:].max()
self.min_close = stock.getClose()[-window:].min()
except IndexError:
return False
def fulfilled(self) -> bool:
"""
If the difference between them is less than 3%
we consider the stock consolidated
"""
return self.min_close > (self.max_close * 0.97)
print("Looking for consolidated stocks.")
data_fetcher = YahooDataFetcher(universe, path)
conditions = [ Above150And200SMA, Consolidating ]
candidates = (
BasicScanner(conditions, data_fetcher)
.loadData()
.getCandidates()
)
print(list(map(lambda x: x.getTicker(), candidates)))
Interfaces
To achieve this goal of modularity, the framework has a few different interfaces you need to implement when writing your own extensions. They are:
- DataFetcher - an interface for downloading stock data and saving it locally.
- DataReader - an interface for reading stock data files.
- Condition - an interface for checking whether a stock fulfills a condition.
- Validator - an interface for deciding whether a stock should be returned as a candidate from the scan or not, given the conditions it fulfills.
- Scanner - an interface for the "main engine" of the scan.
A general outline of the use of these interfaces could look as such:
- Use a DataFetcher to download stock data.
- Use a DataReader to load the stock data into memory.
- Give a Validator a set of Conditions to see if the stock should be returned from the scan or not.
This process usually all takes place in a Scanner.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
stock-scanner-0.0.5.tar.gz
(12.1 kB
view hashes)
Built Distribution
Close
Hashes for stock_scanner-0.0.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cad23a8c1b3b9a0c19aeba7ad8a55f6528538cf3b9b0084979367b1b858be8f7 |
|
MD5 | 201fe07202b7cccf82a46cc6ba87a03a |
|
BLAKE2b-256 | 8df8d9969ed32bea8521c32ab45adb9ba5167a8eb904181cacfe56ffc9497290 |