Exemplo n.º 1
0
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."
    )
Exemplo n.º 2
0
def testTicker():
  startTicker()
  # sleep for 5 seconds and register trading symbols to receive ticks
  time.sleep(5)
  registerSymbols(['SBIN', 'RELIANCE'])

  time.sleep(5)
  exchange = 'NSE';
  tradingSymbol = 'SBIN'
  lastTradedPrice = getCMP(exchange + ':' + tradingSymbol)
  logging.info(tradingSymbol + ' CMP = %f', lastTradedPrice)
  qty = 1
  direction = 'SHORT'

  orderId = placeOrder(tradingSymbol, lastTradedPrice, qty, direction)
  logging.info('placed order: order id = %s', orderId)

  # wait for 120 seconds and stop ticker service
  time.sleep(120)
  logging.info('Going to stop ticker')
  stopTicker()
def main():
    # Test 1 for account funds - should be successful
    success, details = orders.list_orders(appkey, sesstoken)

    if success and type(details) == dict:
        print("Test 1 for getting order details successful")
    else:
        print(
            "Test 1 for getting order details failed, success:{0}, details:{1}"
            .format(success, details))

    # Test 2 for order details - should be unsuccessful

    success, details = orders.list_orders(appkey + "a", sesstoken)

    if success == False and type(details) == str:
        print("Test 2 for getting order details successful")
    else:
        print(
            "Test 2 for getting order details failed, success:{0}, details:{1}"
            .format(success, details))

    # Test 3 for placing an order - should be successful
    # current market is the us master winner
    #success, details = orders.placeOrder(appkey, sesstoken, 1.175611431, 13496411, "BACK", 2, 1 / 8.6)

    #print(details)

    #if success and type(details) == pd.DataFrame:
    #    print("Test 3 for placing order successful")
    #else:
    #    print("Test 3 for placing order failed, success:{0}, details:{1}".format(success, details))

    # Test 4 for order details including time ranges - should be successful

    success, details = orders.list_orders(
        appkey, sesstoken, datetime.datetime(2021, 1, 17, 13, 42, 0),
        datetime.datetime(2021, 1, 29, 23, 59, 00))

    print("Live orders!")
    print(details['live'])

    print("Filled orders!")
    print(details['fills'])

    print("Cancelled orders!")
    print(details['cancelled'])

    if success and type(details) == dict:
        print("Test 4 for getting order details successful")
    else:
        print(
            "Test 4 for getting order details failed, success:{0}, details:{1}"
            .format(success, details))

    # Test 5 for placing an order - should be unsuccessful due to invalid market id

    success, details = orders.placeOrder(appkey, sesstoken, 9999, 1222347,
                                         "BACK", 2, 1.4)

    if success == False and type(details) == str:
        print("Test 5 for placing order successful")
    else:
        print(
            "Test 5 for placing order failed, success:{0}, details:{1}".format(
                success, details))

    # Test 6 for placing an order - should be unsuccessful due to invalid app key

    success, details = orders.placeOrder(
        "u3aNksQGhdXhGldb", "cdo4jfG9+Ym4dtrd1HLhb4FGxHhRetqlsgBCTSSdLf8=",
        1.175611431, 1222347, "BACK", 2, 1.4)

    if success == False and type(details) == str:
        print("Test 6 for placing order successful")
    else:
        print(
            "Test 6 for placing order failed, success:{0}, details:{1}".format(
                success, details))

    # Test 7 for cancelling an order - should be successful
    incomplete_betid = 222909348937
    success, details = orders.cancelOrder(appkey, sesstoken, 1.175611431,
                                          incomplete_betid, 2)

    if success == True and type(details) == pd.DataFrame:
        print("Test 7 for cancelling order successful")
    else:
        print("Test 7 for cancelling order failed, success:{0}, details:{1}".
              format(success, details))

    # Test 8 for cancelling an order - should be unsuccessful

    success, details = orders.cancelOrder(appkey, sesstoken, 999, 222093076985,
                                          2)

    if success == False and type(details) == str:
        print("Test 8 for cancelling order successful")
    else:
        print("Test 8 for cancelling order failed, success:{0}, details:{1}".
              format(success, details))