def addPosOrder(self, bar, addPosSignal): buyExecute, shortExecute = self.priceExecute(bar) holdLong = len(self.orderDict['orderFirstLongSet']|self.orderDict['orderSecondLongSet'])>0 holdShort = len(self.orderDict['orderFirstShortSet']|self.orderDict['orderSecondShortSet'])>0 algorithm = hlBreakSignal() if not (holdLong or holdShort): self.nPos = 0 self.orderLastList = [] else: lastOrderID = self.orderLastList[-1] op = self._orderPacks[lastOrderID] lastOrder = op.order.price_avg if lastOrder!=0: if addPosSignal: if op.order.direction == constant.DIRECTION_LONG and (self.nPos < self.posTime): if ((bar.close/lastOrder - 1) >= self.addPct) and ((bar.close/lastOrder - 1)<=2*self.addPct): self.nPos += 1 addPosLot = algorithm.addLotList(self.paraDict)[self.nPos-1]*self.lot*self.lotMultipler for orderID in self.timeLimitOrder(ctaBase.CTAORDER_BUY, bar.vtSymbol, buyExecute, max(addPosLot,1), 60).vtOrderIDs: self.globalStatus['addPos'] = (self.nPos, addPosLot) self.orderLastList.append(orderID) addOp = self._orderPacks[orderID] self.orderDict['addLongSet'].add(orderID) elif op.order.direction == constant.DIRECTION_SHORT and (self.nPos < self.posTime): if ((lastOrder/bar.close - 1) >= self.addPct) and ((lastOrder/bar.close - 1) <= 2*self.addPct): self.nPos += 1 addPosLot = algorithm.addLotList(self.paraDict)[self.nPos-1]*self.lot*self.lotMultipler for orderID in self.timeLimitOrder(ctaBase.CTAORDER_SHORT, bar.vtSymbol, shortExecute, max(addPosLot,1), 60).vtOrderIDs: self.globalStatus['addPos'] = (self.nPos, addPosLot) self.orderLastList.append(orderID) addOp = self._orderPacks[orderID] self.orderDict['addShortSet'].add(orderID)
def addPosSignal(self, addPosPeriod): dsCanAddPos = 0 arrayPrepared, amAddPos = self.arrayPrepared(addPosPeriod) if arrayPrepared: algorithm = hlBreakSignal() dsCanAddPos, dsSma, dsLma = algorithm.dsAdd(amAddPos, self.paraDict) return dsCanAddPos
def entrySignal(self, envPeriod, filterPeriod, signalPeriod, tradePeriod): entrySignal = 0 arrayPrepared1, amEnv = self.arrayPrepared(envPeriod) arrayPrepared2, amFilter = self.arrayPrepared(filterPeriod) arrayPrepared3, amSignal = self.arrayPrepared(signalPeriod) arrayPrepared4, amTrade = self.arrayPrepared(tradePeriod) arrayPrepared = arrayPrepared1 and arrayPrepared2 and arrayPrepared3 and arrayPrepared4 if arrayPrepared: algorithm = hlBreakSignal() adxCanTrade, adxTrend = algorithm.adxEnv(amEnv, self.paraDict) filterCanTrade, highVolPos = algorithm.fliterVol( amFilter, self.paraDict) if adxCanTrade == 1: highEntryBand, lowEntryBand = algorithm.hlEntryNorrowBand( amSignal, self.paraDict) filterVCanTrade = algorithm.filterNorrowPatternV( amSignal, self.paraDict) else: highEntryBand, lowEntryBand = algorithm.hlEntryWideBand( amSignal, self.paraDict) filterVCanTrade = algorithm.filterWidePatternV( amSignal, self.paraDict) breakHighest = (amTrade.close[-1] > highEntryBand[-2]) and ( amTrade.close[-2] <= highEntryBand[-2]) breakLowest = (amTrade.close[-1] < lowEntryBand[-2]) and ( amTrade.close[-2] >= lowEntryBand[-2]) self.globalStatus['adxCanTrade'] = adxCanTrade self.globalStatus['filterCanTrade'] = filterCanTrade self.globalStatus['breakHighest'] = breakHighest self.globalStatus['breakLowest'] = breakLowest self.globalStatus['adxTrend'] = adxTrend[-1] self.globalStatus['highEntryBand'] = highEntryBand[-1] self.globalStatus['lowEntryBand'] = lowEntryBand[-1] if highVolPos: self.lotMultipler = 0.5 else: self.lotMultipler = 1 if (filterCanTrade == 1) and (filterVCanTrade == 1): if not self.isStopControled(): if breakHighest: entrySignal = 1 elif breakLowest: entrySignal = -1 return entrySignal
def exitSignal(self, envPeriod, signalPeriod, addPosPeriod): highExitBand, lowExitBand = np.array([]) , np.array([]) dsCanAddPos = 0 arrayPrepared1, amEnv = self.arrayPrepared(envPeriod) arrayPrepared2, amSignal = self.arrayPrepared(signalPeriod) arrayPrepared3, amAddPos = self.arrayPrepared(addPosPeriod) if arrayPrepared1 and arrayPrepared2 and arrayPrepared3: algorithm = hlBreakSignal() adxCanTrade, adxTrend = algorithm.adxEnv(amEnv, self.paraDict) if adxCanTrade ==1: highExitBand, lowExitBand = algorithm.hlExitWideBand(amSignal, self.paraDict) else: highExitBand, lowExitBand = algorithm.hlExitNorrowBand(amSignal, self.paraDict) dsCanAddPos, dsSma, dsLma = algorithm.dsAdd(amAddPos, self.paraDict) return highExitBand, lowExitBand, dsCanAddPos
def __init__(self, ctaEngine, setting): super().__init__(ctaEngine, setting) self.paraDict = setting self.barPeriod = 900 self.nPos = 0 self.lot = 0 self.symbol = self.symbolList[0] self.orderAllList = [] # 实例化信号 self.algorithm = hlBreakSignal() # 订单的集合 self.orderDict = { 'orderLongSet': set(), 'orderShortSet': set(), 'order2LongSet': set(), 'order2ShortSet': set(), 'addLongSet': set(), 'addShortSet': set(), } # 画图数据的字典 self.chartLog = { 'datetime': [], 'highEntryBand': [], 'lowEntryBand': [], # 'density': [], 'dsSma': [], 'dsLma': [], 'cmiMa': [], 'atr': [], 'rsi': [], 'sma': [], 'lma': [], 'ema': [] # 'delayHigh':[], # 'delayLow':[], # 'newHigh':[], # 'newLow':[], } # 打印全局信号的字典 self.globalStatus = {}