Hello there:
I am starting to learn how to use IbridgePy for trading in Interactive Brokers, and I wanted to write a code that actually sends an order to buy 1 share of apple once it gets to a specified price ( to practice).
Neverteless The code that I have, does indeed send a market order to buy 1 share of apple, but the problem is that it sends an order very time apple passes to the specified price, when I just wanted to place the order once…
def initialize(context):
context.run_once = True
context.flag = False
context.security = symbol('AAPL')
def handle_data(context, data):
print (get_datetime().strftime("%Y-%m-%d %H:%M:%S %Z"))
print (data.current(context.security,'price'))
if data.current(context.security,'price')==154.13:
order(symbol('AAPL'),1 , style=MarketOrder())
context.flag = True
Could you help me please??
Hi Ghery,
You can set a threshold to buy a share like if data.current(context.security,'price') > 154.13: . And you can also check count_positions() function from IbridgePy documentation. This function returns the number of shares the account is currently holding. You can use this function to check if you already have a position open and avoid opening another one.
Thanks a lot, i'll try it out…