Beispiel #1
0
    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 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)
Beispiel #3
0
    def generateTrade(self, optionSymbol, numLots, lastTradedPrice,
                      counterPosition):
        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
        trade.slPercentage = 25
        trade.moveToCost = True
        trade.counterPosition = counterPosition

        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)
Beispiel #4
0
    def shouldPlaceTrade(self, trade, tick):
        # Each strategy should call this function from its own shouldPlaceTrade() method before working on its own logic
        if trade == None:
            return False
        if trade.qty == 0:
            return False
        numOfTradesPlaced = TradeManager.getNumberOfTradesPlacedByStrategy(
            self.getName())
        if numOfTradesPlaced >= self.maxTradesPerDay:
            TradeManager.disableTrade(trade, 'MaxTradesPerDayReached')
            return False

        return True
Beispiel #5
0
 def __init__(self, name):
   # NOTE: All the below properties should be set by the Derived Class (Specific to each strategy)
   self.name = name # strategy name
   self.enabled = True # Strategy will be run only when it is enabled
   self.productType = ProductType.MIS # MIS/NRML/CNC etc
   self.symbols = [] # List of stocks to be traded under this strategy
   self.slPercentage = 0
   self.targetPerncetage = 0
   self.startTimestamp = Utils.getMarketStartTime() # When to start the strategy. Default is Market start time
   self.stopTimestamp = None # This is not square off timestamp. This is the timestamp after which no new trades will be placed under this strategy but existing trades continue to be active.
   self.squareOffTimestamp = None # Square off time
   self.capital = 10000 # Capital to trade (This is the margin you allocate from your broker account for this strategy)
   self.leverage = 1 # 2x, 3x Etc
   self.maxTradesPerDay = 1 # Max number of trades per day under this strategy
   self.isFnO = False # Does this strategy trade in FnO or not
   self.capitalPerSet = 0 # Applicable if isFnO is True (Set means 1CE/1PE or 2CE/2PE etc based on your strategy logic)
   self.tradesCreatedSymbols = [] # Add symbol to this list when a trade is created
   # Register strategy with trade manager
   TradeManager.registerStrategy(self)
Beispiel #6
0
    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)
  def getTrailingSL(self, trade):
    if trade == None:
      return 0
    if trade.entry == 0:
      return 0
    lastTradedPrice = TradeManager.getLastTradedPrice(trade.tradingSymbol)
    if lastTradedPrice == 0:
      return 0

    trailSL = 0
    profitPoints = int(trade.entry - lastTradedPrice)
    if profitPoints >= 5:
      factor = int(profitPoints / 5)
      trailSL = Utils.roundToNSEPrice(trade.initialStopLoss - factor * 5)
    logging.info('%s: %s Returning trail SL %f', self.getName(), trade.tradingSymbol, trailSL)
    return trailSL
Beispiel #8
0
  def shouldPlaceTrade(self, trade, tick):
    # Each strategy should call this function from its own shouldPlaceTrade() method before working on its own logic
    if trade == None:
      return False
    if trade.qty == 0:
      TradeManager.disableTrade(trade, 'InvalidQuantity')
      return False

    now = datetime.now()
    if now > self.stopTimestamp:
      TradeManager.disableTrade(trade, 'NoNewTradesCutOffTimeReached')
      return False

    numOfTradesPlaced = TradeManager.getNumberOfTradesPlacedByStrategy(self.getName())
    if numOfTradesPlaced >= self.maxTradesPerDay:
      TradeManager.disableTrade(trade, 'MaxTradesPerDayReached')
      return False

    return True