Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to pass None as a value to certain parameters #250

Open
addrob opened this issue Oct 27, 2024 · 3 comments
Open

Unable to pass None as a value to certain parameters #250

addrob opened this issue Oct 27, 2024 · 3 comments

Comments

@addrob
Copy link

addrob commented Oct 27, 2024

If you try to directly pass None as a value for

"qty",
"price",
"triggerPrice",
"takeProfit",
"stopLoss"

it will be passed to API as a "None" value.

My guess is that the pybit._http_manager._V5HTTPManager.prepare_payload method is responsible for that. It tries to cast values for these params straight to str without caring what type they were originally even if it was a NoneType.

@dextertd
Copy link
Collaborator

Why would you pass None? If you don't want to pass anything to a param, there's no need to pass it at all. Adding extra params with dead data is inefficient for an HTTP request.

@kolya5544
Copy link

Passing "None" values might be useful for complex logic, like passing the price only when the order is Limit:

client.place_order(
            category="linear",
            symbol="OPUSDT",
            side="Sell",
            orderType=orderType,
            qty="0",
            price=price if orderType == "Limit" else None,
            reduceOnly=True,
            closeOnTrigger=True
        )

Most libraries using JSON serialization will set the serializer to ignore null values for this reason specifically. It's also considered the best practice to omit null values for query string values (GET HTTP request).

Just my humble opinion 😃

@addrob
Copy link
Author

addrob commented Nov 10, 2024

Why would you pass None? If you don't want to pass anything to a param, there's no need to pass it at all. Adding extra params with dead data is inefficient for an HTTP request.

I want to be able to pass a None value to a method in cases of some conditional param value defining logic. And I obviously expect that a "prepare_payload" method would actually prepare a payload instead of spoiling it when I simply use regular Python syntax.

Just noticed that you actually deal with None's by excluding them from parameters dict, but you do it only for GET methods. Why won't you just do the same thing for other request types by adding smth like

parameters = {k: v for k, v in parameters.items() if v is not None}

before casting values ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants