实盘修改

This commit is contained in:
msincenselee 2015-12-09 23:42:17 +08:00
parent 64f772e650
commit 2e6d994a5d
12 changed files with 145 additions and 44 deletions

2
.gitignore vendored
View File

@ -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

View File

@ -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;

View File

@ -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'):

View File

@ -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()

View File

@ -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):
"""仿真订阅合约"""

View File

@ -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):

View File

@ -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
}
}

View File

@ -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

View File

@ -24,17 +24,17 @@ class CtaStrategyTemplate(object):
self.ctaEngine = ctaEngine
self.name = name
self.vtSymbol = EMPTY_STRING # 交易的合约vt系统代码
self.vtSymbol = EMPTY_STRING # 交易的合约vt系统代码 AU1606SR605
self.symbol = EMPTY_STRING # 交易的合约代码除郑商所外与vtSymbol一致一般为两位代码+两位年份+两位月份AU1606SR1605
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)

View File

@ -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()}
# 交易所类型映射

View File

@ -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'未知'
# 状态常量

View File

@ -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)