Exemplo n.º 1
0
    def test_update_and_draw(self):
        """
        Tests: Evaluator#inspect_over_time

        Inspects portfolios over time and checks the results
        """
        trader = SimpleTrader(RandomPredictor(), RandomPredictor())

        evaluator = PortfolioEvaluator([trader] * 3, draw_results=False)

        full_stock_market_data = read_stock_market_data(
            [CompanyEnum.COMPANY_A, CompanyEnum.COMPANY_B],
            [PERIOD_1, PERIOD_2])

        # Calculate and save the initial total portfolio value (i.e. the cash reserve)
        portfolio1 = Portfolio(50000.0, [], 'portfolio 1')
        portfolio2 = Portfolio(100000.0, [], 'portfolio 2')
        portfolio3 = Portfolio(150000.0, [], 'portfolio 3')

        portfolios_over_time = evaluator.inspect_over_time(
            full_stock_market_data, [portfolio1, portfolio2, portfolio3],
            evaluation_offset=100)

        last_date = list(portfolios_over_time['portfolio 1'].keys())[-1]

        assert last_date == datetime.strptime('2015-12-30', '%Y-%m-%d').date()

        data_row_lengths = set(
            [len(value_set) for value_set in portfolios_over_time.values()])
        assert len(data_row_lengths) == 1
        assert data_row_lengths.pop() == 100
    def test_update__do_not_drop_below_cash_0(self):
        """
        Tests: Portfolio#update

        Flavour: When receiving two BUY orders the `#update` method should regard the available cash and NEVER drop
         below 0

        Creates a portfolio, a stock market data object and a arbitrary `OrderList` and executes these orders on the
        portfolio. Checks if those are applied correctly
        """
        cash_reserve = 16000.0

        data = StockData([(date(2017, 1, 1), 150.0)])
        stock_market_data = StockMarketData({CompanyEnum.COMPANY_A: data})

        portfolio = Portfolio(cash_reserve, [])

        # Create a order list whose individual actions are within the limit but in sum are over the limit
        # Stock price: 150.0, quantity: 100 -> trade volume: 15000.0; cash: 16000.0
        order_list = OrderList()
        order_list.buy(CompanyEnum.COMPANY_A, 100)
        order_list.buy(CompanyEnum.COMPANY_A, 100)

        updated_portfolio = portfolio.update(stock_market_data, order_list)

        assert updated_portfolio.cash >= 0
Exemplo n.º 3
0
    def test_different_mappings(self):
        """
        Tests: Evaluator#inspect_over_time and #inspect_over_time_with_mappings

        Compares if both methods return the same values
        """
        stock_market_data = read_stock_market_data(
            [CompanyEnum.COMPANY_A], [PERIOD_1, PERIOD_2, PERIOD_3])

        portfolio_1 = Portfolio(20000,
                                [SharesOfCompany(CompanyEnum.COMPANY_A, 200)])
        portfolio_2 = Portfolio(20000,
                                [SharesOfCompany(CompanyEnum.COMPANY_A, 200)])
        portfolios = [portfolio_1, portfolio_2]

        perfect_predictor = PerfectPredictor(CompanyEnum.COMPANY_A)

        trader_1 = SimpleTrader(perfect_predictor, perfect_predictor)
        trader_2 = SimpleTrader(perfect_predictor, perfect_predictor)
        trader_3 = SimpleTrader(RandomPredictor(), RandomPredictor())
        traders = [trader_1, trader_2]

        portfolio_trader_mappings = list(
            zip(portfolios, traders, [None] * len(portfolios)))

        evaluator_1 = PortfolioEvaluator([trader_1, trader_1])
        evaluator_2 = PortfolioEvaluator([])
        evaluator_3 = PortfolioEvaluator([trader_3, trader_3])

        # 1 Use evaluator that is initialized *with* traders and call `#inspect_over_time` (i.e. without traders)
        result_1 = evaluator_1.inspect_over_time(stock_market_data, portfolios,
                                                 100)

        # 2 Use evaluator that is initialized *without* traders and call `#inspect_over_time_with_mapping`
        # (i.e. with traders)
        result_2 = evaluator_2.inspect_over_time_with_mapping(
            stock_market_data, portfolio_trader_mappings, 100)

        # 3 Use evaluator that is initialized *with* traders and call `#inspect_over_time_with_mapping`
        # (i.e. with traders) - this should be no problem, because the traders given at initialization time are ignored
        result_3 = evaluator_3.inspect_over_time_with_mapping(
            stock_market_data, portfolio_trader_mappings, 100)

        assert result_1 == result_2 == result_3
    def test_portfolio_construction(self):
        portfolio = Portfolio(1000.0, [
            SharesOfCompany(CompanyEnum.COMPANY_A, 10),
            SharesOfCompany(CompanyEnum.COMPANY_B, 50)
        ])

        assert portfolio.cash == 1000.0
        assert len(portfolio.shares) == 2
        assert portfolio.shares[0].company_enum == CompanyEnum.COMPANY_A
        assert portfolio.shares[0].amount == 10

        assert portfolio.shares[1].company_enum == CompanyEnum.COMPANY_B
        assert portfolio.shares[1].amount == 50

        assert f"{portfolio.cash}" in f"{portfolio}"
