13.08.2022 Views

advanced-algorithmic-trading

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

358

25.4.1 MonthlyLiquidateRebalanceStrategy

MonthlyLiquidateRebalanceStrategy contains new methods not present on the Strategy

base class. The first is _end_of_month. It simply uses the Python calendar module along with

the monthrange method to determine if the date passed is the final day of that particular month:

def _end_of_month(self, cur_time):

"""

Determine if the current day is at the end of the month.

"""

cur_day = cur_time.day

end_day = calendar.monthrange(cur_time.year, cur_time.month)[1]

return cur_day == end_day

The second method is _create_invested_list, which simply creates a dictionary from a

dictionary comprehension of all tickers as keys and boolean False as values. This tickers_invested

dict is used for "housekeeping" to check whether an asset has been purchased at the point of

carrying out subsequent trading logic.

This is necessary because on the first run through of the code the liquidation of the entire

portfolio is not required:

def _create_invested_list(self):

"""

Create a dictionary with each ticker as a key, with

a boolean value depending upon whether the ticker has

been "invested" yet. This is necessary to avoid sending

a liquidation signal on the first allocation.

"""

tickers_invested = {ticker: False for ticker in self.tickers}

return tickers_invested

The core logic for all Strategy-derived classes is encapsulated in the calculate_signals

method. This code initially checks whether the date of the current bar is the final day of the

month. If this is the case it then determines whether the portfolio has previously been purchased,

and if so, whether it should liquidate it fully prior to rebalancing.

Irrespective of the liquidation it simply sends a long signal ("BOT" in Interactive Brokers

terminology) to the events queue. It then updates the tickers_invested dict to show that this

ticker has now been purchased at least once:

def calculate_signals(self, event):

"""

For a particular received BarEvent, determine whether

it is the end of the month (for that bar) and generate

a liquidation signal, as well as a purchase signal,

for each ticker.

"""

if (

event.type in [EventType.BAR, EventType.TICK] and

self._end_of_month(event.time)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!