Skip to content

Commit

Permalink
增加King Keltner策略
Browse files Browse the repository at this point in the history
  • Loading branch information
chenxy123 committed Mar 6, 2017
1 parent 6b8ae72 commit 85f31e7
Show file tree
Hide file tree
Showing 5 changed files with 339 additions and 21 deletions.
43 changes: 26 additions & 17 deletions vn.lhang/vnlhang.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import requests
from Queue import Queue, Empty
from threading import Thread
from time import sleep



LHANG_API_ROOT ="https://api.lhang.com/v1/"
Expand Down Expand Up @@ -47,17 +49,19 @@ def __init__(self):
"""Constructor"""
self.apiKey = ''
self.secretKey = ''


self.interval = 1 # 每次请求的间隔等待
self.active = False # API工作状态
self.reqID = 0 # 请求编号
self.reqQueue = Queue() # 请求队列
self.reqThread = Thread(target=self.processQueue) # 请求处理线程

#----------------------------------------------------------------------
def init(self, apiKey, secretKey):
def init(self, apiKey, secretKey, interval):
"""初始化"""
self.apiKey = apiKey
self.secretKey = secretKey
self.interval = interval

self.active = True
self.reqThread.start()
Expand All @@ -66,7 +70,9 @@ def init(self, apiKey, secretKey):
def exit(self):
"""退出"""
self.active = False
self.reqThread.join()

if self.reqThread.isAlive():
self.reqThread.join()

#----------------------------------------------------------------------
def processRequest(self, req):
Expand Down Expand Up @@ -107,15 +113,18 @@ def processQueue(self):
# 请求失败
if data is None:
error = u'请求失败'
self.onError(error, reqID)
self.onError(error, req, reqID)
elif 'error_code' in data:
error = u'请求出错,错误代码:%s' % data['error_code']
self.onError(error, reqID)
self.onError(error, req, reqID)
# 请求成功
else:
if self.DEBUG:
print callback.__name__
callback(data, reqID)
callback(data, req, reqID)

# 流控等待
sleep(self.interval)

except Empty:
pass
Expand All @@ -138,9 +147,9 @@ def sendRequest(self, function, params, callback):
return self.reqID

#----------------------------------------------------------------------
def onError(self, error, reqID):
def onError(self, error, req, reqID):
"""错误推送"""
print error, reqID
print error, req, reqID

###############################################
# 行情接口
Expand Down Expand Up @@ -192,22 +201,22 @@ def getKline(self, symbol, size, type_, time):
return self.sendRequest(function, params, callback)

#----------------------------------------------------------------------
def onGetTicker(self, data, reqID):
def onGetTicker(self, data, req, reqID):
"""查询行情回调"""
print data, reqID

# ----------------------------------------------------------------------
def onGetDepth(self, data, reqID):
def onGetDepth(self, data, req, reqID):
"""查询深度回调"""
print data, reqID

# ----------------------------------------------------------------------
def onGetTrades(self, data, reqID):
def onGetTrades(self, data, req, reqID):
"""查询历史成交"""
print data, reqID

# ----------------------------------------------------------------------
def onGetKline(self, data, reqID):
def onGetKline(self, data, req, reqID):
"""查询K线回报"""
print data, reqID

Expand Down Expand Up @@ -272,27 +281,27 @@ def getOrdersInfoHistory(self, symbol, status, currentPage, pageLength):
return self.sendRequest(function, params, callback)

# ----------------------------------------------------------------------
def onGetUserInfo(self, data, reqID):
def onGetUserInfo(self, data, req, reqID):
"""查询K线回报"""
print data, reqID

# ----------------------------------------------------------------------
def onCreateOrder(self, data, reqID):
def onCreateOrder(self, data, req, reqID):
"""委托回报"""
print data, reqID

# ----------------------------------------------------------------------
def onCancelOrder(self, data, reqID):
def onCancelOrder(self, data, req, reqID):
"""撤单回报"""
print data, reqID

# ----------------------------------------------------------------------
def onGetOrdersInfo(self, data, reqID):
def onGetOrdersInfo(self, data, req, reqID):
"""查询委托回报"""
print data, reqID

# ----------------------------------------------------------------------
def onGetOrdersInfoHistory(self, data, reqID):
def onGetOrdersInfoHistory(self, data, req, reqID):
"""撤单回报"""
print data, reqID

10 changes: 8 additions & 2 deletions vn.trader/ctaAlgo/CTA_setting.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@
{
"name": "double ema",
"className": "EmaDemoStrategy",
"vtSymbol": "IF1702"
"vtSymbol": "IF1706"
},

{
"name": "atr rsi",
"className": "AtrRsiStrategy",
"vtSymbol": "IC1702"
"vtSymbol": "IC1706"
},

{
"name": "king keltner",
"className": "KkStrategy",
"vtSymbol": "IH1706"
}
]
3 changes: 2 additions & 1 deletion vn.trader/ctaAlgo/ctaBacktesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,8 @@ def crossStopOrder(self):
self.limitOrderDict[orderID] = order

# 从字典中删除该限价单
del self.workingStopOrderDict[stopOrderID]
if stopOrderID in self.workingStopOrderDict:
del self.workingStopOrderDict[stopOrderID]

#----------------------------------------------------------------------
def insertData(self, dbName, collectionName, data):
Expand Down
Loading

0 comments on commit 85f31e7

Please sign in to comment.