Exemplo n.º 5
0
def initialize_portfolios(cash: float, portfolio_trader_mappings: PortfolioNameTraderMappingList):
    """
    Creates a list of portfolios/trader tuples conveniently. Returns as many portfolios as `portfolio_trader_mappings`
    are given. Each portfolio is initialized with `cash` initial cash and an empty list of stocks

    Args:
        cash: The initial cash amount for *each* portfolio
        portfolio_trader_mappings: The portfolio name/trader mappings

    Returns:
        A list of tuples of portfolios and traders, one for/with each `portfolio_trader_mapping`
    """
    portfolios = list()

    for name, trader in portfolio_trader_mappings:
        portfolios.append((Portfolio(cash, [], name), trader))

    return portfolios
Exemplo n.º 6
0
    def test_inspect__date_offset(self):
        """
        Tests: Evaluator#inspect_over_time

        Flavour: Test with an date offset
        """
        data = StockData([(date(2017, 1, 1), 150.0), (date(2017, 1, 2), 200.0),
                          (date(2017, 1, 3), 250.0)])
        stock_market_data = StockMarketData({CompanyEnum.COMPANY_A: data})

        portfolio = Portfolio(20000,
                              [SharesOfCompany(CompanyEnum.COMPANY_A, 200)])

        evaluator = PortfolioEvaluator(
            [SimpleTrader(RandomPredictor(), RandomPredictor())])

        portfolio_over_time: dict = \
            evaluator.inspect_over_time(stock_market_data, [portfolio], date_offset=date(2017, 1, 2))['nameless']

        assert date(2016, 12, 31) not in portfolio_over_time.keys()
        assert date(2017, 1, 1) in portfolio_over_time.keys()
        assert date(2017, 1, 2) in portfolio_over_time.keys()
        assert date(2017, 1, 3) not in portfolio_over_time.keys()
