def main(): var = '' #just want update to repeat not entire thing while var != 'null': company_name = raw_input( "Enter the name of the company you're searching for " ) #THis asks for a company name var = get_companysymbol( company_name ) #this searchs the company list we have and returns null if not found var = company_name if var != 'null': # ONLY PRECED IF WE HAVE A COMPANY timeBegin = int( input('Enter Start year ') ) - 2 #Gets the start date and pushes it back 2 years to show enough data to fit the screen nicely totalDataCurrent = Fetching.fetchDataToday( var, timeBegin ) # This gets all the data from the start year to 3 days ago (give or take a work day) googData = Fetching.fetchGoogData( var) # Fetch Todays data from google finance Prediction_Data = Fetching.fetchDataSpec( var, (datetime.now() + timedelta(days=-45)) ) # Get the data from just the past month for the prediciton part Prediction_Data_Length = len( Prediction_Data.High ) # Lenght of the predictin Data to save the recalc of it Coeffcients = LinearAlgebra.coeffcients_Generator( LinearAlgebra.makeXVals_Matrix(10, timeBegin, Prediction_Data_Length), LinearAlgebra.makeY_Matrix(Prediction_Data.High) ) #coeffcients for prediction fucntion a0-a10 Prediction_Model = LinearAlgebra.makeOutY( Coeffcients, Prediction_Data_Length, timeBegin, totalDataCurrent.High, googData ) # Gets Prediciton Model or scatter of predicted points these points are also normalized pointY = LinearAlgebra.getPointY( Coeffcients, timeBegin, googData[0]['LastTradePrice'], totalDataCurrent.High[len(totalDataCurrent) - 1] ) #gets Predictiion Point for the next day independently so I can calculate individual days R = RSI.PredictRSI(totalDataCurrent.Close) Graphing.totalTogether( var, totalDataCurrent, googData, Prediction_Model, pointY, R) #Print Final Graph with everything together pass
def getGraph(): stockName = request.args.get('s') var=stockName old_stdout = sys.stdout sys.stdout = open(os.devnull, "w") if var != 'null': # ONLY PRECEDE IF WE HAVE A COMPANY Coefficients = numpy.zeros(shape =(11,1)) timeBegin=2010 totalDataCurrent=Fetching.fetchDataToday(var,timeBegin) # This gets all the data from the start year to 3 days ago (give or take a work day) googData=Fetching.fetchGoogData(var) # Fetch Todays data from google finance Prediction_Data=Fetching.fetchDataSpec(var,(datetime.now()+timedelta(days=-45))) # Get the data from just the past month for the prediciton part Prediction_Data_Length=len(Prediction_Data.Close) # Lenght of the predictin Data to save the recalc of it #if Cache2.Search(var) ==0: Coefficients=LinearAlgebra.coefficients_Generator(LinearAlgebra.makeXVals_Matrix(10,timeBegin,Prediction_Data_Length),LinearAlgebra.makeY_Matrix(Prediction_Data.Low)) #coeffcients for prediction fucntion a0-a10 #Cache2.Cache_Predictions(var,Coefficients) # after calculating store the data in cache #else: #Coefficients = Cache2.Fetch_Cache(var) # fetch from cache if the company data is stored and it's recent ( less than 3 days from prediction) Prediction_Model=LinearAlgebra.makeOutY(Coefficients,Prediction_Data_Length,timeBegin,totalDataCurrent.High,googData) # Gets Prediciton Model or scatter of predicted points these points are also normalized pointY=LinearAlgebra.getPointY(Coefficients,timeBegin,googData[0]['LastTradePrice'],totalDataCurrent.High[len(totalDataCurrent)-1]) #gets Predictiion Point for the next day independently so I can calculate individual days R=RSI.PredictRSI(totalDataCurrent.Close) pointY=LinearAlgebra.getPointY(Coefficients,timeBegin,googData[0]['LastTradePrice'],totalDataCurrent.Low[len(totalDataCurrent)-1]) #gets Predictiion Point for the next day independently so I can calculate individual days url=Graphing.totalTogether(var,totalDataCurrent,googData,Prediction_Model,pointY,R) #Print Final Graph with everything together url = url + ".embed?width=640&height=480" ret = "<iframe width='640' height='480' frameborder='0' scrolling='no' src='" + url + "'> </iframe>" sys.stdout.close() sys.stdout = old_stdout return ret else: sys.stdout.close() sys.stdout = old_stdout return "false"
def analyze(stock_code, date): total_score = 0.0 total_score += BBI.analyze(stock_code, date, BBI_PARM_PERIOD_TRACE_FOR_HIGH, BBI_PARM_PERIOD_TRACE_HIS)[0] total_score += BIAS.analyze(stock_code, date)[0] total_score += BOLL.analyze(stock_code, date, BOLL_PARM_PERIOD)[0] total_score += BRAR.analyze(stock_code, date, BRAR_PARM_PERIOD_HIS)[0] total_score += CCI.analyze(stock_code, date, CCI_PARM_PERIOD)[0] total_score += DMA.analyze(stock_code, date, DMA_PARM_PERIOD)[0] total_score += DMI.analyze(stock_code, date, DMI_PARM_PERIOD)[0] total_score += KDJ.analyze(stock_code, date)[0] total_score += MA.analyze(stock_code, date, MA_PARM_PERIOD)[0] total_score += OBV.analyze(stock_code, date, OBV_PARM_PERIOD_TRACE_HIS)[0] total_score += PSY.analyze(stock_code, date)[0] total_score += ROC.analyze(stock_code, date, ROC_PARM_PERIOD_TRACE_HIS)[0] total_score += RSI.analyze(stock_code, date, RSI_PARM_PERIOD_TRACE_HIS)[0] total_score += VR.analyze(stock_code, date)[0] total_score += WR.analyze(stock_code, date, WR_PARM_PERIOD_TRACE_HIS)[0] bd.updateAnalysisData("TOTAL_SCORE", str(total_score), stock_code, date)
def latestScores(start_date, end_date, tickerSymbol): debug = 0 if debug == 1: print("start_date, end_Date, tickersymbol") print(start_date) print(end_date) print(tickerSymbol) url = "https://raw.githubusercontent.com/datasets/covid-19/master/data/time-series-19-covid-combined.csv" filename = url df = pd.read_csv(filename, lineterminator='\n') tdate = start_date sDates, sCloses = Ticker.Ticker(start_date, end_date, tickerSymbol) # Check if Ticker is Valid and if not use default tickersymbol validTicker = True if len(sDates) < 1: validTicker = False sDates, sCloses = Ticker.Ticker(start_date, end_date, defaultTS) scDates, tCases, scCloses = CV_Stats.CV_Stats(tdate, sDates, sCloses, df, end_date) sDates = scDates sCloses = scCloses sDates1 = pd.to_datetime(sDates) sDates1 = sDates1.strftime("%m-%d") score = Correlation.Correlate(sCloses, tCases) if debug == 1: print("sDates") print(sDates) score1 = RSI.computeRSI(sCloses, sDates, 10, 30) return score, score1, validTicker, sDates1, sCloses, tCases
def main(): p_f_st = 0 p_f_no = 0 p_f_k = 0 p_f_h = 0 #--------------------------------------------------------------------- #AntTreeアルゴリズムを用いた(株価データ)クラスタリングシステム #--STOCH -> 確率論的アルゴリズム #--NO_THRESHOLDS -> 決定論的アルゴリズム #---- Memo ----------------------------------------------------------- #--K-means法は比較用に作成 #--株価はyahoo! financeから2015年4/1~11/30までの営業日で取得 #--12/8追記 4/1~11/30(163次元)でメモリエラー発生 -> クラスタリングできていない #--1/12 4/1~11/30(163次元)できた!!(家PC) #--1/12 クラスタリング結果の評価関数(Pseudo_F.py)追加 #--------------------------------------------------------------------- # -*- coding:utf-8 -*- import datetime import pandas as pd from pandas import Series, DataFrame import matplotlib.pyplot as plt #自作モジュールのインポート import STOCH_StockPrice as st_sp import NO_THRESHOLDS_StockPrice as nt_sp import K_means_StockPrice as kmeans_sp import Hierarchy_clustering as h_c import cluster_Ant as cl_ant import Return_Index as r_index import RSI as rsi import Move_Average as m_ave import Pseudo_F as p_f #date 4/1~11/30まで #4/1 ~ 5/18 : 30日分 #4/1 ~ 6/29 : 60日分 #4/1 ~ 8/11 : 90日分 #4/1 ~ 9/27 : 120日分 start_date = datetime.date(2015,4,1) end_date = datetime.date(2015,9,27) #企業コードと企業名 companies_test = {9682:'DTS', 9742:'アイネス', 9613:'NTTデータ', 2327:'新日鉄住金ソリューションズ', 9640:'セゾン情報システムズ', 3626:'ITホールディングス', 2317:'システナ', 4684:'オービック', 9739:'NSW', 4726:'ソフトバンク・テクノロジー', 4307:'野村総合研究所', 9719:'SCSK', 4793:'富士通ビー・エス・シー', 4812:'ISID', 8056:'日本ユニシス'} companies_it = {9682:'DTS', 9742:'アイネス', 9613:'NTTデータ', 2327:'新日鉄住金ソリューションズ', 3626:'ITホールディングス', 2317:'システナ', 4684:'オービック', 9739:'NSW', 4726:'ソフトバンク・テクノロジー', 4307:'野村総合研究所', 9719:'SCSK', 4812:'ISID', 8056:'日本ユニシス', 3765:'ガンホー・オンライン・エンターテイメント', 4689:'ヤフー', 4716:'日本オラクル', 4739:'伊藤忠テクノソリューションズ', 4825:'ウェザーニュース', 7844:'マーベラス', 9432:'日本電信電話', 9433:'KDDI', 9437:'NTTドコモ', 9697:'カプコン', 9766:'コナミ', 9984:'ソフトバンクグループ', 9749:'富士ソフト', 3632:'グリー', 3656:'KLab'} #情報通信系 companies_food = {2001:'日本製粉', 2002:'日清製粉グループ本社', 2003:'日東富士製粉', 2109:'三井製糖', 2201:'森永製菓', 2206:'江崎グリコ', 2211:'不二家', 2212:'山崎製パン', 2217:'モロゾフ', 2220:'亀田製菓', 2229:'カルビー', 2264:'森永乳業', 2267:'ヤクルト本社', 2270:'雪印メグミルク', 2281:'プリマハム', 2282:'日本ハム', 2284:'伊藤ハム', 2288:'丸大食品', 2501:'サッポロホールディングス', 2502:'アサヒホールディングス', 2503:'キリンホールディングス', 2579:'コカ・コーラウエスト', 2593:'伊藤園'} #食料品 companies_retail = {2651:'ローソン', 2681:'ゲオホールディングス', 2730:'エディオン', 3048:'ビックカメラ', 3197:'すかいらーく', 3382:'セブン&アイホールディングス', 7550:'ゼンショーホールディングス', 7581:'サイゼリヤ', 7630:'壱番屋', 8028:'ファミリーマート', 8175:'ベスト電器', 8179:'ロイヤルホールディングス', 8200:'リンガーハット', 9831:'ヤマダ電器'} #小売 companies_device = {4902:'コニカミノルタ', 6448:'ブラザー工業', 6501:'日立製作所', 6502:'東芝', 6503:'三菱電機', 6506:'安川電機', 6516:'三洋電機', 6645:'オムロン', 6701:'NEC', 6702:'富士通', 6703:'OKI', 6724:'セイコーエプソン', 6752:'パナソニック', 6753:'シャープ', 6758:'ソニー', 6762:'TDK', 6981:'村田製作所', 7751:'キャノン', 7752:'リコー', 8035:'東京エレクトロン'} #電気機器 companies_service = {2193:'クックパッド', 2371:'カカクコム', 2379:'ディップ', 2433:'博報堂DIYホールディングス', 2432:'ディー・エヌ・エー', 4324:'電通', 4751:'サイバーエージェント', 4755:'楽天'} #サービス companies_mix = {9437:'NTTドコモ', 4307:'野村総合研究所', 2327:'新日鉄住金ソリューションズ', 8056:'日本ユニシス', 4812:'ISID', 2503:'キリンホールディングス', 2579:'コカ・コーラウエスト', 2288:'丸大食品', 2109:'三井製糖', 2730:'エディオン', 7550:'ゼンショーホールディングス', 7630:'壱番屋', 8175:'ベスト電器', 8179:'ロイヤルホールディングス', 6502:'東芝', 6702:'富士通', 8035:'東京エレクトロン', 6753:'シャープ', 2432:'ディー・エヌ・エー', 4324:'電通', 4751:'サイバーエージェント', 4755:'楽天', 2193:'クックパッド'} #混合 #↑printでの表示は工夫が必要... とりあえず使いたいのはkeyだけ companies = companies_service #クラスタリングする業種 #update → ディクショナリの連結 #companies.update(companies_food) #companies.update(companies_retail) #companies.update(companies_device) #companies.update(companies_service) #print 'companies: ',len(companies) #print 'term : '+str(start_date)+' -- '+str(end_date) #start_dateからend_dateまでの期間のリターンインデックスを計算,csvファイルで保存 #戻り値は営業日のdatetimeオブジェクト #dates = r_index.main(start_date, end_date, companies) #fname1 = 'return_index_values.csv' #fname2 = 'return_index_codes.csv' #start_dateからend_dateまでのRSIを計算,csvファイルで保存 #戻り値はdatetimeオブジェクト n = 10 #10日間でのRSI dates = rsi.main(start_date, end_date, n, companies) fname1 = 'RSI_values.csv' fname2 = 'RSI_codes.csv' #print 'dates : ', len(dates) #--- STOCH_StockPrice --- Ant, X, count = st_sp.main(0.99, 0.1, fname1, fname2) #引数1:alpha1, 引数2:alpha2 label = cl_ant.ant_label(Ant) tmp = [] label_max = max(label) #クラスタ数 for i in range(label_max+1): tmp.append({}) c = 0 #counter codes = companies.keys() #グラフ描画の準備 for i in label: for j in range(label_max+1): if j == i: tmp[i][codes[c]] = X[c] else : pass c = c+1 PseudoF = p_f.main(tmp, label_max+1) p_f_st = PseudoF k = label_max+1 #k-meansのクラスタ数 #--- NO_THRESHOLDS_StockPrice --- Ant, X, count = nt_sp.main(fname1, fname2) label = cl_ant.ant_label(Ant) tmp = [] label_max = max(label) #クラスタ数 for i in range(label_max+1): tmp.append({}) c = 0 #counter codes = companies.keys() #グラフ描画の準備 for i in label: for j in range(label_max+1): if j == i: tmp[i][codes[c]] = X[c] else : pass c = c+1 PseudoF = p_f.main(tmp, label_max+1) p_f_no = PseudoF #--- K-means --- label, X = kmeans_sp.main(fname1, k) #kはstochと同じに指定 tmp = [] label_max = max(label) for i in range(label_max+1): tmp.append({}) c = 0 codes = companies.keys() #グラフ描画の準備 for i in label: for j in range(label_max+1): if j == i: tmp[i][codes[c]] = X[c] else : pass c = c+1 PseudoF = p_f.main(tmp, label_max+1) p_f_k = PseudoF #--- 階層的クラスタリング--- #print 'Hierarchy Clustering' tmp = h_c.main(fname1, fname2, 'single') PseudoF = p_f.main(tmp, len(tmp)) p_f_h = PseudoF return p_f_st, p_f_no, p_f_k, p_f_h
def pairs(cusip1, cusip2, dt): cols = [ 'Stock1', 'Stock2', 'YYYYMM', 'SSD_P', 'MHD_P', 'Std2', 'RTN', 'CNT', 'DAYS', 'SSD_V', 'MHD_V', 'SSD_T', 'MHD_T', 'SSD_R', 'MHD_R' ] pos_pairs = pd.DataFrame(columns=cols) im_cols = [ 'DATE', 'YYYYMM', 'PRC', 'SHROUT', 'DIVAMT', 'OPENPRC', 'ASKHI', 'BIDLO', 'VOL' ] a = dt[im_cols][dt.CUSIP == cusip1] b = dt[im_cols][dt.CUSIP == cusip2] strt = max(min(a.DATE), min(b.DATE)) end = min(max(a.DATE), max(b.DATE)) a = a[(a.DATE >= strt) & (a.DATE <= end)] b = b[(b.DATE >= strt) & (b.DATE <= end)] a = a.sort_values(['DATE']).reset_index(drop=True) b = b.sort_values(['DATE']).reset_index(drop=True) a['RTN'] = daily_return(a) b['RTN'] = daily_return(b) a['Vola'] = Dua.VE(a) b['Vola'] = Dua.VE(b) a['TNOV'] = Tao.turnover(a) b['TNOV'] = Tao.turnover(b) a['RSI'] = Lau.get_rsi(a) b['RSI'] = Lau.get_rsi(b) #pdb.set_trace() merge_col = ['DATE', 'YYYYMM', 'PRC', 'RTN', 'Vola', 'VOL', 'TNOV', 'RSI'] c = pd.merge(a[merge_col], b[merge_col], on=['DATE', 'YYYYMM']) c = c.fillna(0) #pdb.set_trace() mt = sorted(list(set(c.YYYYMM))) for mn in mt[11:len(mt) - 6]: d = c[c.YYYYMM <= mn] e = c[(c.YYYYMM > mn) & (c.YYYYMM <= str(int(mn[0:4]) + int((6 + int(mn[4:6])) / 12)) + str((6 + int(mn[4:6])) % 12).zfill(2))] std2 = std_of_his_sprd(d.PRC_x, d.PRC_y) rtn, cnt, days = pair_trading(e, std2) pos_pairs = pos_pairs.append( { 'Stock1': cusip1, 'Stock2': cusip2, 'YYYYMM': mn, 'SSD_P': sum_sqr_diff_prc(d.PRC_x, d.PRC_y), 'MHD_P': sum_mah_diff_prc(d.PRC_x, d.PRC_y), 'Std2': std2, 'RTN': rtn, 'CNT': cnt, 'DAYS': days, 'SSD_V': Dua.ssd_v(d.Vola_x, d.Vola_y), 'MHD_V': Dua.smd_v(d.Vola_x, d.Vola_y), 'SSD_T': Tao.ssd_t(d.TNOV_x, d.TNOV_y), 'MHD_T': Tao.smd_t(d.TNOV_x, d.TNOV_y), 'SSD_R': Lau.get_sum_of_square_difference_of_rsi(d, d.RSI_x, d.RSI_y), 'MHD_R': Lau.get_maha_distance_of_rsi(d, d.RSI_x, d.RSI_y) }, ignore_index=True) return (pos_pairs)
''' Created on Jun 15, 2017 @author: michaelstecklein ''' import Database import RSI import StockData import matplotlib.pyplot as plt #Database.rsi_add_default_entries() rsis = RSI.get_RSIs(Database.get_stock("AAPL"), StockData.createSDate("2017-01-01"), StockData.createSDate("2017-06-22")) plt.plot(rsis) plt.plot((0, len(rsis)), (70, 70), 'r-') plt.plot((0, len(rsis)), (30, 30), 'g-') plt.ylabel('RSI') plt.show() print "\ndone"
import DataLoading import MACD import RSI f = open("idx.txt","r") fw = open("macd_histogram.csv","w") fw.write("Ticker,macd_histogram,rsi,volume\n") for ticker in f.readlines(): data = DataLoading.getData(ticker.strip(), 700) df = MACD.MACD(data) macd_histogram = df['MACD Histogram'].iat[-1] #if MACD > signal line then buy else sell macd = df['MACD'].iat[-1] > df['Signal Line'].iat[-1] df = RSI.RSI(data) rsi = df['rsi'].iat[-1] volume = df['Volume'].iat[-1] if(macd_histogram <= 2 and rsi <= 30 and macd): fw.write(ticker.strip() + "," + str(macd_histogram) + "," + str(rsi) + "," + str(volume) + "\n") #print(ticker.strip(), " = ", macd_histogram) f.close() fw.close()
#companies.update(companies_retail) #companies.update(companies_device) #companies.update(companies_service) print 'companies: ',len(companies) print 'term : '+str(start_date)+' -- '+str(end_date) #start_dateからend_dateまでの期間のリターンインデックスを計算,csvファイルで保存 #戻り値は営業日のdatetimeオブジェクト #dates = r_index.main(start_date, end_date, companies) #fname1 = 'return_index_values.csv' #fname2 = 'return_index_codes.csv' #start_dateからend_dateまでのRSIを計算,csvファイルで保存 #戻り値はdatetimeオブジェクト n = 10 #10日間でのRSI dates = rsi.main(start_date, end_date, n, companies) fname1 = 'RSI_values.csv' fname2 = 'RSI_codes.csv' print 'dates : ', len(dates) #--- STOCH_StockPrice --- Ant, X, count = st_sp.main(0.99, 0.1, fname1, fname2) #引数1:alpha1, 引数2:alpha2 label = cl_ant.ant_label(Ant) tmp = [] label_max = max(label) #クラスタ数 for i in range(label_max+1): tmp.append({}) c = 0 #counter
import RSI coin = input("Input coin>>") RSI.getRSI() print("The RSI of",coin,"is",RSI) if RSI>80: print("You should sell") elif RSI<20: print("You should buy")
import Datasheet_Download import preprocessing import RSI toDownload = input("Do you want to downlaod the historical data (y/n) :") toContinue = "y" if (toDownload.lower() == "y"): Datasheet_Download.Download_Dataheet() toContinue = input("Do you want to continue(y/n) : ") if (toContinue == "y"): preprocessing.preprocess() RSI.Execute()
def run(self): # 把要执行的代码写到run函数里面 线程在创建后会直接运行run函数 print("Starting " + self.name) print "Exiting2 " + self.name while True: code = 'sh.000001' today = datetime.datetime.now().strftime('%Y%m%d') startdate = '2017-01-01' enddate = f'{today[0:4]}-{today[4:6]}-{today[6:8]}' # 获取当前时间 now_time = datetime.datetime.now() period = 1 Vol = vol.computeVOL(code, startdate, enddate, period) print(Vol[-1]) # rsi_6days = rsi.computeRSI(code, startdate, enddate) # print(rsi_6days[-1]) # imnow = time.strftime("%Y-%m-%d %H:%M:%S") # print(imnow) # time.sleep(1) # print(now_time.time().hour) # print(now_time.time().minute)# 创建新线程 print "Exiting Main Thread" # print(now_time.time().second) # print(now_time.time().hour+":"+now_time.time().second +":"+now_time.time().second()) # 系统时间XX点XX分XX秒,触发策略启动 if now_time.time().hour == 9 and now_time.time( ).minute == 37 and now_time.time().second == 00: #print('启动KDJ策略') # ##########KDJ策略体########### # #enddate不是交易日,测试会出现接收数据异常 list index out of range # # J = kdj.computeKDJ(code, startdate, enddate) # # 买入测试日期 # #J = kdj.computeKDJ(code, startdate,'2019-09-26') # #卖出测试日期 # J = kdj.computeKDJ(code, startdate,'2019-08-21') # print(J[-1]) # # if J[-1] < 0: # # work.button_start(QWidget) # print('启动买入策略') # autoTrade.buy(159928) # # print("交易成功") # ###################### # if J[-1] > 100: # # work.button_start(QWidget) # print('启动卖出策略') # autoTrade.sell(159928) # # # print("交易成功") # ###################### print('启动RSI策略') ##########RSI策略体########### # enddate不是交易日,测试会出现接收数据异常 list index out of range #rsi_6days = rsi.computeRSI(code, startdate, enddate) # 买入测试日期 # J = rsi.computeRSI(code, startdate,'2019-09-26') # 卖出测试日期 rsi_6days = rsi.computeRSI(code, startdate, '2019-09-09') print(rsi_6days[-1]) if rsi_6days[-1] < 20: # work.button_start(QWidget) print('启动买入策略') autoTrade.buy(159928) print("交易成功") ###################### if rsi_6days[-1] > 80: # work.button_start(QWidget) print('启动卖出策略') autoTrade.sell(159928) print("交易成功")
def update_indicators(): RSI.update_RSI()