def test_app_not_initialised_before_use_raises_error(self):
        app = MarketData()

        with self.assertRaises(NotInitialisedError):
            app.add_security('AMZN')

        with self.assertRaises(NotInitialisedError):
            sec_list = app.get_securities_list()
def common_setup(obj):
    obj.da = data_adapter.get_adapter(obj.data_adapter_source)
    obj.da.create_test_database()

    obj.database = MarketData.Database(obj.da.test_database,
                                       obj.data_adapter_source)

    obj.app = MarketData()
    obj.app.run(database=obj.database)
Exemple #3
0
def main():
    clear_screen()

    if len(sys.argv) > 1:
        conn_string = sys.argv[1]
        database = MarketData.Database(conn_string, DATA_ADAPTER_SOURCE)
        try:
            app.run(database=database)

            print(Messages.load_existing_database(database.conn_string))

        except data_adapter.DatabaseNotFoundError:
            da = data_adapter.get_adapter(database.source)
            da.create_database(database.conn_string)
            app.run(database=database)

            print(Messages.new_database_created(database.conn_string))

        running = True
        while running:
            print(Messages.main_menu())
            running = process_user_input()

    else:
        print(Messages.no_database_specified())
    def test_added_security_is_in_list_on_reopen(self):
        ticker = 'TLS'
        self.app.add_security(ticker)
        self.app.close()

        new_app = MarketData()
        new_app.run(database=self.database)
        tickers = new_app.get_securities_list()
        new_app.close()
        self.assertEqual([ticker], tickers)
    def test_add_security_into_app(self):
        # Carol opens up the application
        app = MarketData()
        app.run(database=self.database)

        # Carol adds Amazon(AMZN) to the app as
        # she would like to start tracking the
        # daily market price.
        ticker = 'AMZN'
        app.add_security(ticker)

        # She then checks to make sure that
        # the actual stock has been added to
        # the list of securities
        self.assertTrue(ticker in app.get_securities_list())

        # Satisfied that it is indeed in the app
        # she closes it and gets on with her day
        app.close()
    def test_get_equity_data_on_app_reopen(self):
        ticker, dt, expected_data = test_utils.get_expected_equity_data()
        self.app.add_security(ticker)

        mock_method = 'market_data.scraper.Scraper.scrape_equity_data'
        with patch(mock_method, autospec=True) as mock_scraper:
            mock_scraper.return_value = expected_data
            self.app.update_market_data(ticker, dt)

        actual_data = self.app.get_equity_data(ticker, dt)
        self.assertEqual(expected_data, actual_data)
        self.app.close()

        new_app = MarketData()
        new_app.run(database=self.database)
        actual_data = new_app.get_equity_data(ticker, dt)
        self.assertEqual(expected_data, actual_data)
        new_app.close()
    def test_get_equity_data_for_multiple_securities(self, mock_scraper):
        dt = datetime.datetime(2019, 8, 27)
        tickers = ['AMZN', 'GOOG']
        for ticker in tickers:
            self.app.add_security(ticker)

        params = zip(tickers, [dt] * 2)
        expected_data = self.update_with_test_data(params, mock_scraper)

        self.app.close()

        new_app = MarketData()
        new_app.run(database=self.database)
        self.check_equity_data(params, expected_data)
        new_app.close()
    def test_get_equity_data_for_multiple_dates(self, mock_scraper):
        self.app.add_security(self.ticker)

        dt = [datetime.datetime(2019, 8, 27), datetime.datetime(2019, 8, 26)]
        params = zip([self.ticker] * 2, dt)
        expected_data = self.update_with_test_data(params, mock_scraper)

        self.app.close()

        new_app = MarketData()
        new_app.run(database=self.database)

        self.check_equity_data(params, expected_data)

        # NOTE(steve): should this be in a separate test???
        data_series = new_app.get_equity_data_series(self.ticker)
        self.assertEqual(len(data_series), 2)

        for i in range(2):
            self.assertEqual(dt[i], data_series[i][0])
            self.assertEqual(expected_data[i], data_series[i][1])

        new_app.close()
    def test_bulk_update_market_data_and_get_equity_data(self, mock_scraper):
        self.app.add_security(self.ticker)

        date_list = [
            datetime.datetime(2019, 8, 27),
            datetime.datetime(2019, 8, 26)
        ]
        params = zip([self.ticker] * 2, date_list)
        test_data = test_utils.load_test_data()

        expected_data = []
        for ticker, dt in params:
            data = test_utils.get_test_data(test_data, ticker, dt)
            expected_data.append((dt, data))

        mock_scraper.return_value = expected_data, []
        errors = self.app.bulk_update_market_data(self.ticker, date_list)

        self.assertEqual(len(errors), 0)

        self.app.close()

        new_app = MarketData()
        new_app.run(database=self.database)

        self.check_equity_data(params, expected_data)

        # NOTE(steve): should this be in a separate test???
        data_series = new_app.get_equity_data_series(self.ticker)
        self.assertEqual(len(data_series), 2)

        for i in range(2):
            self.assertEqual(expected_data[i][0], data_series[i][0])
            self.assertEqual(expected_data[i][1], data_series[i][1])

        new_app.close()
