Shoonya-Dev / ShoonyaApi-py

144 stars 133 forks source link

Basket Order & Basket Margin API #45

Closed mohit051091 closed 5 months ago

mohit051091 commented 2 years ago

There is no function to check the margin before placing the order.

sanind commented 1 year ago

This is already available https://www.shoonya.com/api-documentation#basket-margin

I just extended the base class and created my own. Sample code

def GetBasketMargin(self, basket: list): url = f"{self.host}/GetBasketMargin" basket = basket if isinstance(basket, list) else [basket] values = {} for index, order in enumerate(basket): if index == 0: values["uid"] = self.userid values["actid"] = self.userid values["exch"] = order.exch values["tsym"] = order.tsym values["qty"] = order.qty values["prc"] = order.prc values["prd"] = order.prd values["trantype"] = order.trantype values["prctyp"] = order.prctyp values["basketlists"] = [] else: values["basketlists"].append({"exch": order.exch, "tsym": order.tsym, "qty": order.qty, "prc": order.prc, "prd": order.prd, "trantype": order.trantype, "prctyp": order.prctyp}) payload = 'jData=' + \ json.dumps(values) + f'&jKey={self.auth["susertoken"]}' logger.debug(f"{url}, {payload}") res = requests.post(url, data=payload) logger.debug(res.text) resDict = json.loads(res.text) return resDict

@dataclass class GetBasketMargin: exch: str tsym: str qty: str prc: str = "0" prd: str = "M" trantype: str = "S" prctyp: str = "MKT"

Shoonya-Dev commented 5 months ago

Greetings from Shoonya!

Please check the attached documentation; we already provide an order margin API and a basket margin API.

https://shoonya.com/api-documentation#order-margin

https://shoonya.com/api-documentation#basket-margin