def test_fixed_symbol_handler(self): q = EventsQueue() start, end = dt.datetime(2015, 1, 2), dt.datetime(2015, 1, 9) symbols = [Indices.sp_100_etf.value, Indices.sp_500_etf.value] sh = FixedSymbolHandler(symbols, []) dh = DatabaseDataHandler(q, sh, start, end, 10) self.assertEqual(set(sh.select_symbols(dh.current_date)), set(symbols))
def test_markowitz(self): q = EventsQueue() symbols = ["GOOG", "MS", "AMZN", "GS"] sh = FixedSymbolHandler(symbols, []) start, end = dt.datetime(2011, 1, 1), dt.datetime(2017, 1, 1) dh = DatabaseDataHandler(q, sh, start, end, 252 * 5) dh.request_prices() prices = dh.bars["adj_price_close"] series = solve_markowitz(prices, 1.)
def test_to_datebase_portfolio(self): q = EventsQueue() start, end = dt.datetime(2015, 1, 2), dt.datetime(2015, 1, 9) symbols = [Indices.sp_100_etf, Indices.sp_500_etf] sh = FixedSymbolHandler(symbols, []) dh = DatabaseDataHandler(q, sh, start, end, 10) max_cap = 1 capital = 100000.0 ph = PortfolioHandler(max_cap, "test_portfolio_id", capital, "test_fund_id") ph.to_database_portfolio()
def test_from_database_portfolio(self): pid = "test_portfolio_id" q = EventsQueue() start, end = dt.datetime(2015, 1, 2), dt.datetime(2015, 1, 9) symbols = [Indices.sp_100_etf, Indices.sp_500_etf] sh = FixedSymbolHandler(symbols, []) dh = DatabaseDataHandler(q, sh, start, end, 10) ph = PortfolioHandler.from_database_portfolio(pid) self.assertEqual(ph.capital, 100000.0) self.assertEqual(ph.maximum_capacity, 1) self.assertEqual(ph.portfolio_id, pid) self.assertTrue("SPY" in ph.filled_positions)
from odin.events import EventsQueue from odin.handlers.portfolio_handler import PortfolioHandler from odin.handlers.position_handler.templates import ( SuggestedProportionPositionHandler) from odin.handlers.execution_handler import SimulatedExecutionHandler from odin.handlers.symbol_handler import FixedSymbolHandler from odin.handlers.data_handler import DatabaseDataHandler from odin.handlers.data_handler.price_handler import DatabasePriceHandler import settings # Create a portfolio handler to manage transactions and keeping track of # capital. porth = PortfolioHandler(settings.maximum_capacity, settings.pid, settings.init_capital, settings.fid) # Events queue for handling market data, signals, orders, and fills. events = EventsQueue() # Symbol handler will determine which symbols will be processed during trading. # In this example, we will just trade the S&P 500 ETF (SPY). sh = FixedSymbolHandler(settings.symbols, [porth]) # Set up a price handler and a data handler to provide data to the trading # system. dh = DatabaseDataHandler(events, sh, settings.start_date, settings.end_date, settings.n_init) # Execution handler executes trades. eh = SimulatedExecutionHandler(dh, settings.transaction_cost) # Position handler to determine how much of an asset to purchase. posh = SuggestedProportionPositionHandler(dh)