コード例 #1
0
ファイル: runner.py プロジェクト: voidale/quantraider
def run_clusters(strategy_class, clustering_tickers, cluster_num, epochs_num,
                 training_start, training_end, backtest_start, backtest_end,
                 is_graph, is_elbow):
    """ Run the test given command-line args.
        Cluster.
        For each cluster, train a strategy on that cluster.
        For each stock in that cluster, run a backtest.
        Graph results.
    """
    print "\nGathering data..."
    ticker_list, raw_stock_data_list = Manager.getRawStockDataList(
        clustering_tickers, training_start, training_end, 252)
    normalized_stock_data_list = [
        Manager.preprocessData(x) for x in raw_stock_data_list
    ]
    print "\nClustering..."
    tickers, clusters = createClusters(ticker_list, normalized_stock_data_list,
                                       cluster_num)
    print "# of stocks:   " + str(len(normalized_stock_data_list))
    print "# of clusters: " + str(len(clusters))
    print ""
    for t, c in itertools.izip(tickers, clusters):
        print "\tCluster: " + str(len(c)), "stocks: ",
        for symbol in t:
            print symbol,
        print ""

    if is_graph:
        graphClusters(clusters)
    if is_elbow:
        graphElbowMethod(normalized_stock_data_list)

    for t, cluster in itertools.izip(tickers, clusters):
        settings.STRATEGY_OBJECT = trainStrategy(strategy_class, cluster,
                                                 epochs_num)
        for ticker in t:
            print "Cluster:", t
            print "Stock:", ticker
            tmp_ticks, tmp_data = Manager.getRawStockDataList([ticker],
                                                              training_start,
                                                              training_end,
                                                              252)
            settings.BACKTEST_STOCK = ticker
            settings.PRE_BACKTEST_DATA = tmp_data[0]
            print "Create Algorithm..."
            algo_obj = TradingAlgorithm(initialize=initialize,
                                        handle_data=handle_data)
            try:
                backtest_data = load_bars_from_yahoo(stocks=[ticker, 'SPY'],
                                                     start=backtest_start,
                                                     end=backtest_end)
                try:
                    perf = algo_obj.run(backtest_data)
                    analyze([ticker], [perf])
                except ValueError, e:
                    print str(e)
            except IOError, e:
                print "Stock Error: could not load", ticker, "from Yahoo."
        print "Only testing one cluster for now - Done!"
        return
コード例 #2
0
ファイル: trader.py プロジェクト: nsbradford/quantraider
def handle_data(context, data):
    #assert context.portfolio.cash > 0.0, "ERROR: negative context.portfolio.cash"
    #assert len(context.raw_data) == context.training_data_length; "ERROR: "
    
    # data stored as (open, high, low, close, volume, price)
    feed_data = ([  
                    data[context.security].open, 
                    data[context.security].high,
                    data[context.security].low,
                    data[context.security].close
                    #data[context.security].volume,
                    #data[context.security].close,
    ])
    
    #keep track of history. 
    context.raw_data.pop(0)
    context.raw_data.append(feed_data)
    context.normalized_data = Manager.preprocessData(context.raw_data)[:-2]
    prediction = context.strategy.predict(context.normalized_data)[-1]
    print "Value: $%.2f    Cash: $%.2f    Predict: %.5f" % (context.portfolio.portfolio_value, context.portfolio.cash, prediction[0])

    # Do nothing if there are open orders:
    if has_orders(context, data):
        print('has open orders - doing nothing!')
    # Put entire position in
    elif prediction > 0.5:
        order_target_percent(context.security, .95)
    # Take entire position out
    else:
        order_target_percent(context.security, 0)
        #order_target_percent(context.security, -.99)
    record(BENCH=data[context.security].price)
    record(SPY=data[context.benchmark].price)
