def main():
    barFeed = barfeed.LiveTradeFeed()
    brk = broker.PaperTradingBroker(1000, barFeed)
    strat = Strategy(barFeed, brk)

    strat.run()
Exemple #2
0
def main():

    config = ConfigParser.ConfigParser()
    config.read('bot.cfg')

    # Create BitStamp feed
    barFeed = barfeed.LiveTradeFeed()

    # cash
    # bars = BitStamp live feed
    # fee = 0.5 % per order
    cash = int(config.get('main', 'cash'))
    brk = broker.PaperTradingBroker(cash, barFeed)

    strat = Strategy(barFeed, brk)

    # Attach the strategy analyzers
    retAnalyzer = returns.Returns()
    strat.attachAnalyzer(retAnalyzer)
    tradesAnalyzer = trades.Trades()
    strat.attachAnalyzer(tradesAnalyzer)
    sharpeRatioAnalyzer = sharpe.SharpeRatio()
    strat.attachAnalyzer(sharpeRatioAnalyzer)
    drawDownAnalyzer = drawdown.DrawDown()
    strat.attachAnalyzer(drawDownAnalyzer)

    try:
        strat.run()

        #Cool down
        time.sleep(5)

        # Collect the analysis results
        fin_port_val = strat.getResult()
        cum_ret = retAnalyzer.getCumulativeReturns()[-1] * 100
        stratReturns = tradesAnalyzer.getAllReturns()
        return_avg = stratReturns.mean() * 100
        return_std = stratReturns.std() * 100
        return_min = stratReturns.min() * 100
        return_max = stratReturns.max() * 100
        sharpe_ratio = sharpeRatioAnalyzer.getSharpeRatio(0.05)
        max_drawdown = drawDownAnalyzer.getMaxDrawDown() * 100
        longest_drawdown_duration = drawDownAnalyzer.getLongestDrawDownDuration(
        )

        #fin_port_val = 0
        #cum_ret = 0
        #return_avg = 0
        #return_std = 0
        #return_min = 0
        #return_max = 0
        #sharpe_ratio = 0
        #max_drawdown = 0
        #longest_drawdown_duration = 0

        params = urllib.urlencode({
            'op':
            'LOG_RETURNS',
            'strategyid':
            strat.getStrategyUUID(),
            'fin_port_val':
            fin_port_val,
            'cum_returns':
            cum_ret,
            'return_avg':
            return_avg,
            'return_std':
            return_std,
            'return_min':
            return_min,
            'return_max':
            return_max,
            'sharpe_ratio':
            sharpe_ratio,
            'max_drawdown':
            max_drawdown,
            'longest_drawdown_duration':
            longest_drawdown_duration
        })

        # Post the returns analysis results
        strat.postRecord(params)

    except:
        strat.info("An exception has occurred during final shutdown.")
        strat.info(traceback.print_exc())
        params = urllib.urlencode({
            'op':
            'LOG_ENTRY',
            'msg':
            "An exception has occurred during final shutdown: " +
            traceback.format_exc(),
            'engine':
            strat.getEngine(),
            'strategyid':
            strat.getStrategyUUID()
        })
        strat.postRecord(params)
        pass

    time.sleep(5)

    command = "sudo /sbin/reboot"
    subprocess.call(command, shell=True)