49

I am a calling a function like this in python:

order_id = kite.order_place(
    self, exchange, tradingsymbol, transaction_type,
    quantity, price, product, order_type, validity,
    disclosed_quantity=None, trigger_price=None, squareoff_value, 
    stoploss_value, trailing_stoploss, variety, tag='')

Here is the code from the documentation of the function:

def order_place(
    self, exchange, tradingsymbol, transaction_type,
    quantity, price=None, product=None, order_type=None, validity=None,
    disclosed_quantity=None, trigger_price=None, squareoff_value=None,
    stoploss_value=None, trailing_stoploss=None, variety='regular', tag='')

It is giving an error like this:

  File "csvr.py", line 7
    stoploss_value, trailing_stoploss, variety, tag='')
                                                      ^
SyntaxError: positional argument follows keyword argument

How to resolve this error?

0

1 Answer 1

61

The grammar of the language specifies that positional arguments appear before keyword or starred arguments in calls:

argument_list        ::=  positional_arguments ["," starred_and_keywords]
                            ["," keywords_arguments]
                          | starred_and_keywords ["," keywords_arguments]
                          | keywords_arguments

Specifically, a keyword argument looks like this: tag='insider trading!' while a positional argument looks like this: ..., exchange, ....

The problem lies in that you appear to have copy/pasted the parameter list, and left some of the default values in place, which makes them keyword arguments. This is fine, except that you then go back to using positional arguments, which is a syntax error.

Also, when an argument has a default value, such as disclosed_quantity=None, that means you don't have to provide it. If you don't provide it, it will use the default value instead.

Also, self is handled specially and can be left off.

order_id = kite.order_place(
    exchange, tradingsymbol, transaction_type,
    quantity, price, product, order_type, validity,
    squareoff_value, 
    stoploss_value, trailing_stoploss, variety)

Another option is to convert your later positional arguments into keyword arguments:

order_id = kite.order_place(
    exchange, tradingsymbol, transaction_type,
    quantity, price, product, order_type, validity,
    disclosed_quantity=None,
    trigger_price=None,
    squareoff_value=squareoff_value,
    stoploss_value=stoploss_value,
    trailing_stoploss=trailing_stoploss,
    variety=variety,
    tag='')

Or you could even use keyword arguments for everything:

order_id = kite.order_place(
    exchange=exchange,
    tradingsymbol=tradingsymbol,
    transaction_type=transaction_type,
    quantity=quantity,
    price=price,
    product=product,
    order_type=order_type,
    validity=validity,
    disclosed_quantity=None,
    ...
Sign up to request clarification or add additional context in comments.

1 Comment

this is correct, @Ardour Technologies please mark this as correct, to summarize what he is saying - you can either specify arguments like so: function1("arg1", "arg2", "arg3") or you can specify them like so: function1(arg3="arg3", arg1="arg1", arg2="arg2" (that is assuming that each of the argunets is optional)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.