How to use the qstrader.settings function in qstrader

To help you get started, we’ve selected a few qstrader examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mhallsmoore / qstrader / examples / mac_backtest_tearsheet.py View on Github external
def main(config, testing, tickers, filename):
    tickers = tickers.split(",")
    config = settings.from_file(config, testing)
    run(config, testing, tickers, filename)
github mhallsmoore / qstrader / examples / test_examples.py View on Github external
def test_buy_and_hold_backtest(self):
        """
        Test buy_and_hold
        Begins at 2000-01-01 00:00:00
        End at 2014-01-01 00:00:00
        """
        tickers = ["SPY"]
        filename = os.path.join(
            settings.TEST.OUTPUT_DIR,
            "buy_and_hold_backtest.pkl"
        )
        results = examples.buy_and_hold_backtest.run(
            self.config, self.testing, tickers, filename
        )
        for (key, expected) in [
            ('sharpe', 0.25234757),
            ('max_drawdown_pct', 0.79589309),
        ]:
            value = float(results[key])
            self.assertAlmostEqual(expected, value)
github mhallsmoore / qstrader / examples / pandas_examples / test_pandas_examples.py View on Github external
def test_pandas_tick_strategy_backtest(self):
        tickers = ["GOOG"]
        filename = os.path.join(settings.TEST.OUTPUT_DIR, "pandas_tick_strategy_backtest.pkl")
        results = examples.pandas_examples.pandas_tick_strategy_backtest.run(self.config, self.testing, tickers, filename, self.n, self.n_window)
        self.assertAlmostEqual(float(results['sharpe']), -7.1351)
github mhallsmoore / qstrader / tests / broker / test_simulated_broker.py View on Github external
# Test a default SimulatedBroker
        sb1 = SimulatedBroker(start_dt, exchange)
        self.assertEqual(sb1.start_dt, start_dt)
        self.assertEqual(sb1.cur_dt, start_dt)
        self.assertEqual(sb1.exchange, exchange)
        self.assertEqual(sb1.account_id, None)
        self.assertEqual(sb1.base_currency, "USD")
        self.assertEqual(sb1.initial_funds, 0.0)
        self.assertEqual(
            type(sb1.broker_commission),
            ZeroBrokerCommission
        )
        tcb1 = dict(
            zip(
                settings.CURRENCIES,
                [0.0] * len(settings.CURRENCIES)
            )
        )
        self.assertEqual(sb1.cash_balances, tcb1)
        self.assertEqual(sb1.portfolios, {})
        self.assertEqual(sb1.open_orders, {})

        # Test a SimulatedBroker with some parameters set
        sb2 = SimulatedBroker(
            start_dt, exchange, account_id="ACCT1234",
            base_currency="GBP", initial_funds=1e6,
            broker_commission=ZeroBrokerCommission()
        )
        self.assertEqual(sb2.start_dt, start_dt)
        self.assertEqual(sb2.cur_dt, start_dt)
        self.assertEqual(sb2.exchange, exchange)
github mhallsmoore / qstrader / tests / unit / broker / test_simulated_broker.py View on Github external
def test_set_cash_balances():
    """
    Checks _set_cash_balances for zero and non-zero
    initial_funds.
    """
    start_dt = pd.Timestamp('2017-10-05 08:00:00', tz=pytz.UTC)
    exchange = ExchangeMock()
    data_handler = DataHandlerMock()

    # Zero initial funds
    sb1 = SimulatedBroker(
        start_dt, exchange, data_handler, initial_funds=0.0
    )
    tcb1 = dict(
        zip(
            settings.SUPPORTED['CURRENCIES'],
            [0.0] * len(settings.SUPPORTED['CURRENCIES'])
        )
    )
    assert sb1._set_cash_balances() == tcb1

    # Non-zero initial funds
    sb2 = SimulatedBroker(
        start_dt, exchange, data_handler, initial_funds=12345.0
    )
    tcb2 = dict(
        zip(
            settings.SUPPORTED['CURRENCIES'],
            [0.0] * len(settings.SUPPORTED['CURRENCIES'])
        )
    )
    tcb2["USD"] = 12345.0
github mhallsmoore / qstrader / tests / broker / test_simulated_broker.py View on Github external
* If the currency code isn't in the cash_balances
        dictionary, then raise BrokerException
        * Otherwise, return the appropriate cash balance
        """
        start_dt = pd.Timestamp('2017-10-05 08:00:00', tz=pytz.UTC)
        exchange = ExchangeMock()
        sb = SimulatedBroker(
            start_dt, exchange, initial_funds=1000.0
        )

        # If currency is None, return the cash balances
        sbcb1 = sb.get_account_cash_balance()
        tcb1 = dict(
            zip(
                settings.CURRENCIES,
                [0.0] * len(settings.CURRENCIES)
            )
        )
        tcb1["USD"] = 1000.0
        self.assertEqual(
            sbcb1, tcb1
        )

        # If the currency code isn't in the cash_balances
        # dictionary, then raise BrokerException
        with self.assertRaises(BrokerException):
            sb.get_account_cash_balance(currency="XYZ")

        # Otherwise, return appropriate cash balance
        self.assertEqual(
            sb.get_account_cash_balance(currency="USD"), 1000.0
        )
github mhallsmoore / qstrader / examples / display_prices_ig.py View on Github external
@click.option('--config', default=settings.DEFAULT_CONFIG_FILENAME, help='Config filename')
@click.option('--testing/--no-testing', default=False, help='Enable testing mode')
@click.option('--tickers', default='L1:CS.D.GBPUSD.CFD.IP,L1:CS.D.USDJPY.CFD.IP', help='Tickers (use comma)')
@click.option('--filename', default='', help='Pickle (.pkl) statistics filename')
@click.option('--n', default=1, help='Display prices every n price events')
@click.option('--n_window', default=5, help='Display n_window prices')
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)
github mhallsmoore / qstrader / examples / monthly_liquidate_rebalance.py View on Github external
@click.option('--config', default=settings.DEFAULT_CONFIG_FILENAME, help='Config filename')
@click.option('--testing/--no-testing', default=False, help='Enable testing mode')
@click.option('--tickers', default='SP500TR', help='Tickers (use comma)')
@click.option('--filename', default='', help='Pickle (.pkl) statistics filename')
def main(config, testing, tickers, filename):
    tickers = tickers.split(",")
    config = settings.from_file(config, testing)
    run(config, testing, tickers, filename)
github mhallsmoore / qstrader / qstrader / broker / portfolio / portfolio.py View on Github external
current_price=1.0,
            current_dt=self.current_dt
        )
        self.pos_handler.positions[self.cash_position_key] = cash_position

        if self.starting_cash > 0.0:
            self.history.append(
                PortfolioEvent.create_subscription(
                    self.current_dt, self.starting_cash, self.starting_cash
                )
            )

        self.logger.info(
            '(%s) Funds subscribed to portfolio "%s" '
            '- Credit: %0.2f, Balance: %0.2f' % (
                self.current_dt.strftime(settings.LOGGING["DATE_FORMAT"]),
                self.portfolio_id,
                round(self.starting_cash, 2),
                round(self.starting_cash, 2)
            )