-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhigh.py
More file actions
65 lines (56 loc) · 2.56 KB
/
high.py
File metadata and controls
65 lines (56 loc) · 2.56 KB
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
import pandas as pd
import numpy as np
import stockinator as st
offset = 0
df = pd.read_pickle('stocks/all_1d.pkl')
df_year = df.iloc[-250:]
df_diff = df.dropna(thresh=600).diff()
tickers = st.unique_list([x[0] for x in df_year.columns.values])
data = {}
for t in tickers:
fifty_avg = np.around(df_year[t][-50:].mean(), decimals=2),
two_hundred_avg = np.around(df_year[t][-200:].mean(), decimals=2)
last_price = df_year[t]['Close'].iloc[-1]
if last_price > 20:
data[t] = {
'52High': np.around(df_year[t]['High'].max(), decimals=2),
'Last': np.around(df_year[t]['Close'].iloc[-1], decimals=2),
'10d': np.around(
(df_year[t]['Close'].iloc[-1] - df_year[t]['Close'].iloc[-10]) / df_year[t]['Close'].iloc[-10],
decimals=2),
'1mo': np.around(
(df_year[t]['Close'].iloc[-1] - df_year[t]['Close'].iloc[-21]) / df_year[t]['Close'].iloc[-21],
decimals=2),
'2mo': np.around(
(df_year[t]['Close'].iloc[-1] - df_year[t]['Close'].iloc[-42]) / df_year[t]['Close'].iloc[-42],
decimals=2),
'3mo': np.around(
(df_year[t]['Close'].iloc[-1] - df_year[t]['Close'].iloc[-63]) / df_year[t]['Close'].iloc[-63],
decimals=2),
'6mo': np.around(
(df_year[t]['Close'].iloc[-1] - df_year[t]['Close'].iloc[-126]) / df_year[t]['Close'].iloc[-126],
decimals=2),
'1yr': np.around(
(df_year[t]['Close'].iloc[-1] - df_year[t]['Close'].iloc[0]) / df_year[t]['Close'].iloc[0],
decimals=2),
'50': np.around(df_year[t][-50:]['Close'].mean(), decimals=2),
'200': np.around(df_year[t][-200:]['Close'].mean(), decimals=2),
}
df_highs = pd.DataFrame.from_dict(data, orient='index').dropna(how='all')
df_highs['PctHigh'] = df_highs['Last']/df_highs['52High']
df_highs = df_highs.dropna(how='any')
df_highs = df_highs[df_highs['Last'].gt(20)]
df_highs = df_highs[df_highs['6mo'].gt(0)]
df_highs = df_highs[df_highs['1mo'].gt(0)]
df_quantile = df_highs['PctHigh'].quantile(q=[.5])
#print(df_quantile)
is_buy = df_highs['PctHigh'].gt(df_quantile.loc[0.5])
is_sell = df_highs['PctHigh'].lt(df_quantile.loc[0.5])
df_buy = df_highs[is_buy]
df_sell = df_highs[is_sell]
print('High Performing Stocks')
print(df_buy.sort_values('PctHigh', ascending=False))
df_buy.to_pickle('stocks/buy.pkl')
#print("Low Performaing Stocks")
#print(df_sell.sort_values('PctHigh', ascending=True))
df_sell.to_pickle('stocks/sell.pkl')