コード例 #3
0
def handle_data(context, data):
    #assert context.portfolio.cash > 0.0, "ERROR: negative context.portfolio.cash"
    #assert len(context.raw_data) == context.training_data_length; "ERROR: "

    # data stored as (open, high, low, close, volume, price)
    feed_data = ([
        data[context.security].open, data[context.security].high,
        data[context.security].low, data[context.security].close
        #data[context.security].volume,
        #data[context.security].close,
    ])

    #keep track of history.
    context.raw_data.pop(0)
    context.raw_data.append(feed_data)
    context.normalized_data = Manager.preprocessData(context.raw_data)[:-2]
    prediction = context.strategy.predict(context.normalized_data)[-1]
    print "Value: $%.2f    Cash: $%.2f    Predict: %.5f" % (
        context.portfolio.portfolio_value, context.portfolio.cash,
        prediction[0])

    # Do nothing if there are open orders:
    if has_orders(context, data):
        print('has open orders - doing nothing!')
    # Put entire position in
    elif prediction > 0.5:
        order_target_percent(context.security, .95)
    # Take entire position out
    else:
        order_target_percent(context.security, 0)
        #order_target_percent(context.security, -.99)
    record(BENCH=data[context.security].price)
    record(SPY=data[context.benchmark].price)
コード例 #4
0
ファイル: trader.py プロジェクト: nsbradford/quantraider
def initialize(context):
    print "Initialize..."
    context.security = symbol(settings.BACKTEST_STOCK)
    context.benchmark = symbol('SPY')
    context.strategy = settings.STRATEGY_OBJECT
    context.raw_data = settings.PRE_BACKTEST_DATA
    context.normalized_data = Manager.preprocessData(context.raw_data)[:-2]
    print "Backtest symbol:", context.security
    print "Capital Base:", context.portfolio.cash
コード例 #5
0
def initialize(context):
    print "Initialize..."
    context.security = symbol(settings.BACKTEST_STOCK)
    context.benchmark = symbol('SPY')
    context.strategy = settings.STRATEGY_OBJECT
    context.raw_data = settings.PRE_BACKTEST_DATA
    context.normalized_data = Manager.preprocessData(context.raw_data)[:-2]
    print "Backtest symbol:", context.security
    print "Capital Base:", context.portfolio.cash
コード例 #6
0
ファイル: runner.py プロジェクト: nsbradford/quantraider
def run_clusters(strategy_class, clustering_tickers, cluster_num, epochs_num, training_start, 
                    training_end, backtest_start, backtest_end, is_graph, is_elbow):
    """ Run the test given command-line args.
        Cluster.
        For each cluster, train a strategy on that cluster.
        For each stock in that cluster, run a backtest.
        Graph results.
    """
    print "\nGathering data..."
    ticker_list, raw_stock_data_list = Manager.getRawStockDataList(clustering_tickers, training_start, training_end, 252)
    normalized_stock_data_list = [Manager.preprocessData(x) for x in raw_stock_data_list]
    print "\nClustering..."
    tickers, clusters = createClusters(ticker_list, normalized_stock_data_list, cluster_num)
    print "# of stocks:   " + str(len(normalized_stock_data_list))
    print "# of clusters: " + str(len(clusters))
    print ""
    for t, c in itertools.izip(tickers, clusters):
        print "\tCluster: " + str(len(c)), "stocks: ",
        for symbol in t:
            print symbol,
        print ""

    if is_graph:
        graphClusters(clusters)
    if is_elbow:
        graphElbowMethod(normalized_stock_data_list)

    for t, cluster in itertools.izip(tickers, clusters):
        settings.STRATEGY_OBJECT = trainStrategy(strategy_class, cluster, epochs_num)
        for ticker in t:
            print "Cluster:", t
            print "Stock:", ticker
            tmp_ticks, tmp_data = Manager.getRawStockDataList([ticker], training_start, training_end, 252)
            settings.BACKTEST_STOCK = ticker
            settings.PRE_BACKTEST_DATA = tmp_data[0]
            print "Create Algorithm..."
            algo_obj = TradingAlgorithm(initialize=initialize, handle_data=handle_data)
            try:
                backtest_data = load_bars_from_yahoo(stocks=[ticker, 'SPY'], start=backtest_start, end=backtest_end)
                try:
                    perf = algo_obj.run(backtest_data)
                    analyze([ticker], [perf])
                except ValueError, e:
                    print str(e)
            except IOError, e:
                print "Stock Error: could not load", ticker, "from Yahoo."  
        print "Only testing one cluster for now - Done!"
        return