def main(plot):
    
    # instruments and talbename in sql
    instrument = "ibm"
    tableName = "newyorkexchange.ibm_historicalquotes_newyork"
    
    # create instance to connect Mysql
    cnx = cnxStock()
    # create instance to query Mysql data base
    query = historicalPriceQuery(instrument, tableName)    
    df = query.pandasQuerySingle(cnx.connect())
    #close connection
    cnx.close_connection()
    
    # feed data from PyAlgoTrade back testing
    feed = MyLibrary.pandas_feed_mysql.DataFrameBarFeed(df, instrument, barfeed.Frequency.DAY)
    
    smaPeriod = 163
    strat = sma_crossover.SMACrossOver(feed, instrument, smaPeriod)
    sharpeRatioAnalyzer = sharpe.SharpeRatio()
    strat.attachAnalyzer(sharpeRatioAnalyzer)

    if plot:
        plt = plotter.StrategyPlotter(strat, True, False, True)
        plt.getInstrumentSubplot(instrument).addDataSeries("sma", strat.getSMA())

    strat.run()
    print "Sharpe ratio: %.2f" % sharpeRatioAnalyzer.getSharpeRatio(0.05)

    if plot:
        plt.plot()
Ejemplo n.º 2
0
 def getAllHistoricalQuotes(self,cnx, instruments):
     tableNames = dict()
     for instrument  in instruments :
         tableName = str("newyorkexchange.`%s_historicalquotes_newyork`" %instrument)
         tableNames[instrument] = tableName
      
     query = historicalPriceQuery(instruments, tableNames)
     quotes = query.pandasQueryMulitple(cnx)
     return quotes
def main(plot):
    initialCash = 10000
    instrumentsByClass = {
        "US Stocks": ["ibm"],
        "Foreign Stocks": ["ulbi"],
        "US 10 Year Government Bonds": ["alex"],
        "Real Estate": ["cx"],
        "Commodities": ["tpvg"],
    }

    # Download the bars.
    instruments = ["umc"]
    for assetClass in instrumentsByClass:
        instruments.extend(instrumentsByClass[assetClass])
    tableNames = dict()
    for instrument  in instruments :
        tableName = str("newyorkexchange.%s_historicalquotes_newyork" %instrument)
        tableNames[instrument] = tableName
    
    try :
        # create instance to connect Mysql
        cnx = cnxStock()
        # create instance to query Mysql data base
        query = historicalPriceQuery(instruments, tableNames)
        dfs = query.pandasQueryMulitple(cnx.connect())
        #close connection
    finally:
        cnx.close_connection()
    
    feed = DataFrameMulitpleBarFeed(dfs,barfeed.Frequency.DAY)
    feed.addMultipleBars()
    
    strat = MarketTiming(feed, instrumentsByClass, initialCash)
    sharpeRatioAnalyzer = sharpe.SharpeRatio()
    strat.attachAnalyzer(sharpeRatioAnalyzer)
    returnsAnalyzer = returns.Returns()
    strat.attachAnalyzer(returnsAnalyzer)

    if plot:
        plt = plotter.StrategyPlotter(strat, False, False, True)
        plt.getOrCreateSubplot("cash").addCallback("Cash", lambda x: strat.getBroker().getCash())
        # Plot strategy vs. SPY cumulative returns.
        plt.getOrCreateSubplot("returns").addDataSeries("umc", cumret.CumulativeReturn(feed["umc"].getPriceDataSeries()))
        plt.getOrCreateSubplot("returns").addDataSeries("Strategy", returnsAnalyzer.getCumulativeReturns())

    strat.run()
    
    print "Sharpe ratio: %.2f" % sharpeRatioAnalyzer.getSharpeRatio(0.05)
    print "Returns: %.2f %%" % (returnsAnalyzer.getCumulativeReturns()[-1] * 100)

    if plot:
        plt.plot()
Ejemplo n.º 4
0
def main(plot):
    
    # trail
    initialCash = 10000
    instruments = ["aapl", "ibm"]
    windowSize = 50
    tableNames = dict()
    for instrument  in instruments :
        tableName = str("newyorkexchange.%s_historicalquotes_newyork" %instrument)
        tableNames[instrument] = tableName
        
    try :
        # create instance to connect Mysql
        cnx = cnxStock()
        # create instance to query Mysql data base
        query = historicalPriceQuery(instruments, tableNames)
        dfs = query.pandasQueryMulitple(cnx.connect())
        #close connection
    finally:
        cnx.close_connection()
    
    feed = DataFrameMulitpleBarFeed(dfs,barfeed.Frequency.DAY)
    feed.addMultipleBars()
    strat = StatArb(feed,dfs, windowSize, initialCash)
    
    sharpeRatioAnalyzer = sharpe.SharpeRatio()
    strat.attachAnalyzer(sharpeRatioAnalyzer)

    if plot:
        plt = plotter.StrategyPlotter(strat, False, False, True)
        plt.getOrCreateSubplot("zScore").addDataSeries("zScore", strat.getZScoreDS())
        plt.getOrCreateSubplot("spread").addDataSeries("Spread", strat.getSpreadDS())

    strat.run()
    print "Sharpe ratio: %.2f" % sharpeRatioAnalyzer.getSharpeRatio(0.05)

    if plot:
        plt.plot()