合并1.6.1版本
This commit is contained in:
parent
67ea240ccd
commit
1de24b6d43
@ -1,14 +0,0 @@
|
|||||||
[
|
|
||||||
|
|
||||||
{
|
|
||||||
"name": "S22_M09M01",
|
|
||||||
"className": "Strategy22",
|
|
||||||
"vtSymbol": "SP m1609&m1701",
|
|
||||||
"symbol": "SP m1609&m1701",
|
|
||||||
"shortSymbol":"SP m1609&m1701",
|
|
||||||
"D1Symbol": "m1609",
|
|
||||||
"D2Symbol": "m1701",
|
|
||||||
"minDiff":1,
|
|
||||||
"inputSS":2
|
|
||||||
}
|
|
||||||
]
|
|
File diff suppressed because it is too large
Load Diff
@ -1,294 +0,0 @@
|
|||||||
# encoding: UTF-8
|
|
||||||
|
|
||||||
"""
|
|
||||||
这里的Demo是一个最简单的策略实现,并未考虑太多实盘中的交易细节,如:
|
|
||||||
1. 委托价格超出涨跌停价导致的委托失败
|
|
||||||
2. 委托未成交,需要撤单后重新委托
|
|
||||||
3. 断网后恢复交易状态
|
|
||||||
4. 等等
|
|
||||||
这些点是作者选择特意忽略不去实现,因此想实盘的朋友请自己多多研究CTA交易的一些细节,
|
|
||||||
做到了然于胸后再去交易,对自己的money和时间负责。
|
|
||||||
也希望社区能做出一个解决了以上潜在风险的Demo出来。
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
from ctaBase import *
|
|
||||||
from ctaTemplate import CtaTemplate
|
|
||||||
|
|
||||||
|
|
||||||
########################################################################
|
|
||||||
class DoubleEmaDemo(CtaTemplate):
|
|
||||||
"""双指数均线策略Demo"""
|
|
||||||
className = 'DoubleEmaDemo'
|
|
||||||
author = u'用Python的交易员'
|
|
||||||
|
|
||||||
# 策略参数
|
|
||||||
fastK = 0.9 # 快速EMA参数
|
|
||||||
slowK = 0.1 # 慢速EMA参数
|
|
||||||
initDays = 10 # 初始化数据所用的天数
|
|
||||||
|
|
||||||
# 策略变量
|
|
||||||
bar = None
|
|
||||||
barMinute = EMPTY_STRING
|
|
||||||
|
|
||||||
fastMa = [] # 快速EMA均线数组
|
|
||||||
fastMa0 = EMPTY_FLOAT # 当前最新的快速EMA
|
|
||||||
fastMa1 = EMPTY_FLOAT # 上一根的快速EMA
|
|
||||||
|
|
||||||
slowMa = [] # 与上面相同
|
|
||||||
slowMa0 = EMPTY_FLOAT
|
|
||||||
slowMa1 = EMPTY_FLOAT
|
|
||||||
|
|
||||||
# 参数列表,保存了参数的名称
|
|
||||||
paramList = ['name',
|
|
||||||
'className',
|
|
||||||
'author',
|
|
||||||
'vtSymbol',
|
|
||||||
'fastK',
|
|
||||||
'slowK']
|
|
||||||
|
|
||||||
# 变量列表,保存了变量的名称
|
|
||||||
varList = ['inited',
|
|
||||||
'trading',
|
|
||||||
'pos',
|
|
||||||
'fastMa0',
|
|
||||||
'fastMa1',
|
|
||||||
'slowMa0',
|
|
||||||
'slowMa1']
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
|
||||||
def __init__(self, ctaEngine, setting):
|
|
||||||
"""Constructor"""
|
|
||||||
super(DoubleEmaDemo, self).__init__(ctaEngine, setting)
|
|
||||||
|
|
||||||
# 注意策略类中的可变对象属性(通常是list和dict等),在策略初始化时需要重新创建,
|
|
||||||
# 否则会出现多个策略实例之间数据共享的情况,有可能导致潜在的策略逻辑错误风险,
|
|
||||||
# 策略类中的这些可变对象属性可以选择不写,全都放在__init__下面,写主要是为了阅读
|
|
||||||
# 策略时方便(更多是个编程习惯的选择)
|
|
||||||
self.fastMa = []
|
|
||||||
self.slowMa = []
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
|
||||||
def onInit(self):
|
|
||||||
"""初始化策略(必须由用户继承实现)"""
|
|
||||||
self.writeCtaLog(u'双EMA演示策略初始化')
|
|
||||||
|
|
||||||
initData = self.loadBar(self.initDays)
|
|
||||||
for bar in initData:
|
|
||||||
self.onBar(bar)
|
|
||||||
|
|
||||||
self.putEvent()
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
|
||||||
def onStart(self):
|
|
||||||
"""启动策略(必须由用户继承实现)"""
|
|
||||||
self.writeCtaLog(u'双EMA演示策略启动')
|
|
||||||
self.putEvent()
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
|
||||||
def onStop(self):
|
|
||||||
"""停止策略(必须由用户继承实现)"""
|
|
||||||
self.writeCtaLog(u'双EMA演示策略停止')
|
|
||||||
self.putEvent()
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
|
||||||
def onTick(self, tick):
|
|
||||||
"""收到行情TICK推送(必须由用户继承实现)"""
|
|
||||||
# 计算K线
|
|
||||||
tickMinute = tick.datetime.minute
|
|
||||||
|
|
||||||
if tickMinute != self.barMinute:
|
|
||||||
if self.bar:
|
|
||||||
self.onBar(self.bar)
|
|
||||||
|
|
||||||
bar = CtaBarData()
|
|
||||||
bar.vtSymbol = tick.vtSymbol
|
|
||||||
bar.symbol = tick.symbol
|
|
||||||
bar.exchange = tick.exchange
|
|
||||||
|
|
||||||
bar.open = tick.lastPrice
|
|
||||||
bar.high = tick.lastPrice
|
|
||||||
bar.low = tick.lastPrice
|
|
||||||
bar.close = tick.lastPrice
|
|
||||||
|
|
||||||
bar.date = tick.date
|
|
||||||
bar.time = tick.time
|
|
||||||
bar.datetime = tick.datetime # K线的时间设为第一个Tick的时间
|
|
||||||
|
|
||||||
# 实盘中用不到的数据可以选择不算,从而加快速度
|
|
||||||
#bar.volume = tick.volume
|
|
||||||
#bar.openInterest = tick.openInterest
|
|
||||||
|
|
||||||
self.bar = bar # 这种写法为了减少一层访问,加快速度
|
|
||||||
self.barMinute = tickMinute # 更新当前的分钟
|
|
||||||
|
|
||||||
else: # 否则继续累加新的K线
|
|
||||||
bar = self.bar # 写法同样为了加快速度
|
|
||||||
|
|
||||||
bar.high = max(bar.high, tick.lastPrice)
|
|
||||||
bar.low = min(bar.low, tick.lastPrice)
|
|
||||||
bar.close = tick.lastPrice
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
|
||||||
def onBar(self, bar):
|
|
||||||
"""收到Bar推送(必须由用户继承实现)"""
|
|
||||||
# 计算快慢均线
|
|
||||||
if not self.fastMa0:
|
|
||||||
self.fastMa0 = bar.close
|
|
||||||
self.fastMa.append(self.fastMa0)
|
|
||||||
else:
|
|
||||||
self.fastMa1 = self.fastMa0
|
|
||||||
self.fastMa0 = bar.close * self.fastK + self.fastMa0 * (1 - self.fastK)
|
|
||||||
self.fastMa.append(self.fastMa0)
|
|
||||||
|
|
||||||
if not self.slowMa0:
|
|
||||||
self.slowMa0 = bar.close
|
|
||||||
self.slowMa.append(self.slowMa0)
|
|
||||||
else:
|
|
||||||
self.slowMa1 = self.slowMa0
|
|
||||||
self.slowMa0 = bar.close * self.slowK + self.slowMa0 * (1 - self.slowK)
|
|
||||||
self.slowMa.append(self.slowMa0)
|
|
||||||
|
|
||||||
# 判断买卖
|
|
||||||
crossOver = self.fastMa0>self.slowMa0 and self.fastMa1<self.slowMa1 # 金叉上穿
|
|
||||||
crossBelow = self.fastMa0<self.slowMa0 and self.fastMa1>self.slowMa1 # 死叉下穿
|
|
||||||
|
|
||||||
# 金叉和死叉的条件是互斥
|
|
||||||
# 所有的委托均以K线收盘价委托(这里有一个实盘中无法成交的风险,考虑添加对模拟市价单类型的支持)
|
|
||||||
if crossOver:
|
|
||||||
# 如果金叉时手头没有持仓,则直接做多
|
|
||||||
if self.pos == 0:
|
|
||||||
self.buy(bar.close, 1)
|
|
||||||
# 如果有空头持仓,则先平空,再做多
|
|
||||||
elif self.pos < 0:
|
|
||||||
self.cover(bar.close, 1)
|
|
||||||
self.buy(bar.close, 1)
|
|
||||||
# 死叉和金叉相反
|
|
||||||
elif crossBelow:
|
|
||||||
if self.pos == 0:
|
|
||||||
self.short(bar.close, 1)
|
|
||||||
elif self.pos > 0:
|
|
||||||
self.sell(bar.close, 1)
|
|
||||||
self.short(bar.close, 1)
|
|
||||||
|
|
||||||
# 发出状态更新事件
|
|
||||||
self.putEvent()
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
|
||||||
def onOrder(self, order):
|
|
||||||
"""收到委托变化推送(必须由用户继承实现)"""
|
|
||||||
# 对于无需做细粒度委托控制的策略,可以忽略onOrder
|
|
||||||
pass
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
|
||||||
def onTrade(self, trade):
|
|
||||||
"""收到成交推送(必须由用户继承实现)"""
|
|
||||||
# 对于无需做细粒度委托控制的策略,可以忽略onOrder
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
########################################################################################
|
|
||||||
class OrderManagementDemo(CtaTemplate):
|
|
||||||
"""基于tick级别细粒度撤单追单测试demo"""
|
|
||||||
|
|
||||||
className = 'OrderManagementDemo'
|
|
||||||
author = u'用Python的交易员'
|
|
||||||
|
|
||||||
# 策略参数
|
|
||||||
initDays = 10 # 初始化数据所用的天数
|
|
||||||
|
|
||||||
# 策略变量
|
|
||||||
bar = None
|
|
||||||
barMinute = EMPTY_STRING
|
|
||||||
|
|
||||||
|
|
||||||
# 参数列表,保存了参数的名称
|
|
||||||
paramList = ['name',
|
|
||||||
'className',
|
|
||||||
'author',
|
|
||||||
'vtSymbol']
|
|
||||||
|
|
||||||
# 变量列表,保存了变量的名称
|
|
||||||
varList = ['inited',
|
|
||||||
'trading',
|
|
||||||
'pos']
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
|
||||||
def __init__(self, ctaEngine, setting):
|
|
||||||
"""Constructor"""
|
|
||||||
super(OrderManagementDemo, self).__init__(ctaEngine, setting)
|
|
||||||
|
|
||||||
self.lastOrder = None
|
|
||||||
self.orderType = ''
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
|
||||||
def onInit(self):
|
|
||||||
"""初始化策略(必须由用户继承实现)"""
|
|
||||||
self.writeCtaLog(u'双EMA演示策略初始化')
|
|
||||||
|
|
||||||
initData = self.loadBar(self.initDays)
|
|
||||||
for bar in initData:
|
|
||||||
self.onBar(bar)
|
|
||||||
|
|
||||||
self.putEvent()
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
|
||||||
def onStart(self):
|
|
||||||
"""启动策略(必须由用户继承实现)"""
|
|
||||||
self.writeCtaLog(u'双EMA演示策略启动')
|
|
||||||
self.putEvent()
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
|
||||||
def onStop(self):
|
|
||||||
"""停止策略(必须由用户继承实现)"""
|
|
||||||
self.writeCtaLog(u'双EMA演示策略停止')
|
|
||||||
self.putEvent()
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
|
||||||
def onTick(self, tick):
|
|
||||||
"""收到行情TICK推送(必须由用户继承实现)"""
|
|
||||||
|
|
||||||
# 建立不成交买单测试单
|
|
||||||
if self.lastOrder == None:
|
|
||||||
self.buy(tick.lastprice - 10.0, 1)
|
|
||||||
|
|
||||||
# CTA委托类型映射
|
|
||||||
if self.lastOrder != None and self.lastOrder.direction == u'多' and self.lastOrder.offset == u'开仓':
|
|
||||||
self.orderType = u'买开'
|
|
||||||
|
|
||||||
elif self.lastOrder != None and self.lastOrder.direction == u'多' and self.lastOrder.offset == u'平仓':
|
|
||||||
self.orderType = u'买平'
|
|
||||||
|
|
||||||
elif self.lastOrder != None and self.lastOrder.direction == u'空' and self.lastOrder.offset == u'开仓':
|
|
||||||
self.orderType = u'卖开'
|
|
||||||
|
|
||||||
elif self.lastOrder != None and self.lastOrder.direction == u'空' and self.lastOrder.offset == u'平仓':
|
|
||||||
self.orderType = u'卖平'
|
|
||||||
|
|
||||||
# 不成交,即撤单,并追单
|
|
||||||
if self.lastOrder != None and self.lastOrder.status == u'未成交':
|
|
||||||
|
|
||||||
self.cancelOrder(self.lastOrder.vtOrderID)
|
|
||||||
self.lastOrder = None
|
|
||||||
elif self.lastOrder != None and self.lastOrder.status == u'已撤销':
|
|
||||||
# 追单并设置为不能成交
|
|
||||||
|
|
||||||
self.sendOrder(self.orderType, self.tick.lastprice - 10, 1)
|
|
||||||
self.lastOrder = None
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
|
||||||
def onBar(self, bar):
|
|
||||||
"""收到Bar推送(必须由用户继承实现)"""
|
|
||||||
pass
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
|
||||||
def onOrder(self, order):
|
|
||||||
"""收到委托变化推送(必须由用户继承实现)"""
|
|
||||||
# 对于无需做细粒度委托控制的策略,可以忽略onOrder
|
|
||||||
self.lastOrder = order
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
|
||||||
def onTrade(self, trade):
|
|
||||||
"""收到成交推送(必须由用户继承实现)"""
|
|
||||||
# 对于无需做细粒度委托控制的策略,可以忽略onOrder
|
|
||||||
pass
|
|
@ -1,5 +0,0 @@
|
|||||||
{
|
|
||||||
"domain": "http://api.wmcloud.com/data",
|
|
||||||
"version": "v1",
|
|
||||||
"token": "575593eb7696aec7339224c0fac2313780d8645f68b77369dcb35f8bcb419a0b"
|
|
||||||
}
|
|
@ -2247,7 +2247,7 @@ class BacktestingEngine(object):
|
|||||||
self.writeCtaLog(u'{9}@{6} [{7}:开空{0},short:{1}]-[{8}:平空{2},cover:{3},vol:{4}],净盈亏:{5}'
|
self.writeCtaLog(u'{9}@{6} [{7}:开空{0},short:{1}]-[{8}:平空{2},cover:{3},vol:{4}],净盈亏:{5}'
|
||||||
.format(entryTrade.tradeTime, entryTrade.price,
|
.format(entryTrade.tradeTime, entryTrade.price,
|
||||||
trade.tradeTime, trade.price, tradeUnit, result.pnl,
|
trade.tradeTime, trade.price, tradeUnit, result.pnl,
|
||||||
i, shortid, tradeid,gId))
|
i, shortid, tradeid, gId))
|
||||||
i = i+1
|
i = i+1
|
||||||
|
|
||||||
if type(gr) != type(None):
|
if type(gr) != type(None):
|
||||||
@ -2466,7 +2466,7 @@ class BacktestingEngine(object):
|
|||||||
|
|
||||||
d = self.getResult()
|
d = self.getResult()
|
||||||
|
|
||||||
if len(d)== 0:
|
if len(d) == 0:
|
||||||
self.output(u'无交易结果')
|
self.output(u'无交易结果')
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -80,15 +80,19 @@ class CtaGrid(object):
|
|||||||
class CtaGridTrade(object):
|
class CtaGridTrade(object):
|
||||||
"""网格交易类
|
"""网格交易类
|
||||||
包括两个方向的网格队列,
|
包括两个方向的网格队列,
|
||||||
|
v1, 基本版
|
||||||
|
v2,增加更新最小价格跳动,增加动态上下网格间距
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, strategy, maxlots=5, height=2, win=2, vol=1):
|
def __init__(self, strategy, maxlots=5, height=2, win=2, vol=1, minDiff = 1):
|
||||||
"""初始化
|
"""初始化
|
||||||
maxlots,最大网格数
|
maxlots,最大网格数
|
||||||
height,网格高度(绝对值,包含minDiff)
|
height,网格高度(绝对值,包含minDiff)
|
||||||
win,盈利数(包含minDiff)
|
win,盈利数(包含minDiff)
|
||||||
vol,网格开仓数
|
vol,网格开仓数
|
||||||
|
minDiff, 最小价格跳动
|
||||||
"""
|
"""
|
||||||
|
self.minDiff = minDiff
|
||||||
|
|
||||||
self.strategy = strategy
|
self.strategy = strategy
|
||||||
|
|
||||||
@ -107,9 +111,11 @@ class CtaGridTrade(object):
|
|||||||
self.avg_up_open_price = EMPTY_FLOAT # 上网格开仓均价
|
self.avg_up_open_price = EMPTY_FLOAT # 上网格开仓均价
|
||||||
self.avg_dn_open_price = EMPTY_FLOAT # 下网格开仓均价
|
self.avg_dn_open_price = EMPTY_FLOAT # 下网格开仓均价
|
||||||
|
|
||||||
def getVolume(self, gridIndex=EMPTY_INT):
|
self.max_up_open_price = EMPTY_FLOAT # 上网格开仓均价
|
||||||
"""获取网格索引对应的开仓数量比例"""
|
self.min_dn_open_price = EMPTY_FLOAT # 下网格开仓均价
|
||||||
|
|
||||||
|
def getVolumeRate(self, gridIndex=EMPTY_INT):
|
||||||
|
"""获取网格索引对应的开仓数量比例"""
|
||||||
if gridIndex >= len(self.volumeList) or gridIndex < 0:
|
if gridIndex >= len(self.volumeList) or gridIndex < 0:
|
||||||
return 1
|
return 1
|
||||||
rate = self.volumeList[gridIndex]
|
rate = self.volumeList[gridIndex]
|
||||||
@ -138,7 +144,7 @@ class CtaGridTrade(object):
|
|||||||
grid = CtaGrid(direction=DIRECTION_SHORT,
|
grid = CtaGrid(direction=DIRECTION_SHORT,
|
||||||
openprice=upline+self.gridHeight*i,
|
openprice=upline+self.gridHeight*i,
|
||||||
closeprice=upline+self.gridHeight*i-self.gridWin,
|
closeprice=upline+self.gridHeight*i-self.gridWin,
|
||||||
volume=self.volume*self.getVolume(i))
|
volume=self.volume*self.getVolumeRate(i))
|
||||||
self.upGrids.append(grid)
|
self.upGrids.append(grid)
|
||||||
|
|
||||||
self.writeCtaLog(u'上网格{0}~{1}初始化完成'.format(upline,upline+self.gridHeight*self.maxLots))
|
self.writeCtaLog(u'上网格{0}~{1}初始化完成'.format(upline,upline+self.gridHeight*self.maxLots))
|
||||||
@ -160,7 +166,7 @@ class CtaGridTrade(object):
|
|||||||
grid = CtaGrid(direction=DIRECTION_LONG,
|
grid = CtaGrid(direction=DIRECTION_LONG,
|
||||||
openprice=dnline - self.gridHeight * i,
|
openprice=dnline - self.gridHeight * i,
|
||||||
closeprice=dnline - self.gridHeight * i + self.gridWin,
|
closeprice=dnline - self.gridHeight * i + self.gridWin,
|
||||||
volume=self.volume*self.getVolume(i))
|
volume=self.volume*self.getVolumeRate(i))
|
||||||
self.dnGrids.append(grid)
|
self.dnGrids.append(grid)
|
||||||
|
|
||||||
self.writeCtaLog(u'下网格{0}~{1}初始化完成'.format(dnline,dnline-self.gridHeight*self.maxLots))
|
self.writeCtaLog(u'下网格{0}~{1}初始化完成'.format(dnline,dnline-self.gridHeight*self.maxLots))
|
||||||
@ -369,13 +375,20 @@ class CtaGridTrade(object):
|
|||||||
self.writeCtaLog(u'清除上网格[open={0}]'.format(x.openPrice))
|
self.writeCtaLog(u'清除上网格[open={0}]'.format(x.openPrice))
|
||||||
self.upGrids.remove(x)
|
self.upGrids.remove(x)
|
||||||
|
|
||||||
def rebuildGrids(self, direction, upline=EMPTY_FLOAT, dnline=EMPTY_FLOAT, midline=EMPTY_FLOAT):
|
def rebuildGrids(self, direction, upline=EMPTY_FLOAT, dnline=EMPTY_FLOAT, midline=EMPTY_FLOAT, upRate=1, dnRate = 1):
|
||||||
"""重新拉网
|
"""重新拉网
|
||||||
清除未挂单的网格,
|
清除未挂单的网格,
|
||||||
在上轨/下轨位置重新挂单
|
在上轨/下轨位置重新挂单
|
||||||
|
upRate , 上轨网格高度比率
|
||||||
|
dnRate, 下轨网格高度比率
|
||||||
"""
|
"""
|
||||||
self.writeCtaLog(u'重新拉网:upline:{0},dnline:{1}'.format(upline, dnline))
|
self.writeCtaLog(u'重新拉网:upline:{0},dnline:{1}'.format(upline, dnline))
|
||||||
|
|
||||||
|
# 检查上下网格的高度比率,不能低于0.5
|
||||||
|
if upRate < 0.5 or dnRate < 0.5:
|
||||||
|
upRate = max(0.5, upRate)
|
||||||
|
dnRate = max(0.5, dnRate)
|
||||||
|
|
||||||
if direction == DIRECTION_LONG:
|
if direction == DIRECTION_LONG:
|
||||||
minPriceInOrder = midline
|
minPriceInOrder = midline
|
||||||
removePrices = []
|
removePrices = []
|
||||||
@ -399,13 +412,15 @@ class CtaGridTrade(object):
|
|||||||
self.writeCtaLog(u'需要重建的网格数量:{0},起点:{1}'.format(lots, dnline))
|
self.writeCtaLog(u'需要重建的网格数量:{0},起点:{1}'.format(lots, dnline))
|
||||||
|
|
||||||
if lots > 0:
|
if lots > 0:
|
||||||
|
|
||||||
for i in range(0, lots, 1):
|
for i in range(0, lots, 1):
|
||||||
# 做多,开仓价为下阻力线-网格高度*i,平仓价为开仓价+止盈高度,开仓数量为缺省
|
# 做多,开仓价为下阻力线-网格高度*i,平仓价为开仓价+止盈高度,开仓数量为缺省
|
||||||
|
open_price = int((dnline - self.gridHeight * (i - 1 + dnRate)* dnRate) / self.minDiff ) * self.minDiff
|
||||||
|
close_price = int((open_price + self.gridWin* dnRate)/self.minDiff) * self.minDiff
|
||||||
|
|
||||||
grid = CtaGrid(direction=DIRECTION_LONG,
|
grid = CtaGrid(direction=DIRECTION_LONG,
|
||||||
openprice=dnline - self.gridHeight * i,
|
openprice=open_price,
|
||||||
closeprice=dnline - self.gridHeight * i + self.gridWin,
|
closeprice=close_price,
|
||||||
volume=self.volume*self.getVolume(remainLots+i))
|
volume=self.volume*self.getVolumeRate(remainLots + i))
|
||||||
|
|
||||||
self.dnGrids.append(grid)
|
self.dnGrids.append(grid)
|
||||||
self.writeCtaLog(u'重新拉下网格:[{0}~{1}]'.format(dnline, dnline-self.gridHeight * lots))
|
self.writeCtaLog(u'重新拉下网格:[{0}~{1}]'.format(dnline, dnline-self.gridHeight * lots))
|
||||||
@ -434,21 +449,30 @@ class CtaGridTrade(object):
|
|||||||
if lots > 0:
|
if lots > 0:
|
||||||
# 做空,开仓价为上阻力线+网格高度*i,平仓价为开仓价-止盈高度,开仓数量为缺省
|
# 做空,开仓价为上阻力线+网格高度*i,平仓价为开仓价-止盈高度,开仓数量为缺省
|
||||||
for i in range(0, lots, 1):
|
for i in range(0, lots, 1):
|
||||||
|
open_price = int((upline + self.gridHeight *( i -1 + upRate) * upRate) / self.minDiff) * self.minDiff
|
||||||
|
close_price = int((open_price - self.gridWin * upRate) / self.minDiff) * self.minDiff
|
||||||
|
|
||||||
grid = CtaGrid(direction=DIRECTION_SHORT,
|
grid = CtaGrid(direction=DIRECTION_SHORT,
|
||||||
openprice=upline+self.gridHeight*i,
|
openprice=open_price,
|
||||||
closeprice=upline+self.gridHeight*i-self.gridWin,
|
closeprice=close_price,
|
||||||
volume=self.volume*self.getVolume(remainLots+i))
|
volume=self.volume*self.getVolumeRate(remainLots + i))
|
||||||
self.upGrids.append(grid)
|
self.upGrids.append(grid)
|
||||||
|
|
||||||
self.writeCtaLog(u'重新拉上网格:[{0}~{1}]'.format(upline, upline+self.gridHeight * lots))
|
self.writeCtaLog(u'重新拉上网格:[{0}~{1}]'.format(upline, upline+self.gridHeight * lots))
|
||||||
|
|
||||||
def recount_avg_open_price(self):
|
def recount_avg_open_price(self):
|
||||||
"""计算网格的平均开仓价"""
|
"""计算网格的平均开仓价"""
|
||||||
up_open_list = [ x for x in self.upGrids if x.openStatus]
|
up_open_list = [x for x in self.upGrids if x.openStatus]
|
||||||
|
|
||||||
|
self.max_up_open_price = -99999
|
||||||
|
self.avg_up_open_price = -99999
|
||||||
|
self.min_dn_open_price = 99999
|
||||||
|
self.avg_dn_open_price = 99999
|
||||||
|
|
||||||
total_price = EMPTY_FLOAT
|
total_price = EMPTY_FLOAT
|
||||||
total_volume = EMPTY_INT
|
total_volume = EMPTY_INT
|
||||||
for x in up_open_list:
|
for x in up_open_list:
|
||||||
|
self.max_up_open_price = max(self.max_up_open_price, x.openPrice)
|
||||||
total_price += x.openPrice*x.volume
|
total_price += x.openPrice*x.volume
|
||||||
total_volume += x.volume
|
total_volume += x.volume
|
||||||
|
|
||||||
@ -460,6 +484,7 @@ class CtaGridTrade(object):
|
|||||||
|
|
||||||
dn_open_list = [x for x in self.dnGrids if x.openStatus]
|
dn_open_list = [x for x in self.dnGrids if x.openStatus]
|
||||||
for x in dn_open_list:
|
for x in dn_open_list:
|
||||||
|
self.min_dn_open_price = min(self.min_dn_open_price,x.openPrice)
|
||||||
total_price += x.openPrice*x.volume
|
total_price += x.openPrice*x.volume
|
||||||
total_volume += x.volume
|
total_volume += x.volume
|
||||||
|
|
||||||
@ -468,6 +493,10 @@ class CtaGridTrade(object):
|
|||||||
|
|
||||||
def save(self, direction):
|
def save(self, direction):
|
||||||
"""保存网格至本地Json文件"""
|
"""保存网格至本地Json文件"""
|
||||||
|
|
||||||
|
# 更新开仓均价
|
||||||
|
self.recount_avg_open_price()
|
||||||
|
|
||||||
path = os.path.abspath(os.path.dirname(__file__))
|
path = os.path.abspath(os.path.dirname(__file__))
|
||||||
|
|
||||||
# 保存上网格列表
|
# 保存上网格列表
|
||||||
|
@ -203,11 +203,11 @@ class CtaLineBar(object):
|
|||||||
# K线的布林特计算数据
|
# K线的布林特计算数据
|
||||||
self.inputBollLen = EMPTY_INT # K线周期
|
self.inputBollLen = EMPTY_INT # K线周期
|
||||||
self.inputBollStdRate = 1.5 # 两倍标准差
|
self.inputBollStdRate = 1.5 # 两倍标准差
|
||||||
|
self.lineBollClose = [] # 用于运算的close价格列表
|
||||||
self.lineUpperBand = [] # 上轨
|
self.lineUpperBand = [] # 上轨
|
||||||
self.lineMiddleBand = [] # 中线
|
self.lineMiddleBand = [] # 中线
|
||||||
self.lineLowerBand = [] # 下轨
|
self.lineLowerBand = [] # 下轨
|
||||||
self.lineBollStd =[] # 标准差
|
self.lineBollStd = [] # 标准差
|
||||||
|
|
||||||
self.lastBollUpper = EMPTY_FLOAT # 最后一根K的Boll上轨数值(与MinDiff取整)
|
self.lastBollUpper = EMPTY_FLOAT # 最后一根K的Boll上轨数值(与MinDiff取整)
|
||||||
self.lastBollMiddle = EMPTY_FLOAT # 最后一根K的Boll中轨数值(与MinDiff取整)
|
self.lastBollMiddle = EMPTY_FLOAT # 最后一根K的Boll中轨数值(与MinDiff取整)
|
||||||
|
Loading…
Reference in New Issue
Block a user