def test_max_profit_period(self):
     data_rows = [['A', '01-Jan-2012', '100'], ['A', '02-Jan-2012', '95'],
                  ['A', '04-Jan-2012', '100'], ['A', '03-Jan-2012', '96'],
                  ['A', '08-Jan-2012', '8'], ['A', '05-Jan-2012', '0'],
                  ['A', '06-Jan-2012', '3'], ['A', '07-Jan-2012', '5']]
     s = StockService(data_rows)
     stock_search = StockSearchFilter('A',
                                      datetime.fromisoformat('2012-01-02'),
                                      datetime.fromisoformat('2012-02-01'))
     buy_stock, sell_stock, profit = s.get_max_profit_period(stock_search)
     self.assertEquals(
         buy_stock, StockInfo('a', datetime.fromisoformat('2012-01-05'),
                              0.0))
     self.assertEquals(
         sell_stock,
         StockInfo('a', datetime.fromisoformat('2012-01-08'), 8.0))
     self.assertEqual(profit, 8)
Example #2
0
def handle_transaction(
    stocks_service: StockService
) -> Tuple[float, Optional[float], datetime.date, datetime.date, float]:
    stock_search_filter = input_helper.get_stock_search_filter(
        stocks_service.get_unique_stock_names())
    stocks = stocks_service.get_stocks(stock_search_filter)
    stock_prices = [i.price for i in stocks]

    m = None
    s = None
    if len(stock_prices) >= 1:
        m = mean([i.price for i in stocks])
    if len(stock_prices) >= 2:
        s = stdev([i.price for i in stocks])

    buying_stock_info, sell_stock_info, profit_made = stocks_service.get_max_profit_period(
        stock_search_filter)

    return (m, s,
            buying_stock_info.date if buying_stock_info is not None else None,
            sell_stock_info.date if sell_stock_info is not None else None,
            profit_made * 100)