コード例 #1
0
def run_monthly_rebalance(config, testing, filename, benchmark, ticker_weights,
                          title_str, start_date, end_date, equity):
    config = settings.from_file(config, testing)
    tickers = [t for t in ticker_weights.keys()]

    # Set up variables needed for backtest
    events_queue = queue.Queue()
    csv_dir = config.CSV_DATA_DIR
    initial_equity = PriceParser.parse(equity)

    # Use Yahoo Daily Price Handler
    price_handler = YahooDailyCsvBarPriceHandler(csv_dir,
                                                 events_queue,
                                                 tickers,
                                                 start_date=start_date,
                                                 end_date=end_date)

    # Use the monthly liquidate and rebalance strategy
    strategy = MonthlyLiquidateRebalanceStrategy(tickers, events_queue)
    strategy = Strategies(strategy, DisplayStrategy())

    # Use the liquidate and rebalance position sizer
    # with prespecified ticker weights
    position_sizer = LiquidateRebalancePositionSizer(ticker_weights)

    # Use an example Risk Manager
    risk_manager = ExampleRiskManager()

    # Use the default Portfolio Handler
    portfolio_handler = PortfolioHandler(initial_equity, events_queue,
                                         price_handler, position_sizer,
                                         risk_manager)

    # Use the ExampleCompliance component
    compliance = ExampleCompliance(config)
コード例 #2
0
def run_monthly_rebalance(tickers, ticker_weights, title, start_date, end_date,
                          initial_equity):
    testing = False
    config = settings.from_file(settings.DEFAULT_CONFIG_FILENAME, testing)

    # Use the Monthly Liquidate And Rebalance strategy
    events_queue = queue.Queue()
    strategy = MonthlyLiquidateRebalanceStrategy(tickers, events_queue)

    # Use the liquidate and rebalance position sizer
    # with prespecified ticker weights
    position_sizer = LiquidateRebalancePositionSizer(ticker_weights)

    # Set up the backtest
    backtest = TradingSession(
        config,
        strategy,
        tickers,
        initial_equity,
        start_date,
        end_date,
        events_queue,
        position_sizer=position_sizer,
        title=[title],
        benchmark=tickers[0],
    )
    results = backtest.start_trading(testing=testing)
    return results
コード例 #3
0
 def setUp(self):
     self.test_sell_event = OrderEvent("CS.D.AUDUSD.TODAY.IP", "SELL", 1)
     self.test_buy_event = OrderEvent("CS.D.AUDUSD.TODAY.IP", "BOT", 1)
     self.events_queue = queue.Queue()
     self.config = settings.from_file(settings.DEFAULT_CONFIG_FILENAME,
                                      testing=True)
     self.igclient = IGClient(self.config)
コード例 #4
0
def main(max_rows, cache_name, cache_backend, expire_after, data_source, start, end, config, testing, tickers, filename, n, n_window):
    pd.set_option('max_rows', max_rows)
    if cache_name == '':
        cache_name = "requests_cache"
    else:
        if cache_backend.lower() == 'sqlite':
            cache_name = os.path.expanduser(cache_name)
    tickers = tickers.split(",")
    config = settings.from_file(config, testing)
    run(cache_name, cache_backend, expire_after, data_source, start, end, config, testing, tickers, filename, n, n_window)
コード例 #5
0
def run_monthly_rebalance(config, testing, filename, benchmark, ticker_weights,
                          title_str, start_date, end_date, equity):
    config = settings.from_file(config, testing)
    tickers = [t for t in ticker_weights.keys()]

    # Set up variables needed for backtest
    events_queue = queue.Queue()
    csv_dir = config.CSV_DATA_DIR
    initial_equity = PriceParser.parse(equity)

    # Use Yahoo Daily Price Handler
    price_handler = YahooDailyCsvBarPriceHandler(csv_dir,
                                                 events_queue,
                                                 tickers,
                                                 start_date=start_date,
                                                 end_date=end_date)

    # Use the monthly liquidate and rebalance strategy
    strategy = MonthlyLiquidateRebalanceStrategy(tickers, events_queue)
    #strategy = Strategies(strategy, DisplayStrategy())

    # Use the liquidate and rebalance position sizer
    # with prespecified ticker weights
    position_sizer = LiquidateRebalancePositionSizer(ticker_weights)

    # Use an example Risk Manager
    risk_manager = ExampleRiskManager()

    # Use the default Portfolio Handler
    portfolio_handler = PortfolioHandler(initial_equity, events_queue,
                                         price_handler, position_sizer,
                                         risk_manager)

    # Use the ExampleCompliance component
    compliance = ExampleCompliance(config)

    # Use a simulated IB Execution Handler
    execution_handler = IBSimulatedExecutionHandler(events_queue,
                                                    price_handler, compliance)

    # Use the default Statistics
    title = [title_str]
    statistics = TearsheetStatistics(config, portfolio_handler, title,
                                     benchmark)

    # Set up the backtest
    backtest = Backtest(price_handler, strategy, portfolio_handler,
                        execution_handler, position_sizer, risk_manager,
                        statistics, initial_equity)
    results = backtest.simulate_trading(testing=testing)
    statistics.save(filename)
    return results
