Skip to content

Commit 0513d9d

Browse files
author
Stephen Gachoka
committed
initiat backtest
1 parent 521e42c commit 0513d9d

File tree

56 files changed

+159584
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+159584
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tradingbot/

README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,25 @@
1-
# Trading Bot
1+
# TraderBot
2+
3+
Build a trader bot which looks at sentiment of live news events and trades appropriately.
4+
5+
## Screenshots 📺
6+
7+
<img src="./screenshots/Screenshot_chart.png" alt=""/>
8+
9+
<img src="./screenshots/Screenshot_teardown.png" alt=""/>
10+
11+
## Startup 🚀
12+
13+
1. Create a virtual environment `conda create -n trader python=3.10`
14+
2. Activate it `conda activate trader`
15+
3. Install initial deps `pip install lumibot timedelta alpaca-trade-api==3.1.1`
16+
4. Install transformers and friends `pip install torch torchvision torchaudio transformers`
17+
5. Update the `API_KEY` and `API_SECRET` with values from your Alpaca account
18+
6. Run the bot `python tradingbot.py`
19+
20+
<p>N.B. Torch installation instructions will vary depending on your operating system and hardware. See here for more:
21+
<a href="pytorch.org/">PyTorch Installation Instructions</a></p>
22+
23+
## Other References 🔗
24+
25+
<p>-<a href="github.com/Lumiwealth/lumibot)">Lumibot</a>:trading bot library, makes lifecycle stuff easier .</p>

