-
Notifications
You must be signed in to change notification settings - Fork 0
/
stockanalysis.py
executable file
·133 lines (110 loc) · 4.48 KB
/
stockanalysis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
from sqlalchemy import create_engine, Column, Integer, String, ForeignKey, \
Date, DateTime, Boolean
from sqlalchemy.ext.declarative import declarative_base
import requests
import cjson
engine = create_engine('postgresql+psycopg2://localhost/stock_analysis',
echo=True)
Base = declarative_base()
class Stock(Base):
__tablename__ = 'symbols'
symbol = Column(String, primary_key=True)
name = Column(String)
active = Column(Boolean)
def __init__(self, symbol, name, active=True):
self.symbol = symbol
self.name = name
self.active = active | True
class StockPriceDaily(Base):
__tablename__ = 'price_daily'
id = Column(Integer, primary_key=True)
symbol_id = Column(String, ForeignKey('symbols.symbol'))
date = Column(Date)
high = Column(Integer)
low = Column(Integer)
open = Column(Integer)
close = Column(Integer)
def __init__(self):
print('Stock Price Daily initialized...')
class StockPriceMinute(Base):
__tablename__ = 'price_minute'
id = Column(Integer, primary_key=True)
symbol_id = Column(String, ForeignKey('symbols.symbol'))
datetime = Column(DateTime)
high = Column(Integer)
low = Column(Integer)
open = Column(Integer)
close = Column(Integer)
def __init__(self):
print('Stock Price Minute initialized...')
# Create the tables
Base.metadata.create_all(engine)
# Description: Class that retricreateves data from the Google Stock Screener.
# Functonality only implemented as see fit - too many fields to
# implement all at once.
class GoogleScreener:
url = 'https://www.google.com/finance'
market_capital_min = '0'
market_capital_max = '0'
exchange = 'TSE'
dividend_yield_min = 3
dividend_yield_max = 0
last_price_min = 0
last_price_max = 0
result_type = 'company'
sort_by = 'MarketCap'
result_limit = 200
offset = 0
def __init__(self):
print('Google Screener initialized...')
# Class methods
def build_query(self):
result = '[(exchange == "TSE") & '
result += '(market_cap >= ' + self.market_capital_min + ') & '
result += '(market_cap <= ' + self.market_capital_max + ') &'\
if self.market_capital_max > 0 else ''
# This may seem redundant (last_price > 0), but it protects from bogus
# data. It also makes last_price appear in the returned columns.
result += '(last_price > ' + str(self.last_price_min) + ') & '
result += '(last_price <= ' + str(self.last_price_max) + ') &'\
if self.last_price_max > 0 else ''
result += '(dividend_yield >= ' + str(self.dividend_yield_min) + ') & '
result += '(dividend_yield <= ' + str(self.dividend_yield_max) + ') &'\
if self.dividend_yield_max > 0 else ''
# Again, may seem redundant, but this forces the recent dividend to
# return in the result. Should be made dynamic in future.
result += '(dividend_recent_quarter > 0)'
result += ']'
return result
def run(self):
# build and execute query
payload = {'output': 'json',
'start': self.offset,
'num': self.result_limit,
'noIL': 1,
'restype': self.result_type,
'sortas': self.sort_by,
'q': self.build_query()}
response = requests.get(self.url, params=payload)
json_response = cjson.decode(response.text)
# Return object
symbols = []
# Load the list of stocks using the json response
for company in json_response['searchresults']:
# Dynamically load named values into a dictionary
values = {}
for column in company['columns']:
values[column['field']] = column['value']
# Cast yield, dividend and price
dividend_yield = float(values['DividendYield']) / 100
price = float(values['QuoteLast'])
dividend = float(values['DividendRecentQuarter'])
# Append stock to symbols if dividend payouts are nore more than
# quarterly
if dividend_yield / (dividend / price) < 5:
symbols.append({'cid': company['id'],
'symbol': company['ticker'],
'name': company['title']
.decode('unicode-escape')}
)
return symbols