Exemple #10
0
    def test_can_retreive_equity_data_on_app_reopen(self, mock_scrape):
        # Carol opens up the application and add
        # adds Amazon (AMZN) to the list of
        # securities she wants to start tracking
        # and then closes the app.
        app = MarketData()
        app.run(database=self.database)
        new_tickers = ['AMZN', 'TLS.AX']
        for ticker in new_tickers:
            app.add_security(ticker)
        app.close()

        # Jarvis opens up the application and
        # goes through the list of securities
        # and updates them for the latest
        # market data
        app2 = MarketData()
        app2.run(database=self.database)
        tickers = app2.get_securities_list()
        self.assertEqual(set(new_tickers), set(tickers))

        dt = datetime.datetime(2019, 8, 27)
        expected_data = test_utils.get_test_data(self.test_data, 'AMZN', dt)
        mock_scrape.return_value = expected_data
        for ticker in tickers:
            app2.update_market_data(ticker, dt)

        app2.close()

        # Josh now opens the app to checks today's closing
        # price of Amazon after he comes back from work
        app3 = MarketData()
        app3.run(database=self.database)
        actual_data = app3.get_equity_data('AMZN', dt)
        self.assertEqual(expected_data, actual_data)
        app3.close()
Exemple #11
0
    def test_can_get_equity_data_after_multiple_updates(self, mock_scrape):
        # Carol once again opens up the application and
        # adds a couple of securities into the app.
        app = MarketData()
        app.run(database=self.database)
        new_tickers = ['AMZN', 'GOOG']
        for ticker in new_tickers:
            app.add_security(ticker)

        # proceeds to update the data in the application
        # for multiple dates and securities
        for ticker in new_tickers:
            data = self.test_data[ticker]
            for date_string in data:
                dt = datetime.datetime.strptime(date_string, '%d-%b-%Y')
                equity_data = test_utils.get_test_data(self.test_data, ticker,
                                                       dt)
                mock_scrape.return_value = equity_data
                app.update_market_data(ticker, dt)

        # Satisfied that she has updated all she needs to
        # she closes the application down.
        app.close()

        # Josh, her ever inquistive friend checks in on
        # the prices of AMZN and GOOG.
        new_app = MarketData()
        new_app.run(database=self.database)

        # He first checks the prices for GOOG first
        dt = datetime.datetime(2019, 8, 27)
        expected_data = test_utils.get_test_data(self.test_data, 'GOOG', dt)
        actual_data = new_app.get_equity_data('GOOG', dt)
        self.assertEqual(expected_data, actual_data, 'GOOG: 27-Aug-2019')

        # He then checks the prices for AMZN on both dates
        expected_data = test_utils.get_test_data(self.test_data, 'AMZN', dt)
        actual_data = new_app.get_equity_data('AMZN', dt)
        self.assertEqual(expected_data, actual_data, 'AMZN: 27-Aug-2019')

        dt = datetime.datetime(2019, 8, 26)
        expected_data = test_utils.get_test_data(self.test_data, 'AMZN', dt)
        actual_data = new_app.get_equity_data('AMZN', dt)
        self.assertEqual(expected_data, actual_data, 'AMZN: 26-Aug-2019')

        # Satisfied with what he sees he closes the app
        new_app.close()
