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.

441

self.minutes += 1

# Allow enough time to pass to populate the

# returns feature vector

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

pred = self.model.predict(

self.cur_returns.reshape((1, -1))

)[0]

# Long only strategy

if not self.invested and pred == 1:

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

self.events_queue.put(

SignalEvent(

self.tickers[0], "BOT", self.qty

)

)

self.invested = True

if self.invested and pred == -1:

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

self.events_queue.put(

SignalEvent(

self.tickers[0], "SLD", self.qty

)

)

self.invested = False

# intraday_ml_backtest.py

import click

import datetime

from qstrader import settings

from qstrader.compat import queue

from qstrader.price_parser import PriceParser

from qstrader.price_handler.iq_feed_intraday_csv_bar import \

IQFeedIntradayCsvBarPriceHandler

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

from qstrader.trading_session.backtest import Backtest

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

Saved successfully!

Ooh no, something went wrong!