From 78cef9ae941e500629d2bf8bb923ca6245371e61 Mon Sep 17 00:00:00 2001 From: Al4ise Date: Fri, 6 Dec 2024 15:52:50 +0200 Subject: [PATCH] implemented strategy.run_live() in the example strategies --- .../example_strategies/crypto_important_functions.py | 7 +------ lumibot/example_strategies/forex_hold_to_expiry.py | 8 +------- lumibot/example_strategies/futures_hold_to_expiry.py | 8 +------- lumibot/example_strategies/lifecycle_logger.py | 5 +---- lumibot/example_strategies/options_hold_to_expiry.py | 6 +----- .../example_strategies/simple_start_single_file.py | 6 +----- lumibot/example_strategies/stock_bracket.py | 7 +------ lumibot/example_strategies/stock_buy_and_hold.py | 7 +------ .../example_strategies/stock_diversified_leverage.py | 5 +---- .../stock_limit_and_trailing_stops.py | 6 +----- lumibot/example_strategies/stock_momentum.py | 7 +------ lumibot/example_strategies/stock_oco.py | 7 +------ lumibot/example_strategies/strangle.py | 3 +-- lumibot/example_strategies/test_broker_functions.py | 11 +---------- 14 files changed, 14 insertions(+), 79 deletions(-) diff --git a/lumibot/example_strategies/crypto_important_functions.py b/lumibot/example_strategies/crypto_important_functions.py index 5173059aa..3ae355eee 100644 --- a/lumibot/example_strategies/crypto_important_functions.py +++ b/lumibot/example_strategies/crypto_important_functions.py @@ -3,8 +3,6 @@ from lumibot.brokers import Ccxt from lumibot.entities import Asset from lumibot.strategies.strategy import Strategy -from lumibot.traders import Trader - class ImportantFunctions(Strategy): def initialize(self): @@ -123,8 +121,6 @@ def on_trading_iteration(self): if __name__ == "__main__": - trader = Trader() - KRAKEN_CONFIG = { "exchange_id": "kraken", "apiKey": "YOUR_API_KEY", @@ -145,5 +141,4 @@ def on_trading_iteration(self): broker=broker, ) - trader.add_strategy(strategy) - strategy_executors = trader.run_all() + strategy.run_live() diff --git a/lumibot/example_strategies/forex_hold_to_expiry.py b/lumibot/example_strategies/forex_hold_to_expiry.py index 3528c5fc3..810283aa7 100644 --- a/lumibot/example_strategies/forex_hold_to_expiry.py +++ b/lumibot/example_strategies/forex_hold_to_expiry.py @@ -59,14 +59,8 @@ def on_trading_iteration(self): is_live = True if is_live: - from lumibot.traders import Trader - - trader = Trader() - strategy = FuturesHoldToExpiry() - - trader.add_strategy(strategy) - strategy_executors = trader.run_all() + strategy.run_live() else: from lumibot.backtesting import PolygonDataBacktesting diff --git a/lumibot/example_strategies/futures_hold_to_expiry.py b/lumibot/example_strategies/futures_hold_to_expiry.py index dc4588997..48f73a82f 100644 --- a/lumibot/example_strategies/futures_hold_to_expiry.py +++ b/lumibot/example_strategies/futures_hold_to_expiry.py @@ -83,11 +83,5 @@ def on_trading_iteration(self): ) else: - from lumibot.traders import Trader - - trader = Trader() - strategy = FuturesHoldToExpiry() - - trader.add_strategy(strategy) - strategy_executors = trader.run_all() + strategy.run_live() diff --git a/lumibot/example_strategies/lifecycle_logger.py b/lumibot/example_strategies/lifecycle_logger.py index 7306f60fe..ada028cce 100644 --- a/lumibot/example_strategies/lifecycle_logger.py +++ b/lumibot/example_strategies/lifecycle_logger.py @@ -62,10 +62,7 @@ def after_market_closes(self): else: from lumibot.credentials import ALPACA_CONFIG from lumibot.brokers import Alpaca - from lumibot.traders import Trader - trader = Trader() broker = Alpaca(ALPACA_CONFIG) strategy = LifecycleLogger(broker=broker) - trader.add_strategy(strategy) - strategy_executors = trader.run_all() + strategy.run_live() diff --git a/lumibot/example_strategies/options_hold_to_expiry.py b/lumibot/example_strategies/options_hold_to_expiry.py index eb087be12..61dbf3e8c 100644 --- a/lumibot/example_strategies/options_hold_to_expiry.py +++ b/lumibot/example_strategies/options_hold_to_expiry.py @@ -68,15 +68,11 @@ def on_trading_iteration(self): from credentials import INTERACTIVE_BROKERS_CONFIG from lumibot.brokers import InteractiveBrokers - from lumibot.traders import Trader - - trader = Trader() broker = InteractiveBrokers(INTERACTIVE_BROKERS_CONFIG) strategy = OptionsHoldToExpiry(broker=broker) - trader.add_strategy(strategy) - strategy_executors = trader.run_all() + strategy.run_live() else: from lumibot.backtesting import PolygonDataBacktesting diff --git a/lumibot/example_strategies/simple_start_single_file.py b/lumibot/example_strategies/simple_start_single_file.py index 5a1fdca5a..3bb3e59ca 100644 --- a/lumibot/example_strategies/simple_start_single_file.py +++ b/lumibot/example_strategies/simple_start_single_file.py @@ -5,8 +5,6 @@ from lumibot.backtesting import YahooDataBacktesting from lumibot.brokers import Alpaca from lumibot.strategies.strategy import Strategy -from lumibot.traders import Trader - class MyStrategy(Strategy): def initialize(self, symbol=""): @@ -26,7 +24,6 @@ def on_trading_iteration(self): if __name__ == "__main__": live = True - trader = Trader() broker = Alpaca(AlpacaConfig) strategy = MyStrategy(broker, symbol="SPY") @@ -42,5 +39,4 @@ def on_trading_iteration(self): ) else: # Run the strategy live - trader.add_strategy(strategy) - trader.run_all() + strategy.run_live() \ No newline at end of file diff --git a/lumibot/example_strategies/stock_bracket.py b/lumibot/example_strategies/stock_bracket.py index 33caf1232..5e9a8511a 100644 --- a/lumibot/example_strategies/stock_bracket.py +++ b/lumibot/example_strategies/stock_bracket.py @@ -60,16 +60,11 @@ def on_trading_iteration(self): from credentials import ALPACA_CONFIG from lumibot.brokers import Alpaca - from lumibot.traders import Trader - - trader = Trader() broker = Alpaca(ALPACA_CONFIG) strategy = StockBracket(broker=broker) - - trader.add_strategy(strategy) - strategy_executors = trader.run_all() + strategy.run_live() else: from lumibot.backtesting import YahooDataBacktesting diff --git a/lumibot/example_strategies/stock_buy_and_hold.py b/lumibot/example_strategies/stock_buy_and_hold.py index 7bb3cad94..807f96f7e 100644 --- a/lumibot/example_strategies/stock_buy_and_hold.py +++ b/lumibot/example_strategies/stock_buy_and_hold.py @@ -82,13 +82,8 @@ def on_trading_iteration(self): } from lumibot.brokers import Alpaca - from lumibot.traders import Trader - - trader = Trader() broker = Alpaca(ALPACA_CONFIG) strategy = BuyAndHold(broker=broker) - - trader.add_strategy(strategy) - strategy_executors = trader.run_all() + strategy.run_live() diff --git a/lumibot/example_strategies/stock_diversified_leverage.py b/lumibot/example_strategies/stock_diversified_leverage.py index f76c1f944..5afc889ae 100644 --- a/lumibot/example_strategies/stock_diversified_leverage.py +++ b/lumibot/example_strategies/stock_diversified_leverage.py @@ -4,7 +4,6 @@ from lumibot.brokers import Alpaca from lumibot.entities import TradingFee from lumibot.strategies.strategy import Strategy -from lumibot.traders import Trader """ Strategy Description @@ -127,11 +126,9 @@ def rebalance_portfolio(self): #### from credentials import ALPACA_CONFIG - trader = Trader() broker = Alpaca(ALPACA_CONFIG) strategy = DiversifiedLeverage(broker=broker) - trader.add_strategy(strategy) - trader.run_all() + strategy.run_live() else: #### diff --git a/lumibot/example_strategies/stock_limit_and_trailing_stops.py b/lumibot/example_strategies/stock_limit_and_trailing_stops.py index 45a932ada..919c86750 100644 --- a/lumibot/example_strategies/stock_limit_and_trailing_stops.py +++ b/lumibot/example_strategies/stock_limit_and_trailing_stops.py @@ -68,16 +68,12 @@ def on_trading_iteration(self): from credentials import ALPACA_CONFIG from lumibot.brokers import Alpaca - from lumibot.traders import Trader - - trader = Trader() broker = Alpaca(ALPACA_CONFIG) strategy = LimitAndTrailingStop(broker=broker) - trader.add_strategy(strategy) - strategy_executors = trader.run_all() + strategy.run_live() else: from lumibot.backtesting import YahooDataBacktesting diff --git a/lumibot/example_strategies/stock_momentum.py b/lumibot/example_strategies/stock_momentum.py index 08f5bc91d..d7a04a671 100644 --- a/lumibot/example_strategies/stock_momentum.py +++ b/lumibot/example_strategies/stock_momentum.py @@ -153,16 +153,11 @@ def get_assets_momentums(self): if is_live: from lumibot.credentials import ALPACA_CONFIG from lumibot.brokers import Alpaca - from lumibot.traders import Trader - - trader = Trader() broker = Alpaca(ALPACA_CONFIG) strategy = Momentum(broker=broker) - - trader.add_strategy(strategy) - strategy_executors = trader.run_all() + strategy.run_live() else: from lumibot.backtesting import YahooDataBacktesting diff --git a/lumibot/example_strategies/stock_oco.py b/lumibot/example_strategies/stock_oco.py index 9130a8b3d..47655585a 100644 --- a/lumibot/example_strategies/stock_oco.py +++ b/lumibot/example_strategies/stock_oco.py @@ -66,16 +66,11 @@ def on_trading_iteration(self): from credentials import ALPACA_CONFIG from lumibot.brokers import Alpaca - from lumibot.traders import Trader - - trader = Trader() broker = Alpaca(ALPACA_CONFIG) strategy = StockOco(broker=broker) - - trader.add_strategy(strategy) - strategy_executors = trader.run_all() + strategy.run_live() else: from lumibot.backtesting import YahooDataBacktesting diff --git a/lumibot/example_strategies/strangle.py b/lumibot/example_strategies/strangle.py index d8f336746..b48261613 100644 --- a/lumibot/example_strategies/strangle.py +++ b/lumibot/example_strategies/strangle.py @@ -3,8 +3,7 @@ import time from itertools import cycle -import pandas as pd -from yfinance import Ticker, download +from yfinance import Ticker from lumibot.strategies.strategy import Strategy diff --git a/lumibot/example_strategies/test_broker_functions.py b/lumibot/example_strategies/test_broker_functions.py index 0faa6e935..16531214e 100644 --- a/lumibot/example_strategies/test_broker_functions.py +++ b/lumibot/example_strategies/test_broker_functions.py @@ -1,12 +1,6 @@ -from datetime import date, datetime -from decimal import Decimal - import logging -from lumibot.brokers import InteractiveBrokers, Tradier, InteractiveBrokersREST, ExampleBroker from lumibot.entities import Asset, Order from lumibot.strategies.strategy import Strategy -from lumibot.traders import Trader -import yfinance as yf from datetime import timedelta # from lumiwealth_tradier import Tradier as _Tradier @@ -132,8 +126,5 @@ def on_new_order(self, order): # broker = ExampleBroker() strategy = BrokerTest() + strategy.run_live() - trader = Trader() - - trader.add_strategy(strategy) - strategy_executors = trader.run_all()