コード例 #6
0
def main(config, testing, tickers, filename, lookback, entry_z, exit_z):

    df = pd.DataFrame()
    config = settings.from_file(config, testing)
    tickers = tickers.split(",")

    lookback = np.linspace(lookback[0], lookback[1],
                           lookback[1] - lookback[0] + 1)
    entry_range = np.linspace(entry_z[0] - entry_z[1], entry_z[0] + entry_z[1],
                              entry_z[2])
    exit_range = np.linspace(exit_z[0] - exit_z[1], exit_z[0] + exit_z[1],
                             exit_z[2])
    print(lookback)
    print(entry_range)
    print(exit_range)

    range_list = []
    entry_list = []
    exit_list = []
    sharpe_list = []
    alpha_list = []

    #alter parameters for rolling mean and std
    for i in lookback:
        #alter parameters for z-score
        for j in entry_range:
            for k in exit_range:
                trial = run(config, testing, tickers, filename, i, j, k)
                range_list.append(i)
                entry_list.append(j)
                exit_list.append(k)
                sharpe_list.append(trial.get('sharpe'))
                alpha_list.append(trial.get('alpha'))

    data = [
        list(i) for i in zip(range_list, entry_list, exit_list, sharpe_list,
                             alpha_list)
    ]
    df = pd.DataFrame(data,
                      columns=['Range', 'Entry', 'Exit', 'Sharpe', 'Alpha'])
    df.to_csv("Nice.csv")
コード例 #7
0
def main():
    config = settings.DEFAULT_CONFIG_FILENAME
    testing = False

    tickers = [
        '43350', '82651', '44644', '90458', '24969', '24985', '42585', '83621',
        '60186', '81095', '16548', '81577'
    ]
    # tickers = [,]
    # tickers =           []
    # pair = pair_list[1]
    # print(pair)
    # tickers = pair.split(",")
    # tickers = ['43350', '82651']
    # tickers = ['44644', '90458']
    # tickers = ['24969', '24985']
    # tickers = ['42585', '83621']
    # tickers = ['60186', '81095']
    # tickers = ['16548', '81577']

    filename = 'vs'.join(tickers)
    config = settings.from_file(config, testing)
    run(config, testing, tickers, filename)
コード例 #8
0
def main(config, testing, tickers, filename):
    tickers = tickers.split(",")
    config = settings.from_file(config, testing)
    run(config, testing, tickers, filename)
コード例 #9
0
ファイル: test.py プロジェクト: ajmal017/Alpha-1
        start_date,
        end_date,
        events_queue,
        title=title,
        benchmark=tickers[1],
    )
    results = backtest.start_trading(testing=testing)
    return results


if __name__ == "__main__":
    # Configuration data
    testing = False
    tickers = ["AAPL", "SPY"]
    filename = None
    config = settings.from_file(settings.DEFAULT_CONFIG_FILENAME, testing)
    run(config, testing, tickers, filename)

    # ph = QuandlPriceHandler([], ['AAPL', 'GS'], 'WIKI', '1960-1-1', '2017-6-14')
    # # Set up the backtest
    # title = ['Moving Average Crossover Example on AAPL: 100x300']
    # initial_equity = 10000.0
    # start_date = datetime.datetime(2000, 1, 1)
    # end_date = datetime.datetime(2014, 1, 1)
    # tickers = ["AAPL", "SPY"]

    # Use the MAC Strategy
    # events_queue = queue.Queue()
    # strategy = MovingAverageCrossStrategy(
    #     tickers[0], events_queue,
    #     short_window=100,
コード例 #10
0
def main(config, testing, tickers, filename, n, n_window):
    tickers = tickers.split(",")
    config = settings.from_file(config, testing)
    run(config, testing, tickers, filename, n, n_window)
コード例 #11
0
def main(config, testing, tickers):
    tickers = tickers.split(",")
    config = settings.from_file(config, testing)
    run(config, testing, tickers)
コード例 #12
0
def main(config, testing, tickers, filename, start_date=None, end_date=None):
    tickers = tickers.split(",")
    config = settings.from_file(config, testing)
    run(config, testing, tickers, filename, start_date, end_date)
コード例 #13
0
def main(config, testing, tickers, filename):
    tickers = ['IEI','TLT']#tickers.split(",")
    config = settings.from_file(config, testing)
    run(config, testing, tickers, filename)