Exemple #1
0
def testStrategy():

    from pyalgotrade import bar

    strat = MyStrategy
    instrument = '600288'
    market = 'SH'
    fromDate = '20150101'
    toDate = '20150601'
    frequency = bar.Frequency.MINUTE

    import os

    if frequency == bar.Frequency.MINUTE:
        path = os.path.join('..', 'histdata', 'minute')
    elif frequency == bar.Frequency.DAY:
        path = os.path.join('..', 'histdata', 'day')
    filepath = os.path.join(path, instrument + market + ".csv")

    #############################################don't change ############################33
    from pyalgotrade.cn.csvfeed import Feed

    barfeed = Feed(frequency)
    barfeed.setDateTimeFormat('%Y-%m-%d %H:%M:%S')
    barfeed.loadBars(instrument, market, fromDate, toDate, filepath)

    pyalgotrade_id = instrument + '.' + market
    strat = strat(barfeed, pyalgotrade_id)
    print filepath
Exemple #2
0
def testStrategy():
    from pyalgotrade import bar
    from pyalgotrade import plotter

    strat = thrSMA
    instrument = '600519'
    market = 'SH'
    fromDate = '20101001'
    toDate = '20150601'
    frequency = bar.Frequency.DAY
    paras = [5, 17, 40, 10]
    plot = True
    import os
    filepath = os.path.join(r'E:\xsj\day', instrument + market + ".csv")

    #############################################don't change ############################33
    from pyalgotrade.cn.csvfeed import Feed

    barfeed = Feed(frequency)
    barfeed.setDateTimeFormat('%Y-%m-%d')
    barfeed.loadBars(instrument, market, fromDate, toDate, filepath)

    pyalgotrade_id = instrument + '.' + market
    strat = strat(barfeed, pyalgotrade_id, *paras)

    from pyalgotrade.stratanalyzer import returns
    from pyalgotrade.stratanalyzer import sharpe
    from pyalgotrade.stratanalyzer import drawdown
    from pyalgotrade.stratanalyzer import trades

    retAnalyzer = returns.Returns()
    strat.attachAnalyzer(retAnalyzer)
    sharpeRatioAnalyzer = sharpe.SharpeRatio()
    strat.attachAnalyzer(sharpeRatioAnalyzer)
    drawDownAnalyzer = drawdown.DrawDown()
    strat.attachAnalyzer(drawDownAnalyzer)
    tradesAnalyzer = trades.Trades()
    strat.attachAnalyzer(tradesAnalyzer)

    if plot:
        plt = plotter.StrategyPlotter(strat, True, True, True)

    strat.run()

    if plot:
        plt.plot()

    #夏普率
    sharp = sharpeRatioAnalyzer.getSharpeRatio(0.05)
    #最大回撤
    maxdd = drawDownAnalyzer.getMaxDrawDown()
    #收益率
    return_ = retAnalyzer.getCumulativeReturns()[-1]
    #收益曲线
    return_list = []
    for item in retAnalyzer.getCumulativeReturns():
        return_list.append(item)
Exemple #3
0
from pyalgotrade import bar
from pyalgotrade import plotter

instrument = '600288'
market = 'SH'
fromDate = '20150201'
toDate = '20150221'
frequency = bar.Frequency.DAY

import os
if frequency == bar.Frequency.MINUTE:
    path = os.path.join('histdata', 'minute')
elif frequency == bar.Frequency.DAY:
    path = os.path.join(r'C:\Users\linner\Desktop\learn\pyalgo\histdata',
                        'day')
filepath = os.path.join(path, instrument + market + ".csv")

#############################################don't change ############################33
from pyalgotrade.cn.csvfeed import Feed

barfeed = Feed(frequency)
barfeed.setDateTimeFormat('%Y-%m-%d')
barfeed.loadBars(instrument, market, fromDate, toDate, filepath)

