init
This commit is contained in:
176
screener.py
Normal file
176
screener.py
Normal file
@@ -0,0 +1,176 @@
|
||||
from pandas_datareader import data as pdr
|
||||
import json
|
||||
from yahoo_fin import stock_info as si
|
||||
from pandas import ExcelWriter
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
import yfinance as yf
|
||||
import pandas as pd
|
||||
import requests
|
||||
import datetime
|
||||
import time
|
||||
import sys
|
||||
|
||||
|
||||
def roc11(closes):
|
||||
print(len(closes))
|
||||
roc11_val = []
|
||||
roc14_val = []
|
||||
roc_sum = []
|
||||
x = len(closes)
|
||||
for i in range(x):
|
||||
if i+11 == x: # Fixed
|
||||
break
|
||||
# cur_indx = x - i
|
||||
# temp_11 = (closes[cur_indx] - closes[cur_indx-11])/closes[cur_indx-11]*100
|
||||
temp_11 = (closes[i+11] - closes[i])/closes[i]*100
|
||||
roc11_val.append(temp_11)
|
||||
|
||||
for i in range(x):
|
||||
if i+14 == x: # Fixed
|
||||
break
|
||||
temp_14 = (closes[i+14] - closes[i])/closes[i]*100
|
||||
roc14_val.append(temp_14)
|
||||
|
||||
for i in range(len(roc14_val)):
|
||||
roc_sum.append(roc11_val[i+3]+roc14_val[i])
|
||||
print("Finished ")
|
||||
return roc_sum
|
||||
|
||||
def wma10(roc_sum, n=10):
|
||||
roc_sum = pd.DataFrame(roc_sum, columns=['COPPOCK'])
|
||||
weights = np.arange(1, n+1)
|
||||
wmas = roc_sum.rolling(n).apply(lambda x:np.dot(x, weights)/weights.sum(), raw=True)
|
||||
|
||||
print(wmas)
|
||||
return wmas
|
||||
|
||||
|
||||
yf.pdr_override()
|
||||
|
||||
# stocklist = si.tickers_sp500()
|
||||
stock_file = open("./symbols_backup.txt", "r")
|
||||
stocklist = stock_file.readlines()
|
||||
stock_file.close()
|
||||
index_name = '^GSPC' # S&P 500
|
||||
|
||||
final = []
|
||||
index = []
|
||||
n = -1
|
||||
f = open("./Samples/SHOP/2020-01-22/SHOP0.vezpal2")
|
||||
ticker = sys.argv[1]
|
||||
path='./Samples/'+ticker+'/2021-01-30/'+ticker+'0.vezpal2'
|
||||
path = open(path)
|
||||
a = json.load(path)
|
||||
|
||||
pred_stock = a[0]
|
||||
|
||||
|
||||
|
||||
n += 1
|
||||
time.sleep(1)
|
||||
|
||||
|
||||
# RS_Rating
|
||||
start_date = datetime.datetime.now() - datetime.timedelta(days=365)
|
||||
end_date = datetime.date.today()
|
||||
#
|
||||
# df = pdr.get_data_yahoo(stock, start=start_date, end=end_date)
|
||||
# df['Percent Change'] = df['Adj Close'].pct_change()
|
||||
# stock_return = df['Percent Change'].sum() * 100
|
||||
#
|
||||
# index_df = pdr.get_data_yahoo(index_name, start=start_date, end=end_date)
|
||||
# index_df['Percent Change'] = index_df['Adj Close'].pct_change()
|
||||
# index_return = index_df['Percent Change'].sum() * 100
|
||||
#
|
||||
# RS_Rating = round((stock_return / index_return) * 10, 2)
|
||||
#
|
||||
# sma = [50, 150, 200]
|
||||
# for x in sma:
|
||||
# df["SMA_"+str(x)] = round(df.iloc[:,4].rolling(window=x).mean(), 2)
|
||||
|
||||
# currentClose = df["Adj Close"][-1]
|
||||
# moving_average_50 = df["SMA_50"][-1]
|
||||
# moving_average_150 = df["SMA_150"][-1]
|
||||
# moving_average_200 = df["SMA_200"][-1]
|
||||
# low_of_52week = min(df["Adj Close"][-260:])
|
||||
# high_of_52week = max(df["Adj Close"][-260:])
|
||||
# closePrice = df["Close"]
|
||||
closePrice = pred_stock
|
||||
# print(closePrice.head())
|
||||
|
||||
roc_res = roc11(closePrice)
|
||||
wma_res = wma10(roc_res)
|
||||
print("==========")
|
||||
print(len(wma_res));
|
||||
print(len(closePrice));
|
||||
print(closePrice.index)
|
||||
entry_buy = []
|
||||
entry_sell= []
|
||||
entry_date = []
|
||||
|
||||
# for i in range(len(wma_res)):
|
||||
# # mark_1 = sum(wma_res["COPPOCK"][i:i+3])
|
||||
# if ( i+6 < len(wma_res)):
|
||||
# mark_1 = wma_res["COPPOCK"][i]
|
||||
# mark_2 = wma_res["COPPOCK"][i+3]
|
||||
# mark_3 = wma_res["COPPOCK"][i+6]
|
||||
# if (mark_2 < mark_1 and mark_2 < mark_3):
|
||||
# # entry_date.append(closePrice.index[-237:][i])
|
||||
# entry_buy.append(i)
|
||||
# print("Down ",i)
|
||||
# # print(entry_date)
|
||||
#
|
||||
# if (mark_2 > mark_1 and mark_2 > mark_3):
|
||||
# # entry_date.append(closePrice.index[-237:][i])
|
||||
# entry_sell.append(i)
|
||||
# print("Up ",i)
|
||||
# # print(entry_date)
|
||||
|
||||
# i = 0
|
||||
for i in range(len(wma_res)):
|
||||
# mark_1 = sum(wma_res["COPPOCK"][i:i+3])
|
||||
if ( i+15 < len(wma_res)):
|
||||
mark_1 = wma_res["COPPOCK"][i]
|
||||
mark_2 = wma_res["COPPOCK"][i+8]
|
||||
mark_3 = wma_res["COPPOCK"][i+15]
|
||||
|
||||
mark_h = wma_res["COPPOCK"][i+10]
|
||||
|
||||
if ((mark_2*3 < mark_1) and ( mark_2*3 < mark_3 ) and ( mark_2 > mark_h )):
|
||||
# entry_date.append(closePrice.index[-237:][i])
|
||||
entry_buy.append(i+8)
|
||||
print("Down ",i," ",mark_2)
|
||||
# print(entry_date)
|
||||
|
||||
if (mark_2 > mark_1 and mark_2 > mark_3 and mark_2 < mark_h):
|
||||
# entry_date.append(closePrice.index[-237:][i])
|
||||
entry_sell.append(i+8)
|
||||
print("Up ",i," ",mark_2)
|
||||
# print(entry_date)
|
||||
# i = i + 7
|
||||
# print(i)
|
||||
|
||||
# wma_res['Date'] = closePrice.index[-237:]
|
||||
ax1 = plt.subplot(211)
|
||||
plt.title("COPPOCK Indicator "+str(ticker))
|
||||
# for i in entry_points:
|
||||
# plt.vlines(x=closePrice[-237:][i], ymin=0, ymax=max(closePrice))
|
||||
plt.plot(closePrice, markevery=entry_buy, marker="^", ms=4, mfc="y", linewidth=1)
|
||||
plt.plot(closePrice, markevery=entry_sell, marker="v", ms=4, mfc="r", linewidth=1)
|
||||
|
||||
ax2 = plt.subplot(212, sharex=ax1)
|
||||
# plt.axhline(y=0, color='r')
|
||||
# plt.plot(closePrice[-len(wma_res):], wma_res, markevery=entry_sell, marker="o")
|
||||
plt.plot(range(len(wma_res)), wma_res, markevery=entry_sell, marker="o", mfc='r' )
|
||||
plt.plot(range(len(wma_res)), wma_res, markevery=entry_buy, marker="+", mfc='g', linewidth=0.1)
|
||||
plt.show()
|
||||
plt.savefig("./screening_result/pred.png")
|
||||
# plt.savefig("./screening_result/"+str(stock)+".png", dpi=1200)
|
||||
plt.close()
|
||||
|
||||
|
||||
|
||||
# writer = ExcelWriter("ScreenOutput.xlsx")
|
||||
# exportList.to_excel(writer, "Sheet1")
|
||||
# writer.save()
|
||||
Reference in New Issue
Block a user