Last active
August 15, 2021 03:31
-
-
Save McKlayne/f740fcf0e2718b1064863e1638805302 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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