Skip to content

Instantly share code, notes, and snippets.

@LiorB-D
Created July 9, 2021 00:59
Show Gist options
  • Select an option

  • Save LiorB-D/2a14ef6d898caa8d3707829832cac41b to your computer and use it in GitHub Desktop.

Select an option

Save LiorB-D/2a14ef6d898caa8d3707829832cac41b to your computer and use it in GitHub Desktop.
import alpaca_trade_api as tradeapi
SEC_KEY = '' # Enter Your Secret Key Here
PUB_KEY = '' # Enter Your Public Key Here
BASE_URL = 'https://paper-api.alpaca.markets' # This is the base URL for paper trading
api = tradeapi.REST(key_id= PUB_KEY, secret_key=SEC_KEY, base_url=BASE_URL) # For real trading, don't enter a base_url
# Buy a stock
api.submit_order(
symbol='SPY'; # Replace with the ticker of the stock you want to buy
qty=1,
side='buy',
type='market',
time_in_force='gtc' # Good 'til cancelled
)
# Sell a stock(Just change side to 'sell')
api.submit_order(
symbol='SPY',
qty=1,
side='sell',
type='market',
time_in_force='gtc'
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment