Skip to main content
Join the official 2020 Python Developers SurveyStart the survey!

pyalgotrade tushare module

Project description Documentation Status Updates


# PIP 自动安装方法
pip install pyalgotrade_tushare

# 手动下载源码安装
git clone
cd pyalgotrade_tushare
python install



from pyalgotrade import plotter, strategy
from pyalgotrade.stratanalyzer import sharpe
from pyalgotrade.technical import ma

from pyalgotrade_tushare import tools

class MyStrategy(strategy.BacktestingStrategy):
    def __init__(self, feed, instrument):

        self.__position = None
        self.__sma = ma.SMA(feed[instrument].getCloseDataSeries(), 150)
        self.__instrument = instrument

    def onEnterOk(self, position):
        execInfo = position.getEntryOrder().getExecutionInfo()"买入 %.2f" % (execInfo.getPrice()))

    def onEnterCanceled(self, position):
        self.__position = None

    def onExitOk(self, position):
        execInfo = position.getExitOrder().getExecutionInfo()"卖出 %.2f" % (execInfo.getPrice()))
        self.__position = None

    def onExitCanceled(self, position):
        # If the exit was canceled, re-submit it.

    def getSMA(self):
        return self.__sma

    def onBars(self, bars):
        # 每一个数据都会抵达这里,就像becktest中的next
        # Wait for enough bars to be available to calculate a SMA.
        if self.__sma[-1] is None:

        # bar.getTyoicalPrice = (bar.getHigh() + bar.getLow() + bar.getClose())/ 3.0
        bar = bars[self.__instrument]

        # If a position was not opened, check if we should enter a long position.
        if self.__position is None:
            if bar.getPrice() > self.__sma[-1]:
                # 开多头.
                self.__position = self.enterLong(self.__instrument, 100, True)

        # 平掉多头头寸.
        elif bar.getPrice() < self.__sma[-1] and not self.__position.exitActive():

if __name__ == '__main__':
    instruments = ["600036"]

    feeds = tools.build_feed(instruments, 2013, 2018, "histdata")

    # 3.实例化策略
    strat = MyStrategy(feeds, instruments[0])

    # 4.设置指标和绘图
    ratio = sharpe.SharpeRatio()
    plter = plotter.StrategyPlotter(strat)

    # 5.运行策略"最终收益: %.2f" % strat.getResult())

    # 6.输出夏普率、绘图"夏普比率: " + str(ratio.getSharpeRatio(0)))


  • 修改了 PIP 安装程序问题
  • 本程序只支持 python3.



Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for pyalgotrade_tushare, version 0.1.2
Filename, size File type Python version Upload date Hashes
Filename, size pyalgotrade_tushare-0.1.2-py2.py3-none-any.whl (7.1 kB) File type Wheel Python version 3.6 Upload date Hashes View
Filename, size pyalgotrade_tushare-0.1.2.tar.gz (6.4 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page