Basket margin error

srinidhihebbar
def get_basket_margin(call_buy_strike,call_strike_sell,put_buy_strike,put_strike_sell,qty):
global initial_margin
global final_margin
basket_order_margin=kite.basket_order_margins([{
"exchange": "NFO",
"tradingsymbol": call_buy_strike['tradingsymbol'],
"transaction_type": "BUY",
"variety": "regular",
"product": "MIS",
"order_type": "MARKET",
"quantity": qty,
"price": 0,
"trigger_price": 0,
},{
"exchange": "NFO",
"tradingsymbol": call_strike_sell['tradingsymbol'],
"transaction_type": "SELL",
"variety": "regular",
"product": "MIS",
"order_type": "MARKET",
"quantity": qty,
"price": 0,
"trigger_price": 0
},{"exchange": "NFO",
"tradingsymbol": put_buy_strike['tradingsymbol'],
"transaction_type": "BUY",
"variety": "regular",
"product": "MIS",
"order_type": "MARKET",
"quantity": qty,
"price": 0,
"trigger_price": 0},{"exchange": "NFO",
"tradingsymbol": put_strike_sell['tradingsymbol'],
"transaction_type": "BUY",
"variety": "regular",
"product": "MIS",
"order_type": "MARKET",
"quantity": qty,
"price": 0,
"trigger_price": 0}])




initial_margin=basket_order_margin['initial']['total']
final_margin=basket_order_margin['final']['total']



The above code is throwing an error as Object of type int64 is not JSON serializable
  • srinidhihebbar
    Run it in a loop too just to make sure that the error is not from my end, thank you for any help regarding this .
  • rakeshr
    I just tried the below basket order parameters and it's working fine. Recheck your order params and if still facing issue, you can enable debug logs and paste here the complete stake trace.
    [{
    'exchange': 'NFO',
    'tradingsymbol': 'BANKNIFTY23APR37000CE',
    'transaction_type': 'BUY',
    'variety': 'regular',
    'product': 'MIS',
    'order_type': 'MARKET',
    'quantity': 50,
    'price': 0,
    'trigger_price': 0,
    'squareoff': 0,
    'stoploss': 0,
    },
    {'exchange': 'NFO',
    'tradingsymbol': 'BANKNIFTY23APR37000PE',
    'transaction_type': 'SELL',
    'variety': 'regular',
    'product': 'MIS',
    'order_type': 'MARKET',
    'quantity': 50,
    'price': 0,
    'trigger_price': 0,
    'squareoff': 0,
    'stoploss': 0,
    },
    {"exchange": "NFO",
    "tradingsymbol": 'BANKNIFTY23APR43700PE',
    "transaction_type": "BUY",
    "variety": "regular",
    "product": "MIS",
    "order_type": "MARKET",
    "quantity": 50,
    "price": 0,
    "trigger_price": 0},
    {"exchange": "NFO",
    "tradingsymbol": 'BANKNIFTY23APR43700CE',
    "transaction_type": "BUY",
    "variety": "regular",
    "product": "MIS",
    "order_type": "MARKET",
    "quantity": 50,
    "price": 0,
    "trigger_price": 0
    }]
  • srinidhihebbar
    Rakesh, please run it in a loop and check, it is throwing an error, 7 out of 10 times.
  • srinidhihebbar

    Please find the screenshot for reference
  • rakeshr
    please run it in a loop and check, it is throwing an error, 7 out of 10 times.
    I ran it in the loop of 20. Still no issue.
    Can you paste here the debug log of the request?
  • srinidhihebbar
    File "C:\Users\Achalah\miniconda3\lib\site-packages\spyder_kernels\customize\spyderpdb.py", line 881, in run
    super(SpyderPdb, self).run(cmd, globals, locals)

    File "C:\Users\Achalah\miniconda3\lib\bdb.py", line 597, in run
    exec(cmd, globals, locals)

    File "c:\users\achalah\untitled1.py", line 135, in <module>
    basket_order_margin = kite.basket_order_margins([{

    File "C:\Users\Achalah\miniconda3\lib\site-packages\kiteconnect\connect.py", line 784, in basket_order_margins
    return self._post("order.margins.basket",

    File "C:\Users\Achalah\miniconda3\lib\site-packages\kiteconnect\connect.py", line 855, in _post
    return self._request(route, "POST", url_args=url_args, params=params, is_json=is_json, query_params=query_params)

    File "C:\Users\Achalah\miniconda3\lib\site-packages\kiteconnect\connect.py", line 906, in _request
    raise e

    File "C:\Users\Achalah\miniconda3\lib\site-packages\kiteconnect\connect.py", line 894, in _request
    r = self.reqsession.request(method,

    File "C:\Users\Achalah\miniconda3\lib\site-packages\requests\sessions.py", line 573, in request
    prep = self.prepare_request(req)

    File "C:\Users\Achalah\miniconda3\lib\site-packages\requests\sessions.py", line 484, in prepare_request
    p.prepare(

    File "C:\Users\Achalah\miniconda3\lib\site-packages\requests\models.py", line 371, in prepare
    self.prepare_body(data, files, json)

    File "C:\Users\Achalah\miniconda3\lib\site-packages\requests\models.py", line 511, in prepare_body
    body = complexjson.dumps(json, allow_nan=False)

    File "C:\Users\Achalah\miniconda3\lib\json\__init__.py", line 238, in dumps
    **kw).encode(obj)

    File "C:\Users\Achalah\miniconda3\lib\json\encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)

    File "C:\Users\Achalah\miniconda3\lib\json\encoder.py", line 257, in iterencode
    return _iterencode(o, 0)

    File "C:\Users\Achalah\miniconda3\lib\json\encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '

    TypeError: Object of type int64 is not JSON serializable



    I
    n:(call_buy_strike, call_strike_sell, put_buy_strike, put_strike_sell, qty)
    Out[38]:
    (instrument_token 10660610
    exchange_token 41643
    tradingsymbol BANKNIFTY2332341400CE
    name BANKNIFTY
    last_price 0.0
    expiry 2023-03-23
    strike 41400.0
    tick_size 0.05
    lot_size 25
    instrument_type CE
    segment NFO-OPT
    exchange NFO
    Name: 1715, dtype: object,
    instrument_token 10652162
    exchange_token 41610
    tradingsymbol BANKNIFTY2332339900CE
    name BANKNIFTY
    last_price 0.0
    expiry 2023-03-23
    strike 39900.0
    tick_size 0.05
    lot_size 25
    instrument_type CE
    segment NFO-OPT
    exchange NFO
    Name: 1685, dtype: object,
    instrument_token 10643202
    exchange_token 41575
    tradingsymbol BANKNIFTY2332338400PE
    name BANKNIFTY
    last_price 0.0
    expiry 2023-03-23
    strike 38400.0
    tick_size 0.05
    lot_size 25
    instrument_type PE
    segment NFO-OPT
    exchange NFO
    Name: 1656, dtype: object,
    instrument_token 10652418
    exchange_token 41611
    tradingsymbol BANKNIFTY2332339900PE
    name BANKNIFTY
    last_price 0.0
    expiry 2023-03-23
    strike 39900.0
    tick_size 0.05
    lot_size 25
    instrument_type PE
    segment NFO-OPT
    exchange NFO
    Name: 1686, dtype: object,
    75)
  • srinidhihebbar
    I have posted the debug log, it is waiting for approval
  • srinidhihebbar
    Found the error, was passing numpy.int 64 instead of int for qty , please close the thread
  • srinidhihebbar
    Thank you for the support , Rakesh, have a good one!
This discussion has been closed.