Python: Buy/Sell Loop With Price Check and Fill Status Check

Hello all. I’m new to Python and Alpaca (but learning quickly with your help, thanks in advance).

I’m looking for a Python code snippet that will:

  1. Check for a stock price (I can’t find documentation on this but I’ve read that I can’t get live price data in Paper trading)
  2. Submit buy order with appropriate parameters including “client_order_id” (I already know how to do this)
  3. Listen for order to be filled (I’ve read about streams with “account_updates” and “trade_updates” but don’t know how to implement this within this scenario)
  4. Once order is filled check for price again
  5. Then submit sell order
  6. Loop back and repeat

So it would look something like this:

If stock price is less than xx.xx
---- Then submit buy order_abc
-------- Listen for order_abc to be filled
------------ If order_abc is filled
---------------- and if stock price is greater than xx.xx
-------------------- then submit sell order_xyz
Go back to start of loop (to buy again)

The prices would be hardcoded so no need to make them variables. I would need the snippet to be repeated several times for different price points so I can have an independent snippet/algo for different price segments for the same stock (hence I would name each buy order with a unique client_order_id).

I can’t find info on getting price data. I have read about streams and already know how to submit an order. I just can’t seem to put it all together.

EDIT: I also just realized that if it repeats I need to append something to the “client_order_id” to make it unique for the next round (or else I’d be submitting the same “client_order_id” to buy again.)

Any help would be greatly appreciated. And if there is someone out there who is willing to tutor me, I wouldn’t mind compensating them for their time to do even more complex algorithms.

Thanks all.

Maybe I’m going about this wrong. Maybe i don’t need a loop.

Would it be better to have two separate “if” statements (one for the buy and one for the sell) that check each other based on a “ready_status” variable that gets updated when they complete. And a “cycle_count” variable that keeps track of how many times they executed and adds 1 each time (which would also be used to append to the “client_order_id” to identify each order).

Am I making any sense at all?

Hi,
listening to trade_updates is definitely to way to go because, you need to make sure an order was filled before adding the sell order. otherwise you might short a stock, even if you didn’t mean to.

a good place to start would be this code example which demonstrate how to use the data streams:

and be sure to read the documentation here: https://github.com/alpacahq/alpaca-trade-api-python

Thanks @Shlomik . Unfortunately this is beyond my Python expertise. I wish there was more commenting to explain what each segment of the code does and how I can use it in a practical use-case.

Maybe I need to break it down:

Is there a simple code example that does the following?

Submit buy order
Listen for fill
Once filled, submit sell order

Is there another sample code that does the following:

If price is (greater than / less than / equal to)
Then submit order

Thanks

are you placing a market order? market orders are filled right away …so no need to wait for the order to be filled …if not, then what you can do is place a bracket order

check this guy repository: https://github.com/hackingthemarkets
he also has a youtube channel in which he shows you how to code in python Alpaca (and other brokers APIs)

No. It’s a limit order I think I have it figured out using the client_order_id like this:

my_order_variable = api.get_order_by_client_order_id(my_client_order_id)

if my_order_variable.status == ('filled'):

Thanks guys.