Home >  > Vn.py学习记录四–日内交易

Vn.py学习记录四–日内交易

0

一、其他

def onBar(self,bar):
	#更新策略执行的时间(用于回测时记录发生时间)
        #从datasource拿到的bar.datetime是bar的结束时间,比如:9:00-9:05的time就是9:05=curDatetime因为策略中已经将9:05调整为9:00,所以这里要加回来。
	self.curDatetime = bar.datetime + timedelta(seconds = self.lineM5.barTimeInterval)

	#2 计算交易时间和平仓时间
	self.__timeWindow(bar.datetime)

	#推送tick到15分钻K线
	self.lineM5.addBar(bar)

def __timeWindow(self,dt):
	"""交易与平仓窗口"""
	self.tradeWindow = False
	self.closeWindow = False
	self.openWindow =  False


	#开市期,波动较大,用于判断止损止盈,或开仓
	if (dt.hour == 9 or dt.hour == 21) and dt.minute < 2:
		self.openWindow = Ture

	#日盘
	if dt.hour == 9 and dt.minute >= 0:
		self.tradeWindow = Ture
		return

	if dt.hour == 10:
		if dt.minute <= 15 or  dt.minute >= 30:
		self.tradeWindow = Ture
		return		

	if dt.hour == 11 and dt.minute <= 30:
		self.tradeWindow = Ture
		return

	if dt.hour == 13 and dt.minute >= 30:
		self.tradeWindow = Ture
		return

	if dt.hour == 14:

		if dt.minute < 59:
			self.tradeWindow = Ture
			return

		if dt.minute == 59: #日盘平仓
			self.closeWindow = Ture
			return

	#夜盘
	if dt.hour == 21 and dt.minute >= 0:
		self.tradeWindow = Ture
		return

	#上期 贵金属  次日 02:30

	if self.shortSymbol in NIGHT_MARKET_SQ1:

		if dt.hour == 22 or dt.hour == 23 or dt.hour == 0 or dt.hour == 1:
			self.tradeWindow = Ture
			return

		if dt.hour == 2:
			if dt.minute < 29: #收市前29分钟
				self.tradeWindow = Ture
				return

			if dt.minute == 29: #夜盘平仓
				self.closeWindow = Ture
				return

		return

	#上期 有色金额 黑色金额 沥青  次日 01:00

	if self.shortSymbol in NIGHT_MARKET_SQ2:
		if dt.hour == 22 or dt.hour == 23:
			self.tradeWindow = Ture
			return

		if dt.hour == 0:
			if dt.minute < 59: #收市前59分钟
				self.tradeWindow = Ture
				return

			if dt.minute == 59: #夜盘平仓
				self.closeWindow = Ture
				return

		return


	#上期  天然橡胶 23:00

	if self.shortSymbol in NIGHT_MARKET_SQ3:

		if dt.hour ==22:
			if dt.minute < 59: #收市前1分钟
				self.tradeWindow = Ture
				return

			if dt.minute == 59: #夜盘平仓
				self.closeWindow = Ture
				return

		return


#也可以写死
def __dailyClosecheck(self,bar):
	"""
	每天收盘前检查,如果是亏损单,则平掉
	"""
	if self.position.pos == 0 and self.entrust == 0:
		return False


	if bar.time not in ['14:45:00','14:50:00','14:55:00','22:45:00','22:50:00','22:55:00',]:
		return False

	#撤销未成交的订单
	if len(self.uncompletedOrders) > 0:
		for order in self.uncompletedOrders.keys():
			self.writeCtaLog("{},收盘前15分钟,仍未成交,取消委托单:{}".format(bar.datetime,order))
			self.cancelOrder(str(order))

		self.uncompletedOrders.clear()

	self.entrust = 0


	#强制平仓
	if self.position.pos > 0 and bar.close < self.policey.entryPrice + self.atr:
		self.writeCtaLog("强制日内平亏损多仓")
		#降低两个滑点
		orderid = self.sell(price = bar.close-2*self.minDiff, volume = self.inputSS, orderTime = self.curDatetime)
	    if orderid:
	    	#更新下单时间
	    	self.lastOrderTime = self.curDatetime
	    return Ture	

	if self.position.pos < 0 and bar.close > self.policey.entryPrice - self.atr:
		self.writeCtaLog("强制日内平亏损空仓")
		#降低两个滑点
		orderid = self.over(price = bar.close+2*self.minDiff, volume = self.inputSS, orderTime = self.curDatetime)
	    if orderid:
	    	#更新下单时间
	    	self.lastOrderTime = self.curDatetime
	    return Ture

    return Ture
本文暂无标签

发表评论

*

*