13.08.2022 Views

advanced-algorithmic-trading

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

440

events_queue - A handle to the system events queue

"""

def __init__(

self, tickers, events_queue,

model_pickle_file, lags=5

):

self.tickers = tickers

self.events_queue = events_queue

self.model_pickle_file = model_pickle_file

self.lags = lags

self.invested = False

self.cur_prices = np.zeros(self.lags+1)

self.cur_returns = np.zeros(self.lags)

self.minutes = 0

self.qty = 10000

self.model = joblib.load(model_pickle_file)

def _update_current_returns(self, event):

"""

Updates the array of current returns "features"

used by the machine learning model for prediction.

"""

# Adjust the feature vector to move all lags by one

# and then recalculate the returns

for i, f in reversed(list(enumerate(self.cur_prices))):

if i > 0:

self.cur_prices[i] = self.cur_prices[i-1]

else:

self.cur_prices[i] = event.close_price / float(

PriceParser.PRICE_MULTIPLIER

)

if self.minutes > (self.lags + 1):

for i in range(0, self.lags):

self.cur_returns[i] = ((

self.cur_prices[i]/self.cur_prices[i+1]

)-1.0)*100.0

def calculate_signals(self, event):

"""

Calculate the intraday machine learning

prediction strategy.

"""

if event.type == EventType.BAR:

self._update_current_returns(event)

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

Saved successfully!

Ooh no, something went wrong!