pyalgotrade_id = instrument + '.' + market
strat = MyStrategy(barfeed, pyalgotrade_id)
plt = plotter.StrategyPlotter(strat, True, True, True)
strat.run()
plt.plot()
Exemple #4
0
def testStrategy():
    from pyalgotrade import bar
    from pyalgotrade import plotter
    
    strat = DoubleMA    
    instrument = '600288'
    market = 'SH'
    fromDate = '20150101'
    toDate ='20150601'
    frequency = bar.Frequency.MINUTE
    paras = [5, 20]
    plot = True
    
    #############################################path set ############################33 
    import os
    if frequency == bar.Frequency.MINUTE:
        path = os.path.join('..', 'histdata', 'minute')
    elif frequency == bar.Frequency.DAY:
        path = os.path.join('..', 'histdata', 'day')
    filepath = os.path.join(path, instrument + market + ".csv")
    
    
    #############################################don't change ############################33  
    from pyalgotrade.cn.csvfeed import Feed
    
    barfeed = Feed(frequency)
    barfeed.setDateTimeFormat('%Y-%m-%d %H:%M:%S')
    barfeed.loadBars(instrument, market, fromDate, toDate, filepath)
    
    pyalgotrade_id = instrument + '.' + market
    strat = strat(barfeed, pyalgotrade_id, *paras)
    
    from pyalgotrade.stratanalyzer import returns
    from pyalgotrade.stratanalyzer import sharpe
    from pyalgotrade.stratanalyzer import drawdown
    from pyalgotrade.stratanalyzer import trades
    
    retAnalyzer = returns.Returns()
    strat.attachAnalyzer(retAnalyzer)
    sharpeRatioAnalyzer = sharpe.SharpeRatio()
    strat.attachAnalyzer(sharpeRatioAnalyzer)
    drawDownAnalyzer = drawdown.DrawDown()
    strat.attachAnalyzer(drawDownAnalyzer)
    tradesAnalyzer = trades.Trades()
    strat.attachAnalyzer(tradesAnalyzer)
    
    if plot:
        plt = plotter.StrategyPlotter(strat, True, True, True)
        
    strat.run()
    
    if plot:
        plt.plot()
        


    #夏普率
    sharp = sharpeRatioAnalyzer.getSharpeRatio(0.05)
    #最大回撤
    maxdd = drawDownAnalyzer.getMaxDrawDown()
    #收益率
    return_ = retAnalyzer.getCumulativeReturns()[-1]
    #收益曲线
    return_list = []
    for item in retAnalyzer.getCumulativeReturns():
        return_list.append(item)
def testStrategy():
    from pyalgotrade import bar
    from pyalgotrade import plotter

    strat = OrderBook
    instrument = '600288'
    market = 'SH'
    fromDate = '20160815'
    toDate = '20160820'
    frequency = bar.Frequency.TRADE
    paras = [5, 20]
    plot = True

    #############################################path set ############################33
    import os
    if frequency == bar.Frequency.TRADE:
        path = os.path.join('..', 'histdata', 'tick')
    elif frequency == bar.Frequency.MINUTE:
        path = os.path.join('..', 'histdata', 'minute')
    elif frequency == bar.Frequency.DAY:
        path = os.path.join('..', 'histdata', 'day')
    path = os.path.join(path, instrument + market + ".csv")

    #############################################don't change ############################33
    from pyalgotrade.cn.csvfeed import Feed

    barfeed = Feed(frequency)
    barfeed.setDateTimeFormat('%Y-%m-%d %H:%M:%S.%f')
    barfeed.loadTicks(instrument, market, fromDate, toDate, path)

    pyalgotrade_id = instrument + '.' + market
    strat = strat(barfeed, pyalgotrade_id, *paras)

    from pyalgotrade.stratanalyzer import returns
    from pyalgotrade.stratanalyzer import sharpe
    from pyalgotrade.stratanalyzer import drawdown
    from pyalgotrade.stratanalyzer import trades

    retAnalyzer = returns.Returns()
    strat.attachAnalyzer(retAnalyzer)
    sharpeRatioAnalyzer = sharpe.SharpeRatio()
    strat.attachAnalyzer(sharpeRatioAnalyzer)
    drawDownAnalyzer = drawdown.DrawDown()
    strat.attachAnalyzer(drawDownAnalyzer)
    tradesAnalyzer = trades.Trades()
    strat.attachAnalyzer(tradesAnalyzer)

    if plot:
        plt = plotter.StrategyPlotter(strat, True, True, True)

    strat.run()

    if plot:
        plt.plot()

    #夏普率
    sharp = sharpeRatioAnalyzer.getSharpeRatio(0.05)
    #最大回撤
    maxdd = drawDownAnalyzer.getMaxDrawDown()
    #收益率
    return_ = retAnalyzer.getCumulativeReturns()[-1]
    #收益曲线
    return_list = []
    for item in retAnalyzer.getCumulativeReturns():
        return_list.append(item)
