square off


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


  • @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

  • This post is deleted!