forked from SamPom100/UnusualVolumeDetector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarket_scanner.py
84 lines (74 loc) · 3.01 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
import os
import time
import yfinance as yf
import dateutil.relativedelta
from datetime import date
import datetime
import numpy as np
import sys
from stocklist import NasdaqController
from tqdm import tqdm
class mainObj:
def getData(self, ticker):
currentDate = datetime.datetime.strptime(
date.today().strftime("%Y-%m-%d"), "%Y-%m-%d")
pastDate = currentDate - dateutil.relativedelta.relativedelta(months=5)
sys.stdout = open(os.devnull, "w")
data = yf.download(ticker, pastDate, currentDate)
sys.stdout = sys.__stdout__
return data[["Volume"]]
def find_anomalies(self, data, cutoff):
anomalies = []
data_std = np.std(data['Volume'])
data_mean = np.mean(data['Volume'])
anomaly_cut_off = data_std * cutoff
upper_limit = data_mean + anomaly_cut_off
indexs = data[data['Volume'] > upper_limit].index.tolist()
outliers = data[data['Volume'] > upper_limit].Volume.tolist()
index_clean = [str(x)[:-9] for x in indexs]
d = {'Dates': index_clean, 'Volume': outliers}
return d
def find_anomalies_two(self, data, cutoff):
indexs = []
outliers = []
data_std = np.std(data['Volume'])
data_mean = np.mean(data['Volume'])
anomaly_cut_off = data_std * cutoff
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:
indexs.append(str(data['Date'].iloc[i])[:-9])
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):
d1 = datetime.datetime.strptime(d1, "%Y-%m-%d")
d2 = datetime.datetime.strptime(d2, "%Y-%m-%d")
return abs((d2 - d1).days)
def main_func(self, cutoff):
StocksController = NasdaqController(True)
list_of_tickers = StocksController.getList()
currentDate = datetime.datetime.strptime(
date.today().strftime("%Y-%m-%d"), "%Y-%m-%d")
start_time = time.time()
for x in tqdm(list_of_tickers):
d = (self.find_anomalies_two(self.getData(x), cutoff))
if d['Dates']:
for i in range(len(d['Dates'])):
if self.days_between(str(currentDate)[:-9], str(d['Dates'][i])) <= 3:
self.customPrint(d, x)
print("\n\n\n\n--- this took %s seconds to run ---" %
(time.time() - start_time))
# input desired anomaly standard deviation cuttoff
# run time around 50 minutes for every single ticker.
mainObj().main_func(10)