def run(history, tweek, pref, selections): logstr = '' # w, x, y = history.grab(nsample, ntracks, tweek + 1, pref, 180, 90, 80000) x = xfilter(x) timer = timer_c() lsvm = svmutil.svm_train(y, x, options) #svmutil.svm_save_model('benchmark.svm', lsvm) #lsvm = svmutil.svm_load_model('benchmark.svm') print 'svm trained successfully in %s sec with %d samples.' % (str( float('{0:.3f}'.format(timer.lag()))), len(w)) # timer = timer_c() w, x, y = history.grab(1, ntracks, tweek, pref, 180, 90, 80000) x = xfilter(x) p_labels, p_acc, p_vals = svmutil.svm_predict(y, x, lsvm, '') print 'svm predicted successfully in %s sec with %d samples.' % (str( float('{0:.3f}'.format(timer.lag()))), len(w)) foo = [] for i in xrange(len(y)): foo.append((p_labels[i], y[i], w[i])) foo.sort(key=lambda tup: (-tup[0])) selections['code'].append(foo[0][2]) selections['tweek'].append(tweek) return [row[1] for row in foo]
def run(tday, history, logfile): logstr = '' # w, x, y = history.grab(nsample, ntracks, tday + 1) x = xfilter(x) timer = timer_c() lsvm = svmutil.svm_train(y, x, options) #svmutil.svm_save_model('benchmark.svm', lsvm) #lsvm = svmutil.svm_load_model('benchmark.svm') print 'svm trained successfully in %s sec.' % (str( float('{0:.3f}'.format(timer.lag())))) # timer = timer_c() p_labels, p_acc, p_vals = svmutil.svm_predict(y, x, lsvm, '') print 'svm predicted successfully in %s sec.' % (str( float('{0:.3f}'.format(timer.lag())))) plotdraw(p_labels, y) # timer = timer_c() w, x, y = history.grab(1, ntracks, tday) x = xfilter(x) p_labels, p_acc, p_vals = svmutil.svm_predict(y, x, lsvm, '') print 'svm predicted successfully in %s sec.' % (str( float('{0:.3f}'.format(timer.lag())))) plotdraw(p_labels, y)
def run(tday, history): logstr = '' # w, x, y = history.grab(nsample, ntracks, tday + 1) lsvmL = [] for options in optionL: timer = timer_c() lsvmL.append(svmutil.svm_train(y, x, options)) print 'options = %s' % (options) print 'svm trained successfully in %s sec.' % (str( float('{0:.3f}'.format(timer.lag())))) # plt.axvline(x=0.0, color='k', alpha=0.2) plt.axhline(y=0.0, color='k', alpha=0.2) for i in xrange(len(lsvmL)): timer = timer_c() p_labels, p_acc, p_vals = svmutil.svm_predict(y, x, lsvmL[i], '') print 'svm predicted successfully in %s sec.' % (str( float('{0:.3f}'.format(timer.lag())))) plt.plot(p_labels, y, colorsL[i], alpha=0.5) plt.show() # w, x, y = history.grab(1, ntracks, tday) plt.axvline(x=0.0, color='k', alpha=0.2) plt.axhline(y=0.0, color='k', alpha=0.2) for i in xrange(len(lsvmL)): timer = timer_c() p_labels, p_acc, p_vals = svmutil.svm_predict(y, x, lsvmL[i], '') print 'svm predicted successfully in %s sec.' % (str( float('{0:.3f}'.format(timer.lag())))) plt.plot(p_labels, y, colorsL[i], alpha=0.5) plt.show()
def run(history): # w, x, y = history.grab(nsample, ntracks, 0) timer = timer_c() lsvm = svmutil.svm_train(y, x, options) svmutil.svm_save_model('temp.svm', lsvm) print 'svm trained successfully in %s sec.' % (str(float('{0:.3f}'.format(timer.lag())))) # timer = timer_c() p_labels, p_acc, p_vals = svmutil.svm_predict(y, x, lsvm, '') print 'svm predicted successfully in %s sec.' % (str(float('{0:.3f}'.format(timer.lag())))) plotdraw(p_labels, y)
def run(tday, history): logstr = '' # w, x, y = history.grab(nsample, ntracks, tday + 1) timer = timer_c() lsvm = svmutil.svm_train(y, x, options) print 'svm trained successfully in %s sec.' % (str( float('{0:.3f}'.format(timer.lag())))) # timer = timer_c() w, x, y = history.grab(1, ntracks, tday) p_labels, p_acc, p_vals = svmutil.svm_predict(y, x, lsvm, '') print 'svm predicted successfully in %s sec.' % (str( float('{0:.3f}'.format(timer.lag())))) pos = np.argmax(np.array(p_labels)) return p_labels[pos], y[pos]
def run(tday, history): logstr = '' # w, x, y = history.grab(nsample, ntracks, tday+1) timer = timer_c() lsvm = svmutil.svm_train(y, x, options) #svmutil.svm_save_model('benchmark.svm', lsvm) #lsvm = svmutil.svm_load_model('benchmark.svm') print 'svm trained successfully in %s sec.' % (str(float('{0:.3f}'.format(timer.lag())))) # timer = timer_c() w, x, y = history.grab(1, ntracks, tday) p_labels, p_acc, p_vals = svmutil.svm_predict(y, x, lsvm, '') print 'svm predicted successfully in %s sec.' % (str(float('{0:.3f}'.format(timer.lag())))) foo = [] for i in xrange(len(y)): foo.append((p_labels[i], y[i])) foo.sort(key = lambda tup: (-tup[0])) return [row[1] for row in foo]
def run(history): # timer = timer_c() lsvm = svmutil.svm_load_model('temp.svm') print 'svm loaded successfully in %s sec.' % (str( float('{0:.3f}'.format(timer.lag())))) # timer = timer_c() w, x, y = history.grab(1, ntracks, -1) p_labels, p_acc, p_vals = svmutil.svm_predict(y, x, lsvm, '') print 'svm predicted successfully in %s sec.' % (str( float('{0:.3f}'.format(timer.lag())))) plotdraw(p_labels, y) # foo = [] for i in xrange(len(w)): foo.append((w[i], p_labels[i], y[i])) foo.sort(key=lambda tup: (tup[1])) for row in foo: print '%s y\' = %.6f, y = %.6f' % row
def main(): timer = timer_c() res = numpy.array([], dtype='i') mat = numpy.empty((0, len(key) * ntracks), dtype='f') with open('../data/dbf/2017.dbf', 'r') as dbf: history = json.load(dbf) seven = history['layout'].index('PCHG') print 'seven at %d.' % (seven) first = None for code, table in history['record'].iteritems(): if len(table) < 180: continue if first is None or first < table[89][0]: first = table[89][0] for code, table in history['record'].iteritems(): # skip the stock has less than 180 rows of record if len(table) < 180: continue # skip the stock has suspended in 90 trading days if table[89][0] != first: continue # form mat & res for i in xrange(nsample): # mat++ row = [] for r in xrange(i + 1, i + ntracks + 1): for offset in key: row.append(table[r][offset]) mat = numpy.append(mat, numpy.array([row], dtype='f'), axis=0) # res++ hg = table[i][seven] + 10.0 hg = min(hg, 19.99) hg = max(hg, 0.0) res = numpy.append(res, int(hg / (20.0 / nlabels))) print 'mat & res loaded successfully in %s sec.' % (str( float('{0:.3f}'.format(timer.lag())))) # timer.reset() lsvm = svmutil.svm_train(res.tolist(), mat.tolist(), '-s 0 -t 0 -g 1.00 -c 1000000.00 -b 0 -q') svmutil.svm_save_model('temp.svm', lsvm) print 'svm trained successfully in %s sec.' % (str( float('{0:.3f}'.format(timer.lag())))) p_labels, p_acc, p_vals = svmutil.svm_predict(res.tolist(), mat.tolist(), lsvm, '')
def run(history, tweek, pref): # w, x, y = history.grab(nsample, ntracks, tweek, pref) x = xfilter(x) timer = timer_c() lsvm = svmutil.svm_train(y, x, options) #print 'svm trained successfully in %s sec with %d samples.' % (str(float('{0:.3f}'.format(timer.lag()))), len(w)) # w, x, y = history.grab(1, ntracks, tweek - 1, pref) x = xfilter(x) timer.reset() p_labels, p_acc, p_vals = svmutil.svm_predict(y, x, lsvm, '') #print 'svm predicted successfully in %s sec with %d samples.' % (str(float('{0:.3f}'.format(timer.lag()))), len(w)) # foo = [] for i in xrange(len(w)): foo.append((w[i], p_labels[i], y[i])) foo.sort(key=lambda tup: (-tup[1])) for i in xrange(3): print '%s y\' = %.6f, y = %.6f' % foo[i]
def run(tday=0): timer = timer_c() matT = numpy.empty((0, len(key) * ntracks), dtype='f') resT = numpy.array([], dtype='i') boundary = None if enscale: numpy.zeros((len(key), 2), dtype='f') for i in xrange(len(boundary)): boundary[i][0] = numpy.inf boundary[i][1] = -numpy.inf with open('../data/dbf/2017.dbf', 'r') as dbf: history = json.load(dbf) print 'run tday = %s' % (history['record'].values()[0][tday][0]) seven = history['layout'].index('PCHG') print 'seven at %d.' % (seven) first = None for code, table in history['record'].iteritems(): if len(table) < 180: continue if first is None or first < table[89][0]: first = table[89][0] for code, table in history['record'].iteritems(): # skip the stock has less than 180 rows of record if len(table) < tday + 181: continue # skip the stock has suspended in 90 trading days if table[89][0] != first: continue # form mat & res for i in xrange(tday + 1, tday + 1 + nsample): # mat++ row = [] for r in xrange(i + 1, i + ntracks + 1): for offset in key: row.append(table[r][offset]) matT = numpy.append(matT, numpy.array([row], dtype='f'), axis=0) # res++ hg = table[i][seven] + 10.0 hg = min(hg, 19.99) hg = max(hg, 0.0) resT = numpy.append(resT, int(hg / (20.0 / nlabels))) # boundray-update if enscale: for i in xrange(nsample + ntracks + 1): for j in xrange(len(key)): boundary[j][0] = min(boundary[j][0], table[i][key[j]]) boundary[j][1] = max(boundary[j][1], table[i][key[j]]) if enscale: autoscale(matT, boundary) print 'matT & resT loaded successfully in %s sec.' % (str( float('{0:.3f}'.format(timer.lag())))) # timer.reset() matQ = numpy.empty((0, len(key) * ntracks), dtype='f') resQ = numpy.array([], dtype='i') with open('../data/dbf/2017.dbf', 'r') as dbf: history = json.load(dbf) seven = history['layout'].index('PCHG') print 'seven at %d.' % (seven) first = None for code, table in history['record'].iteritems(): if len(table) < 180: continue if first is None or first < table[89][0]: first = table[89][0] for code, table in history['record'].iteritems(): # skip the stock has less than 180 rows of record if len(table) < 180: continue # skip the stock has suspended in 90 trading days if table[89][0] != first: continue # mat++ row = [] for r in xrange(tday + 1, tday + 1 + ntracks): for offset in key: row.append(table[r][offset]) matQ = numpy.append(matQ, numpy.array([row], dtype='f'), axis=0) # res++ hg = table[tday][seven] + 10.0 hg = min(hg, 19.99) hg = max(hg, 0.0) resQ = numpy.append(resQ, int(hg / (20.0 / nlabels))) if enscale: autoscale(matQ, boundary) print 'matQ & resQ loaded successfully in %s sec.' % (str( float('{0:.3f}'.format(timer.lag())))) sys.stdout.flush() for i in xrange(-6, 6, 2): for j in xrange(-6, 6, 2): cost = 10.**i gamma = 10.**j param = '-s 0 -t 2 -c %f -g %f -b 0 -q' % (cost, gamma) print 'param = %s' % param # timer.reset() lsvm = svmutil.svm_train(resT.tolist(), matT.tolist(), param) print 'svm trained successfully in %s sec.' % (str( float('{0:.3f}'.format(timer.lag())))) timer.reset() p_labels, p_acc, p_vals = svmutil.svm_predict( resT.tolist(), matT.tolist(), lsvm, '') print 'svm predicted successfully in %s sec.' % (str( float('{0:.3f}'.format(timer.lag())))) timer.reset() p_labels, p_acc, p_vals = svmutil.svm_predict( resQ.tolist(), matQ.tolist(), lsvm, '') print 'svm predicted successfully in %s sec.' % (str( float('{0:.3f}'.format(timer.lag())))) print[[int(x) for x in p_labels].count(i) for i in xrange(nlabels)] sys.stdout.flush()
def run(tday, logfile): logstr = '' timer = timer_c() res = numpy.array([], dtype='i') rat = numpy.array([], dtype='f') mat = numpy.empty((0, len(key) * ntracks), dtype='f') boundary = None if enscale: numpy.zeros((len(key), 2), dtype='f') for i in xrange(len(boundary)): boundary[i][0] = numpy.inf boundary[i][1] = -numpy.inf with open('../data/dbf/2017.dbf', 'r') as dbf: history = json.load(dbf) date = '1990-01-01' date2 = '1990-01-01' for table in history['record'].values(): if len(table) < 180: continue if date < table[tday][0]: date = table[tday][0] if date2 < table[tday + 1][0]: date2 = table[tday + 1][0] print 'run tday = %s' % (date) logstr += 'tday = %s\n' % (date) seven = history['layout'].index('PCHG') print 'seven at %d.' % (seven) first = '1990-01-01' for code, table in history['record'].iteritems(): offset2 = -1 for i in xrange(len(table)): if table[i][0] < date2: break elif table[i][0] == date2: offset2 = i break if offset2 < 0: continue if len(table) < offset2 + 180: continue if first < table[offset2 + 89][0]: first = table[offset2 + 89][0] for code, table in history['record'].iteritems(): offset2 = -1 for i in xrange(len(table)): if table[i][0] < date2: break elif table[i][0] == date2: offset2 = i break if offset2 < 0: continue # skip the stock has less than 180 rows of record if len(table) < offset2 + 180: continue # skip the stock has suspended in 90 trading days if table[offset2 + 89][0] != first: continue # form mat & res for i in xrange(offset2, offset2 + nsample): # mat++ row = [] for r in xrange(i + 1, i + ntracks + 1): for pos in key: row.append(table[r][pos]) mat = numpy.append(mat, numpy.array([row], dtype='f'), axis=0) # res++ hg = table[i][seven] + 10.0 hg = min(hg, 19.99) hg = max(hg, 0.0) res = numpy.append(res, int(hg / (20.0 / nlabels))) rat = numpy.append(rat, table[i][seven]) # boundray-update if enscale: for i in xrange(nsample + ntracks + 1): for j in xrange(len(key)): boundary[j][0] = min(boundary[j][0], table[i][key[j]]) boundary[j][1] = max(boundary[j][1], table[i][key[j]]) if enscale: autoscale(mat, boundary) print 'mat & res loaded successfully in %s sec.' % (str( float('{0:.3f}'.format(timer.lag())))) # timer.reset() lsvm = svmutil.svm_train(res.tolist(), mat.tolist(), '-s 0 -t 0 -g 1.00 -c 1000000.00 -b 0 -q') #svmutil.svm_save_model('benchmark.svm', lsvm) #lsvm = svmutil.svm_load_model('benchmark.svm') print 'svm trained successfully in %s sec.' % (str( float('{0:.3f}'.format(timer.lag())))) p_labels, p_acc, p_vals = svmutil.svm_predict(res.tolist(), mat.tolist(), lsvm, '') # timer.reset() tag = [] res = numpy.array([], dtype='i') rat = numpy.array([], dtype='f') mat = numpy.empty((0, len(key) * ntracks), dtype='f') with open('../data/dbf/2017.dbf', 'r') as dbf: history = json.load(dbf) seven = history['layout'].index('PCHG') print 'seven at %d.' % (seven) first = '1990-01-01' for code, table in history['record'].iteritems(): offset = -1 for i in xrange(len(table)): if table[i][0] < date: break elif table[i][0] == date: offset = i break if offset < 0: continue if len(table) < offset + 181: continue if first < table[offset + 90][0]: first = table[offset + 90][0] for code, table in history['record'].iteritems(): offset = -1 for i in xrange(len(table)): if table[i][0] < date: break elif table[i][0] == date: offset = i break if offset < 0: continue # skip the stock has less than 180 rows of record if len(table) < offset + 181: continue # skip the stock has suspended in 90 trading days if table[offset + 90][0] != first: continue # mat++ row = [] for r in xrange(offset + 1, offset + 1 + ntracks): for pos in key: row.append(table[r][pos]) mat = numpy.append(mat, numpy.array([row], dtype='f'), axis=0) # res++ hg = table[offset][seven] + 10.0 hg = min(hg, 19.99) hg = max(hg, 0.0) res = numpy.append(res, int(hg / (20.0 / nlabels))) rat = numpy.append(rat, table[offset][seven]) # tag++ tag.append(code) if enscale: autoscale(mat, boundary) print 'mat & res loaded successfully in %s sec.' % (str( float('{0:.3f}'.format(timer.lag())))) # timer.reset() labels = lsvm.get_labels() nlabel = len(labels) p_labels, p_acc, p_vals = svmutil.svm_predict(res.tolist(), mat.tolist(), lsvm, '') print 'svm predicted successfully in %s sec.' % (str( float('{0:.3f}'.format(timer.lag())))) summary = numpy.zeros((nlabels, 2), dtype='i') details = numpy.zeros((nlabels, nlabels), dtype='i') #goals = numpy.zeros((nlabels, nlabels), dtype='i') profit2 = [[[] for i in xrange(nlabels)] for j in xrange(nlabels)] #for idx in xrange(len(tag)): # votes = numpy.zeros(nlabel, dtype='i') # k = 0 # for i in xrange(nlabel): # for j in xrange(i+1, nlabel): # if p_vals[idx][k] > 0.0: # votes[i] += 1 # else: # votes[j] += 1 # k += 1 # bar = [] # for i in xrange(nlabel): # bar.append((labels[i], votes[i])) # bar = sorted(bar, key = lambda tup: (-tup[1], tup[0])) # if bar[0][0] == res[idx]: # summary[res[idx]][0] += 1 # summary[res[idx]][1] += 1 #print summary for idx in xrange(len(tag)): votes = numpy.zeros(nlabel, dtype='i') k = 0 for i in xrange(nlabel): for j in xrange(i + 1, nlabel): if p_vals[idx][k] > 0.0: votes[i] += 1 else: votes[j] += 1 k += 1 bar = [] for i in xrange(nlabel): bar.append((labels[i], votes[i])) bar = sorted(bar, key=lambda tup: (-tup[1], tup[0])) if p_labels[idx] == res[idx]: summary[res[idx]][0] += 1 summary[res[idx]][1] += 1 details[int(p_labels[idx])][res[idx]] += 1 if int(p_labels[idx]) in (bar[0][0], bar[1][0]): profit2[int(p_labels[idx])][bar[0][0] ^ bar[1][0] ^ int(p_labels[idx])].append(rat[idx]) #if tag[idx]=='sh600326': # print '%d %d %d => %.6f' % (bar[0][0], bar[1][0], bar[2][0], rat[idx]) #details[min(bar[0][0], bar[1][0])][res[idx]] += 1 #if p_labels[idx]>=nlabels-1: #if max(bar[0][0], bar[1][0])>=9: #goals[min(bar[0][0], bar[1][0])][res[idx]] += 1 print summary print details for i in xrange(nlabels): foo = '' for j in xrange(nlabels): if j > 0: foo += ', ' logstr += ',' if len(profit2[i][j]) > 0: foo += '%.6f (%d)' % (sum(profit2[i][j]) / len(profit2[i][j]), len(profit2[i][j])) logstr += '%.6f (%d)' % (sum(profit2[i][j]) / len( profit2[i][j]), len(profit2[i][j])) else: foo += 'nil' logstr += 'nil' print foo logstr += '\n' #print goals logstr += '\n' with open(logfile, 'a') as log: log.write(logstr)
def main(): timer = timer_c() tag = [] res = numpy.array([], dtype='i') mat = numpy.empty((0, len(key) * ntracks), dtype='f') with open('../data/dbf/2016.dbf', 'r') as dbf: history = json.load(dbf) seven = history['layout'].index('PCHG') first = None for code, table in history['record'].iteritems(): # skip the stock has less than 180 rows of record if len(table) < 180: continue # skip the stock has suspended in 90 trading days if len([i for i in xrange(90) if None in table[i]]) > 0: continue # mat++ row = [] for r in xrange(ntracks): for offset in key: row.append(table[r][offset]) mat = numpy.append(mat, numpy.array([row], dtype='f'), axis=0) # res++ hg = table[0][seven] + 10.0 hg = min(hg, 19.99) hg = max(hg, 0.0) res = numpy.append(res, int(hg / (20.0 / nlabels))) # tag++ tag.append(code) print 'mat & res loaded successfully in %s sec.' % (str( float('{0:.3f}'.format(timer.lag())))) # timer.reset() lsvm = svmutil.svm_load_model('temp.svm') labels = lsvm.get_labels() nlabel = len(labels) p_labels, p_acc, p_vals = svmutil.svm_predict(res.tolist(), mat.tolist(), lsvm, '') print 'svm predicted successfully in %s sec.' % (str( float('{0:.3f}'.format(timer.lag())))) best = [] for idx in xrange(len(tag)): votes = numpy.zeros(nlabel, dtype='i') k = 0 for i in xrange(nlabel): for j in xrange(i + 1, nlabel): if p_vals[idx][k] > 0.0: votes[i] += 1 else: votes[j] += 1 k += 1 bar = [] for i in xrange(nlabel): bar.append((labels[i], votes[i])) bar = sorted(bar, key=lambda tup: (-tup[1], tup[0])) #if tag[idx]=='sh600326': # print '%d, %d, %d \n' % (bar[0][0], bar[1][0], bar[2][0]) if int(p_labels[idx]) in [bar[0][0], bar[1][0]]: if int(p_labels[idx]) == bar[1][0]: bar[0], bar[1] = bar[1], bar[0] else: continue #print bar if bar[0][0] >= nlabels - 2: best.append((tag[idx], bar[0][0], bar[1][0], bar[2][0])) best = sorted(best, key=lambda tup: (-tup[1], -tup[2], -tup[3], tup[0])) for row in best: print '%s => %d %d %d' % row