def startAlgo(): logging.info("Algo started...") kite = getKite() fetchInstruments(kite) #testOrders() testTicker()
def getCMP(tradingSymbol): kite = getKite() quote = kite.quote(tradingSymbol) if quote: return quote[tradingSymbol]['last_price'] else: return 0
def cancelOrder(orderId): logging.info('Going to cancel order %s', orderId) kite = getKite() try: orderId = kite.cancel_order(variety=kite.VARIETY_REGULAR, order_id=orderId) logging.info('Order cancelled successfully, orderId = %s', orderId) return orderId except Exception as e: logging.info('Order cancel failed: %s', e.message)
def modifyOrder(orderId, newPrice=0, newQty=0): logging.info('Going to modify order %s %f %d', orderId, newPrice, newQty) kite = getKite() try: orderId = kite.modify_order(variety=kite.VARIETY_REGULAR, order_id=orderId, quantity=newQty if newQty > 0 else None, price=newPrice if newPrice > 0 else None) logging.info('Order modified successfully, orderId = %s', orderId) return orderId except Exception as e: logging.info('Order modify failed: %s', e.message)
def startAlgo(): logging.info("Algo started...") kite = getKite() fetchInstruments(kite) exchange = 'NSE' tradingSymbol = 'SBIN' lastTradedPrice = getCMP(exchange + ':' + tradingSymbol) logging.info(tradingSymbol + ' CMP = %f', lastTradedPrice) limitPrice = lastTradedPrice - lastTradedPrice * 1 / 100 limitPrice = roundToNSEPrice(limitPrice) qty = 1 direction = 'LONG' # place order origOrderId = placeOrder(tradingSymbol, limitPrice, qty, direction) logging.info('Original order Id %s', origOrderId) # sleep for 10 seconds then modify order time.sleep(10) newPrice = lastTradedPrice modifyOrder(origOrderId, newPrice) # sleep for 10 seconds and then place SL order time.sleep(10) slPrice = newPrice - newPrice * 1 / 100 slPrice = roundToNSEPrice(slPrice) slDirection = 'SHORT' if direction == 'LONG' else 'LONG' slOrderId = placeSLOrder(tradingSymbol, slPrice, qty, slDirection) logging.info('SL order Id %s', slOrderId) # sleep for 10 seconds and then place target order time.sleep(10) targetPrice = newPrice + newPrice * 2 / 100 targetPrice = roundToNSEPrice(targetPrice) targetDirection = 'SHORT' if direction == 'LONG' else 'LONG' targetOrderId = placeOrder(tradingSymbol, targetPrice, qty, targetDirection) logging.info('Target order Id %s', targetOrderId) # sleep for 10 seconds and cancel target order time.sleep(10) cancelOrder(targetOrderId) logging.info('Cancelled Target order Id %s', targetOrderId) logging.info( "Algo done executing all orders. Check ur orders and positions in broker terminal." )
def placeSLOrder(tradingSymbol, triggerPrice, qty, direction): logging.info('Going to place SL order %s %f %d %s', tradingSymbol, triggerPrice, qty, direction) kite = getKite() try: orderId = kite.place_order( variety=kite.VARIETY_REGULAR, exchange=kite.EXCHANGE_NSE, tradingsymbol=tradingSymbol, transaction_type=kite.TRANSACTION_TYPE_BUY if direction == 'LONG' else kite.TRANSACTION_TYPE_SELL, quantity=qty, trigger_price=triggerPrice, product=kite.PRODUCT_MIS, order_type=kite.ORDER_TYPE_SLM) logging.info('SL Order placed successfully, orderId = %s', orderId) return orderId except Exception as e: logging.info('SL Order placement failed: %s', e.message)
def holdings(): kite = getKite() holdings = kite.holdings() print('getKite holdings => ', holdings) return json.dumps(holdings)
def positions(): kite = getKite() positions = kite.positions() print('getKite positions => ', positions) return json.dumps(positions)