Exemple #6
0
def testStrategy():
    from pyalgotrade import bar
    from pyalgotrade import plotter

    strat = DoubleMA
    instrument = '00700'
    #instrument = '03988'
    market = 'HK'
    fromDate = '20160101'
    toDate = '20170601'
    #frequency = bar.Frequency.MINUTE
    frequency = bar.Frequency.DAY
    paras = [5, 20]
    plot = True

    #############################################path set ############################33
    import os
    if frequency == bar.Frequency.MINUTE:
        path = os.path.join('..', 'histdata', 'minute')
        ktype = "K_1M"
    elif frequency == bar.Frequency.DAY:
        path = os.path.join('..', 'histdata', 'day')
        ktype = "K_DAY"
    filepath = os.path.join(path, instrument + market + ".csv")

    #############################################don't change ############################33

    #如果使用富途客户端,请链接127.0.0.1本地端口
    #quote_ctx = futu_open.OpenQuoteContext(host='127.0.0.1', async_port=11111)
    #云服务器ip,方便测试
    quote_ctx = futu_open.OpenQuoteContext(host='119.29.141.202',
                                           async_port=11111)
    kline_table = downloadDataFromFutu(instrument, market, ktype, 500,
                                       quote_ctx)
    kline_table.to_csv(filepath,
                       header=[
                           'id', 'datetime', 'open', 'close', 'high', 'low',
                           'volume', 'amount'
                       ])

    from pyalgotrade.cn.csvfeed import Feed

    barfeed = Feed(frequency)
    if frequency == bar.Frequency.MINUTE:
        barfeed.setDateTimeFormat('%Y-%m-%d %H:%M:%S')
    elif frequency == bar.Frequency.DAY:
        barfeed.setDateTimeFormat('%Y-%m-%d %H:%M:%S')

    barfeed.loadBars(instrument, market, fromDate, toDate, filepath)

    pyalgotrade_id = instrument + '.' + market

    strat = strat(barfeed, pyalgotrade_id, *paras)
    """
    from pyalgotrade.stratanalyzer import returns
    from pyalgotrade.stratanalyzer import sharpe
    from pyalgotrade.stratanalyzer import drawdown
    from pyalgotrade.stratanalyzer import trades
    
    retAnalyzer = returns.Returns()
    strat.attachAnalyzer(retAnalyzer)
    sharpeRatioAnalyzer = sharpe.SharpeRatio()
    strat.attachAnalyzer(sharpeRatioAnalyzer)
    drawDownAnalyzer = drawdown.DrawDown()
    strat.attachAnalyzer(drawDownAnalyzer)
    tradesAnalyzer = trades.Trades()
    strat.attachAnalyzer(tradesAnalyzer)
    """

    if plot:
        plt = plotter.StrategyPlotter(strat, True, True, True)

    strat.run()

    if plot:
        plt.plot()
