diff --git a/.gitignore b/.gitignore index 489cceea..9f76cae8 100644 --- a/.gitignore +++ b/.gitignore @@ -74,3 +74,5 @@ vn.trader/CTP_connect.json vn.trader/CTP_connect.json vn.trader/CTP_connect.json *.vt +vn.trader/ctaStrategy02.py +*.pyd diff --git a/vn.ctp/vnctptd/vnctptd/vnctptd.cpp b/vn.ctp/vnctptd/vnctptd/vnctptd.cpp index 56fcd157..2e41065f 100644 --- a/vn.ctp/vnctptd/vnctptd/vnctptd.cpp +++ b/vn.ctp/vnctptd/vnctptd/vnctptd.cpp @@ -3891,7 +3891,7 @@ void TdApi::processRspQryInvestorPosition(Task task) data["FrozenCommission"] = task_data.FrozenCommission; data["CombShortFrozen"] = task_data.CombShortFrozen; #ifndef __GNU__ - data["OptionValue"] = task_data.OptionValue; + //data["OptionValue"] = task_data.OptionValue; #endif data["CloseProfitByDate"] = task_data.CloseProfitByDate; data["SettlementPrice"] = task_data.SettlementPrice; @@ -3930,7 +3930,7 @@ void TdApi::processRspQryTradingAccount(Task task) data["Available"] = task_data.Available; data["FundMortgageAvailable"] = task_data.FundMortgageAvailable; #ifndef __GNU__ - data["OptionCloseProfit"] = task_data.OptionCloseProfit; + //data["OptionCloseProfit"] = task_data.OptionCloseProfit; #endif data["PreCredit"] = task_data.PreCredit; data["PreMortgage"] = task_data.PreMortgage; @@ -3948,7 +3948,7 @@ void TdApi::processRspQryTradingAccount(Task task) data["FrozenCash"] = task_data.FrozenCash; data["Withdraw"] = task_data.Withdraw; #ifndef __GNU__ - data["OptionValue"] = task_data.OptionValue; + //data["OptionValue"] = task_data.OptionValue; #endif data["Balance"] = task_data.Balance; data["SpecProductMargin"] = task_data.SpecProductMargin; diff --git a/vn.datayes/api.py b/vn.datayes/api.py index 667df21f..b1dc2b47 100644 --- a/vn.datayes/api.py +++ b/vn.datayes/api.py @@ -617,9 +617,10 @@ class PyApi(object): def get_future_main_D1(self, field='', start='', end='', mark='', obj='', main=1, one=20150513): """ - + #MktFutureBarRTIntraDayGet """ - pass + + def get_fund_D1(self, field='', start='', end='', secID='', ticker='', one=20150513, output='df'): diff --git a/vn.datayes/tests.py b/vn.datayes/tests.py index 7d63dcea..2f045df8 100644 --- a/vn.datayes/tests.py +++ b/vn.datayes/tests.py @@ -134,7 +134,7 @@ if __name__ == '__main__': #test_config() #test_mktbar_D1() #test_bond_D1() - #test_fut_D1() + test_fut_D1() #test_fund_D1() #test_index_D1() #test_option_D1() @@ -149,4 +149,4 @@ if __name__ == '__main__': #test_mktbar_M1_get_all() #test_mktbar_M1_get_interM() #test_mktfutd() - test_MktEqudGet() + #test_MktEqudGet() diff --git a/vn.strategy/strategydemo/backtestingEngine.py b/vn.strategy/strategydemo/backtestingEngine.py index 1ccee708..2d5b5e33 100644 --- a/vn.strategy/strategydemo/backtestingEngine.py +++ b/vn.strategy/strategydemo/backtestingEngine.py @@ -79,6 +79,7 @@ class BacktestingEngine(object): # 回测结束日期 self.endDate = None + self.eventEngine.register(EVENT_LOG, self.printLog) #---------------------------------------------------------------------- def setStrategyEngine(self, engine): @@ -515,7 +516,9 @@ class BacktestingEngine(object): """写日志""" print log - + def printLog(self, event): + log = event.dict_['log'] + print log #---------------------------------------------------------------------- def subscribe(self, symbol, exchange): """仿真订阅合约""" diff --git a/vn.strategy/strategydemo/strategyEngine.py b/vn.strategy/strategydemo/strategyEngine.py index c850a1cf..5b309615 100644 --- a/vn.strategy/strategydemo/strategyEngine.py +++ b/vn.strategy/strategydemo/strategyEngine.py @@ -691,13 +691,13 @@ class StrategyEngine(object): tick.time = data['UpdateTime'] - tick.bidPrice1 = float(data['BidPrice1']) + tick.bidPrice1 = float(data['BidPrice1']) # 叫买价1 # tick.bidPrice2 = data['BidPrice2'] # tick.bidPrice3 = data['BidPrice3'] # tick.bidPrice4 = data['BidPrice4'] # tick.bidPrice5 = data['BidPrice5'] - tick.askPrice1 = float(data['AskPrice1']) + tick.askPrice1 = float(data['AskPrice1']) # 叫卖价1 # tick.askPrice2 = data['AskPrice2'] # tick.askPrice3 = data['AskPrice3'] # tick.askPrice4 = data['AskPrice4'] @@ -903,6 +903,7 @@ class StrategyEngine(object): # 注册 订阅交易响应事件 self.__eventEngine.register(EVENT_TRADE ,self.updateTrade) + #---------------------------------------------------------------------- def writeLog(self, log): diff --git a/vn.trader/CTA_setting.json b/vn.trader/CTA_setting.json index 8c245b0b..afcf03a6 100644 --- a/vn.trader/CTA_setting.json +++ b/vn.trader/CTA_setting.json @@ -1,10 +1,86 @@ { - "DR_IF1512": { - "strategyClassName": "DataRecorder", - "vtSymbol": "IF1512" - }, - "DR_IH1512": { - "strategyClassName": "DataRecorder", - "vtSymbol": "IH1512" + "SQ_AU1606": { + "strategyClassName": "Strategy02", + "vtSymbol": "au1606", + "symbol":"au1606", + "inputN":9, + "inputX":1, + "inputP":10, + "inputM":1, + "inputMM":42, + "inputSS":2, + "inputR":2 + }, + "SQ_AG1606": { + "strategyClassName": "Strategy02", + "vtSymbol": "ag1606", + "symbol":"ag1606", + "inputN":9, + "inputX":1, + "inputP":7, + "inputM":1, + "inputMM":38, + "inputSS":2, + "inputR":2 + }, + "SQ_AL1602": { + "strategyClassName": "Strategy02", + "vtSymbol": "al1602", + "symbol":"al1602", + "inputN":9, + "inputX":1, + "inputP":7, + "inputM":1, + "inputMM":42, + "inputSS":2, + "inputR":2 + }, + "SQ_CU1602": { + "strategyClassName": "Strategy02", + "vtSymbol": "cu1602", + "symbol":"cu1602", + "inputN":9, + "inputX":1, + "inputP":7, + "inputM":1, + "inputMM":42, + "inputSS":2, + "inputR":2 + }, + "SQ_RB1605": { + "strategyClassName": "Strategy02", + "vtSymbol": "rb1605", + "symbol":"rb1605", + "inputN":9, + "inputX":1, + "inputP":7, + "inputM":1, + "inputMM":42, + "inputSS":2, + "inputR":2 + }, + "ZZ_SR605": { + "strategyClassName": "Strategy02", + "vtSymbol": "SR605", + "symbol":"SR1605", + "inputN":9, + "inputX":1, + "inputP":10, + "inputM":1, + "inputMM":42, + "inputSS":2, + "inputR":2 + }, + "DL_jd1605": { + "strategyClassName": "Strategy02", + "vtSymbol": "jd1605", + "symbol":"jd1605", + "inputN":9, + "inputX":1, + "inputP":7, + "inputM":1, + "inputMM":42, + "inputSS":2, + "inputR":2 } } \ No newline at end of file diff --git a/vn.trader/ctaEngine.py b/vn.trader/ctaEngine.py index fe85eb00..48c26abc 100644 --- a/vn.trader/ctaEngine.py +++ b/vn.trader/ctaEngine.py @@ -71,24 +71,27 @@ class CtaEngine(object): req.priceType = PRICETYPE_LIMITPRICE # 价格类型 # CTA委托类型映射 + # 对于中金所,只有close有用 + # 对于上期所,应该分别使用closeYesterday或者closeToday + if orderType == CTAORDER_BUY: req.direction = DIRECTION_LONG # 合约方向 req.offset = OFFSET_OPEN # 开/平 elif orderType == CTAORDER_SELL: req.direction = DIRECTION_SHORT - req.offset = OFFSET_CLOSE + req.offset = OFFSET_CLOSETODAY #OFFSET_CLOSE elif orderType == CTAORDER_SHORT: req.direction = DIRECTION_SHORT req.offset = OFFSET_OPEN elif orderType == CTAORDER_COVER: req.direction = DIRECTION_LONG - req.offset = OFFSET_CLOSE + req.offset = OFFSET_CLOSETODAY #OFFSET_CLOSE # 3.调用主引擎的发单接口进行发单,保存OrderID与策略映射关系 vtOrderID = self.mainEngine.sendOrder(req, contract.gatewayName) # 发单 - self.orderDict[vtOrderID] = strategy # 保存vtOrderID和策略的映射关系 + self.orderStrategyDict[vtOrderID] = strategy # 保存vtOrderID和策略的映射关系 return vtOrderID diff --git a/vn.trader/ctaStrategyTemplate.py b/vn.trader/ctaStrategyTemplate.py index 2b0917fe..ddfd583b 100644 --- a/vn.trader/ctaStrategyTemplate.py +++ b/vn.trader/ctaStrategyTemplate.py @@ -24,17 +24,17 @@ class CtaStrategyTemplate(object): self.ctaEngine = ctaEngine self.name = name - self.vtSymbol = EMPTY_STRING # 交易的合约vt系统代码 + self.vtSymbol = EMPTY_STRING # 交易的合约vt系统代码 AU1606,SR605 + self.symbol = EMPTY_STRING # 交易的合约代码(除郑商所外与vtSymbol一致,一般为两位代码+两位年份+两位月份)AU1606,SR1605 self.tickDbName = EMPTY_STRING # tick数据库名称 self.barDbName = EMPTY_STRING # bar数据库名称 self.trading = False # 控制是否启动交易 - - self.init() # 初始化策略 - - if setting: - self.setParam(setting) + + #self.init() # 初始化策略 Move to inhert strategy + #if setting: + # self.setParam(setting) # ---------------------------------------------------------------------- def init(self): @@ -77,8 +77,10 @@ class CtaStrategyTemplate(object): # 如果stop为True,则意味着发本地停止单 if self.trading: if stop: + self.writeCtaLog(u'本地停止单,Buy买开,Price:{0},Volume:{1}'.format(price, volume)) orderID = self.ctaEngine.sendStopOrder(self.vtSymbol, CTAORDER_BUY, price, volume, self) else: + self.writeCtaLog(u'Buy买开,Price:{0},Volume:{1}'.format(price, volume)) orderID = self.ctaEngine.sendOrder(self.vtSymbol, CTAORDER_BUY, price, volume, self) return orderID else: @@ -90,8 +92,10 @@ class CtaStrategyTemplate(object): # 如果stop为True,则意味着发本地停止单 if self.trading: if stop: + self.writeCtaLog(u'本地停止单,sell卖平,Price:{0},Volume:{1}'.format(price, volume)) orderID = self.ctaEngine.sendStopOrder(self.vtSymbol, CTAORDER_SELL, price, volume, self) else: + self.writeCtaLog(u'sell卖平,Price:{0},Volume:{1}'.format(price, volume)) orderID = self.ctaEngine.sendOrder(self.vtSymbol, CTAORDER_SELL, price, volume, self) return orderID else: @@ -103,8 +107,10 @@ class CtaStrategyTemplate(object): # 如果stop为True,则意味着发本地停止单 if self.trading: if stop: + self.writeCtaLog(u'本地停止单,short卖开,Price:{0},Volume:{1}'.format(price, volume)) orderID = self.ctaEngine.sendStopOrder(self.vtSymbol, CTAORDER_SHORT, price, volume, self) else: + self.writeCtaLog(u'short卖开,Price:{0},Volume:{1}'.format(price, volume)) orderID = self.ctaEngine.sendOrder(self.vtSymbol, CTAORDER_SHORT, price, volume, self) return orderID else: @@ -116,8 +122,11 @@ class CtaStrategyTemplate(object): if self.trading: # 如果stop为True,则意味着发本地停止单 if stop: + self.writeCtaLog(u'本地停止单,cover,买平,Price:{0},Volume:{1}'.format(price, volume)) orderID = self.ctaEngine.sendStopOrder(self.vtSymbol, CTAORDER_COVER, price, volume, self) else: + self.writeCtaLog(u'Short卖开,cover:{0},Volume:{1}'.format(price, volume)) + orderID = self.ctaEngine.sendOrder(self.vtSymbol, CTAORDER_COVER, price, volume, self) return orderID else: @@ -169,7 +178,7 @@ class CtaStrategyTemplate(object): # ---------------------------------------------------------------------- def writeCtaLog(self, content): """记录CTA日志""" - self.ctaEngine.writeCtaLog(content) + self.ctaEngine.writeCtaLog(self.name+':'+content) diff --git a/vn.trader/ctpGateway.py b/vn.trader/ctpGateway.py index 0d376d9c..36fda6d6 100644 --- a/vn.trader/ctpGateway.py +++ b/vn.trader/ctpGateway.py @@ -33,8 +33,11 @@ directionMapReverse = {v: k for k, v in directionMap.items()} offsetMap = {} offsetMap[OFFSET_OPEN] = defineDict['THOST_FTDC_OF_Open'] offsetMap[OFFSET_CLOSE] = defineDict['THOST_FTDC_OF_Close'] +offsetMap[OFFSET_FORCECLOSE] = defineDict['THOST_FTDC_OF_ForceClose'] offsetMap[OFFSET_CLOSETODAY] = defineDict['THOST_FTDC_OF_CloseToday'] offsetMap[OFFSET_CLOSESYESTERDAY] = defineDict['THOST_FTDC_OF_CloseYesterday'] +offsetMap[OFFSET_FORCECOFF] = defineDict['THOST_FTDC_OF_ForceOff'] +offsetMap[OFFSET_LOCALFORCECLOSE] = defineDict['THOST_FTDC_OF_LocalForceClose'] offsetMapReverse = {v:k for k,v in offsetMap.items()} # 交易所类型映射 diff --git a/vn.trader/vtConstant.py b/vn.trader/vtConstant.py index 60d3620b..228c7f1f 100644 --- a/vn.trader/vtConstant.py +++ b/vn.trader/vtConstant.py @@ -18,8 +18,11 @@ DIRECTION_SELL = u'卖出' # IB接口 OFFSET_NONE = u'无开平' OFFSET_OPEN = u'开仓' OFFSET_CLOSE = u'平仓' +OFFSET_FORCECLOSE = u'强平' OFFSET_CLOSETODAY = u'平今' OFFSET_CLOSESYESTERDAY = u'平昨' +OFFSET_FORCECOFF = u'强减' +OFFSET_LOCALFORCECLOSE = u'本地强平' OFFSET_UNKNOWN = u'未知' # 状态常量 diff --git a/vn.trader/vtEngine.py b/vn.trader/vtEngine.py index 71b8e568..0cc0be90 100644 --- a/vn.trader/vtEngine.py +++ b/vn.trader/vtEngine.py @@ -49,31 +49,31 @@ class MainEngine(object): except Exception, e: print e - try: - from ltsGateway import LtsGateway - self.addGateway(LtsGateway, 'LTS') - self.gatewayDict['LTS'].setQryEnabled(True) - except Exception, e: - print e + #try: + # from ltsGateway import LtsGateway + # self.addGateway(LtsGateway, 'LTS') + # self.gatewayDict['LTS'].setQryEnabled(True) + #except Exception, e: + # print e - try: - from windGateway import WindGateway - self.addGateway(WindGateway, 'Wind') - except Exception, e: - print e + #try: + # from windGateway import WindGateway + # self.addGateway(WindGateway, 'Wind') + #except Exception, e: + # print e - try: - from ibGateway import IbGateway - self.addGateway(IbGateway, 'IB') - except Exception, e: - print e + #try: + # from ibGateway import IbGateway + # self.addGateway(IbGateway, 'IB') + #except Exception, e: + # print e # MongoDB数据库相关 self.dbClient = None # MongoDB客户端对象 - self.addGateway(IbGateway, 'IB') +# self.addGateway(IbGateway, 'IB') # CTA引擎 self.ctaEngine = CtaEngine(self, self.eventEngine, self.dataEngine)