I want to help Noobs!

Josh. Thank you for the reply. I did a 4.5 hour Python tutorial today. Amazing stuff. Thank you for the links. I’d love to find something already built and tweak it some to process trades on rules that I want… such as
if todays price at 9:30:00 (open) is lower than yesterday’s closing price…buy x shares. And immediately process a sell limit order based on the fill price.

We shall see what I can dig up.

Thanks again!

@Brian_Devereaux, That’s definitely a good place to start. Check out these projects on GitHub:





Those examples and some youtubers have helped a lot. It’s a damn good feeling when you finally get things to run and you import prices and it buys through an if statement.

Also, alpha vantage says its default return is json. After hours of trying to extract a previous day’s close price, I figured out its returning a tuple, converted that to a dict, and extracted the data I needed from the dict because that’s all I could figure out how to do.

Now I want to get the a stocks open price directly when market opens, do you have a go to source for your stock data that you enjoy working with, and returns data fast?

After I’m completely done, would you be cool with me sending you the code for a quality check? Its going to be simple and stupid execution conditions but I just want to make sure I’m writing things in an efficient way and I want to execute 2 purchases at once…I think async is what I’m referring to.

Thanks,

Brian

1 Like

I’m getting frustrated with this one piece.
I want to use the alpaca streamconn to listen for trade_updates off a simple market buy.

Can you show me how?

Everything I try, I get an error of loop is already running

Or… teach me a way to receive fill price data. I tried to retrieve order information but it comes back in an unsubscribable object so i havent figured a way to extract specific data from that response.

1 Like

@Brian_Devereaux As for real-time market data, I would highly recommend using the Polygon.io API. Though you would need a live trading account to or you could create an account with them separate from Alpaca. The later could be quite expensive though depending on what plan you choose. They have plans that have latencies under just a few milliseconds.

2 Likes

@Brian_Devereaux

In regards to the streamconn object, this object uses the websocket protocol instead of http. An http connection is a unidirectional connection. The life cycle of an http request ends when the server responds to the client. A websocket connection is a bidirectional will remain open until the client or server terminated the connection. This allows the both the client & server to communicate with each other perpetually. A major benefit in the case of market/trading data, is that anytime there is an update on the server, the server will send that data to the client immediately.

2 Likes

If you’d like go ahead and send your code, I’ll take a look at it and give you some feedback. If you would like more clarification on streamconn, implementation of the websocket protocol, or asynchronous/non-asynchronous execution. I just let me know, I really enjoy sharing my knowledge & helping others with their code.

1 Like

the main help i need at this point is
Overall goal: submit a buy order. retrieve its fill price. submit a sell limit order based off its fill price. so I figured, since the streamconn listen is giving me the devil, I would set the ‘client_order_ID’, and do a loop with list_order and that ID until ‘filled_avg_price’ is no longer == to none for that particular order… store the price that is in the ‘filled_avg_price’ and submit my sell order based off that.

so here, i am trying to get a list of my orders. I have 4 at the moment, and was playing with the [:1] to pull the first one by itself. then i want to reach inside that order and pull ‘filled_avg_price’ out of it. i thought converting it to a pandas dataframe would be easiest… but honestly i dont know much coding at all and dont know anything about pandas, just trying to find examples in other codes and apply things until it works.

if extract_open < extract_close:
#buy_order = api.submit_order(“ENPH”, 1, “buy”, “market”, “day”)
get_order = api.list_orders()
order = get_order[:2]
print(order)
order = pd.DataFrame(order)

print(order)

here is my printed stuff