Exemplo n.º 7
0
    def testCreateActionList(self):
        trader = DqlTrader(PerfectPredictor(CompanyEnum.COMPANY_A), PerfectPredictor(CompanyEnum.COMPANY_B), False)
        self.assertIsNotNone(trader)
        portfolio = Portfolio(10000, [])
        stock_market_data = read_stock_market_data([CompanyEnum.COMPANY_A, CompanyEnum.COMPANY_B], [PERIOD_3])
        self.assertIsNotNone(stock_market_data)

        # Check doing nothing
        # commented because that STOCKACTION is not used anymore
        # action_a, action_b = 0.0, 0.0
        # order_list = trader.create_order_list(action_a, action_b, portfolio, stock_market_data)
        # self.assertIsNotNone(order_list)
        # self.assertTrue(order_list.is_empty())

        # Check buying halve stock
        # commented because that STOCKACTION is not used anymore
        # action_a, action_b = 0.5, 0.5
        # order_list = trader.create_order_list(action_a, action_b, portfolio, stock_market_data)
        # self.assertIsNotNone(order_list)
        # self.assertEqual(order_list.get(0).action, OrderType.BUY)
        # self.assertEqual(order_list.get(0).shares.company_enum, CompanyEnum.COMPANY_A)
        # self.assertEqual(order_list.get(0).shares.amount, 49)
        # self.assertEqual(order_list.get(1).action, OrderType.BUY)
        # self.assertEqual(order_list.get(1).shares.company_enum, CompanyEnum.COMPANY_B)
        # self.assertEqual(order_list.get(1).shares.amount, 33)

        # Check buying full stock
        action_a, action_b = 1.0, 1.0
        order_list = trader.create_order_list(action_a, action_b, portfolio, stock_market_data)
        self.assertIsNotNone(order_list)

        order_1 = order_list[0]
        order_2 = order_list[1]

        self.assertEqual(order_1.action, OrderType.BUY)
        self.assertEqual(order_1.shares.company_enum, CompanyEnum.COMPANY_A)
        self.assertEqual(order_1.shares.amount, 98)
        self.assertEqual(order_2.action, OrderType.BUY)
        self.assertEqual(order_2.shares.company_enum, CompanyEnum.COMPANY_B)
        self.assertEqual(order_2.shares.amount, 66)

        # Check selling stock without enough owned shares
        action_a, action_b = -1.0, -1.0
        order_list = trader.create_order_list(action_a, action_b, portfolio, stock_market_data)
        self.assertIsNotNone(order_list)
        self.assertTrue(order_list.is_empty())

        # Check selling halve stock with enough owned shares
        # commented because that STOCKACTION is not used anymore
        # portfolio = Portfolio(10000, [SharesOfCompany(CompanyEnum.COMPANY_A, 2), SharesOfCompany(CompanyEnum.COMPANY_B, 2)])
        # action_a, action_b = -0.5, -0.5
        # order_list = trader.create_order_list(action_a, action_b, portfolio, stock_market_data)
        # self.assertIsNotNone(order_list)
        # self.assertEqual(order_list.get(0).action, OrderType.SELL)
        # self.assertEqual(order_list.get(0).shares.company_enum, CompanyEnum.COMPANY_A)
        # self.assertEqual(order_list.get(0).shares.amount, 2)
        # self.assertEqual(order_list.get(1).action, OrderType.SELL)
        # self.assertEqual(order_list.get(1).shares.company_enum, CompanyEnum.COMPANY_B)
        # self.assertEqual(order_list.get(1).shares.amount, 1)

        # Check selling full stock with enough owned shares
        portfolio = Portfolio(10000,
                              [SharesOfCompany(CompanyEnum.COMPANY_A, 2), SharesOfCompany(CompanyEnum.COMPANY_B, 2)])
        action_a, action_b = -1.0, -1.0
        order_list = trader.create_order_list(action_a, action_b, portfolio, stock_market_data)
        self.assertIsNotNone(order_list)

        order_1 = order_list[0]
        order_2 = order_list[1]

        self.assertEqual(order_1.action, OrderType.SELL)
        self.assertEqual(order_1.shares.company_enum, CompanyEnum.COMPANY_A)
        self.assertEqual(order_1.shares.amount, 2)
        self.assertEqual(order_2.action, OrderType.SELL)
        self.assertEqual(order_2.shares.company_enum, CompanyEnum.COMPANY_B)
        self.assertEqual(order_2.shares.amount, 2)
Exemplo n.º 8
0











if __name__ == '__main__':

    portfolio = 'Norway'
    portfolio_obj = Portfolio('Norway')
    powerplant_df = get_powerplants_by_portfolio(portfolio)

    # powerplant_df.to_csv("ppd.csv")

    """ 1 Convert PCUC file and save it to KEAN """
    # plant_tech_master_file = r"C:\Users\cliu\Kindle Energy Dropbox\Chang Liu\LBO\data\Norway\pcuc\Norway plant char assumption_input v11.xlsx"
    # pc_date_start = date(2020, 1, 1)
    # pc_date_end = date(2027,12,31)
    # pc_scenario = 'Norway Converted'
    # pc_version = 'v1'
    # # run_convert_uc(plant_tech_master_file, pc_date_start, pc_date_end, pc_scenario, pc_version)
    # run_convert_uc('Norway', pc_date_start, pc_date_end, pc_scenario, pc_version, plant_tech_master_file=plant_tech_master_file, push_to_powerplant=False, push_to_technology=True, push_to_plant_characteristics=False)
    #