Exemple #12
0
 def setUp(self):
     self.test_data = test_utils.load_test_data()
     self.da = data_adapter.get_adapter(self.data_adapter_source)
     self.database = MarketData.Database(self.da.test_database,
                                         self.data_adapter_source)
     self.da.create_test_database()
Exemple #13
0
    def test_get_equity_data_from_app(self, mock_scrape):
        # Josh has heard of this new app from
        # Carol and decides to open the app
        # and play with it.
        app = MarketData()
        app.run(database=self.database)

        # Carol told Josh that she has already
        # added a security into the app
        ticker = 'AMZN'
        dt = datetime.datetime(2019, 8, 27)
        app.add_security(ticker)

        # Josh proceeds to check what the security price
        # on the 31 July 2019 is of the stock Carol added
        # but accidentally enters the wrong ticker name
        with self.assertRaises(InvalidTickerError):
            data = app.get_equity_data('AMZNN', dt)

        # He then tries again but with the correct
        # ticker this time but with the wrong date..
        with self.assertRaises(InvalidDateError):
            data = app.get_equity_data(ticker, datetime.datetime(2017, 8, 25))

        # Third time lucky, he enters in the correct
        # ticker and date and gets the results!
        expected_data = test_utils.get_test_data(self.test_data, ticker, dt)
        mock_scrape.return_value = expected_data
        app.update_market_data(ticker, dt)

        data = app.get_equity_data(ticker, dt)

        # He then goes to his trusty source, Yahoo to
        # confirm that the security price is indeed correct.
        self.assertEqual(data, expected_data)
Exemple #14
0
 def test_raise_database_not_found_error(self):
     with self.assertRaises(data_adapter.DatabaseNotFoundError):
         d = MarketData.Database('db.json',
                                 data_adapter.DataAdapterSource.JSON)
         self.app.run(database=d)
Exemple #15
0
#!/usr/bin/env python

import os
import sys
import datetime
from decimal import Decimal
from enum import IntEnum, unique
from market_data.market_data import MarketData
from market_data.data import InvalidTickerError, InvalidDateError, NoDataError
import market_data.data_adapter as data_adapter

DATA_ADAPTER_SOURCE = data_adapter.DataAdapterSource.SQLITE3
TEST_MODE = False

app = MarketData()


def clear_screen():
    if not TEST_MODE:
        if os.name == 'nt':
            _ = os.system('cls')
        else:
            _ = os.system('clear')


@unique
class MenuOptions(IntEnum):
    VIEW_SECURITIES = 1
    ADD_SECURITIES = 2
    UPDATE_MARKET_DATA = 3
    QUIT = 4
Exemple #16
0
		new_db = False
		print('New DB server:', new_db)
	if args.reset:
		delete_db(args.database)
		new_db = True
		print('New DB reset:', new_db)
	print('New DB:', new_db)
	if args.seed:
		random.seed(args.seed)
	else:
		random.seed()

	accts = acct.Accounts(conn=args.database, standard_accts=trade_accts)
	ledger = acct.Ledger(accts, ledger_name=args.ledger, entity=args.entity)
	trade = trade.Trading(ledger, sim=args.simulation)
	data = MarketData()
	combine_data = CombineData(trade.data_location)
	algo = TradingAlgo(ledger, trade, combine_data)

	if new_db:
		accts.clear_tables(v=True)
	if args.entity:
		ledger.default = args.entity
	if args.train_new:
		print(time_stamp() + 'Will train new models if they don\'t already exist.')
		# TODO Implement this
	if args.train:
		print(time_stamp() + 'Will train all new models.')
	if args.tickers:
		if '.csv' not in args.tickers and '.xls' not in args.tickers:
			args.tickers = [x.strip().upper() for x in args.tickers.split(',')]