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.

407

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

self.cur_hedge_qty = int(floor(self.qty*self.theta[0]))

self.events_queue.put(

SignalEvent(self.tickers[1], "BOT", self.qty)

)

self.events_queue.put(

SignalEvent(self.tickers[0], "SLD", self.cur_hedge_qty)

)

self.invested = "long"

elif e > sqrt_Q:

# Short Entry

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

self.cur_hedge_qty = int(floor(self.qty*self.theta[0]))

self.events_queue.put(

SignalEvent(self.tickers[1], "SLD", self.qty)

)

self.events_queue.put(

SignalEvent(self.tickers[0], "BOT", self.cur_hedge_qty)

)

self.invested = "short"

# If we are in the market...

if self.invested is not None:

if self.invested == "long" and e > -sqrt_Q:

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

self.events_queue.put(

SignalEvent(self.tickers[1], "SLD", self.qty)

)

self.events_queue.put(

SignalEvent(self.tickers[0], "BOT", self.cur_hedge_qty)

)

self.invested = None

elif self.invested == "short" and e < sqrt_Q:

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

self.events_queue.put(

SignalEvent(self.tickers[1], "BOT", self.qty)

)

self.events_queue.put(

SignalEvent(self.tickers[0], "SLD", self.cur_hedge_qty)

)

self.invested = None

This is all of the code necessary for the Strategy object. We also need to create a backtest

file to encapsulate all of our trading logic and class choices. The particular version is very similar

to those used in the examples directory and replaces the equity of 500,000 USD with 100,000

USD.

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

Saved successfully!

Ooh no, something went wrong!