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)

1 Like

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.

2 Likes

i do have a funded live acount with alpaca, and i was successful in accessing
the polygon.io websocket real-time quotes.
but in trading, i only need to work with one stock and do buy and sell,with the
real time bid and ask quotes i obtained for that exact ticker of moment.
so, what’s the simplest way to submit an order to buy and sell? using the alpaca
api or polygon api(if i am allowed)? or do you think i should put something
together(buy.py, sell.py) for you to correct for me? pls help me get started
trading.

@Joe_Ching The only way to place orders is with the Alpaca APIs. Polygon is only for getting market data.

Placing an order is as simple as a single line of code in Python

api.submit_order('AAPL', side='buy' qty=1, type='market', time_in_force='gtc')

Maybe look at this forum post as a good tutorial on using Google colab for an interactive notebook to code and place orders.

than you very much. i was caught in the maze for a while.