-
Notifications
You must be signed in to change notification settings - Fork 268
/
Copy pathmarket_scanner.py
138 lines (116 loc) · 4.46 KB
/
market_scanner.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
134
135
136
137
138
import os
import time
import yfinance as yf
from datetime import date
import datetime
import numpy as np
import sys
from stocklist import NasdaqController
from tqdm import tqdm
from joblib import Parallel, delayed, parallel_backend
import multiprocessing
import pandas as pd
import quandl
from dateutil.parser import parse
from yfinance.exceptions import YFInvalidPeriodError
import random
###########################
# THIS IS THE MAIN SCRIPT #
###########################
# Change variables to your liking then run the script
MONTH_CUTTOFF = 6 # 6
DAY_CUTTOFF = 4 # 3
STD_CUTTOFF = 7 # 9
MIN_STOCK_VOLUME = 10000
MIN_PRICE = 20
class mainObj:
def __init__(self):
pass
def getDataQuandl(self, ticker, pastDate, currentDate):
ticker = "WIKI/"+ticker
mydata = quandl.get(ticker, start_date=pastDate,
end_date=currentDate, rows=50)
mydata = mydata["Volume"]
return mydata
def getData(self, ticker):
try:
global MONTH_CUTOFF
sys.stdout = open(os.devnull, "w")
# maybe swap yahoo finance to quandl due to rate limits
try:
data = yf.Ticker(ticker).history(period=str(MONTH_CUTTOFF) + "mo", raise_errors=True)
except (YFInvalidPeriodError) as e:
try:
data = yf.Ticker(ticker).history(period=e.valid_ranges[-1])
except:
return pd.DataFrame(columns=["Volume"])
sys.stdout = sys.__stdout__
if data.tail(1)["Close"].values[0] < MIN_PRICE:
return pd.DataFrame(columns=["Volume"])
# avoid yahoo finance rate limits
time.sleep(random.uniform(0.2, 1.5))
return data[["Volume"]]
except:
return pd.DataFrame(columns=["Volume"])
def find_anomalies(self, data):
global STD_CUTTOFF
global MIN_STOCK_VOLUME
indexs = []
outliers = []
data_std = np.std(data['Volume'])
data_mean = np.mean(data['Volume'])
anomaly_cut_off = data_std * STD_CUTTOFF
upper_limit = data_mean + anomaly_cut_off
data.reset_index(level=0, inplace=True)
for i in range(len(data)):
temp = data['Volume'].iloc[i]
if temp > upper_limit and temp > MIN_STOCK_VOLUME:
indexs.append(str(data['Date'].iloc[i])[:-15])
outliers.append(temp)
d = {'Dates': indexs, 'Volume': outliers}
return d
def customPrint(self, d, tick):
print("\n\n\n******* " + tick.upper() + " *******")
print("Ticker is: "+tick.upper())
for i in range(len(d['Dates'])):
str1 = str(d['Dates'][i])
str2 = str(d['Volume'][i])
print(str1 + " - " + str2)
print("*********************\n\n\n")
def days_between(self, d1, d2):
return abs((parse(d2) - parse(d1)).days)
def parallel_wrapper(self, x, currentDate, positive_scans):
global DAY_CUTTOFF
d = (self.find_anomalies(self.getData(x)))
if d['Dates']:
for i in range(len(d['Dates'])):
if self.days_between(str(currentDate), str(d['Dates'][i])) <= DAY_CUTTOFF:
self.customPrint(d, x)
stock = dict()
stock['Ticker'] = x
stock['TargetDate'] = d['Dates'][0]
stock['TargetVolume'] = str(
'{:,.2f}'.format(d['Volume'][0]))[:-3]
positive_scans.append(stock)
def main_func(self):
StocksController = NasdaqController(False)
list_of_tickers = StocksController.getList()
currentDate = datetime.date.today().strftime("%m-%d-%Y")
start_time = time.time()
# positive_scans = []
# for x in tqdm(list_of_tickers):
# self.parallel_wrapper(x, currentDate, positive_scans)
manager = multiprocessing.Manager()
positive_scans = manager.list()
cpu_count = multiprocessing.cpu_count()
try:
with parallel_backend('loky', n_jobs=cpu_count):
Parallel()(delayed(self.parallel_wrapper)(x, currentDate, positive_scans)
for x in tqdm(list_of_tickers))
except Exception as e:
print(e)
print("\n\n\n\n--- this took %s seconds to run ---" %
(time.time() - start_time))
return positive_scans
if __name__ == '__main__':
mainObj().main_func()