def formatData(lines): stock = Stock() if len(lines) > 1: values = lines[0].split(' ') stock.id = values[0] i = 1 while values[i] != '日线': stock.name += values[i] i += 1 lines.pop(0) if len(lines) <= 1: return stock for line in lines: dayvalue = DayValue() values = line.split('\t') if getTime(values[0]) < time.strptime('2017/1/01', '%Y/%m/%d'): continue dayvalue.date = values[0] dayvalue.open = float(values[1]) dayvalue.high = float(values[2]) dayvalue.low = float(values[3]) dayvalue.close = float(values[4]) dayvalue.tradeamount = float(values[5]) dayvalue.tradevolume = float(values[6]) stock.dayvalues.append(dayvalue) return stock
def getTradeInfoAfter(date, stockname, days) -> [RangeTrend]: stock = Stock.fromJson(loadStock(stockname)) if stock is None: return [] index = 0 for dayvalue in stock.dayvalues: if getTime(dayvalue.date) > date: break index += 1 index -= 1 if index >= len(stock.dayvalues): return [] results = [] for i in days: if index + i < len(stock.dayvalues): trend = getRangeTrend(index, i, stock.dayvalues) results.append(trend) return results
def getCloseAfter(date, stockname, days) -> [int]: stock = Stock.fromJson(loadStock(stockname)) if stock is None: return [] index = 0 for dayvalue in stock.dayvalues: if getTime(dayvalue.date) > date: break index += 1 index -= 1 results = [] for i in days: if index + i < len(stock.dayvalues): results.append(100 * (stock.dayvalues[index + i].close - stock.dayvalues[index].close) / stock.dayvalues[index].close) else: results.append(0) return results
def addStock(self, config): stock = Stock(config["id"], config["name"], config["area"], config["ex"]) print(" Stock: " + stock.getFullID() + " " + stock.getName()) self.stockSet[stock.getFullID()] = stock stock.setInfo(config) pass
def loadFromDB(): stocks = [] client = MongoClient('localhost', 27017) db = client["test"] coll = db['stocks'] results = coll.find({}, {'_id': 0}) for r in results: stocks.append(Stock.fromJson(r)) return stocks
def loadAllStockFromDB() -> Dict[str, Stock]: stocks = dict() items = DatabaseMgr.instance().stocks.find({}, {'_id': 0}) for item in items: if 'id' in item: print(item['id']) stockId = item['id'] stock = Stock.fromJson(item) stock.calcMinsAndMaxs() stocks[stockId] = stock return stocks
from data.stock import Stock from data.storemgr import StockMgr items = DatabaseMgr.instance().stocks.find({'id': '002807'}, {'_id': 0}) stock = None for item in items: if 'id' in item: # print(item['id']) stockId = item['id'] stock = Stock.fromJson(item) break value = stock.findHeighestValue('2017/1/1') value0 = stock.findLowestValue(value.date) value1 = stock.findHeighestValue(value0.date) if value1 is not None: if (value1.high - value0.low) / value0.open < 0.15: pass
import sys from data.stock import Stock from predictor import Predictor from score import auc from preprocess import Preprocess submission_number = sys.argv[1] print submission_number def submission(ids, preds): name = "submissions/submission{0}.csv".format(submission_number) f = open(name, 'w') if len(ids) != len(preds): raise Exception("The number of IDs and the number of predictions are different") string = 'id,prediction\n' for index,i in enumerate(ids): string += str(i) + ',' + str(preds[index]) + "\n" f.write(str(string)) f.close() data, targets, _ = Stock.train() holdout_data, ids = Stock.test() assert len(data) == len(targets) Predictor.train(data, targets) preds = Predictor.multi_predict(holdout_data) print preds[0:50] submission(ids, preds)
from data.stock import Stock from predictor import Predictor from score import auc from preprocess import Preprocess data, targets, cv_targets = Stock.train() #data = Preprocess.scale(data) #cv_targets = Preprocess.scale(cv_targets) #data = Preprocess.polynomial(data, 5) half = len(data)/2 tr_data, holdout_data = data[:half], data[half:] train_targets, holdout_targets = targets[:half], targets[half:] Predictor.train(tr_data, train_targets) preds = Predictor.multi_predict(holdout_data) print 'AUC ', auc(preds, cv_targets) print Predictor.multi_predict([[0.]]) print Predictor.multi_predict([[0.1]]) print Predictor.multi_predict([[-0.1]]) print Predictor.multi_predict([[0.01]]) print Predictor.multi_predict([[-0.01]])
def test(): print("Test StockProxySina") stock1 = Stock('01810', '小米集团-W', 'hangkong', 'hk') stock2 = Stock('002415', '海康威视', 'shenzhen', 'sz') sinaproxy = StockProxySina() stock1.addProxy(sinaproxy) print(stock1.getFullID() + "\tprice:" + str(stock1.getPrice()) + "\t" + str(stock1.getPriceDatetime())) stock2.addProxy(sinaproxy) print(stock2.getFullID() + "\tprice:" + str(stock2.getPrice()) + "\t" + str(stock2.getPriceDatetime()))
from data.stock import Stock data, targets = Stock.train(categorize=False) pos, neg = [], [] for delta in targets: if delta > 0: pos.append(delta) elif delta < 0: neg.append(delta) print sum(pos)/len(pos) print sum(neg)/len(neg)