-
Notifications
You must be signed in to change notification settings - Fork 3
/
alpaca_bot_tester.py
57 lines (43 loc) · 1.6 KB
/
alpaca_bot_tester.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import os
import dotenv
from alpaca.trading.client import TradingClient
from alpaca.trading.requests import MarketOrderRequest, GetOrdersRequest
from alpaca.trading.requests import LimitOrderRequest
from alpaca.trading.enums import OrderSide, TimeInForce, QueryOrderStatus
from alpaca.data import StockHistoricalDataClient, StockTradesRequest
from datetime import datetime
dotenv.load_dotenv()
ALPACA_KEY = os.environ["ALPACA_KEY"]
ALPACA_SECRET_KEY = os.environ["ALPACA_SECRET_KEY"]
trading_cli = TradingClient(ALPACA_KEY, ALPACA_SECRET_KEY, paper=True)
# data_cli = StockHistoricalDataClient("PKWP7PIJWJNRCM78Q38C", "imam9M5DEZafUD0OBPfvjhwpQbdbEErSubmj3ZNP")
# print(trading_cli.get_account().account_number)
# print(trading_cli.get_account().buying_power)
def buy_shares(ticker: str, amount: int) -> str:
# CREATING AN ORDER
market_order_data = MarketOrderRequest(
symbol=ticker,
qty=amount,
side=OrderSide.BUY,
time_in_force=TimeInForce.DAY
)
market_order = trading_cli.submit_order(order_data=market_order_data)
print(market_order)
print(market_order.status.name)
def sell_shares(ticker: str, amount: int) -> str:
# CREATING AN ORDER
market_order_data = MarketOrderRequest(
symbol=ticker,
qty=amount,
side=OrderSide.SELL,
time_in_force=TimeInForce.DAY
)
market_order = trading_cli.submit_order(order_data=market_order_data)
print(market_order)
print(market_order.status.name)
buy_shares("SPY", 1)
buy_shares("AAPL", 1)
buy_shares("MSFT", 1)
buy_shares("GME", 1)
buy_shares("ENB", 1)
sell_shares("NTDOY", 1)