finbert_utils.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
2+
import torch
3+
from typing import Tuple
4+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
5+
6+
import os
7+
os.environ["TOKENIZERS_PARALLELISM"] = "false"
8+
9+
tokenizer = AutoTokenizer.from_pretrained("ProsusAI/finbert")
10+
model = AutoModelForSequenceClassification.from_pretrained("ProsusAI/finbert").to(device)
11+
labels = ["positive", "negative", "neutral"]
12+
13+
def estimate_sentiment(news):
14+
if news:
15+
tokens = tokenizer(news, return_tensors="pt", padding=True).to(device)
16+
17+
result = model(tokens["input_ids"], attention_mask=tokens["attention_mask"])[
18+
"logits"
19+
]
20+
result = torch.nn.functional.softmax(torch.sum(result, 0), dim=-1)
21+
probability = result[torch.argmax(result)]
22+
sentiment = labels[torch.argmax(result)]
23+
return probability, sentiment
24+
else:
25+
return 0, labels[-1]
26+
27+
28+
if __name__ == "__main__":
29+
tensor, sentiment = estimate_sentiment(['markets responded negatively to the news!','traders were displeased!'])
30+
print(tensor, sentiment)
31+
print(torch.cuda.is_available())
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
2024-06-08 15:32:30,977: root: INFO: MLTrader : Executing the initialize lifecycle method
2+
2024-06-08 15:32:33,170: root: INFO: Current backtesting datetime 2023-12-15 08:30:00-05:00
3+
2024-06-08 15:32:33,173: root: INFO: MLTrader : Executing the before_market_opens lifecycle method
4+
2024-06-08 15:32:33,174: root: INFO: Current backtesting datetime 2023-12-15 09:30:00-05:00
5+
2024-06-08 15:32:33,175: root: INFO: MLTrader : Executing the before_starting_trading lifecycle method
6+
2024-06-08 15:32:33,178: root: INFO: MLTrader : Executing the on_trading_iteration lifecycle method at 2024-06-08 15:32:33
7+
2024-06-08 15:32:33,179: root: INFO: New market order of | 10 SPY buy | with status unprocessed was submitted.
8+
2024-06-08 15:32:33,186: root: INFO: MLTrader : Trading iteration ended at 2024-06-08 15:32:33
9+
2024-06-08 15:32:33,186: root: INFO: Getting historical prices for SPY, 1 bars,
10+
2024-06-08 15:32:33,186: root: WARNING: quote is not implemented for YahooData, but USD was passed as the quote
11+
2024-06-08 15:32:35,300: root: WARNING: quote is not implemented for YahooData, but USD was passed as the quote
12+
2024-06-08 15:32:35,302: root: INFO: Filled Transaction: buy 10 of SPY at 469.48999023 USD per share
13+
2024-06-08 15:32:35,302: root: INFO: market order of | 10 SPY buy | with status new was filled
14+
2024-06-08 15:32:35,309: root: INFO: MLTrader : Sleeping for 86400 seconds
15+
2024-06-08 15:32:35,312: root: INFO: Current backtesting datetime 2023-12-15 16:00:00-05:00
16+
2024-06-08 15:32:35,312: root: INFO: Current backtesting datetime 2023-12-16 09:30:00-05:00
17+
2024-06-08 15:32:35,315: root: INFO: MLTrader : The market is not currently open, skipping this trading iteration
18+
2024-06-08 15:32:35,317: root: INFO: Current backtesting datetime 2023-12-16 09:29:00-05:00
19+
2024-06-08 15:32:35,321: root: INFO: Current backtesting datetime 2023-12-16 09:29:00-05:00
20+
2024-06-08 15:32:35,321: root: INFO: MLTrader : Executing the after_market_closes lifecycle method
21+
2024-06-08 15:32:35,326: root: INFO: Current backtesting datetime 2023-12-18 08:30:00-05:00
22+
2024-06-08 15:32:35,326: root: WARNING: quote is not implemented for YahooData, but USD was passed as the quote
23+
2024-06-08 15:32:35,328: root: WARNING: quote is not implemented for YahooData, but USD was passed as the quote
24+
2024-06-08 15:32:35,329: root: INFO: MLTrader : Executing the before_market_opens lifecycle method
25+
2024-06-08 15:32:35,330: root: INFO: Current backtesting datetime 2023-12-18 09:30:00-05:00
26+
2024-06-08 15:32:35,331: root: INFO: MLTrader : Executing the before_starting_trading lifecycle method
27+
2024-06-08 15:32:35,342: root: INFO: MLTrader : Executing the on_trading_iteration lifecycle method at 2024-06-08 15:32:35
28+
2024-06-08 15:32:35,342: root: INFO: MLTrader : Trading iteration ended at 2024-06-08 15:32:35
29+
2024-06-08 15:32:35,346: root: INFO: MLTrader : Sleeping for 86400 seconds
30+
2024-06-08 15:32:35,348: root: INFO: Current backtesting datetime 2023-12-18 16:00:00-05:00
31+
2024-06-08 15:32:35,348: root: INFO: Current backtesting datetime 2023-12-19 09:30:00-05:00
32+
2024-06-08 15:32:35,358: root: INFO: MLTrader : Executing the on_trading_iteration lifecycle method at 2024-06-08 15:32:35
33+
2024-06-08 15:32:35,358: root: INFO: MLTrader : Trading iteration ended at 2024-06-08 15:32:35
34+
2024-06-08 15:32:35,363: root: INFO: MLTrader : Sleeping for 86400 seconds
35+
2024-06-08 15:32:35,364: root: INFO: Current backtesting datetime 2023-12-19 16:00:00-05:00
36+
2024-06-08 15:32:35,365: root: INFO: Current backtesting datetime 2023-12-20 09:30:00-05:00
37+
2024-06-08 15:32:35,370: root: INFO: MLTrader : Executing the on_trading_iteration lifecycle method at 2024-06-08 15:32:35
38+
2024-06-08 15:32:35,372: root: INFO: MLTrader : Trading iteration ended at 2024-06-08 15:32:35
39+
2024-06-08 15:32:35,378: root: INFO: MLTrader : Sleeping for 86400 seconds
40+
2024-06-08 15:32:35,380: root: INFO: Current backtesting datetime 2023-12-20 16:00:00-05:00
41+
2024-06-08 15:32:35,380: root: INFO: Current backtesting datetime 2023-12-21 09:30:00-05:00
42+
2024-06-08 15:32:35,382: root: INFO: MLTrader : Executing the on_trading_iteration lifecycle method at 2024-06-08 15:32:35
43+
2024-06-08 15:32:35,382: root: INFO: MLTrader : Trading iteration ended at 2024-06-08 15:32:35
44+
2024-06-08 15:32:35,387: root: INFO: MLTrader : Sleeping for 86400 seconds
45+
2024-06-08 15:32:35,394: root: INFO: Current backtesting datetime 2023-12-21 16:00:00-05:00
46+
2024-06-08 15:32:35,395: root: INFO: Current backtesting datetime 2023-12-22 09:30:00-05:00
47+
2024-06-08 15:32:35,398: root: INFO: MLTrader : Executing the on_trading_iteration lifecycle method at 2024-06-08 15:32:35
48+
2024-06-08 15:32:35,399: root: INFO: MLTrader : Trading iteration ended at 2024-06-08 15:32:35
49+
2024-06-08 15:32:35,410: root: INFO: MLTrader : Sleeping for 86400 seconds
50+
2024-06-08 15:32:35,413: root: INFO: Current backtesting datetime 2023-12-22 16:00:00-05:00
51+
2024-06-08 15:32:35,413: root: INFO: Current backtesting datetime 2023-12-23 09:30:00-05:00
52+
2024-06-08 15:32:35,426: root: INFO: MLTrader : The market is not currently open, skipping this trading iteration
53+
2024-06-08 15:32:35,431: root: INFO: Current backtesting datetime 2023-12-23 09:29:00-05:00
54+
2024-06-08 15:32:35,440: root: INFO: Current backtesting datetime 2023-12-23 09:29:00-05:00
55+
2024-06-08 15:32:35,441: root: INFO: MLTrader : Executing the after_market_closes lifecycle method
56+
2024-06-08 15:32:35,445: root: INFO: Current backtesting datetime 2023-12-26 08:30:00-05:00
57+
2024-06-08 15:32:35,445: root: WARNING: quote is not implemented for YahooData, but USD was passed as the quote
58+
2024-06-08 15:32:35,447: root: WARNING: quote is not implemented for YahooData, but USD was passed as the quote
59+
2024-06-08 15:32:35,450: root: INFO: MLTrader : Executing the before_market_opens lifecycle method
60+
2024-06-08 15:32:35,459: root: INFO: Current backtesting datetime 2023-12-26 09:30:00-05:00
61+
2024-06-08 15:32:35,460: root: INFO: MLTrader : Executing the before_starting_trading lifecycle method
62+
2024-06-08 15:32:35,467: root: INFO: MLTrader : Executing the on_trading_iteration lifecycle method at 2024-06-08 15:32:35
63+
2024-06-08 15:32:35,467: root: INFO: MLTrader : Trading iteration ended at 2024-06-08 15:32:35
64+
2024-06-08 15:32:35,476: root: INFO: MLTrader : Sleeping for 86400 seconds
65+
2024-06-08 15:32:35,478: root: INFO: Current backtesting datetime 2023-12-26 16:00:00-05:00
66+
2024-06-08 15:32:35,479: root: INFO: Current backtesting datetime 2023-12-27 09:30:00-05:00
67+
2024-06-08 15:32:35,481: root: INFO: MLTrader : Executing the on_trading_iteration lifecycle method at 2024-06-08 15:32:35
68+
2024-06-08 15:32:35,481: root: INFO: MLTrader : Trading iteration ended at 2024-06-08 15:32:35
69+
2024-06-08 15:32:35,483: root: INFO: MLTrader : Sleeping for 86400 seconds
70+
2024-06-08 15:32:35,490: root: INFO: Current backtesting datetime 2023-12-27 16:00:00-05:00
71+
2024-06-08 15:32:35,491: root: INFO: Current backtesting datetime 2023-12-28 09:30:00-05:00
72+
2024-06-08 15:32:35,494: root: INFO: MLTrader : Executing the on_trading_iteration lifecycle method at 2024-06-08 15:32:35
73+
2024-06-08 15:32:35,494: root: INFO: MLTrader : Trading iteration ended at 2024-06-08 15:32:35
74+
2024-06-08 15:32:35,496: root: INFO: MLTrader : Sleeping for 86400 seconds
75+
2024-06-08 15:32:35,500: root: INFO: Current backtesting datetime 2023-12-28 16:00:00-05:00
76+
2024-06-08 15:32:35,500: root: INFO: Current backtesting datetime 2023-12-29 09:30:00-05:00
77+
2024-06-08 15:32:35,510: root: INFO: MLTrader : Executing the on_trading_iteration lifecycle method at 2024-06-08 15:32:35
78+
2024-06-08 15:32:35,510: root: INFO: MLTrader : Trading iteration ended at 2024-06-08 15:32:35
79+
2024-06-08 15:32:35,513: root: INFO: MLTrader : Sleeping for 86400 seconds
80+
2024-06-08 15:32:35,516: root: INFO: Current backtesting datetime 2023-12-29 16:00:00-05:00
81+
2024-06-08 15:32:35,519: root: INFO: Current backtesting datetime 2023-12-30 09:30:00-05:00
82+
2024-06-08 15:32:35,529: root: INFO: MLTrader : The market is not currently open, skipping this trading iteration
83+
2024-06-08 15:32:35,533: root: INFO: Current backtesting datetime 2023-12-30 09:29:00-05:00
84+
2024-06-08 15:32:35,543: root: INFO: Current backtesting datetime 2023-12-30 09:29:00-05:00
85+
2024-06-08 15:32:35,544: root: INFO: MLTrader : Executing the after_market_closes lifecycle method
86+
2024-06-08 15:32:35,548: root: INFO: Current backtesting datetime 2024-01-02 08:30:00-05:00
87+
2024-06-08 15:32:35,549: root: WARNING: quote is not implemented for YahooData, but USD was passed as the quote
88+
2024-06-08 15:32:35,551: root: WARNING: quote is not implemented for YahooData, but USD was passed as the quote
89+
2024-06-08 15:32:35,562: root: INFO: MLTrader : Executing the before_market_opens lifecycle method
90+
2024-06-08 15:32:35,564: root: INFO: Current backtesting datetime 2024-01-02 09:30:00-05:00
91+
2024-06-08 15:32:35,564: root: INFO: MLTrader : Executing the before_starting_trading lifecycle method
92+
2024-06-08 15:32:35,572: root: INFO: Current backtesting datetime 2024-01-02 15:59:00-05:00
93+
2024-06-08 15:32:35,576: root: INFO: MLTrader : Executing the before_market_closes lifecycle method
94+
2024-06-08 15:32:35,579: root: INFO: Current backtesting datetime 2024-01-02 16:00:00-05:00
95+
2024-06-08 15:32:35,579: root: INFO: MLTrader : Executing the after_market_closes lifecycle method
96+
2024-06-08 15:32:35,579: root: INFO: MLTrader : Executing the on_strategy_end lifecycle method
97+
2024-06-08 15:32:36,814: root: INFO: Backtesting finished
98+
2024-06-08 15:32:45,594: backtest_stats: INFO: Backtest took 0:00:14.616819 for a speed of 0.000
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
datetime,portfolio_value,cash,return
2+
2023-12-15 09:30:00-05:00,100000.0,100000.0,
3+
2023-12-16 09:30:00-05:00,100000.0,95305.10009765625,0.0
4+
2023-12-18 09:30:00-05:00,100033.96020751953,95324.16009765625,0.00033960207519534436
5+
2023-12-19 09:30:00-05:00,100049.46008544922,95324.16009765625,0.00015494615925959465
6+
2023-12-20 09:30:00-05:00,100063.76001220703,95324.16009765625,0.00014292857498277023
7+
2023-12-21 09:30:00-05:00,100037.4599633789,95324.16009765625,-0.000262832905988275
8+
2023-12-22 09:30:00-05:00,100062.75995117187,95324.16009765625,0.0002529051397568427
9+
2023-12-23 09:30:00-05:00,100062.75995117187,95324.16009765625,0.0
10+
2023-12-26 09:30:00-05:00,100064.86017089844,95324.16009765625,2.09890245641553e-05
11+
2023-12-27 09:30:00-05:00,100078.56012207031,95324.16009765625,0.00013691071119747988
12+
2023-12-28 09:30:00-05:00,100092.96014648437,95324.16009765625,0.00014388720617586337
13+
2023-12-29 09:30:00-05:00,100089.06,95324.16009765625,-3.8965242697086566e-05
14+
2023-12-30 09:30:00-05:00,100089.06,95324.16009765625,0.0
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"name": "MLTrader", "backtesting_start": {"py/object": "datetime.datetime", "__reduce__": [{"py/type": "datetime.datetime"}, ["B+cMDwAAAAAAAA==", {"py/reduce": [{"py/function": "pytz._p"}, {"py/tuple": ["America/New_York", -18000, 0, "EST"]}]}]]}, "backtesting_end": {"py/object": "datetime.datetime", "__reduce__": [{"py/type": "datetime.datetime"}, ["B+cMHhc7AAAAAA==", {"py/id": 2}]]}, "budget": 100000, "risk_free_rate": {"py/reduce": [{"py/function": "numpy.core.multiarray.scalar"}, {"py/tuple": [{"py/reduce": [{"py/type": "numpy.dtype"}, {"py/tuple": ["f8", false, true]}, {"py/tuple": [3, "<", null, null, null, -1, -1, 0]}]}, {"py/b64": "7FG4Ho3Rqj8="}]}]}, "minutes_before_closing": 1, "minutes_before_opening": 60, "sleeptime": "24H", "auto_adjust": false, "quote_asset": {"py/object": "lumibot.entities.asset.Asset", "symbol": "USD", "asset_type": "forex", "strike": 0.0, "multiplier": 1, "precision": null, "expiration": null}, "benchmark_asset": "SPY", "starting_positions": null, "parameters": {"pandas_data": null, "symbol": "SPY"}}

0 commit comments

Comments
 (0)