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.

397

self.invested = None

def calculate_signals(self, event):

"""

Calculate the signals for the strategy.

"""

if event.type == EventType.BAR:

self._set_correct_time_and_price(event)

# Only trade if we have all prices

if all(self.latest_prices > -1.0):

# Calculate portfolio market value via dot product

# of ETF prices with portfolio weights

self.port_mkt_val.append(

np.dot(self.latest_prices, self.weights)

)

# If there is enough data to form a full lookback

# window, then calculate zscore and carry out

# respective trades if thresholds are exceeded

if self.bars_elapsed > self.lookback:

zscore = (

self.port_mkt_val[-1] - np.mean(self.port_mkt_val)

) / np.std(self.port_mkt_val)

self.zscore_trade(zscore, event)

# coint_bollinger_backtest.py

import datetime

import click

import numpy as np

from qstrader import settings

from qstrader.compat import queue

from qstrader.price_parser import PriceParser

from qstrader.price_handler.yahoo_daily_csv_bar import \

YahooDailyCsvBarPriceHandler

from qstrader.strategy import Strategies, DisplayStrategy

from qstrader.position_sizer.naive import NaivePositionSizer

from qstrader.risk_manager.example import ExampleRiskManager

from qstrader.portfolio_handler import PortfolioHandler

from qstrader.compliance.example import ExampleCompliance

from qstrader.execution_handler.ib_simulated import \

IBSimulatedExecutionHandler

from qstrader.statistics.tearsheet import TearsheetStatistics

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

Saved successfully!

Ooh no, something went wrong!