def generateTrade(self, tradingSymbol, direction, breakoutPrice): trade = Trade(tradingSymbol) trade.strategy = self.getName() trade.direction = direction trade.productType = self.productType trade.placeMarketOrder = True trade.requestedEntry = breakoutPrice trade.timestamp = self.startTimestamp # setting this to strategy timestamp trade.qty = int(self.calculateCapitalPerTrade() / breakoutPrice) if trade.qty == 0: trade.qty = 1 # Keep min 1 qty if direction == 'LONG': trade.stopLoss = Utils.roundToNSEPrice(breakoutPrice - breakoutPrice * self.slPercentage / 100) else: trade.stopLoss = Utils.roundToNSEPrice(breakoutPrice + breakoutPrice * self.slPercentage / 100) if direction == 'LONG': trade.target = Utils.roundToNSEPrice(breakoutPrice + breakoutPrice * self.targetPerncetage / 100) else: trade.target = Utils.roundToNSEPrice(breakoutPrice - breakoutPrice * self.targetPerncetage / 100) # Hand over the trade to TradeManager TradeManager.addNewTrade(trade) # add symbol to created list self.tradesCreatedSymbols.append(tradingSymbol)
def generateTrade(self, tradingSymbol, direction, high, low): trade = Trade(tradingSymbol) trade.strategy = self.getName() trade.isFutures = True trade.direction = direction trade.productType = self.productType trade.placeMarketOrder = True trade.requestedEntry = high if direction == Direction.LONG else low trade.timestamp = Utils.getEpoch( self.startTimestamp) # setting this to strategy timestamp # Calculate lots numLots = self.calculateLotsPerTrade() isd = Instruments.getInstrumentDataBySymbol( tradingSymbol) # Get instrument data to know qty per lot trade.qty = isd['lot_size'] trade.stopLoss = low if direction == Direction.LONG else high slDiff = high - low # target is 1.5 times of SL if direction == 'LONG': trade.target = Utils.roundToNSEPrice(trade.requestedEntry + 1.5 * slDiff) else: trade.target = Utils.roundToNSEPrice(trade.requestedEntry - 1.5 * slDiff) trade.intradaySquareOffTimestamp = Utils.getEpoch( self.squareOffTimestamp) # Hand over the trade to TradeManager TradeManager.addNewTrade(trade)
def convertJSONToTrade(jsonData): trade = Trade(jsonData['tradingSymbol']) trade.tradeID = jsonData['tradeID'] trade.strategy = jsonData['strategy'] trade.direction = jsonData['direction'] trade.productType = jsonData['productType'] trade.isFutures = jsonData['isFutures'] trade.isOptions = jsonData['isOptions'] trade.optionType = jsonData['optionType'] trade.placeMarketOrder = jsonData['placeMarketOrder'] trade.intradaySquareOffTimestamp = jsonData['intradaySquareOffTimestamp'] trade.requestedEntry = jsonData['requestedEntry'] trade.entry = jsonData['entry'] trade.qty = jsonData['qty'] trade.filledQty = jsonData['filledQty'] trade.initialStopLoss = jsonData['initialStopLoss'] trade.stopLoss = jsonData['stopLoss'] trade.target = jsonData['target'] trade.cmp = jsonData['cmp'] trade.tradeState = jsonData['tradeState'] trade.timestamp = jsonData['timestamp'] trade.createTimestamp = jsonData['createTimestamp'] trade.startTimestamp = jsonData['startTimestamp'] trade.endTimestamp = jsonData['endTimestamp'] trade.pnl = jsonData['pnl'] trade.pnlPercentage = jsonData['pnlPercentage'] trade.exit = jsonData['exit'] trade.exitReason = jsonData['exitReason'] trade.exchange = jsonData['exchange'] trade.entryOrder = TradeManager.convertJSONToOrder(jsonData['entryOrder']) trade.slOrder = TradeManager.convertJSONToOrder(jsonData['slOrder']) trade.targetOrder = TradeManager.convertJSONToOrder(jsonData['targetOrder']) return trade
def generateTrade(self, optionSymbol, numLots, lastTradedPrice): trade = Trade(optionSymbol) trade.strategy = self.getName() trade.isOptions = True trade.direction = Direction.SHORT # Always short here as option selling only trade.productType = self.productType trade.placeMarketOrder = True trade.requestedEntry = lastTradedPrice trade.timestamp = Utils.getEpoch(self.startTimestamp) # setting this to strategy timestamp isd = Instruments.getInstrumentDataBySymbol(optionSymbol) # Get instrument data to know qty per lot trade.qty = isd['lot_size'] * numLots trade.stopLoss = Utils.roundToNSEPrice(trade.requestedEntry + trade.requestedEntry * self.slPercentage / 100) trade.target = 0 # setting to 0 as no target is applicable for this trade trade.intradaySquareOffTimestamp = Utils.getEpoch(self.squareOffTimestamp) # Hand over the trade to TradeManager TradeManager.addNewTrade(trade)
def generateTrade(self, tradingSymbol, direction, breakoutPrice, cmp): trade = Trade(tradingSymbol) trade.strategy = self.getName() trade.direction = direction trade.productType = self.productType trade.placeMarketOrder = True trade.requestedEntry = breakoutPrice trade.timestamp = Utils.getEpoch( self.startTimestamp) # setting this to strategy timestamp trade.qty = int(self.calculateCapitalPerTrade() / breakoutPrice) if trade.qty == 0: trade.qty = 1 # Keep min 1 qty if direction == 'LONG': trade.stopLoss = Utils.roundToNSEPrice(breakoutPrice - breakoutPrice * self.slPercentage / 100) if cmp < trade.stopLoss: trade.stopLoss = Utils.roundToNSEPrice(cmp - cmp * 1 / 100) else: trade.stopLoss = Utils.roundToNSEPrice(breakoutPrice + breakoutPrice * self.slPercentage / 100) if cmp > trade.stopLoss: trade.stopLoss = Utils.roundToNSEPrice(cmp + cmp * 1 / 100) if direction == 'LONG': trade.target = Utils.roundToNSEPrice(breakoutPrice + breakoutPrice * self.targetPercentage / 100) else: trade.target = Utils.roundToNSEPrice(breakoutPrice - breakoutPrice * self.targetPercentage / 100) trade.intradaySquareOffTimestamp = Utils.getEpoch( self.squareOffTimestamp) # Hand over the trade to TradeManager TradeManager.addNewTrade(trade)