Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save McKlayne/f740fcf0e2718b1064863e1638805302 to your computer and use it in GitHub Desktop.
Save McKlayne/f740fcf0e2718b1064863e1638805302 to your computer and use it in GitHub Desktop.
os.environ["APCA_API_BASE_URL"] = "https://paper-api.alpaca.markets"
api = tradeapi.REST('{YOUR_ALPACA_API_KEY}','{YOUR_ALPACA_KEY_SECRET}', api_version='v2')
for ticker in top_movers.ticker.head(10):
try:
# Submit a market order to buy an amount of each ticker that was pulled from the top mover list
api.submit_order(
symbol=ticker,
qty=1,
side='buy',
type='market',
time_in_force='gtc'
)
except:
# log out the tickers that did not work. Not all tickers are avaiable via the Alpaca API.
print(f"A market buy order was no opened for {ticker}")
#allow for all marekt buy orders to be placed before submitting the trailing stop orders
time.sleep(3)
for ticker in top_movers.ticker.head(10):
try:
# Submit trailing stop orders for each of the 10 stock with a trail of 1%
api.submit_order(
symbol=ticker,
qty=1,
side='sell',
type='trailing_stop',
trail_percent=1.0, # stop price will be hwm*0.99
time_in_force='gtc',
)
except:
# log out the tickers that did not work. Not all tickers are avaiable via the Alpaca API.
print(f"A trailing stop sell order was not opened for {ticker}")
for ticker in top_movers.ticker.head(10):
try:
# Submit a market order to buy an amount of each ticker that was pulled from the top mover list
api.submit_order(
symbol=ticker,
qty=1,
side='buy',
type='market',
time_in_force='gtc'
)
except:
# log out the tickers that did not work. Not all tickers are avaiable via the Alpaca API.
print(f"A market buy order was no opened for {ticker}")
#allow for all marekt buy orders to be placed before submitting the trailing stop orders
time.sleep(3)
for ticker in top_movers.ticker.head(10):
try:
# Submit trailing stop orders for each of the 10 stock with a trail of 1%
api.submit_order(
symbol=ticker,
qty=1,
side='sell',
type='trailing_stop',
trail_percent=1.0, # stop price will be hwm*0.99
time_in_force='gtc',
)
except:
# log out the tickers that did not work. Not all tickers are avaiable via the Alpaca API.
print(f"A trailing stop sell order was not opened for {ticker}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment