def __init__(self, parent, id, title): wx.Frame.__init__(self, parent, id, title, size=(800, 600)) if len(sys.argv) == 3 and sys.argv[2] == 'wave': records = wave(sys.argv[1] + ".TW", "2011/1/1", "2012/7/1", 5) chart_title = "%s wave chart" % sys.argv[1] else: y = yahoo() records = y.ma(sys.argv[1] + ".TW", "2011/1/1", "2012/7/1", 5) chart_title = "%s K chart" % sys.argv[1] dat = [] vdat = [] fdat = [] for i in records: i['Volumn'] = i['Volumn']/1000 dat.append([i['Date'], i['Close']]) vdat.append([i['Date'], i['Volumn']]) fdat = MOPS_fetch().report(sys.argv[1]) self.panel = wx.Panel(self, -1) self.panel.SetBackgroundColour('WHITE') hbox = wx.BoxSizer(wx.HORIZONTAL) linechart = LineChart(self.panel) linechart.SetSize((800, 600)) linechart.SetTitle(chart_title) linechart.SetData(dat) linechart.SetAllData(records) linechart.SetVolumnData(vdat) linechart.SetFinancialStatement(fdat) hbox.Add(linechart, 1, wx.EXPAND | wx.ALL, 15) self.panel.SetSizer(hbox) self.Centre() self.Show(True)
def wave(stock, start_day, end_day, ma): ma_key = "MA" + str(ma) profit_key = "Profit" wave_key = "Wave" wave_peak_key = "WavePeak" y = yahoo() result = y.ma(stock, start_day, end_day, ma) if result == "": return "" for i in result: if string.atof(i[y.CLOSE]) < i[ma_key]: i[profit_key] = "-" else: i[profit_key] = "+" result.sort(key=lambda x:x[y.DATE], reverse=False) sign = result[0][profit_key] for i in result: i[wave_peak_key] = "" si = 0 ei = 0 index = 0 peak_id = 0 for i in result: i[wave_key] = 0 if sign != i[profit_key]: ei = index if sign != i[profit_key] and sign == "-": low = 10000 for ii in range(si, ei): if string.atof(result[ii][y.LOW]) < low: low = string.atof(result[ii][y.LOW]) peak_id = ii result[peak_id][wave_key] = string.atof(result[peak_id][y.LOW]) result[peak_id][wave_peak_key] = "+" #print("peak_index = %d, price = %s" % (peak_id, result[peak_id][y.LOW])) elif sign != i[profit_key] and sign == "+": high = 0 for ii in range(si, ei): if string.atof(result[ii][y.HIGH]) > high: high = string.atof(result[ii][y.HIGH]) peak_id = ii result[peak_id][wave_key] = string.atof(result[peak_id][y.HIGH]) result[peak_id][wave_peak_key] = "-" #print("peak_index = %d, price = %s" % (peak_id, result[peak_id][y.HIGH])) if sign != i[profit_key]: sign = i[profit_key] si = index index += 1 ii = 0 for i in result: if i[wave_key] != 0: break ii += 1 rr = result result = [] for i in range(ii, len(rr)): result.append(rr[i]) count = 0 ii = 0 for i in result: i.pop(profit_key) if i[wave_key] == 0: count += 1 elif i[wave_key] > 0 and count > 0: j = ii - count for r in range(j, ii): result[r][wave_key] = (result[ii][wave_key] - result[j-1][wave_key]) / (count+1) * (r - j + 1) + result[j-1][wave_key] #print "[" + str(r) + "]\t" + str(result[r][wave_key]) count = 0 ii += 1 ''' ii = 0 for i in result: if i[wave_key] == 0: if ii-2 > 0: i[wave_key] = (result[ii-1][wave_key] - result[ii-2][wave_key]) + result[ii-1][wave_key] else: i[wave_key] = i[y.CLOSE] ii += 1 ''' rr = result result = [] for i in rr: if i[wave_key] != 0: result.append(i) ''' ii = 0 for i in result: print str(ii) + "\t" + i[y.DATE] + "\t" + str(i[y.LOW]) + "\t" + str(i[y.HIGH]) + "\t" + str(i[y.CLOSE]) + "\t" + str(i[ma_key]) + "\t" + str(i[wave_key]) + "\t" + i[wave_peak_key] ii += 1 ''' return result
def ma_normal_rule(stock, start_day, end_day, ma): print stock ma_key = "MA" + str(ma) signal_key = "signal" operation_key = "operation" yf = yahoo() result = yf.ma(stock, start_day, end_day, ma) if result == "": #print stock + " NORMAL: No data" return result.sort(key=lambda x:x[yf.DATE], reverse=False) bought = False buy_stop = READY_BUY sell_stop = READY_SELL price = 0.0 for line in result: if (line[ma_key] != 0) and (string.atof(line[yf.ADJCLOSE]) > line[ma_key]) and (bought == False): buy_stop = buy_stop -1 if buy_stop == 0: line[signal_key] = "buy" bought = True price = string.atof(line[yf.HIGH]) sell_stop = READY_SELL else: if bought == True: line[signal_key] = "keep" else: line[signal_key] = "none" elif (line[ma_key] != 0) and (string.atof(line[yf.ADJCLOSE]) < line[ma_key]) and (bought == True): sell_stop = sell_stop -1 if sell_stop == 0: line[signal_key] = "sell" bought = False buy_stop = READY_BUY else: if bought == True: line[signal_key] = "keep" else: line[signal_key] = "none" elif (bought == True) and ((string.atof(line[yf.CLOSE])-price) < 0 and (price - string.atof(line[yf.CLOSE])) > price*ACCEPT_LOSS): line[signal_key] = "sell" bought = False else: if bought == True: line[signal_key] = "keep" else: line[signal_key] = "none" bought = False profit = 0.0 cost = 0.0 price = 0.0 count = 0 # only can order off day result[0][operation_key] = "none" for i in range(result.__len__()): if (i+1) >= result.__len__(): break if (bought == False) and (result[i][signal_key] == "buy"): result[i+1][operation_key] = "buy" bought = True price = string.atof(result[i+1][yf.HIGH]) count += 1 cost += string.atof(result[i+1][yf.HIGH]) * 1000 * COST_RATE elif (bought == True) and (result[i][signal_key] == "sell"): result[i+1][operation_key] = "sell" bought = False profit += (string.atof(result[i+1][yf.HIGH]) - price) * 1000 cost += string.atof(result[i+1][yf.HIGH]) * 1000 * (COST_RATE + FAX_RATE) else: if bought == True: result[i+1][operation_key] = "keep" else: result[i+1][operation_key] = "none" result.sort(key=lambda x:x[yf.DATE], reverse=True) #f = open("data/." + stock + "." + str(ma) + ".normal", "w") f = open(".normal", "w") f.write("DATE\t\tOPEN\tCLOSE\tHIGH\tLOW\tVOLUME\t" + ma_key + "\tSIGNAL\tOPERATION\n") for line in result: f.write(str(line[yf.DATE])) f.write("\t") f.write(str(line[yf.OPEN])) f.write("\t") f.write(str(line[yf.CLOSE])) f.write("\t") f.write(str(line[yf.HIGH])) f.write("\t") f.write(str(line[yf.LOW])) f.write("\t") f.write(str(line[yf.VOLUME]/1000)) f.write("\t") f.write(str(line[ma_key])) f.write("\t") f.write(str(line[signal_key])) f.write("\t") f.write(str(line[operation_key])) f.write("\n") f.close() if bought == True: print stock + " kept,\tNORMAL:\tprice = " + str(result[0][yf.HIGH]) + " (" + result[0][yf.DATE] + "),\tresult = " + str(profit - cost) + ",\tprofit = " + str(profit) + ",\tcost = " + str(cost) + ",\tcount = " + str(count) + ",\tROI= " + str((profit-cost)/string.atof(result[0][yf.HIGH])) else: print stock + " empty,\tNORMAL:\tprice = " + str(result[0][yf.HIGH]) + " (" + result[0][yf.DATE] + "),\tresult = " + str(profit - cost) + ",\tprofit = " + str(profit) + ",\tcost = " + str(cost) + ",\tcount = " + str(count) + ",\tROI= " + str((profit-cost)/string.atof(result[0][yf.HIGH]))
from financial_statement import * from yahoo import * candidates = [] mops = MOPS_fetch() f = open("stock_list.txt", "r") for sym in f.readlines(): sym = sym.strip('\n') result = mops.report(sym) if result != "no data": if result['ROE'] > 0.02: if result['Current Ratio'] >= 1.2: if result['Real Debt Ratio'] < 0.01: candidates.append(sym) f.close() y = yahoo() for i in candidates: records = y.ma(str(i) + ".TW", "2012/1/1", "2012/7/1", 5) if len(records) < 10: continue avg_vol = 0 avg_count = 0 for j in records: avg_vol += j['Volumn']/1000 avg_count += 1 avg_vol = avg_vol / avg_count if avg_vol > 1000 and records[0]['Close'] < 50 and records[0]['Close'] > 10: print "found " + str(i)
#! /usr/bin/env python from yahoo import * yf = yahoo() yf.get_quote("2002.TW", "b4") #yf.oscillators("2002.TW", "2012/4/1", "2012/6/14", 5)