def getStockInfoFromMsg(targetStockMsg): tempStockNumber = int(filter(str.isdigit, targetStockMsg)) #取得要查詢的目標股票號碼 tempAskString = "系統接收到使用者請求對股票資訊的查詢 股票查詢代碼為" + str( tempStockNumber) + NEWLINE #Debug Log紀錄 stock = Stock(str(tempStockNumber)) #擷取特定股價相關資訊 tempMovingAverageString = "計算五日均價與持續天數" + str( stock.moving_average(5)) + NEWLINE tempMovingAverageValueString = "計算五日均量與持續天數" + str( stock.moving_average_value(5)) + NEWLINE tempMovingAverageBiasRatio = "計算五日、十日乖離值與持續天數" + str( stock.moving_average_bias_ratio(5, 10)) + NEWLINE return tempAskString + tempMovingAverageString + tempMovingAverageValueString + tempMovingAverageBiasRatio + STOCKRIGHT
def DBuild(): global remove_obj_list, obj_list, dead obj_list = diff(obj_list, remove_obj_list) remove_obj_list = [] if len(obj_list) > 0: for i in obj_list: dead = i remove_obj_list.append(i) stock = Stock(i, 24) file_locate = "/Users/Mac/Desktop/Stock Research/DataBase_test/" + i + ".csv" stock.out_putfile(file_locate) dead = 0 print "Yeah! F**k you finished!"
def DBuild(): global remove_obj_list, obj_list, month if len(obj_list) > 0: for i in obj_list: try: stock = Stock(i, month) file_locate = "/Users/Mac/Desktop/Stock Research/DataBase_test/" + i + ".csv" stock.out_putfile(file_locate) except AttributeError: print "Error >>", i, "can not fetch month = ", month else: print "Get stock no.", i, "info for", month, "month" remove_obj_list.append(i) print "Month = ", month, " : Fetching finished!" month = month - 12 #
def getRealtimeStock(stock_no): realtime = {True: RealtimeTWSE, False: RealtimeOTC} try: stock = Stock(stock_no) info = realtime[stock._twse](stock_no) data = info.data[stock_no] return (stock_no, data['info']['name'], data['price']) except Exception, ex: print("Fail to get realtime information of '{}'".format(stock_no)) print("> " + ex.message) return None
#!/usr/bin/env python # -*- coding: utf-8 -*- from grs import Stock from grs import TWSENo import sys sys.path.append('../') from MACD import MACD stock = Stock('2330', 14) macd = MACD(stock) print "MACD: len: " + str(len(macd.macd)) print macd.macd print "MACDSignal: len: " + str(len(macd.macdsignal)) print macd.macdsignal print "MACDhist: len: " + str(len(macd.macdhist)) print macd.macdhist print "Conti Day:" print macd.contiDay
# -*- coding: utf-8 -*- from grs import Stock from grs import TWSENo import sys sys.path.append('./src') from Rule import Rule from Trade import Trade # get all stock no list stock_no_list = [i for i in sorted(TWSENo().all_stock_no)[:-2] ] print 'Total stock: '+str(len(stock_no_list)) # parameters contiDay_thd = 3 day = -1; # process for i in stock_no_list: try: stock = Stock(i) rule = Rule(stock) if rule.buyRule1(-1): print stock.info[0], print "Buy" except: print i print "[Error] "+i+" "+stock.info[0]
import grs from grs import TWSENo from grs import Stock twse_no = TWSENo() for i in twse_no.all_stock_no: if len(i) <= 4 : stock = Stock(i, 12) file_locate = "/Users/Mac/Desktop/Stock Research/DataBase2/" + i + ".csv" stock.out_putfile(file_locate)
from grs import Stock stock = Stock('2454') #get the price for Stock number 2618 print stock.moving_average(5) print stock.moving_average_value(5) print stock.moving_average_bias_ratio(5, 10) stock = Stock('2454', 12) stock.out_putfile('2454.csv')
# Stock('1101').raw[-1][-2] 漲跌點數 # Stock('1101').raw[-1][4] 昨天收盤價 # float(Stock('1101').raw[-1][-2]) / float(Stock('1101').raw[-1][4]) * today_weight * float(i[2]) / 100 #for i in a: # print i[0],i[1],i[2] fileopen.write( "各股加權貢獻依據2016年03月31日下的權重計算".decode('utf8').encode('big5-hkscs') + "\n") total = 0 for i in a: contribution = 0 try: contribution = float(Stock(i[0]).raw[-1][-2]) / float( Stock(i[0]).raw[-1][-3]) * today_weight * float(i[2]) / 100 except: pass #抓到非數字跳過 total = total + contribution print i[0], i[1], Stock(i[0]).raw[-1][-3], Stock( i[0]).raw[-1][-2], "%.2f" % (contribution), "%.2f" % ( total), today_weight, i[2] # fileopen.write(str(i[0])+','+str(i[1]).decode('utf8').encode('big5-hkscs')+','+str(contribution)+','+ str(total)+"\n") # fileopen.write(str(i[0])+', '+str(i[1]).decode('utf8').encode('big5-hkscs')+', '+"%.2f"%(contribution)+', '+"%.2f"%(total)+"\n") fileopen.write( str(i[0]) + ', ' + str(i[1]).decode('utf8').encode('big5-hkscs') + ', ' + "%.2f" %
def main_process(stock_number): global PIECE global sim_day global cdate1 global cdate2 global cdate3 global sdate1 global sdate2 global sdate3 cdate1 = datetime.date.today() cdate2 = datetime.date.today() cdate3 = datetime.date.today() sdate1 = '0' sdate2 = '0' sdate3 = '0' stock_wis = Stock(stock_number) #// 3. create folder for save csv data mkpath="/home/pi/Hades_project/csv_repository" mkdir(mkpath) get_date = datetime.date.today() get_sdate = str(get_date) csv_file = "/home/pi/Hades_project/csv_repository/" + stock_number + '_' + get_sdate + '.csv' stock_wis.out_putfile(csv_file) #// 3.1 create share of stock folder mkpath="/home/pi/Hades_project/share_stock" mkdir(mkpath) #// 3.2 record share of stock file filepath = '/home/pi/Hades_project/share_stock/' + stock_number + '.txt' isExists = os.path.exists(filepath) if not isExists: fd = open(filepath, 'w') piece_num = '0' fd.write(piece_num) fd.close PIECE = '0' print filepath+' create ok!!' else: #print filepath+' folder already exist!!' fd = open(filepath, 'r') PIECE = fd.read() fd.close PIECE = PIECE.rstrip('\n') #// 4. filter highest, lower, end, start of value and show thos value # http://swaywang.blogspot.tw/2012/05/pythoncsv.html # http://www.lfhacks.com/tech/python-read-specific-column-csv #//--------------------------------- #// get time function #//--------------------------------- #sdate1 = strftime("%D") #ssdate1 = sdate1[0:5] #cdate1 = datetime.date.today() + (datetime.timedelta(days=sim_day)) #cdate1 = datetime.date.today() + (datetime.timedelta(days=-4)) #cdate2 = cdate1 + (datetime.timedelta(days=-1)) #cdate3 = cdate1 + (datetime.timedelta(days=-2)) #//current date,6/20 check_done = 0 delta_day = 0 while(check_done == 0): cdate1 = datetime.date.today() + (datetime.timedelta(days=delta_day)) sdate1 = str(cdate1) sdate1 = sdate1[5:10] sdate1 = sdate1.replace('-','/') res = test_day_avaliable(sdate1, csv_file) if(res == 0): #print sdate1 #print "find a right day1!" check_done = 1 #print "---------------" else: #print "can't find match day1!" delta_day = (delta_day - 1) #//yesterday,6/19 check_done = 0 delta_day = -1 while(check_done == 0): cdate2 = cdate1 + (datetime.timedelta(days=delta_day)) sdate2 = str(cdate2) sdate2 = sdate2[5:10] sdate2 = sdate2.replace('-','/') res = test_day_avaliable(sdate2, csv_file) if(res == 0): #print sdate2 #print "find a right day2!" check_done = 1 #print "---------------" else: #print "can't find match day2!" delta_day = (delta_day - 1) #//the day before yesterday,6/18 check_done = 0 delta_day = -1 while(check_done == 0): cdate3 = cdate2 + (datetime.timedelta(days=delta_day)) sdate3 = str(cdate3) sdate3 = sdate3[5:10] sdate3 = sdate3.replace('-','/') res = test_day_avaliable(sdate3, csv_file) if(res == 0): #print sdate3 #print "find a right day3!" check_done = 1 #print "---------------" else: #print "can't find match day3!" delta_day = (delta_day - 1) #//--------------------------------- #//find the specific date data #//--------------------------------- #print sdate1 res1 = gen_target_file(sdate1, csv_file, '/home/pi/Hades_project/csv_repository/target_sdate1_val.txt') if(res1 == 0): #//day1,current date #// 4.1 hishest value day1_h = search_s_e_h_l_fun('highest_val', '/home/pi/Hades_project/csv_repository/target_sdate1_val.txt') #// 4.2 lower value day1_l = search_s_e_h_l_fun('lowest_val', '/home/pi/Hades_project/csv_repository/target_sdate1_val.txt') #// 4.3 end value day1_e = search_s_e_h_l_fun('end_val', '/home/pi/Hades_project/csv_repository/target_sdate1_val.txt') #// 4.4 start value day1_s = search_s_e_h_l_fun('start_val', '/home/pi/Hades_project/csv_repository/target_sdate1_val.txt') #print 'h:'+day1_h #print 'l:'+day1_l #print 'e:'+day1_e #print 's:'+day1_s clean_garbage_file('/home/pi/Hades_project/csv_repository/target_sdate1_val.txt') else: print "Error :day1 not avaible~" return 1 #print "---------------" #print sdate2 res2 = gen_target_file(sdate2, csv_file, '/home/pi/Hades_project/csv_repository/target_sdate2_val.txt') #//day2,yesterday if(res2 == 0): day2_h = search_s_e_h_l_fun('highest_val', '/home/pi/Hades_project/csv_repository/target_sdate2_val.txt') day2_l = search_s_e_h_l_fun('lowest_val', '/home/pi/Hades_project/csv_repository/target_sdate2_val.txt') day2_e = search_s_e_h_l_fun('end_val', '/home/pi/Hades_project/csv_repository/target_sdate2_val.txt') day2_s = search_s_e_h_l_fun('start_val', '/home/pi/Hades_project/csv_repository/target_sdate2_val.txt') #print 'h:'+day2_h #print 'l:'+day2_l #print 'e:'+day2_e #print 's:'+day2_s clean_garbage_file('/home/pi/Hades_project/csv_repository/target_sdate2_val.txt') else: print "Error :day2 not avaible~" return 1 #print "---------------" #print sdate3 res3 = gen_target_file(sdate3, csv_file, '/home/pi/Hades_project/csv_repository/target_sdate3_val.txt') #//day3,the day before yesterday if(res3 == 0): day3_h = search_s_e_h_l_fun('highest_val', '/home/pi/Hades_project/csv_repository/target_sdate3_val.txt') day3_l = search_s_e_h_l_fun('lowest_val', '/home/pi/Hades_project/csv_repository/target_sdate3_val.txt') day3_e = search_s_e_h_l_fun('end_val', '/home/pi/Hades_project/csv_repository/target_sdate3_val.txt') day3_s = search_s_e_h_l_fun('start_val', '/home/pi/Hades_project/csv_repository/target_sdate3_val.txt') #print 'h:'+day3_h #print 'l:'+day3_l #print 'e:'+day3_e #print 's:'+day3_s clean_garbage_file('/home/pi/Hades_project/csv_repository/target_sdate3_val.txt') else: print "Error :day3 not avaible~" return 1 #print "---------------" #// 5. algorithm to compare value bigger or lower #// 5.3 if you have share of stock, if you have 1 PIECE go to sale session, #// otherwise go to buy session if(PIECE == '0'): #// share of stock is none #// 5.1 three day detection algorithm, buy in #// 5.4 stage = 1 -> buy, 2 -> do not buy print "+++++ BUY Session ++++" if(day1_h > day3_h): print (sdate1+" is high than "+sdate3+", BUY!") stage = 1 print "buy in share of 1000" PIECE = 1000 fd = open(filepath, 'w') piece_num = '1000' fd.write(piece_num) fd.close record_trade_price(stock_number, day1_h, 'buy') else: print (sdate1+" is high than "+sdate3+", DO NOT BUY!") stage = 2 elif(PIECE == '1000'): #// share of stock is 1 #// 5.2 three day detection algorithm, sale out #// 5.4 stage = 3 ->sale out, 4 -> keep print "+++++ SALE Session ++++" if(day1_e < day3_e): print (sdate1+" is lower than "+sdate3+", SALE OUT!") stage = 3 print "sale out share of 1000" PIECE = 0 fd = open(filepath, 'w') piece_num = '0' fd.write(piece_num) fd.close record_trade_price(stock_number, day1_e, 'sale') else: print sdate1+" is higher or equal than "+sdate3+", KEEP!" stage = 4 else: print "something wrong about share of stock..." #// 9. CPU record cpu_tempertrue_value = get_cpu_temp() #cpu_tempertrue_value = str(40) write_cpu_temp_file(cpu_tempertrue_value) cpu_tempertrue_value = "now CPU " + str(cpu_tempertrue_value) today_date = datetime.date.today() today_date = str(today_date) #// 5.5 use a switch to give different msg to "result_message" for mail if(stage == 1): result_message = today_date+" "+stock_number+" \nmachine say: "+" BUY!!\n"+"today final price: "+day1_e+"\n"+cpu_tempertrue_value elif(stage == 2): result_message = today_date+" "+stock_number+" \nmachine say: "+" DO NOT BUY!!\n"+"today final price: "+day1_e+"\n"+cpu_tempertrue_value elif(stage == 3): result_message = today_date+" "+stock_number+" \nmachine say: "+" SALE OUT!!\n"+"today final price: "+day1_e+"\n"+cpu_tempertrue_value elif(stage == 4): result_message = today_date+" "+stock_number+" \nmachine say: "+" KEEP!!\n"+"today final price: "+day1_e+"\n"+cpu_tempertrue_value else: print "stage going wrong..." #// 6. send mail to notice user #// issue: cc wouldn't work! #// to_addr_list = ['*****@*****.**','*****@*****.**'] #if(DEBUG == TURN_OFF): if(DEBUG_MAIL == TURN_ON): #sendemail(from_addr = '*****@*****.**', # to_addr_list = ['*****@*****.**'], # cc_addr_list = [''], # subject = 'Make money machine letter', # message = result_message, # login = '******', # password = '******') deal_result_message = result_message #//read trade record file file_name = '/home/pi/Hades_project/trade_record/' + stock_number + '_tr_price.txt' tr_file = open(file_name, 'r') trade_result_message = tr_file.read() result_message = deal_result_message + '\n' + trade_result_message tr_file.close() write_mail_msg(result_message) between_line = "\n--------------------------------------------------------\n" write_mail_msg(between_line)
f = open('/home/tim/GMAIL.txt', 'r') #於前一個相對目錄中放置登入GMAIL帳號密碼,目的為了不再GitHub顯示出來. ID = f.readline().strip('\n') #不包含換行符號\n:q PW = f.readline().strip('\n') fileopen.write('上市公司股票篩選\n\n\n') #fileopen.write("股票",",","100",",","倍週均量",",","成交張數",",","殖利率","100",",","倍月均量") #===================== for i in stock_no_list: #print i realtime_data = RealtimeTWSE(i) try: if (realtime_data.data[i]['volume_acc'] > Stock( i, mons=3).moving_average_value(5)[0][-2] or realtime_data.data[i]['volume_acc'] > Stock( i, mons=3).moving_average_value(20)[0][-2] ) and float( realtime_data.data[i]['diff'][1]) > 0: #今天的量大於5日週或月均量, 目前要漲 print i, 'TWSE123' #暴量長紅2天 close_price = Stock(i).raw[-1][6] MA5 = Stock(i, mons=2).moving_average(5)[0][-1] MA10 = Stock(i, mons=2).moving_average(10)[0][-1] MA20 = Stock(i, mons=2).moving_average(20)[0][-1] Bias5 = (close_price - MA5) / MA5 * 100 Bias10 = (close_price - MA10) / MA10 * 100 Bias20 = (close_price - MA20) / MA20 * 100 turnover_ration = float(Stock(i).raw[-1][1] / 1000) / ( float(rank_market_value()[i][3]) * 10000) * 100
from grs import Stock stock = Stock('2618') print stock.moving_average(0) print stock.moving_average_value(1) print stock.moving_average_bias_ratio(1, 3)
#!/usr/bin/env python # -*- coding: utf-8 -*- from grs import Stock import matplotlib.pyplot as plt import sys sys.path.append('../') from MA import MA stock = Stock.createByCSV('2330', '../../data/2330_14MonData.csv') ma = MA(stock) print "MA5=> len: " + str(len(ma.ma5)) print ma.ma5 print "MA10=> len: " + str(len(ma.ma10)) print ma.ma10 print "MA20=> len: " + str(len(ma.ma20)) print ma.ma20 print "MA5 diff MA10:" + str(len(ma.ma5diffma10)) ma.printMA5DiffMA10() #print "Buy point short term:" #ma.printBuyPoints_ST() #print "MA5 gradient=> len: "+str(len(ma.ma5_gradient)) #ma.printMA5_gradient() #plt.figure(1) #plt.subplot(211) #plt.plot(stock.price, 'r', ma.ma5, 'b', ma.ma10, 'yellow')
db='news-set', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor) com_list =['1101','1102','1216','1301','1303','1326', '1402','1722','2002','2105','2201','2207', '2301','2303','2311','2317','2324','2325', '2330','2347','2353','2354','2357','2382', '2409','2412','2454','2474','2498','2801', '2880','2881','2882','2883','2885','2886', '2890','2891','2892','2912','3008','3045', '3231','3481','3673','4904','4938','5880','6505'] try: with connection.cursor() as cursor: for com in com_list: com = com.split('+')[0] stock = Stock(com,30) stock.out_putfile(com+'[30-month].csv') with open(com+'[30-month].csv', 'rb') as csvfile: info = list() spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|') for r in spamreader: if len(r[0].split(','))>=9: info.append(r[0].split(',')) for r2 in info: r2[0] = str(int(r2[0].split('/')[0])+1911) + '-' +r2[0].split('/')[1]+'-'+r2[0].split('/')[2] for i in range(len(info)): today_open = float(info[i][3]) today_close = float(info[i][6]) if i-1 >=0 : pre_close = float(info[i-1][6]) else:
def WorkerJob(stock_index, days): print('# index {}'.format(stock_index)) stock = Stock(stock_index, days)
def WorkerJob(stock_index, days): print('# index {}'.format(stock_index)) stock = Stock(stock_index, days) #stock.out_putfile('data/{}.csv'.format(stock_index)) """ with ThreadPoolExecutor(max_workers=10) as executor: for i in sorted(stock_no_list): if len(i) != 4: continue #stock = Stock(i, 12 * 30) #stock = Stock(i, 3 * 1) #stock.out_putfile('data/{}.csv'.format(i)) print(type(i)) a = executor.submit(WorkerJob, i, 3) #threading.Thread(target = WorkerJob, args = (i, 3)).start() print(a.result()) """ for i in sorted(stock_no_list): if len(i) != 4: continue print('[compute] adgroup {}'.format(i)) #stock = Stock(i, 12 * 30) stock = Stock(i, 1) stock.out_putfile( '/Users/coha/git/time-series-predictor/data/{}.csv'.format(i))
fileopen = open(attachment, 'w') #開啟檔案,w沒有該檔案就新增 f = open('/home/tim/GMAIL.txt', 'r') #於前一個相對目錄中放置登入GMAIL帳號密碼,目的為了不再GitHub顯示出來. ID = f.readline().strip('\n') #不包含換行符號\n PW = f.readline().strip('\n') fileopen.write('上市公司股票篩選\n\n\n') fileopen.write("\n" + "傳統黃金交叉定義:昨天短均線低於長均線,今天短均線高於長均線" + "\n\n") #傳統黃金交叉(5日均線向上穿越10日均線) index = 1 for i in stock_no_list: try: if BestFourPoint(Stock(i)).golden_cross_no_back_test(m=5, n=10): fileopen.write( str(index) + " " + "傳統黃金交叉(5日均線向上穿越10日均線)" + "-" + Stock_no_name[i].encode("UTF-8") + "-" + i + "-" + "成交張數" + "-" + str(int(Stock(i).raw[-1][1] / 1000)) + "\n") index = index + 1 except: # 回傳為None 或 資料不足導致ValueError pass fileopen.write("\n\n") #傳統黃金交叉(5日均線向上穿越20日均線) index = 1 for i in stock_no_list: try: if BestFourPoint(Stock(i)).golden_cross_no_back_test(m=5, n=20): fileopen.write(
import sys if len(sys.argv) != 3: print "usage: python fuckGRS.py [from_id] [to_id(not_include)]" sys.exit(1) initial_idx = int(sys.argv[1]) initial_month = 240 record_csv_name = 'record.csv' record = open(record_csv_name, "ab") writer = csv.writer(record, delimiter=',') i = initial_idx month = initial_month while i < int(sys.argv[2]): print "fetch id = ", i, ", month = ", month try: stock = Stock(str(i), month)# may cause not exist error if month > 0: stock.out_putfile(str(i)+'.csv')# may cause index out of range error writer.writerow([i, month]) except AttributeError:# index out of range print " >> id = ", i, "out of range! month = ", month month -= 12 except: # no such id print " >> id = ", i, " not exist" i += 1 month = initial_month else: i += 1 month = initial_month
#!/usr/bin/env python # -*- coding: utf-8 -*- from grs import Stock from grs import TWSENo import sys sys.path.append('../') from Trade import Trade from KD import KD from MA import MA from DMI import DMI from MACD import MACD stock = Stock.createByCSV('2454', '../../data/2454_14MonData.csv') trade = Trade(stock) dmi = DMI(stock) macd = MACD(stock) print "MACD with adx >16:" trade.MACD_DMI_1(macd, dmi) trade.printTradeResult() kd = KD(stock) #print "Trade by perfect buy and perfect sell KD:" #trade.KD_PerfectBuyPerfectSell(kd) #trade.printTradeResult() #print "Trade by perfect buy and normal sell KD:" #trade.KD_PerfectBuyNormalSell(kd) #trade.printTradeResult() #print "Trade by normal buy and normal sell KD:" #trade.KD_NormalBuyNormalSell(kd) #trade.printTradeResult()
fileopen = open("Stock_5_10_20.txt", 'w') #開啟檔案,w沒有該檔案就新增 f = open('/home/tim/GMAIL.txt', 'r') #於前一個相對目錄中放置登入GMAIL帳號密碼,目的為了不再GitHub顯示出來. ID = f.readline().strip('\n') #不包含換行符號\n PW = f.readline().strip('\n') fileopen.write("\n" + "二十个交易日內, 5日均線>10日均線>20均線" + "\n\n") #===================== fileopen.write('\n\n\n上櫃公司股票篩選\n\n\n') index = 1 for i in OTC_no_list: #print i try: if BestFourPoint(Stock(i, mons=3)).best_5_10_20_backtest(day): print i, 'otc' #暴量長紅2天 fileopen.write( str(index) + " " + "-" + OTC_no_name[i].encode("UTF-8") + "-" + i + "-" + "殖益率" + str(fields_otc()[i][2]) + "\n") index = index + 1 except: # 回傳為None 或 資料不足導致ValueError pass fileopen.close() #關閉檔案 os.system('sendEmail -o \ -f [email protected] \ -t "WEI <*****@*****.**>" \ -s smtp.gmail.com:587 \
def getgrsdata(): stock = Stock('2618') print stock.moving_average(5)
# -*- coding: utf-8 -*- import sys reload(sys) sys.setdefaultencoding("utf-8") from grs import BestFourPoint from grs import Stock stock = Stock('2618') result = BestFourPoint(stock) print result.best_four_point_to_buy() # 判斷是否為四大買點 print result.best_four_point_to_sell() # 判斷是否為四大賣點 print result.best_four_point() # 綜合判斷
pass except: # 回傳為None 或 資料不足導致ValueError pass """ fileopen.write('\n\n\n\n') fileopen.write('上櫃公司股票篩選\n\n\n\n') for i in OTC_no_list: #print i realtime_data = RealtimeOTC(i) try: if (realtime_data.data[i]['volume_acc'] > Stock(i, mons=3).moving_average_value(5)[0][-2] * 1000 or realtime_data.data[i]['volume_acc'] > Stock(i, mons=3).moving_average_value(20)[0][-2] * 1000 ) and float( realtime_data.data[i]['diff'][1]) > 0: #今天的量大於5日週或月均量, 目前要漲 print i, 'TWSE123' #暴量長紅2天 close_price = Stock(i).raw[-1][6] MA5 = Stock(i, mons=2).moving_average(5)[0][-1] MA10 = Stock(i, mons=2).moving_average(10)[0][-1] MA20 = Stock(i, mons=2).moving_average(20)[0][-1] Bias5 = (close_price - MA5) / MA5 * 100 Bias10 = (close_price - MA10) / MA10 * 100 Bias20 = (close_price - MA20) / MA20 * 100 turnover_ration = float(Stock(i).raw[-1][1]) / ( float(rank_market_value()[i][3]) * 10000) * 100
f = open('/home/tim/GMAIL.txt', 'r') #於前一個相對目錄中放置登入GMAIL帳號密碼,目的為了不再GitHub顯示出來. ID = f.readline().strip('\n') #不包含換行符號\n PW = f.readline().strip('\n') fileopen.write('上市公司股票篩選\n\n\n') fileopen.write("\n" "+暴量長紅2天" + "\n\n") #abc = BestFourPoint(Stock('1101')).moving_average_value(5) index = 1 for i in stock_no_list: #今天量大於昨天前的五日均量2倍,且今天量大長紅 try: if BestFourPoint(Stock(i)).today_great_ma5(2): print i, '今天量大於昨天前的五日均量2倍,且今天量大長紅' #今天量大於昨天前的五日均量N倍,且今天量大長紅 fileopen.write( str(index) + " " + "今天量大於昨天前的五日均量2倍,且今天量大長紅,成交量要大於1000張" + "-" + Stock_no_name[i].encode("UTF-8") + "-" + i + "-" + "成交張數" + "-" + str(int(Stock(i).raw[-1][1] / 1000)) + "\n") index = index + 1 except: # 回傳為None 或 資料不足導致ValueError pass index = 1 for i in stock_no_list: #今天量大於昨天前的五日均量3倍,且今天量大長紅 try: if BestFourPoint(Stock(i)).today_great_ma5(3): print i, '今天量大於昨天前的五日均量3倍,且今天量大長紅' #今天量大於昨天前的五日均量N倍,且今天量大長紅
pass """ fileopen.write('\n\n\n上櫃公司股票篩選\n\n\n') fileopen.write("\n"+"新豪式黃金交叉定義:今天短均線高於長均線,且回測N天內的短均線皆要低於長均線"+"\n\n") #新豪式黃金交叉定義:今天短均線高於長均線,且前回測N天中其短均線皆要低於長均線 #===================== index = 1 for i in OTC_no_list: try: if BestFourPoint(Stock(i)).golden_cross(m=5,n=10,back_to_test_n_days=5) and (BestFourPoint(Stock(i)).data.moving_average(5)[0][-1] > BestFourPoint(Stock(i)).data.moving_average(10)[0][-1] ): print i,'123' # 5日均線黃金交叉10日均線,並且要回測5天. fileopen.write(str(index)+" "+"新豪式黃金交叉(5日均線向上穿越10日均線且符合5天回測)"+"-"+OTC_no_name[i].encode("UTF-8")+"-"+i+"-"+"成交張數"+"-"+str(int(Stock(i).raw[-1][1]))+"\n") index = index + 1 except: # 回傳為None 或 資料不足導致ValueError pass #===================== #fileopen.write("\n\n") #index = 1 #for i in OTC_no_list: # try: # if BestFourPoint(Stock(i)).golden_cross(m=5,n=10,back_to_test_n_days=10) and (BestFourPoint(Stock(i)).data.moving_average(5)[0][-1] > BestFourPoint(Stock(i)).data.moving_average(10)[0][-1] ): # print i,'123' # 5日均線黃金交叉10日均線,並且要回測5天. # fileopen.write(str(index)+" "+"新豪式黃金交叉(5日均線向上穿越10日均線且符合10天回測)"+"-"+OTC_no_name[i].encode("UTF-8")+"-"+i+"-"+"成交張數"+"-"+str(int(Stock(i).raw[-1][1]))+"\n") # index = index + 1
fileopen = open(attachment, 'w') #開啟檔案,w沒有該檔案就新增 f = open('/home/tim/GMAIL.txt', 'r') #於前一個相對目錄中放置登入GMAIL帳號密碼,目的為了不再GitHub顯示出來. ID = f.readline().strip('\n') #不包含換行符號\n PW = f.readline().strip('\n') fileopen.write('上市公司股票篩選\n\n\n') fileopen.write("\n" + "回測n天中,5與10與20日均線總和平均在m%盤整,今天暴nn倍的mm日均量" + "\n\n") #===================== index = 1 for i in stock_no_list: #print i try: if BestFourPoint(Stock(i, mons=3)).breakthrough(20, 1, 1.5, 20): # n, m ,nn, mm # 回測天數, 盤整%數, 暴量nn倍, mm日均量 # 回測n天中,5與10與20日均線總和平均在m%盤整,今天暴nn倍的mm日均量 print i, 'twse' #暴量長紅2天 fileopen.write( str(index) + " " + "-" + Stock_no_name[i].encode("UTF-8") + "-" + i + "-" + "殖益率" + str(fields()[i][2]) + "\n") index = index + 1 except: # 回傳為None 或 資料不足導致ValueError pass fileopen.close() #關閉檔案
fileopen = open("less20.txt", 'w') #開啟檔案,w沒有該檔案就新增 f = open('/home/tim/GMAIL.txt', 'r') #於前一個相對目錄中放置登入GMAIL帳號密碼,目的為了不再GitHub顯示出來. ID = f.readline().strip('\n') #不包含換行符號\n PW = f.readline().strip('\n') fileopen.write("\n" + "量縮20%" + "\n\n") #===================== fileopen.write('\n\n\n上櫃公司股票篩選\n\n\n') index = 1 for i in OTC_no_list: #print i try: if BestFourPoint(Stock(i, mons=1)).volumeless20(): print i, 'otc' #暴量長紅2天 fileopen.write( str(index) + " " + "-" + OTC_no_name[i].encode("UTF-8") + "-" + i + "-" + "殖益率" + str(fields_otc()[i][2]) + "\n") index = index + 1 except: # 回傳為None 或 資料不足導致ValueError pass fileopen.close() #關閉檔案 os.system('sendEmail -o \ -f [email protected] \ -t "WEI <*****@*****.**>" \ -s smtp.gmail.com:587 \
ID = f.readline().strip('\n') #不包含換行符號\n:q PW = f.readline().strip('\n') fileopen.write('上市公司股票篩選\n\n\n'.decode('utf8').encode('big5-hkscs')) #fileopen.write("股票",",","100",",","倍週均量",",","成交張數",",","殖利率","100",",","倍月均量") #===================== index = 1 for i in stock_no_list: #print i realtime_data = RealtimeTWSE(i) try: if realtime_data.data[i]['volume_acc'] > Stock( i, mons=3).moving_average_value( 5)[0][-2] or realtime_data.data[i]['volume_acc'] > Stock( i, mons=3).moving_average_value(20)[0][-2]: #今天的量大於5日週均量 print i, 'TWSE123' #暴量長紅2天 fileopen.write(i+"-"+Stock_no_name[i].encode('big5-hkscs')+"-"+"目前累積成交量".decode('utf8').encode('big5-hkscs')+","+ \ str(float(realtime_data.data[i]['volume_acc'])/float(Stock(i,mons=3).moving_average_value(5)[0][-2]))+","+"倍週均量".decode('utf8').encode('big5-hkscs')+\ ","+"成交張數".decode('utf8').encode('big5-hkscs')+"-"+str(realtime_data.data[i]['volume_acc'])+","+"殖益率".decode('utf8').encode('big5-hkscs')+str(fields()[i][2])+"-"+ \ ","+str(float(realtime_data.data[i]['volume_acc'])/float(Stock(i,mons=3).moving_average_value(20)[0][-2]))+","+"倍月均量".decode('utf8').encode('big5-hkscs')+ \ ","+"漲跌百分比".decode('utf8').encode('big5-hkscs')+","+str(float(realtime_data.data[i]['diff'][1]))+","+"昨天收盤價".decode('utf8').encode('big5-hkscs')+","+str(float(realtime_data.data[i]['yesterday_price']))+ "\n") index = index + 1 except: # 回傳為None 或 資料不足導致ValueError pass fileopen.write('\n\n\n\n')
PW = f.readline().strip('\n') fileopen.write('\n\n\n上櫃公司股票篩選===>'+"昨天成交量比前天多,昨天收盤價又比昨天開盤價(暴量長紅),今天收盤又上漲1~10%,且今天成交張數要大於50張"+"\n") """ result = self.data.value[-2] > self.data.value[-3] and self.data.price[-2] > self.data.openprice[-2] and \ self.data.price[-1]/self.data.price[-2] >= 1.01 and self.data.price[-1]/self.data.price[-2] <= 1.11 and self.data.value[-1]*1000 > 500 """ index = 1 for i in OTC_no_list: try: if BestFourPoint(Stock(i,mons=2)).otc_y_v_t_r(): print i,'otc' #暴量長紅2天 close_price = Stock(i).raw[-1][6] MA5 = Stock(i,mons=2).moving_average(5)[0][-1] MA10 = Stock(i,mons=2).moving_average(10)[0][-1] MA20 = Stock(i,mons=2).moving_average(20)[0][-1] Bias5 = (close_price-MA5)/MA5*100 Bias10 = (close_price-MA10)/MA10*100 Bias20 = (close_price-MA20)/MA20*100 turnover_ration = float(Stock(i).raw[-1][1])/(float(rank_market_value()[i][3])*10000)*100 try: fileopen.write(OTC_no_name[i].encode("UTF-8")+"`("+i+")"+"OTC"+","+"成交張數"+str(int(Stock(i).raw[-1][1]))+","+ \ "收盤價"+str(close_price)+","+"去年殖利率"+str(fields_otc()[i][2])+","+"目前價格相當於週均價成長"+"%.1f"%Bias5+"%"+","+"雙週均價"+"%.1f"%Bias10+"%"+ \
ID = f.readline().strip('\n') #不包含換行符號\n:q PW = f.readline().strip('\n') fileopen.write('上市公司股票篩選\n\n\n') #fileopen.write("股票",",","100",",","倍週均量",",","成交張數",",","殖利率","100",",","倍月均量") #===================== index = 1 for i in stock_no_list: #print i realtime_data = RealtimeTWSE(i) try: if realtime_data.data[i]['volume_acc'] > Stock( i, mons=3).moving_average_value( 5)[0][-2] or realtime_data.data[i]['volume_acc'] > Stock( i, mons=3).moving_average_value(20)[0][-2]: #今天的量大於5日週均量 print i, 'TWSE123' #暴量長紅2天 fileopen.write(i+"-"+Stock_no_name[i].encode("UTF-8")+"-"+"目前累積成交量"+","+ \ str(float(realtime_data.data[i]['volume_acc'])/float(Stock(i,mons=3).moving_average_value(5)[0][-2]))+","+"倍週均量"+ \ ","+"成交張數"+"-"+str(realtime_data.data[i]['volume_acc'])+","+"殖益率"+str(fields()[i][2])+"-"+ \ ","+str(float(realtime_data.data[i]['volume_acc'])/float(Stock(i,mons=3).moving_average_value(20)[0][-2]))+","+"倍月均量"+","+"漲跌百分比"+","+str(float(realtime_data.data[i]['diff'][1]))+","+"昨天收盤價"+","+str(float(realtime_data.data[i]['yesterday_price']))+ "\n") except: # 回傳為None 或 資料不足導致ValueError pass fileopen.write('\n\n\n\n') fileopen.write('上櫃公司股票篩選\n\n\n\n')
ID = f.readline().strip('\n') #不包含換行符號\n PW = f.readline().strip('\n') fileopen.write("\n"+"增3倍"+"\n\n") #===================== fileopen.write('\n\n\n上櫃公司股票篩選\n\n\n') index = 1 for i in OTC_no_list: #print i try: if BestFourPoint(Stock(i,mons=1)).volumegreat3(): print i,'otc' #暴量長紅2天 fileopen.write(str(index)+" "+"-"+OTC_no_name[i].encode("UTF-8")+"-"+i+"-"+"殖益率"+str(fields_otc()[i][2])+"\n") index = index + 1 except: # 回傳為None 或 資料不足導致ValueError pass fileopen.close() #關閉檔案 os.system('sendEmail -o \ -f [email protected] \ -t "WEI <*****@*****.**>" \ -s smtp.gmail.com:587 \ -xu %s \ -xp %s \
from grs import Stock stock = Stock('2002') stock.out_putfile('/2002.csv')
fileopen = open(attachment, 'w') #開啟檔案,w沒有該檔案就新增 f = open('/home/tim/GMAIL.txt', 'r') #於前一個相對目錄中放置登入GMAIL帳號密碼,目的為了不再GitHub顯示出來. ID = f.readline().strip('\n') #不包含換行符號\n PW = f.readline().strip('\n') fileopen.write('上市公司股票篩選\n\n\n') fileopen.write("\n" + "昨天暴量長紅,今天又上漲1~7%,成交張數要大於1000張" + "\n") #===================== index = 1 for i in stock_no_list: #print i try: if BestFourPoint(Stock(i, mons=2)).y_v_t_r(): print i, 'twse' #暴量長紅2天 try: if oneday()[i][1] == '': one_day = "買賣現沖" elif oneday()[i][1] == 'Y': one_day = "先買現沖" else: one_day = "" except: one_day = "" #csv找不到該股票代碼,即不開放買賣現沖 close_price = Stock(i).raw[-1][6] MA5 = Stock(i, mons=2).moving_average(5)[0][-1] MA10 = Stock(i, mons=2).moving_average(10)[0][-1] MA20 = Stock(i, mons=2).moving_average(20)[0][-1]
def main(): PATH_OF_DATA = 'data' index_lists = [] ### 從 stocknumber 中讀出看要抓哪幾隻股票的資料 f = open('stocknumber.csv', 'rb') cr = csv.reader(f, delimiter=',') for row in cr: index_lists.append(row[0]) skipFlag = True if len(sys.argv) > 1 else False tillFlag = True if len(sys.argv) > 2 else False for stock_index in index_lists: if skipFlag: if stock_index != sys.argv[1]: continue else: skipFlag = False if tillFlag: if stock_index == sys.argv[2]: break filename = join(PATH_OF_DATA, stock_index+'.csv') if isfile(filename):# 如果已經有檔案,就讀出最後一行然後插入在後面 print stock_index, 'exist!' lastline = get_last_row(filename) try: st = Stock(stock_index) except grs.error.StockNoError: print stock_index, "not found!" continue try: if len(st.raw) == 0:# 近三個月無資料 print "近一個月無資料" continue except:# len(st.raw) 沒資料可能會 error continue # 預設不會超過三個月沒有抓資料 i = 0 print lastline for i in xrange(len(st.raw)):# 從尾巴開始找看差幾筆 if lastline[0] == st.raw[-(i+1)][0]: break print "st.raw[-1]", st.raw[-1] print "缺", i, "筆資料" fo = open(filename, 'ab') cw = csv.writer(fo, delimiter=',') for j in range(0, i):# 把後來的都補上 cw.writerow(st.raw[-i+j]) else: # 如果沒有檔案,就從頭開始抓 print stock_index, ' not exist!' # 二分搜尋月分, 最大預設 480 個月 (40 年) top_month = 481 bot_month = 0 while top_month != bot_month: month = (top_month + bot_month)/2 print "top: ", top_month, "bottom: ", bot_month, "now:", month st = Stock(stock_index, month) try: len(st.raw)# 如果 list index out of range 就會 AttributeError bot_month = month if top_month - bot_month == 1: break except AttributeError: top_month = month st = Stock(stock_index, bot_month) st.out_putfile(filename)