'IB API - Specify order amount

I am doing algo-trading using the IB Python API. When my algorithm finds a suitable trade, it queries the last ask price. With this price and the amount of money I want to invest per trade, the order size is specified. For example, if I want to invest 2,500$ and the price per share is 25$, the order size is 100.

order.totalQuantity = round((2500 // price), 0)

I sometimes have the problem that the query gives a prices of zero, especially for very small capitalized stocks even though there is a price when I look for the stock in TWS. If the price found is zero, I can't calculate an order size and my order fails.

Does someone have an idea how to solve this? For example, is there a way to specify an order quantity relative to my investment amount in a stock without using the latest ask price?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source