我的python回测系统创建之路(1)

简介: 我的python回测系统创建之路(1)

接触到了不少Python相关的开源项目,也接触到了不少回测框架,感觉这些框架都比较难懂,加上自己用pandas做回测,效率有点低,要建立一套自己的回测框架。


       读了不少Python回测框架作者创建框架的思路与理念,觉得使用事件驱动型框架比较好,另外,我要创建的这个框架将会模仿文华财经或者TB进行创建。正好最近读master Python for finnace 这本书,第九章有讲怎么创建一个回测框架。

20170916115012782.png



     在本文中,将这篇章节的大体思路翻译成汉语和代码,分享给大家。

    +++++++++++++++++++++++++++++++++++++++++++++++++

    翻译的部分内容

    +++++++++++++++++++++++++++++++++++++++++++++++++

    事件驱动回测系统的概念:

    在真实的交易环境中,一般要包含以下模块:数据,订单匹配模块,订单管理,账户,更新仓位;


""" Store a single unit of data """
class TickData:
  def __init__(self, symbol, timestamp,
  last_price=0, total_volume=0):
  self.symbol = symbol
  self.timestamp = timestamp
  self.open_price = 0
  self.last_price = last_price
  self.total_volume = total_volume
class MarketData:
  def __init__(self):
      self.__recent_ticks__ = dict()
  def add_last_price(self, time, symbol, price, volume):
      tick_data = TickData(symbol, time, price, volume)
      self.__recent_ticks__[symbol] = tick_data
  def add_open_price(self, time, symbol, price):
      tick_data = self.get_existing_tick_data(symbol, time)
      tick_data.open_price = price
  def get_existing_tick_data(self, symbol, time):
      if not symbol in self.__recent_ticks__:
      tick_data = TickData(symbol, time)
      self.__recent_ticks__[symbol] = tick_data
      return self.__recent_ticks__[symbol]
  def get_last_price(self, symbol):
      return self.__recent_ticks__[symbol].last_price
  def get_open_price(self, symbol):
      return self.__recent_ticks__[symbol].open_price
  def get_timestamp(self, symbol):
      return self.__recent_ticks__[symbol].timestamp
import pandas.io.data as web
""" Download prices from an external data source """
class MarketDataSource:
  def __init__(self):
    self.event_tick = None
       self.ticker, self.source = None, None
       self.start, self.end = None, None
       self.md = MarketData()
  def start_market_simulation(self):
        data = web.DataReader(self.ticker, self.source,
                              self.start, self.end)
        for time, row in data.iterrows():
        self.md.add_last_price(time, self.ticker,
        row["Close"], row["Volume"])
        self.md.add_open_price(time, self.ticker, row["Open"])
        if not self.event_tick is None:
            self.event_tick(self.md)
class Order:
  def __init__(self, timestamp, symbol, qty, is_buy,is_market_order, price=0):
     self.timestamp = timestamp
     self.symbol = symbol
     self.qty = qty
     self.price = price
     self.is_buy = is_buy
     self.is_market_order = is_market_order
     self.is_filled = False
     self.filled_price = 0
     self.filled_time = None
     self.filled_qty = 0
class Position:
  def __init__(self):
      self.symbol = None
      self.buys, self.sells, self.net = 0, 0, 0
      self.realized_pnl = 0
      self.unrealized_pnl = 0
      self.position_value = 0
  def event_fill(self, timestamp, is_buy, qty, price):
      if is_buy:
         self.buys += qty
      else:
         self.sells += qty
      self.net = self.buys - self.sells
      changed_value = qty * price * (-1 if is_buy else 1)
      self.position_value += changed_value
      if self.net == 0:
         self.realized_pnl = self.position_value
  def update_unrealized_pnl(self, price):
      if self.net == 0:
         self.unrealized_pnl = 0
      else:
         self.unrealized_pnl = price * self.net + \
                                self.position_value
      return self.unrealized_pnl
""" Base strategy for implementation """
class Strategy:
  def __init__(self):
      self.event_sendorder = None
  def event_tick(self, market_data):
      pass
  def event_order(self, order):
      pass
  def event_position(self, positions):
      pass
  def send_market_order(self, symbol, qty, is_buy, timestamp):
      if not self.event_sendorder is None:
         order = Order(timestamp, symbol, qty, is_buy, True)
         self.event_sendorder(order)
目录
相关文章
|
1月前
|
人工智能 机器人 测试技术
【Python】Python房屋销售系统(源码)【独一无二】(课程设计)
【Python】Python房屋销售系统(源码)【独一无二】(课程设计)
|
1月前
|
存储 人工智能 机器人
【Python】Python医疗门诊系统(源码+报告)【独一无二】
【Python】Python医疗门诊系统(源码+报告)【独一无二】
|
1月前
|
存储 人工智能 搜索推荐
【python】python用户管理系统[简易版](源码+报告)【独一无二】
【python】python用户管理系统[简易版](源码+报告)【独一无二】
|
2月前
|
数据采集 机器学习/深度学习 算法框架/工具
利用Python实现基于图像识别的自动化数据采集系统
本文介绍了如何利用Python编程语言结合图像识别技术,构建一个自动化的数据采集系统。通过分析图像内容,实现对特定信息的提取和识别,并将其转化为结构化数据,从而实现高效、准确地采集需要的信息。本文将详细讨论系统的设计思路、技术实现以及应用场景。
|
4天前
|
机器学习/深度学习 IDE 数据挖掘
如何系统地自学python?
如何系统地自学python?
15 1
|
1月前
|
JavaScript 前端开发 关系型数据库
事件管理工具:用Python和Vue打造在线预订和票务系统
【4月更文挑战第11天】构建一个在线预订和票务系统,结合Python(Flask或Django)后端与Vue.js前端。准备工作包括设置Python环境、Node.js、数据库和Git。后端创建RESTful API,Flask适合轻量级,Django提供完整框架。前端使用Vue CLI、Vuex和Vue Router构建用户界面。通过Vuex管理状态,Vue Router定义路由,Axios与后端通信。这种架构支持团队协作,代码维护和扩展。
|
6天前
|
前端开发 UED Python
Wagtail-基于Python Django的内容管理系统CMS实现公网访问
Wagtail-基于Python Django的内容管理系统CMS实现公网访问
|
8天前
|
运维 监控 Ubuntu
Python实现ubuntu系统进程内存监控
Python实现ubuntu系统进程内存监控
14 1
|
19天前
|
数据可视化 Python
Python的分子模拟动态促进DF Theory理论对二进制硬盘系统的适用性
Python的分子模拟动态促进DF Theory理论对二进制硬盘系统的适用性
|
21天前
|
数据采集 机器学习/深度学习 数据挖掘
如何系统地自学 Python?
如何系统地自学 Python?
20 0