Reputation: 1
Trying to program an API trading algorithm- I need to check whether an order has been filled, and if it has been filled, place the new order, and if the order has not been filled or there is no order, do nothing (return). The variable that holds 'Filled' or 'Not filled' is only declared if there is an open order. I want it to basically send the order if the variable is 'Filled', and ALSO send the order if the variable is undefined, but NOT send the order if it is unfilled and undefined (no orders ever sent, ie. initial run through)...
The original problem is that it sends a second order even if the previous order is not filled yet, so I created a variable in the 'openOrders' function that shows the status of the order. But if there are no orders, the variable is undefined. So I want it to send the order if it is undefined (no orders), and send the order if the variable is 'filled'.
I had this code but the first line throws it off because it doesn't recognize none as undefined. Any suggestions?
if self.openOrderStatus == None: #<--
# SELL
elif self.openOrderStatus != 'Filled':
return
else:
# SELL
Upvotes: 0
Views: 58
Reputation: 295363
If you want to coerce attributes that don't exist to None
, an easy way to do that is with getattr()
with the default value filled in:
if getattr(self, 'openOrderStatus', None) == None: #<--
pass # SELL
elif getattr(self, 'openOrderStatus', None) != 'Filled':
return
else:
pass # SELL
However, it would be much better practice to initialize your objects such that every attribute you want always has a value, even if that value is None
.
Upvotes: 0
Reputation: 23079
Just give openOrderStatus
an initial value of None
when you create the containing object. That represents a non-existent order. Then the code you show us will work as is. For example:
class SomeClass:
def __init__(self):
self.openOrderStatus = None
...
def some_method(self):
if self.openOrderStatus == None: # <-- no throw now
# SELL
...
elif self.openOrderStatus != 'Filled':
return
else:
# SELL
...
Upvotes: 1