イベント駆動バックテスト Python - Part VI

作者: リン・ハーン優しさ作成日: 2019-03-26 09:13:08,更新日:

この記事では,Python でのイベント駆動バックテストの議論を継続しています. 前回の記事では,現在のポジションを処理し,取引オーダーを生成し,利益と損失 (PnL) を追跡するポートフォリオクラスの階層を考慮しました.

この記事では,シミュレーションされたオーダー処理メカニズムを代表するクラス階層を作成し,最終的にブローカリングまたは他の市場接続手段に結びつくことで,これらのオーダーの実行を研究します.

ここで説明されている ExecutionHandler は,現在の市場価格ですべての注文を完了するので,非常にシンプルです.これは非常に非現実的です.しかし,改善のための良いベースラインとして機能します.

前回の抽象的なベースクラス階層と同様に,必要なプロパティとデコラターをabcライブラリからインポートする必要があります. さらに,FillEventとOrderEventをインポートする必要があります:

# execution.py

import datetime
import Queue

abcから輸入 ABCMeta,抽象方法

イベントインポートから FillEvent, OrderEvent ExecutionHandlerは,以前の抽象的なベースクラスと類似しており,単に1つの純粋な仮想メソッド,execut_orderを持っています:

# execution.py

class ExecutionHandler(object):
    """
    The ExecutionHandler abstract class handles the interaction
    between a set of order objects generated by a Portfolio and
    the ultimate set of Fill objects that actually occur in the
    market. 

    The handlers can be used to subclass simulated brokerages
    or live brokerages, with identical interfaces. This allows
    strategies to be backtested in a very similar manner to the
    live trading engine.
    """

    __metaclass__ = ABCMeta

    @abstractmethod
    def execute_order(self, event):
        """
        Takes an Order event and executes it, producing
        a Fill event that gets placed onto the Events queue.

        Parameters:
        event - Contains an Event object with order information.
        """
        raise NotImplementedError("Should implement execute_order()")

バックテスト戦略は,取引がどのように行われるかをシミュレートする必要があります.可能な限り最も簡単な実装は,すべての注文がすべての量に対する現在の市場価格で満たされていることを仮定することです.これは明らかに非常に非現実的で,バックテストのリアリズムを改善する大きな部分は,滑り込みと市場影響のより洗練されたモデルを設計することから来ます.

FillEvent は fill_cost の値 None が与えられていることに注意してください (execute_order の前列行を参照してください).前記記事で説明した NaivePortfolio オブジェクトの Fill のコストについては,既に取り扱っています.より現実的な実装では,現実的な Fill のコストを得るために,current 市場データ 値を利用します.

ARCA を交換として使用しただけです.しかし,バックテストの目的では,これは単なるプレスホルダーです.ライブ実行環境では,この場所依存ははるかに重要です:

# execution.py

class SimulatedExecutionHandler(ExecutionHandler):
    """
    The simulated execution handler simply converts all order
    objects into their equivalent fill objects automatically
    without latency, slippage or fill-ratio issues.

    This allows a straightforward "first go" test of any strategy,
    before implementation with a more sophisticated execution
    handler.
    """
    
    def __init__(self, events):
        """
        Initialises the handler, setting the event queues
        up internally.

        Parameters:
        events - The Queue of Event objects.
        """
        self.events = events

    def execute_order(self, event):
        """
        Simply converts Order objects into Fill objects naively,
        i.e. without any latency, slippage or fill ratio problems.

        Parameters:
        event - Contains an Event object with order information.
        """
        if event.type == 'ORDER':
            fill_event = FillEvent(datetime.datetime.utcnow(), event.symbol,
                                   'ARCA', event.quantity, event.direction, None)
            self.events.put(fill_event)

これは,イベント駆動バックテストを作成するために必要なクラス階層を結論付けます.次の記事では,バックテストされた戦略のためのパフォーマンスメトリックのセットを計算する方法について議論します.


もっと