def startAlgo(): if Algo.isAlgoRunning == True: logging.info("Algo has already started..") return logging.info("Starting Algo...") Instruments.fetchInstruments() # start trade manager in a separate thread tm = threading.Thread(target=TradeManager.run) tm.start() # sleep for 2 seconds for TradeManager to get initialized time.sleep(2) # start running strategies: Run each strategy in a separate thread #threading.Thread(target=SampleStrategy.getInstance().run).start() #threading.Thread(target=BNFORB30Min.getInstance().run).start() #threading.Thread(target=OptionSelling.getInstance().run).start() threading.Thread(target=ShortStraddleBNF.getInstance().run).start() #threading.Thread(target=OptionBuyingStrategy.getInstance().run).start() #threading.Thread(target=TestStrategy.getInstance().run).start() Algo.isAlgoRunning = True logging.info("Algo started.")
def startAlgo(): if Algo.isAlgoRunning == True: logging.info("Algo has already started..") return logging.info("Starting Algo...") Instruments.fetchInstruments() Algo.isAlgoRunning = True logging.info("Algo started.") Test.testTicker()
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, 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)
def unregisterSymbols(self, symbols): tokens = [] for symbol in symbols: isd = Instruments.getInstrumentDataBySymbol(symbol) token = isd['instrument_token'] logging.info('ZerodhaTicker unregisterSymbols: %s token = %s', symbol, token) tokens.append(token) logging.info('ZerodhaTicker Unsubscribing tokens %s', tokens) self.ticker.unsubscribe(tokens)
def on_ticks(self, ws, brokerTicks): # convert broker specific Ticks to our system specific Ticks (models.TickData) and pass to super class function ticks = [] for bTick in brokerTicks: isd = Instruments.getInstrumentDataByToken(bTick['instrument_token']) tradingSymbol = isd['tradingsymbol'] tick = TickData(tradingSymbol) tick.lastTradedPrice = bTick['last_price'] tick.lastTradedQuantity = bTick['last_quantity'] tick.avgTradedPrice = bTick['average_price'] tick.volume = bTick['volume'] tick.totalBuyQuantity = bTick['buy_quantity'] tick.totalSellQuantity = bTick['sell_quantity'] tick.open = bTick['ohlc']['open'] tick.high = bTick['ohlc']['high'] tick.low = bTick['ohlc']['low'] tick.close = bTick['ohlc']['close'] tick.change = bTick['change'] ticks.append(tick) self.onNewTicks(ticks)