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.

388

that arrive out of order in the events queue.

"""

# Set the first instance of time

if self.time is None:

self.time = event.time

# Set the correct latest prices depending upon

# order of arrival of market bar event

price = event.adj_close_price/float(

PriceParser.PRICE_MULTIPLIER

)

if event.time == self.time:

for i in range(0, len(self.tickers)):

if event.ticker == self.tickers[i]:

self.latest_prices[i] = price

else:

self.time = event.time

self.bars_elapsed += 1

self.latest_prices = np.full(len(self.tickers), -1.0)

for i in range(0, len(self.tickers)):

if event.ticker == self.tickers[i]:

self.latest_prices[i] = price

go_long_units is a helper method that longs the appropriate quantity of portfolio "units"

by purchasing their individual components separately in the correct quantities. It achieves this

by shorting any component that has a negative value in the self.weights array and by longing

any component that has a positive value. Note that it multiplies this by the self.qty value,

which is the base number of units to transact for a portfolio "unit":

def go_long_units(self):

"""

Go long the appropriate number of "units" of the

portfolio to open a new position or to close out

a short position.

"""

for i, ticker in enumerate(self.tickers):

if self.weights[i] < 0.0:

self.events_queue.put(SignalEvent(

ticker, "SLD",

int(floor(-1.0*self.qty*self.weights[i])))

)

else:

self.events_queue.put(SignalEvent(

ticker, "BOT",

int(floor(self.qty*self.weights[i])))

go_short_units is almost identical to the above method except that it swaps the long/short

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

Saved successfully!

Ooh no, something went wrong!