Skip to main content

PyAlgoTrade tushare module

https://img.shields.io/pypi/v/pyalgotrade_tushare.svg https://img.shields.io/travis/bopo/pyalgotrade_tushare.svg Documentation Status Updates

安装方法

# PIP 自动安装方法
pip install pyalgotrade_tushare

# 手动下载源码安装
git clone https://github.com/bopo/pyalgotrade_tushare.git
cd pyalgotrade_tushare
python setup.py 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):
        super().__init__(feed)

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

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

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

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

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

    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:
            return

        # 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():
            self.__position.exitMarket()


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

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

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

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

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

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

版本更新

  • 修改了 PIP 安装程序问题

  • 本程序只支持 python3.

贡献名单

  • bopo.wang

Metadata

Release files for pyalgotrade_tushare 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyalgotrade_tushare 0.1.2
File Size Uploaded
pyalgotrade_tushare-0.1.2.tar.gz 6.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyalgotrade_tushare 0.1.2
File Interpreter ABI Platform
pyalgotrade_tushare-0.1.2-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 13.5 kB

Release files / pyalgotrade_tushare-0.1.2.tar.gz

Download URL pyalgotrade_tushare-0.1.2.tar.gz
Size 6.4 kB
Tags Source
SHA-256 checksum
How to use checksums
7cb05ff5f9876e9fdad895d8ae8a106da047ea49ae90744a8296ac3fd582a4a2
BLAKE2b-256 checksum
How to use checksums
6d6d820533b61e531fd7ed172f28cf9fa7502adbd387d77dc1c58c8bbf317db5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Python-urllib/3.6

Release files / pyalgotrade_tushare-0.1.2-py2.py3-none-any.whl

Download URL pyalgotrade_tushare-0.1.2-py2.py3-none-any.whl
Size 7.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
25b75d5c531174915079ff8dc050972dc498364cea1ebf5d714a970998f2aebc
BLAKE2b-256 checksum
How to use checksums
8df56cb5a632a55366187ac10b86fbaf417a39d16872d33a3d5ee888d284e6bc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Python-urllib/3.6

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page