def testStrategy():
    from pyalgotrade import bar
    from pyalgotrade import plotter

    strat = Bollinger_Bandit
    instrument = "600288"
    market = "SH"
    fromDate = "20150101"
    toDate = "20150601"
    frequency = bar.Frequency.MINUTE
    paras = [40, 15, 35, 15, 60, 2]
    plot = True

    #############################################path set ############################33
    import os

    if frequency == bar.Frequency.MINUTE:
        path = os.path.join("..", "histdata", "minute")
    elif frequency == bar.Frequency.DAY:
        path = os.path.join("..", "histdata", "day")
    filepath = os.path.join(path, instrument + market + ".csv")

    #############################################don't change ############################33
    from pyalgotrade.cn.csvfeed import Feed

    barfeed = Feed(frequency)
    barfeed.setDateTimeFormat("%Y-%m-%d %H:%M:%S")
    barfeed.loadBars(instrument, market, fromDate, toDate, filepath)

    pyalgotrade_id = instrument + "." + market
    strat = strat(barfeed, pyalgotrade_id, *paras)

    from pyalgotrade.stratanalyzer import returns
    from pyalgotrade.stratanalyzer import sharpe
    from pyalgotrade.stratanalyzer import drawdown
    from pyalgotrade.stratanalyzer import trades

    retAnalyzer = returns.Returns()
    strat.attachAnalyzer(retAnalyzer)
    sharpeRatioAnalyzer = sharpe.SharpeRatio()
    strat.attachAnalyzer(sharpeRatioAnalyzer)
    drawDownAnalyzer = drawdown.DrawDown()
    strat.attachAnalyzer(drawDownAnalyzer)
    tradesAnalyzer = trades.Trades()
    strat.attachAnalyzer(tradesAnalyzer)

    if plot:
        plt = plotter.StrategyPlotter(strat, True, True, True)

    strat.run()

    if plot:
        plt.plot()

    # 夏普率
    sharp = sharpeRatioAnalyzer.getSharpeRatio(0.05)
    # 最大回撤
    maxdd = drawDownAnalyzer.getMaxDrawDown()
    # 收益率
    return_ = retAnalyzer.getCumulativeReturns()[-1]
    # 收益曲线
    return_list = []
    for item in retAnalyzer.getCumulativeReturns():
        return_list.append(item)
Exemple #8
0
def testStrategy():

    #sys.path.append("D:\spyderProject\pyalgotrade_cn\pyalgotrade-cn-master")

    from pyalgotrade import bar
    from pyalgotrade import plotter

    # get the test strategy
    strat = DoubleMA
    instrument = '600288'  #security code
    market = 'SH'
    fromDate = '20150101'
    toDate = '20150601'

    #get the test frequency
    frequency = bar.Frequency.MINUTE
    paras = [5, 20]
    plot = True

    #############################################path set ############################33
    import os
    if frequency == bar.Frequency.MINUTE:
        path = os.path.join('..', 'histdata', 'minute')
    elif frequency == bar.Frequency.DAY:
        path = os.path.join('..', 'histdata', 'day')
    filepath = os.path.join(path, instrument + market + ".csv")

    #############################################don't change ############################33
    from pyalgotrade.cn.csvfeed import Feed

    barfeed = Feed(frequency)
    barfeed.setDateTimeFormat('%Y-%m-%d %H:%M:%S')
    barfeed.loadBars(instrument, market, fromDate, toDate, filepath)

    pyalgotrade_id = instrument + '.' + market
    strat = strat(barfeed, pyalgotrade_id, *paras)

    # calculate the indexs
    from pyalgotrade.stratanalyzer import returns
    from pyalgotrade.stratanalyzer import sharpe
    from pyalgotrade.stratanalyzer import drawdown
    from pyalgotrade.stratanalyzer import trades

    retAnalyzer = returns.Returns()
    strat.attachAnalyzer(retAnalyzer)
    sharpeRatioAnalyzer = sharpe.SharpeRatio()
    strat.attachAnalyzer(sharpeRatioAnalyzer)
    drawDownAnalyzer = drawdown.DrawDown()
    strat.attachAnalyzer(drawDownAnalyzer)
    tradesAnalyzer = trades.Trades()
    strat.attachAnalyzer(tradesAnalyzer)

    if plot:
        plt = plotter.StrategyPlotter(strat, True, True, True)

    strat.run()

    if plot:
        plt.plot()

    #夏普率
    sharp = sharpeRatioAnalyzer.getSharpeRatio(0.05)
    #最大回撤
    maxdd = drawDownAnalyzer.getMaxDrawDown()
    #收益率
    return_ = retAnalyzer.getCumulativeReturns()[-1]
    #收益曲线
    return_list = []
    for item in retAnalyzer.getCumulativeReturns():
        return_list.append(item)

    print sharp
    print maxdd
    print return_