-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlpacaBot.py
87 lines (73 loc) · 2.91 KB
/
AlpacaBot.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import os
from dotenv import load_dotenv
from lumibot.brokers import Alpaca
from lumibot.strategies.strategy import Strategy
from timedelta import Timedelta
from alpaca.data.historical.news import NewsClient
from alpaca.data.requests import NewsRequest
from finbert_utils import estimate_sentiment
from lumibot.traders import Trader
load_dotenv()
BASE_URL = "https://paper-api.alpaca.markets/v2"
ALPACA_CREDS = {
"API_KEY": os.getenv("API_KEY"),
"API_SECRET": os.getenv("API_SECRET"),
"PAPER": True
}
class MLTrader(Strategy):
def initialize(self, symbol: str = "SPY", cash_at_risk: float = .2):
self.symbol = symbol
self.sleeptime = "24H"
self.last_trade = None
self.cash_at_risk = cash_at_risk
def get_dates(self):
today = self.get_datetime()
three_days_prior = today - Timedelta(days=3)
return today.strftime('%Y-%m-%d'), three_days_prior.strftime('%Y-%m-%d')
def get_sentiment(self):
today, three_days_prior = self.get_dates()
news_client = NewsClient(api_key=os.getenv("API_KEY"), secret_key=os.getenv("API_SECRET"))
news_list = news_client.get_news(NewsRequest(symbol="SPY", start=three_days_prior, end=today))
headlines = [news.headline for news in news_list.news]
probability, sentiment = estimate_sentiment(headlines)
return probability, sentiment
def position_sizing(self):
cash = self.get_cash()
last_price = self.get_last_price(self.symbol)
quantity = round(cash * self.cash_at_risk / last_price, 0)
return cash, last_price, quantity
def on_trading_iteration(self):
cash, last_price, quantity = self.position_sizing()
probability, sentiment = self.get_sentiment()
if cash > last_price:
if sentiment == "positive" and probability > .999:
if self.last_trade == "sell":
self.sell_all()
order = self.create_order(
self.symbol,
quantity,
"buy",
type="bracket",
take_profit_price=last_price * 1.15,
stop_loss_price=last_price * .95
)
self.submit_order(order)
self.last_trade = "buy"
elif sentiment == "negative" and probability > .999:
if self.last_trade == "buy":
self.sell_all()
order = self.create_order(
self.symbol,
quantity,
"sell",
type="bracket",
take_profit_price=last_price * .85,
stop_loss_price=last_price * 1.05
)
self.submit_order(order)
self.last_trade = "sell"
trader = Trader()
broker = Alpaca(ALPACA_CREDS)
strategy = MLTrader(broker=broker)
trader.add_strategy(strategy)
trader.run_all()