Example #1
0
 def showTicker(self, symbol):
     try:
         ticker = self.rebuiltTickers[symbol]
         if not ticker:
             return
     except (KeyError, ):
         try:
             secs, count, ticker = \
                 tools.timed_ticker_rebuild(self.tickers[symbol], 
                                            self.strategyName, ltrim=50)
             self.rebuiltTickers[symbol] = ticker
             print 'rebuilt ticker in %s seconds' % (secs, )
         except (Exception, ), ex:
             print 'Exception rebuilding ticker: %r, %s' % (ex, ex, )
             import traceback
             traceback.print_exc()
             ticker = None
Example #2
0
 def showTicker(self, symbol):
     try:
         ticker = self.rebuiltTickers[symbol]
         if not ticker:
             return
     except (KeyError, ):
         try:
             secs, count, ticker = \
                 tools.timed_ticker_rebuild(self.tickers[symbol],
                                            self.strategyName, ltrim=50)
             self.rebuiltTickers[symbol] = ticker
             print 'rebuilt ticker in %s seconds' % (secs, )
         except (Exception, ), ex:
             print 'Exception rebuilding ticker: %r, %s' % (
                 ex,
                 ex,
             )
             import traceback
             traceback.print_exc()
             ticker = None
Example #3
0
def strategy_report(strategy, supervisors, fh=None, print_headfoot=True, 
                    print_subtotal=True, print_grandtotal=True):
    start = time.time()
    total_profit = 0
    total_trades = 0
    report = {}
    
    if print_headfoot:
        print >> fh, 'Strategy coverage run started at %s' % (time.ctime(), )
        print >> fh, altsep
        print >> fh, 'Using strategy name %s' % (strategy, )
        print >> fh

    for file_name, source_tickers in supervisors:
        source_tickers.sort(lambda a, b: cmp(a.symbol, b.symbol))
        file_profit = 0
        file_trades = 0
        file_report = report[file_name] = {}
        
        print >> fh, 'File %s' % (file_name, )
        print >> fh, 'Symbol\tTrades\t  Profit\tEffective'
        print >> fh, sep

        for source_ticker in source_tickers:
            symbol = source_ticker.symbol
            secs, count, rebuilt_ticker = \
                tools.timed_ticker_rebuild(source_ticker, strategy)
            
            strat_objs = [rebuilt_ticker.series[key].strategy 
                            for key in rebuilt_ticker.strategy_keys]
            try:
                strat_obj = strat_objs[0]
            except (IndexError, ):
                pass
            tick_results = list(strat_obj.gauge())
            tick_trades = len(tick_results)
            tick_profit = 0.0
            tick_effective = 0.0

            if tick_trades:
                last_trade = tick_results[-1]
                if last_trade[1][1]:
                    tick_profit = profit_on_close(strat_obj, tick_results)
                else:
                    tick_profit = last_trade[1][0]
                if tick_profit:
                    tick_effective = tick_profit / tick_trades

            file_trades += tick_trades
            file_profit += tick_profit
            tick_record = (symbol, tick_trades, tick_profit, tick_effective)
            print >> fh, '%4s\t%6s\t%8.2f\t%8.2f' % tick_record
            file_report[symbol] = (tick_trades, tick_profit)

        if file_trades:
            rpt = (file_trades, file_profit, file_profit/file_trades)
        else:
            rpt = (file_trades, file_profit, 0)
        total_trades += file_trades
        total_profit += file_profit
        if print_subtotal:
            print >> fh, 'Sub Total'
            print >> fh, '\t%6s\t%8.2f\t%8.2f' % rpt
            print

    if total_trades:
        rpt = (total_trades, total_profit, total_profit/total_trades)
    else:
        rpt = (total_trades, total_profit, 0)

    if print_grandtotal:
        print >> fh, 'Grand Total'
        print >> fh, sep
        print >> fh, '\t%6s\t%8.2f\t%8.2f' % rpt
        print >> fh
    if print_headfoot:
        print >> fh, altsep
        rpt = 'Strategy coverage run completed in %2.2f seconds' 
        print >> fh, rpt % (time.time() - start, )

    return (strategy, report)
def strategy_report(strategy,
                    supervisors,
                    fh=None,
                    print_headfoot=True,
                    print_subtotal=True,
                    print_grandtotal=True):
    start = time.time()
    total_profit = 0
    total_trades = 0
    report = {}

    if print_headfoot:
        print >> fh, 'Strategy coverage run started at %s' % (time.ctime(), )
        print >> fh, altsep
        print >> fh, 'Using strategy name %s' % (strategy, )
        print >> fh

    for file_name, source_tickers in supervisors:
        source_tickers.sort(lambda a, b: cmp(a.symbol, b.symbol))
        file_profit = 0
        file_trades = 0
        file_report = report[file_name] = {}

        print >> fh, 'File %s' % (file_name, )
        print >> fh, 'Symbol\tTrades\t  Profit\tEffective'
        print >> fh, sep

        for source_ticker in source_tickers:
            symbol = source_ticker.symbol
            secs, count, rebuilt_ticker = \
                tools.timed_ticker_rebuild(source_ticker, strategy)

            strat_objs = [
                rebuilt_ticker.series[key].strategy
                for key in rebuilt_ticker.strategy_keys
            ]
            try:
                strat_obj = strat_objs[0]
            except (IndexError, ):
                pass
            tick_results = list(strat_obj.gauge())
            tick_trades = len(tick_results)
            tick_profit = 0.0
            tick_effective = 0.0

            if tick_trades:
                last_trade = tick_results[-1]
                if last_trade[1][1]:
                    tick_profit = profit_on_close(strat_obj, tick_results)
                else:
                    tick_profit = last_trade[1][0]
                if tick_profit:
                    tick_effective = tick_profit / tick_trades

            file_trades += tick_trades
            file_profit += tick_profit
            tick_record = (symbol, tick_trades, tick_profit, tick_effective)
            print >> fh, '%4s\t%6s\t%8.2f\t%8.2f' % tick_record
            file_report[symbol] = (tick_trades, tick_profit)

        if file_trades:
            rpt = (file_trades, file_profit, file_profit / file_trades)
        else:
            rpt = (file_trades, file_profit, 0)
        total_trades += file_trades
        total_profit += file_profit
        if print_subtotal:
            print >> fh, 'Sub Total'
            print >> fh, '\t%6s\t%8.2f\t%8.2f' % rpt
            print

    if total_trades:
        rpt = (total_trades, total_profit, total_profit / total_trades)
    else:
        rpt = (total_trades, total_profit, 0)

    if print_grandtotal:
        print >> fh, 'Grand Total'
        print >> fh, sep
        print >> fh, '\t%6s\t%8.2f\t%8.2f' % rpt
        print >> fh
    if print_headfoot:
        print >> fh, altsep
        rpt = 'Strategy coverage run completed in %2.2f seconds'
        print >> fh, rpt % (time.time() - start, )

    return (strategy, report)