forked from Lumiwealth/lumibot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
159 lines (143 loc) · 4.53 KB
/
main.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import argparse
import logging
from datetime import datetime
from time import perf_counter, time
from credentials import AlpacaConfig
from lumibot.backtesting import YahooDataBacktesting
from lumibot.brokers import Alpaca
from lumibot.data_sources import AlpacaData
from lumibot.strategies.examples import (
BuyAndHold,
DebtTrading,
Diversification,
FastTrading,
IntradayMomentum,
Momentum,
Simple,
)
from lumibot.tools import indicators, perf_counters
from lumibot.traders import Trader
# Global parameters
debug = False
budget = 40000
backtesting_start = datetime(2019, 1, 1)
backtesting_end = datetime(2020, 12, 31)
logfile = "logs/test.log"
# Trading objects
alpaca_broker = Alpaca(AlpacaConfig)
alpaca_data_source = AlpacaData(AlpacaConfig)
trader = Trader(logfile=logfile, debug=debug)
# Strategies mapping
mapping = {
"momentum": {
"class": Momentum,
"backtesting_datasource": YahooDataBacktesting,
"kwargs": {"symbols": ["SPY", "VEU", "AGG"]},
"config": None,
},
"diversification": {
"class": Diversification,
"backtesting_datasource": YahooDataBacktesting,
"kwargs": {},
"config": None,
},
"debt_trading": {
"class": DebtTrading,
"backtesting_datasource": YahooDataBacktesting,
"kwargs": {},
"config": None,
},
"intraday_momentum": {
"class": IntradayMomentum,
"backtesting_datasource": None,
"kwargs": {},
"config": None,
},
"fast_trading": {
"class": FastTrading,
"backtesting_datasource": None,
"kwargs": {},
"backtesting_cache": False,
"config": None,
},
"buy_and_hold": {
"class": BuyAndHold,
"backtesting_datasource": YahooDataBacktesting,
"kwargs": {},
"backtesting_cache": False,
"config": None,
},
"simple": {
"class": Simple,
"backtesting_datasource": YahooDataBacktesting,
"kwargs": {},
"backtesting_cache": False,
"config": None,
},
}
if __name__ == "__main__":
# Set the benchmark asset for backtesting to be "SPY" by default
benchmark_asset = "SPY"
parser = argparse.ArgumentParser(
f"\n\
Running AlgoTrader\n\
Usage: ‘python main.py [strategies]’\n\
Where strategies can be any of diversification, momentum, intraday_momentum, simple\n\
Example: ‘python main.py momentum’ "
)
parser.add_argument("strategies", nargs="+", help="list of strategies")
parser.add_argument(
"-l",
"--live-trading",
default=False,
action="store_true",
help="enable live trading",
)
args = parser.parse_args()
strategies = args.strategies
live_trading = args.live_trading
for strategy_name in strategies:
strategy_params = mapping.get(strategy_name)
if strategy_params is None:
raise ValueError(f"Strategy {strategy_name} does not exist")
strategy_class = strategy_params["class"]
backtesting_datasource = strategy_params["backtesting_datasource"]
kwargs = strategy_params["kwargs"]
config = strategy_params["config"]
stats_file = f"logs/strategy_{strategy_class.__name__}_{int(time())}.csv"
if live_trading:
strategy = strategy_class(
strategy_name,
budget=budget,
broker=alpaca_broker,
stats_file=stats_file,
**kwargs,
)
trader.add_strategy(strategy)
else:
if backtesting_datasource is None:
raise ValueError(
f"Backtesting is not supported for strategy {strategy_name}"
)
tic = perf_counter()
strategy_class.backtest(
strategy_name,
budget,
backtesting_datasource,
backtesting_start,
backtesting_end,
stats_file=stats_file,
config=config,
**kwargs,
)
toc = perf_counter()
print("Elpased time:", toc - tic)
logging.info(f"*** Benchmark Performance for {benchmark_asset} ***")
indicators.calculate_returns(
benchmark_asset, backtesting_start, backtesting_end
)
if live_trading:
trader.run_all()
for counter, values in perf_counters.counters.items():
print("Count %s spent %fs" % (counter, values[0]))
logging.info("The end")