-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiversified_Leverage.py
46 lines (46 loc) · 1.58 KB
/
Diversified_Leverage.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
from datetime import datetime
import streamlit as st
from lumibot.backtesting import YahooDataBacktesting
from lumibot.brokers import Alpaca
from lumibot.entities import TradingFee
from lumibot.traders import Trader
from lumibot.example_strategies.stock_diversified_leverage import DiversifiedLeverage
"""
Strategy Description
This strategy will buy a few symbols that have 2x or 3x returns (have leverage), but will
also diversify and rebalance the portfolio often.
"""
if __name__ == "__main__":
is_live = False
if is_live:
####
# Run the strategy live
####
from lumibot.credentials import ALPACA_CREDS
trader = Trader()
broker = Alpaca(ALPACA_CREDS)
strategy = DiversifiedLeverage(broker=broker)
trader.add_strategy(strategy)
trader.run_all()
else:
####
# Backtest the strategy
####
# Choose the time from and to which you want to backtest
backtesting_start = datetime(2023, 6, 1)
backtesting_end = datetime(2023, 7, 31)
# 0.01% trading/slippage fee
trading_fee = TradingFee(percent_fee=0.005)
# Initialize the backtesting object
print("Starting Backtest...")
result = DiversifiedLeverage.backtest(
YahooDataBacktesting,
backtesting_start,
backtesting_end,
benchmark_asset="SPY",
parameters={},
buy_trading_fees=[trading_fee],
sell_trading_fees=[trading_fee],
)
print("Backtest result: ", result)
st.write(f"Backtest result: {result}")