Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def main(config, testing, tickers, filename):
tickers = tickers.split(",")
config = settings.from_file(config, testing)
run(config, testing, tickers, filename)
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)
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)
# 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)
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
def setUp(self):
self.config = settings.TEST
* 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
)
@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)
@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)
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)
)