Navigation

    SmartAPI Forum
    • Register
    • Login
    • Search
    • Categories
    • Popular
    • Groups
    • FAQs
    • API Docs

    square off

    General Discussion
    2
    3
    91
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • R
      rishi6310 last edited by

      @admin how to square off position in python can you provide code for it

      A 2 Replies Last reply Reply Quote 0
      • A
        admin @rishi6310 last edited by admin

        This post is deleted!
        1 Reply Last reply Reply Quote 0
        • A
          admin @rishi6310 last edited by admin

          @rishi6310 This is a complete user specific requirement but you can go through below piece of code for reference and try to write your own piece of code.
          def closePositionOnStart(position):
          for i in range(len(position)):
          pos = position[i]
          log.info(f'{pos["tradingsymbol"]}, {pos["symboltoken"]}, {pos["netqty"]}, {pos["producttype"]}')

              if int(pos["netqty"]) == 0:
                      continue
              elif int(pos["netqty"]) > 0:
                  _transactiontype = "SELL"
              else:
                  _transactiontype = "BUY"
              # pprint(pos)
              
              orderId = None
              try:
                  orderparams = {
                                  "variety": "NORMAL",
                                  "tradingsymbol":  pos["tradingsymbol"],
                                  "symboltoken": pos["symboltoken"],
                                  "transactiontype": _transactiontype,
                                  "exchange": "NSE",
                                  "ordertype": "MARKET",
                                  "producttype": pos['producttype'],
                                  "duration": "DAY",
                                  "price": 0,
                                  "triggerprice": 0,
                                  "quantity": abs(int(pos["netqty"]))
                                  }
                   
                  log.info(f'{orderparams}')
                  orderId=sapi.placeOrder(orderparams)
                 
              except Exception as e :
                  log.info(f"Order placement failed: {traceback.print_exc()}")
                  log.info('MESSAGE :: ', e)
                  
              log.info(f"The order CLOSED is: {orderId}")
              print('\n')    
          

          def closeAll():
          try:
          position = sapi.position()
          pos = position["data"]

              orderBook = sapi.orderBook()
              ordb = orderBook["data"]
              
              if len(pos) != 0:
                  closePositionOnStart(pos)  
                  
              if len(ordb) != 0:
                  closePendingOrders(ordb)    
          except:
              log.info('EXCEPTION : IN CLOSING POSITION AT START..!!!')
          pass
          
          1 Reply Last reply Reply Quote 0
          • First post
            Last post