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:
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)
Submit buy order with appropriate parameters including “client_order_id” (I already know how to do this)
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)
Once order is filled check for price again
Then submit sell order
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.
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).
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:
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
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)
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.