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.

396

def go_short_units(self):

"""

Go short the appropriate number of "units" of the

portfolio to open a new position or to close out

a long position.

"""

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

if self.weights[i] < 0.0:

self.events_queue.put(SignalEvent(

ticker, "BOT",

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

)

else:

self.events_queue.put(SignalEvent(

ticker, "SLD",

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

)

def zscore_trade(self, zscore, event):

"""

Determine whether to trade if the entry or exit zscore

threshold has been exceeded.

"""

# If we’re not in the market...

if self.invested is None:

if zscore < -self.entry_z:

# Long Entry

print("LONG: %s" % event.time)

self.go_long_units()

self.invested = "long"

elif zscore > self.entry_z:

# Short Entry

print("SHORT: %s" % event.time)

self.go_short_units()

self.invested = "short"

# If we are in the market...

if self.invested is not None:

if self.invested == "long" and zscore >= -self.exit_z:

print("CLOSING LONG: %s" % event.time)

self.go_short_units()

self.invested = None

elif self.invested == "short" and zscore <= self.exit_z:

print("CLOSING SHORT: %s" % event.time)

self.go_long_units()

https://sanet.cd/blogs/polatebooks/

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

Saved successfully!

Ooh no, something went wrong!