def analysicQuantDataOnly(start: datetime, end: datetime): souces = ZZ500DataSource(start, end) model = KDJMovementEngineModel() create = False engine = None if create: engine = CoreEngine.create(dirName, model, souces, build_quant_data_only=True, min_size=200) else: engine = CoreEngine.load(dirName, model) engine.buildPredictModel(useSVM=False) pass
def generatePredictOrder(self, engine: CoreEngine, predict: PredictData,debugPrams:{}=None) -> PredictOrder: if debugPrams is None: debugPrams = {} quantData = engine.queryQuantData(predict.dimen) code = predict.collectData.occurBars[-1].symbol name = self.sw.getSw2Name(code) order = PredictOrder(dimen=predict.dimen, code=code, name=name) start_price = engine.getEngineModel().getYBasePrice(predict.collectData) _min, _max = quantData.getSellFloatEncoder().parseEncode(quantData.sellRange[0].encode) order.suggestSellPrice = start_price * (1 + (_min + _max) / 2 / 100) _min, _max = quantData.getBuyFloatEncoder().parseEncode(quantData.buyRange[0].encode) order.suggestBuyPrice = start_price * (1 + (_min + _max) / 2 / 100) order.power_rate = quantData.getPowerRate(); ##for backTest self.checkIfBuyPrice(order,predict.collectData.occurBars[-1].close_price,debugPrams) return order
def generatePredictOrder(self, engine: CoreEngine, predict: PredictData,debugPrams:{}=None) -> PredictOrder: if debugPrams is None: debugPrams = {} code = predict.collectData.occurBars[-1].symbol name = self.sw.getSw2Name(code) order = PredictOrder(dimen=predict.dimen, code=code, name=name) predict_sell_pct = predict.getPredictSellPct(engine.getEngineModel()) predict_buy_pct = predict.getPredictBuyPct(engine.getEngineModel()) start_price = engine.getEngineModel().getYBasePrice(predict.collectData) order.suggestSellPrice = start_price * (1 + predict_sell_pct / 100) order.suggestBuyPrice = start_price * (1 + predict_buy_pct / 100) order.power_rate = engine.queryQuantData(predict.dimen).getPowerRate() ##for backTest occurBar: BarData = predict.collectData.occurBars[-2] skipBar: BarData = predict.collectData.occurBars[-1] buy_price = skipBar.close_price predict_sell_pct = 100 * (order.suggestSellPrice - start_price) / start_price predict_buy_pct = 100 * (order.suggestBuyPrice - start_price) / start_price buy_point_pct = 100 * (buy_price - occurBar.close_price) / occurBar.close_price ##买入的价格 abilityData = engine.queryPredictAbilityData(predict.dimen) quantData = engine.queryQuantData(predict.dimen) delta = abs(quantData.sellCenterPct) - abs(quantData.buyCenterPct) if abs(delta) < 0.05: # 多空力量差不多 power = 0 if delta > 0: # 适合做多 power= (quantData.sellCenterPct + quantData.buyCenterPct) / quantData.sellCenterPct else: power = - (quantData.sellCenterPct + quantData.buyCenterPct) / quantData.buyCenterPct extraCondition = True quant_power = debugPrams.get("quant_power") if not quant_power is None: extraCondition = extraCondition and predict.quantData.getPowerRate() >= quant_power predict_buy_pct_param = debugPrams.get("predict_buy_pct") if not predict_buy_pct_param is None: extraCondition = extraCondition and predict_buy_pct >= predict_buy_pct_param if extraCondition and predict_sell_pct - buy_point_pct > 1 \ and abilityData.trainData.biasSellLoss < 10: order.status = PredictOrderStatus.HOLD order.buyPrice = buy_price else: order.status = PredictOrderStatus.STOP return order
def build(self, engine: CoreEngine, sampleData: Sequence['CollectData'], quantData: QuantData): start = timeit.default_timer() self.quantData = quantData trainDataList = engine.getEngineModel().generateSampleData( engine, sampleData) size = len(trainDataList) engine.printLog( f"build PredictModel:dime={self.dimen}, sample size:{size} use SVM ={self.useSVM}", True) engine.printLog(f" history quantdata: {self.quantData}") ##建立特征值 x, y_sell_1, y_buy_1, y_sell_2, y_buy_2 = self.__generateFeature( trainDataList) self.labelListSell1 = np.sort(np.unique(y_sell_1)) self.labelListSell2 = np.sort(np.unique(y_sell_2)) self.labelListBuy1 = np.sort(np.unique(y_buy_1)) self.labelListBuy2 = np.sort(np.unique(y_buy_2)) engine.printLog(f" labelListSell1: {self.labelListSell1}") engine.printLog(f" labelListSell2: {self.labelListSell2}") engine.printLog(f" labelListBuy1: {self.labelListBuy1}") engine.printLog(f" labelListBuy2: {self.labelListBuy2}") self.classifierSell_1 = self.__createClassifier(x, y_sell_1) self.classifierSell_2 = self.__createClassifier(x, y_sell_2) self.classifierBuy_1 = self.__createClassifier(x, y_buy_1) self.classifierBuy_2 = self.__createClassifier(x, y_buy_2) elapsed = (timeit.default_timer() - start) engine.printLog( f"build PredictModel finished! elapsed = %.3fs" % (elapsed), True) pass
return x def percent_to_one(x): return int(x * 100) / 1000.0 data = [] data.append(percent_to_one(buy_pct)) data.append(percent_to_one(sell_pct)) data.append(set_0_between_100(kdj[0]) / 100) data.append(set_0_between_100(kdj[2]) / 100) return data if __name__ == "__main__": dirName = "models/sw_sample_model_analysis" trainDataSouce = SWDataSource(start=datetime(2014, 2, 1), end=datetime(2019, 9, 1)) testDataSouce = SWDataSource(datetime(2019, 9, 1), datetime(2020, 9, 1)) model = EngineModel2KAlgo2() #engine = CoreEngine.create(dirName,model,trainDataSouce,limit_dimen_size=99999999) engine = CoreEngine.load(dirName, model) runner = CoreEngineRunner(engine) strategy = MyStrategy() pdData = runner.backtest(testDataSouce, strategy, min_deal_count=15) writer = pd.ExcelWriter('models\output.xlsx') pdData.to_excel(writer, sheet_name="data", index=False) writer.save() writer.close()