def initialize#

This lifecycle methods is executed only once, when the strategy execution starts. Use this lifecycle method to initialize parameters like:

# self.sleeptime: the sleeptime duration between each trading iteration in minutes
# self.minutes_before_closing: number of minutes before the market closes to stop trading
class MyStrategy(Strategy):
    def initialize(self, my_custom_parameter=True):
        self.sleeptime = "5M"
        self.minutes_before_closing = 15
        self.my_custom_parameter = my_custom_parameter

Custom Parameters

You can also use the initialize method to define custom parameters like my_custom_parameter in the example above. You can name these parameters however you’d like, and add as many as you’d like.

These parameters can easily be set using the strategy constructor later on.

strategy_1 = MyStrategy(
    name="strategy_1",
    budget=budget,
    broker=broker,
    my_custom_parameter=False,
    my_other_parameter=50
)
strategy_2 = MyStrategy(
    name="strategy_2",
    budget=budget,
    broker=broker,
    my_custom_parameter=True,
    my_last_parameter="SPY"
)

or just for backtesting

options = [True, False]
for option in options:
    MyStrategy.backtest(
        YahooDataBacktesting,
        backtesting_start,
        backtesting_end,
        stats_file=stats_file,
        my_custom_parameter=option,
        my_last_parameter="SPY"
        budget=budget,
    )

# `options` in this example is not referring to trading options contracts.

Changing Market Hours

If you’d like to change the market hours for which the bot operates, then you can use the set_market() function like this:

def initialize(self, asset_symbol="MNQ", expiration=datetime.date(2021, 9, 17)):
    self.set_market('24/7')

Default is NASDAQ days and hours.

Possible calendars include:

['MarketCalendar', 'ASX', 'BMF', 'CFE', 'NYSE', 'stock', 'NASDAQ', 'BATS', 'CME_Equity', 'CBOT_Equity', 'CME_Agriculture', 'CBOT_Agriculture', 'COMEX_Agriculture', 'NYMEX_Agriculture', 'CME_Rate', 'CBOT_Rate', 'CME_InterestRate', 'CBOT_InterestRate', 'CME_Bond', 'CBOT_Bond', 'EUREX', 'HKEX', 'ICE', 'ICEUS', 'NYFE', 'JPX', 'LSE', 'OSE', 'SIX', 'SSE', 'TSX', 'TSXV', 'BSE', 'TASE', 'TradingCalendar', 'ASEX', 'BVMF', 'CMES', 'IEPA', 'XAMS', 'XASX', 'XBKK', 'XBOG', 'XBOM', 'XBRU', 'XBUD', 'XBUE', 'XCBF', 'XCSE', 'XDUB', 'XFRA', 'XETR', 'XHEL', 'XHKG', 'XICE', 'XIDX', 'XIST', 'XJSE', 'XKAR', 'XKLS', 'XKRX', 'XLIM', 'XLIS', 'XLON', 'XMAD', 'XMEX', 'XMIL', 'XMOS', 'XNYS', 'XNZE', 'XOSL', 'XPAR', 'XPHS', 'XPRA', 'XSES', 'XSGO', 'XSHG', 'XSTO', 'XSWX', 'XTAE', 'XTAI', 'XTKS', 'XTSE', 'XWAR', 'XWBO', 'us_futures', '24/7', '24/5']

Reference#

lumibot.strategies.strategy.Strategy.initialize(self, parameters=None)#

Initialize the strategy. Use this lifecycle method to initialize parameters.

This method is called once before the first time the strategy is run.

Return type:

None

Example

>>> # Initialize the strategy
>>> def initialize(self):
>>>   self.sleeptime = 5
>>>   self.ticker = "AAPL"
>>>   self.minutes_before_closing = 5
>>>   self.max_bars = 100
>>> # Initialize the strategy
>>> def initialize(self):
>>>   # Set the strategy to call on_trading_interation every 2 seconds
>>>   self.sleeptime = "2S"
>>>   self.count = 0
>>> # Initialize the strategy
>>> def initialize(self):
>>>   # Set the strategy to call on_trading_interation every 10 minutes
>>>   self.sleeptime = "10M"
>>>   self.count = 0
>>> # Initialize the strategy
>>> def initialize(self):
>>>   # Set the strategy to call on_trading_interation every 20 hours
>>>   self.sleeptime = "20H"
>>>   self.count = 0
>>> # Initialize the strategy
>>> def initialize(self):
>>>   # Set the strategy to call on_trading_interation every 2 days (48 hours)
>>>   self.sleeptime = "2D"
>>>   self.count = 0