Yesterday’s Close 36.1300
Today’s Open 34.0500
[Order({ ‘asset_class’: ‘us_equity’,
‘asset_id’: ‘b0b6dd9d-8b9b-48a9-ba46-b9d54906e415’,
‘canceled_at’: None,
‘client_order_id’: ‘5a5a6b3c-5d28-4b16-9fee-a8c2982a3f09’,
‘created_at’: ‘2020-03-30T16:11:24.27903Z’,
‘expired_at’: None,
‘extended_hours’: False,
‘failed_at’: None,
‘filled_at’: None,
‘filled_avg_price’: None,
‘filled_qty’: ‘0’,
‘id’: ‘95a88880-bfdd-42c7-99e1-d6c0742854c4’,
‘legs’: None,
‘limit_price’: ‘200’,
‘order_class’: ‘’,
‘order_type’: ‘limit’,
‘qty’: ‘1’,
‘replaced_at’: None,
‘replaced_by’: None,
‘replaces’: None,
‘side’: ‘buy’,
‘status’: ‘new’,
‘stop_price’: None,
‘submitted_at’: ‘2020-03-30T16:11:24.243821Z’,
‘symbol’: ‘AAPL’,
‘time_in_force’: ‘day’,
‘type’: ‘limit’,
‘updated_at’: ‘2020-03-30T16:11:24.370317Z’})]
0
0 Order({ ‘asset_class’: ‘us_equity’,\n 'as…

so i think this tells me column is labeled 0 and row is labeled 0 in the dataframe. now how do i reach inside this and extract info??

i think i uploaded things right. check that out and let me know. the comments at the bottom are kind of my framework for what I want to happen

Hi, perhaps you can help with this error…trying just to replicate a paper API script to see if it works from Pycharm IDE…but get this error…here is the code and error…thanks in advance for your help.

import requests

API_KEY = “PKVPYB865NXXX25YZXXX”
SECRET_KEY = “BE76FFsBHgeZ46VXxxOHAInpuwW6V7VXHn1gTGsy”

BASE_URL = “https://paper-api.alpaca.markets
ACCOUNT_URL = “{}/V2/account”.format(BASE_URL)

r = requests.get(ACCOUNT_URL, headers={‘APCA-API-KEY-ID’: API_KEY, ‘APCA-API-SECRET-KEY’: SECRET_KEY})

print(r.content)

Error Message is:

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “alpaca_client.py”, line 6, in
account = api.get_account()
File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/alpaca_trade_api/rest.py”, line 171, in get_account
resp = self.get(’/account’)
File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/alpaca_trade_api/rest.py”, line 152, in get
return self._request(‘GET’, path, data)
File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/alpaca_trade_api/rest.py”, line 115, in _request
return self._one_request(method, url, opts, retry)
File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/alpaca_trade_api/rest.py”, line 144, in _one_request
raise APIError(error, http_error)
alpaca_trade_api.rest.APIError: account not found for 67527d0e-0357-4c10-b3fa-b1c2d6f825e7

Hey Josh. I deleted my Alpaca files off of github. I want to clean them up some and try my hand at figuring out the sticking point before submitting them to you.
In the past day or two, coding kind of clicked for me and I have a much better understanding of how things work. While waiting for your reply, I made a completely different program using TD Ameritrade’s API. I uploaded them https://github.com/BSDevereaux/TD-Ameritrade-OvC

I feel really good about this one, and was able to work through the same sticking points which I believe will help me with my Alpaca one. I also learned a little more about coding etiquettes and program structure. So, on to overhaul my Alpaca one. In the meantime, will you give my TDAm a look over. There’s is a little different, with authenticating before using. I know for 100% the authentication piece works…learned it from a youtube video and tried it out to submit a saved order. But the piece I’d like for you to look over is the main piece. Thanks for all you do.

@ari_de_souza, Did you previously store your keys in the environment variables?
Also, I would recommend not posting your actual keys publicly, it is a major security risk. Since you’ve posted them in this thread I would also recommend that you create another pair of keys and discontinue using the old ones.

1 Like

Sure no problem, I’ll take a look & get back to you with some feedback.

thanks for the reply…no I did not store the keys in the environment,(Need to figure out how to do that as it wasn’t in the tutorial I am using). The keys I posted are not complete for security…

@joshcogburn thanks so much for creating this thread.

I am a noob, and was wondering if you could help me figure out how to get the purchase price of stock.

No problem, do you have a live trading account with Alpaca or only a paper account?

1 Like

@joshcogburn I have a live trading account, but i want to practice w/ paper first.

im trying to create a simple scalping algorithm that sells the equity (e.g. Apple) if it’s a $1 higher than my purchase price. i noticed there is a scalping code example (https://github.com/alpacahq/example-scalping), but they used a moving average or something to determine when to sell.

Is it possible to adjust the stop price of a bracket order ? For example I submitted a buy side market bracket order and would like to raise the stop price if the price goes up. Basically implement a trailing stop. When I try to do a replace on the stop order to update the stop price I get a ‘unable to replace order, order is not open’ w/ an HTTP 422 code. I see that the stop order is in a ‘held’ status which seems to be the normal state for stop order portion of a bracket order. Any help or advice appreciated !

I think I found the answer to my question - in the Bracket Order section of https://alpaca.markets/docs/trading-on-alpaca/orders/ it mentions specifically “Order replacement is not supported”. So it appears that trailing stops are not supported - unless someone knows better ?

Hi @joshcogburn,

I’m part of a German student initiative that organizes a trading game competition for high school students in Germany to support their financial education. We are currently in the process of redeveloping our trading game from scratch. I came across Alpaca because we’re still looking for an API for the game to fetch market data. As I said, it’s just a game, so no one would actually execute any trades with real money, we just need market data.
Do you think Alpaca would be an appropriate API for our use case?

Thank you,
Oliver

1 Like