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.8.tar.gz
(13.2 kB
view hashes)
Built Distribution
Close
Hashes for stock_scanner-0.0.8-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 729914d127f0274971e06b2e8977e57a7630bf281e57c9c6e6accb04786419cd |
|
MD5 | 3be6011b390546b790f5da2925e4c93e |
|
BLAKE2b-256 | 7e76d73ab6bfba5d23451467b814a2761c54ab3ee38ae33bff84b5dbf8cd76df |