Esempio n. 1
0
def get_products():
    try:
        items = browser.find_elements_by_css_selector('#J_goodsList .gl-warp li')
        rows = []
        for item in items:
            price = item.find_element_by_css_selector('.p-price strong i').text
            name = item.find_element_by_css_selector('.p-name a em').text
            rows.append(Data(title=name, price=price))

        Data().insert_data(rows)
    except TimeoutException:
        print('get products exception')
        get_products()
Esempio n. 2
0
def test_loads_with_data():
    simple_fixture()
    with freeze_time(dt(2017, 3, 7)):
        t = Tickers(data=Data())

    assert len(t.ticker_names) == 2
    assert t.ticker_names[0] == "VCN.TO"
    assert t.ticker_names[1] == "VEE.TO"

    assert len(t.tickers) == 2

    assert t.prices.columns[0] == "VCN.TO"
    assert t.prices.columns[1] == "VEE.TO"
    assert t.prices.loc["2017-03-02"]["VCN.TO"] == approx(30.00)
    assert t.changes.loc["2017-03-03"]["VCN.TO"] == approx(30.10 / 30.00 - 1)
    assert t.changes_from_start.loc["2017-03-06"]["VCN.TO"] == approx(29.85 /
                                                                      30.00 -
                                                                      1)
    assert t.yields_from_start.loc["2017-03-06"]["VCN.TO"] == approx(0.02 /
                                                                     3.0)
    assert t.volatilities["VEE.TO"] == t.tickers["VEE.TO"].volatility
    assert t.correlations["VCN.TO"]["VCN.TO"] == approx(1.00)
    assert t.correlations["VCN.TO"]["VEE.TO"] == approx(-0.9922778767136671)

    assert t.price("2017-03-02", "VCN.TO") == approx(30.00)
    simple_fixture_teardown()
Esempio n. 3
0
 def __init__(self,
              accounts=None,
              from_day=None,
              update=True,
              verbose=True):
     """Instantiate a Portfolio object."""
     self._data = Data()
     self.accounts = determine_accounts(accounts)
     if from_day is not None:
         self.from_day = from_day
     else:
         self.from_day = self._get_start_date(self.accounts)
     if update:
         PriceUpdater(verbose).update()
     self.deposits = Deposits(self.accounts, self.from_day, self._data)
     self.tickers = Tickers(self.accounts, self.from_day, data=self._data)
     self.positions = Positions(self.accounts,
                                self.from_day,
                                self.tickers,
                                data=self._data)
     self.by_day = self._calc_daily()
     self.by_month = self._summarize_by("month")
     self.by_year = self._summarize_by("year")
     if len(self.by_day.index) > 0:
         self.positions.calc_weights(self.by_day["total_value"])
Esempio n. 4
0
def test_loads_with_data():
    simple_fixture()
    with freeze_time(dt(2017, 3, 7)):
        p = Positions(data=Data())

    assert len(p.ticker_names) == 2
    assert p.ticker_names[0] == "VCN.TO"
    assert p.ticker_names[1] == "VEE.TO"

    assert len(p.positions) == 2

    assert p.units.columns[0] == "VCN.TO"
    assert p.units.columns[1] == "VEE.TO"
    assert p.units.loc["2017-03-02"]["VCN.TO"] == approx(0)
    assert p.units.loc["2017-03-03"]["VCN.TO"] == approx(100)
    assert p.costs.loc["2017-03-03"]["VCN.TO"] == approx(3010.35)
    assert p.costs_per_unit.loc["2017-03-03"]["VCN.TO"] == approx(30.1035)
    assert p.current_prices.loc["2017-03-03"]["VCN.TO"] == approx(30.10)
    assert p.market_values.loc["2017-03-03"]["VCN.TO"] == approx(3010)
    assert p.open_profits.loc["2017-03-03"]["VCN.TO"] == approx(-0.35)
    assert p.distributions.loc["2017-03-03"]["VCN.TO"] == approx(10.1)
    assert p.distribution_returns.loc["2017-03-03"]["VCN.TO"] == approx(
        10.1 / 3010.35)
    assert p.appreciation_returns.loc["2017-03-03"]["VCN.TO"] == approx(
        (3010.0 - 3010.35) / 3010.35)
    assert p.total_returns.loc["2017-03-03"]["VCN.TO"] == approx(
        (3010.0 + 10.1 - 3010.35) / 3010.35)
    simple_fixture_teardown()
Esempio n. 5
0
def test_values():
    simple_fixture()
    with freeze_time(dt(2017, 3, 7)):
        vcn = Ticker("VCN.TO", data=Data())

    assert vcn.price("2017-03-01") is None
    assert vcn.price("2017-03-02") == approx(30.00)
    assert vcn.price("2017-03-03") == approx(30.10)
    assert vcn.price("2017-03-04") == approx(30.10)
    assert vcn.price("2017-03-05") == approx(30.10)
    assert vcn.price("2017-03-06") == approx(29.85)
    assert vcn.price("2017-03-08") is None

    assert vcn.change("2017-03-01") is None
    assert pd.isnull(vcn.change("2017-03-02"))  # Because the previous price is None
    assert vcn.change("2017-03-03") == approx(30.10 / 30.00 - 1)
    assert vcn.change("2017-03-04") == 0
    assert vcn.change("2017-03-05") == 0
    assert vcn.change("2017-03-06") == approx(29.85 / 30.10 - 1)

    assert vcn.change_from_start("2017-03-01") is None
    assert vcn.change_from_start("2017-03-02") == approx(0)
    assert vcn.change_from_start("2017-03-03") == approx(30.10 / 30.00 - 1)
    assert vcn.change_from_start("2017-03-04") == approx(30.10 / 30.00 - 1)
    assert vcn.change_from_start("2017-03-05") == approx(30.10 / 30.00 - 1)
    assert vcn.change_from_start("2017-03-06") == approx(29.85 / 30.00 - 1)

    assert vcn.distribution("2017-03-01") is None
    assert vcn.distribution("2017-03-02") == approx(0)
    assert vcn.distribution("2017-03-03") == approx(0.1010)
    assert vcn.distribution("2017-03-04") == approx(0)
    assert vcn.distribution("2017-03-05") == approx(0)
    assert vcn.distribution("2017-03-06") == approx(0.0990)

    assert vcn.distributions_from_start("2017-03-01") is None
    assert vcn.distributions_from_start("2017-03-02") == approx(0)
    assert vcn.distributions_from_start("2017-03-03") == approx(0.1010)
    assert vcn.distributions_from_start("2017-03-04") == approx(0.1010)
    assert vcn.distributions_from_start("2017-03-05") == approx(0.1010)
    assert vcn.distributions_from_start("2017-03-06") == approx(0.2)

    assert vcn.yield_from_start("2017-03-01") is None
    assert vcn.yield_from_start("2017-03-02") == approx(0)
    assert vcn.yield_from_start("2017-03-03") == approx(0.1010 / 30.00)
    assert vcn.yield_from_start("2017-03-04") == approx(0.1010 / 30.00)
    assert vcn.yield_from_start("2017-03-05") == approx(0.1010 / 30.00)
    assert vcn.yield_from_start("2017-03-06") == approx(0.2 / 30.00)

    assert vcn.returns("2017-03-01") is None
    assert vcn.returns("2017-03-02") == approx(0)
    assert vcn.returns("2017-03-03") == approx(
        vcn.change_from_start("2017-03-03") + vcn.yield_from_start("2017-03-03")
    )
    assert vcn.returns("2017-03-06") == approx(
        vcn.change_from_start("2017-03-06") + vcn.yield_from_start("2017-03-06")
    )

    assert vcn.volatility == approx(vcn.values[(vcn.values.open)]["change"].std(axis=0))
    simple_fixture_teardown()
Esempio n. 6
0
    def test_query_custom_values(self, data_call):
        d = Deposits(accounts=["Account1"],
                     from_day=date(2017, 1, 1),
                     data=Data())
        _, kwargs = data_call.call_args

        assert kwargs["params"]["accounts"] == ["Account1"]
        assert kwargs["params"]["from_day"] == date(2017, 1, 1)
        assert kwargs["params"]["today"] == date.today()
Esempio n. 7
0
    def test_query_default_values(self, data_call):
        d = Deposits(data=Data())
        _, kwargs = data_call.call_args

        assert len(d.deposits) == 2
        assert d.deposits.loc[date(2017, 1, 1)]["amount"] == 10000
        assert kwargs["params"]["accounts"] == []
        assert kwargs["params"]["from_day"] is None
        assert kwargs["params"]["today"] == date.today()
Esempio n. 8
0
def test_position_class_instantiates_with_data():
    simple_fixture()
    with freeze_time(dt(2017, 3, 7)):
        vcn = Position("VCN.TO", data=Data())

    assert vcn is not None
    assert vcn.ticker_name == "VCN.TO"
    assert "total_returns" in vcn.__repr__()
    assert "total_returns" in str(vcn)
    simple_fixture_teardown()
Esempio n. 9
0
def main ():
    
    data = Data()
    
    demand = data.get_demand()
    waiting = data.get_waiting()
    distances = data.get_distances()
    crime = data.get_crime()

    # plt.plot(demand['lon'], demand['lat'], 'rs', label="demand points")
    # plt.plot(waiting['lon'].astype(np.float64), waiting['lat'].astype(np.float64), 'bs', label="waiting points")
    # plt.legend()
    # plt.show()
    # mplleaflet.show()

    distances['in_range'] = (distances['distance'] <= RADIUS).astype(int)
    distances = distances[ distances['WPname'] != 'Holland Park']
    distances = distances[ distances['in_range'] == 1]
    unique_wp = distances['WPname'].unique()
    unique_wp = [unique_wp[60]]
    unique_dp = distances [ distances['WPname'] == unique_wp[0] ]['DAuid']

    demand = demand[demand['DAuid'].isin(unique_dp)]
    waiting = waiting[waiting['name'].isin(unique_wp)]
    plt.plot(demand['lon'], demand['lat'], 'rs')
    plt.plot(waiting['lon'].astype(np.float64), waiting['lat'].astype(np.float64), 'bs')
    mplleaflet.show()
Esempio n. 10
0
    def test_df_from_sql(self):
        d = Data()
        sql = """SELECT SUM(total)::double precision AS amount, day
            FROM transactions
            WHERE account = ANY(%(accounts)s)
                AND (%(from_day)s IS NULL OR day >= %(from_day)s)
                AND day <= %(today)s
                AND txtype = 'deposit'
            GROUP BY day
            ORDER BY day ASC;
        """
        params = {
            "accounts": ["RRSP1"],
            "from_day": None,
            "today": datetime(2017, 3, 7),
        }
        index_col = "day"
        parse_dates = ["day"]

        df = d.df_from_sql(sql, params, index_col, parse_dates)

        assert len(df) == 1
        assert df.loc[datetime(2017, 3, 2)]["amount"] == 10000
Esempio n. 11
0
class Deposits:
    """DataFrame-based structure to keep track of investment deposits.

    Public methods:
    amount(date) -- Return the total sum deposited on this day to all accounts requested

    Instance variables:
    deposits -- DataFrame, day-indexed, with the total sum deposited on each day into
                any of the accounts requested
    """
    def amount(self, day):
        """Return the amount deposited on the requested day."""
        return self.deposits["amount"].get(day, 0)

    def __init__(self, accounts=None, from_day=None, data=None):
        """Instantiate a Deposits object."""
        self._data = Data() if data is None else data
        self._accounts = determine_accounts(accounts)
        self._from_day = from_day
        self.deposits = self._get_deposits()

    def __str__(self):
        return str(self.deposits)

    def _get_deposits(self):
        return self._data.df_from_sql(
            """SELECT SUM(total)::double precision AS amount, day
            FROM transactions
            WHERE account = ANY(%(accounts)s)
                AND (%(from_day)s IS NULL OR day >= %(from_day)s)
                AND day <= %(today)s
                AND txtype = 'deposit'
            GROUP BY day
            ORDER BY day ASC;""",
            params={
                "accounts": self._accounts,
                "from_day": self._from_day,
                "today": date.today(),
            },
            index_col="day",
            parse_dates=["day"],
        )
Esempio n. 12
0
    def test_amount(self, data_call):
        d = Deposits(data=Data())

        assert d.amount(date(2016, 12, 31)) == 0  # out of range
        assert d.amount(date(2017, 1, 3)) == 0
        assert d.amount(date(2017, 2, 1)) == 5000
Esempio n. 13
0
    def test_create(self, data_call):
        d = Deposits(data=Data())

        assert d is not None
        assert "Empty" in str(d)
Esempio n. 14
0
 def __init__(self, accounts=None, from_day=None, data=None):
     """Instantiate a Deposits object."""
     self._data = Data() if data is None else data
     self._accounts = determine_accounts(accounts)
     self._from_day = from_day
     self.deposits = self._get_deposits()
Esempio n. 15
0
def test_price_does_not_crash_when_empty():
    assert Ticker("VCN.TO", data=Data()).price(date.today()) is None
Esempio n. 16
0
from db.data import Data
import gurobipy as gp
from gurobipy import GRB
from scipy.special import comb

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import mplleaflet

# set data class as global
data = Data()

# define constants
RADIUS = 4200
ALPHA = 1
LAMBDA = 1.35
SERVICE_TIME = 1

incident_weight = {
    'Break and Enter - Business': 3,
    'Break and Enter - Residence': 5,
    'Fatal/Injury Collision': 5,
    'Graffiti': 2,
    'Shoplifting': 1,
    'Theft from Motor Vehicle': 4,
    'Theft of Motor Vehicle': 4
}


def relocation_model_con_pop(distances, waiting, pop_dict, crime_dict,
Esempio n. 17
0
def test_values():
    simple_fixture()
    with freeze_time(dt(2017, 3, 7)):
        vcn = Position("VCN.TO", data=Data())
        vcn.calc_weight(vcn.values["market_value"])

    assert vcn.units("2017-03-01") is None
    assert vcn.units("2017-03-02") == 0
    assert vcn.units("2017-03-03") == 100
    assert vcn.units("2017-03-04") == 100
    assert vcn.units("2017-03-08") is None

    assert vcn.cost("2017-03-01") is None
    assert vcn.cost("2017-03-02") == 0
    assert vcn.cost("2017-03-03") == 3010.35
    assert vcn.cost("2017-03-04") == 3010.35
    assert vcn.cost("2017-03-08") is None

    assert vcn.cost_per_unit("2017-03-01") is None
    assert math.isnan(vcn.cost_per_unit("2017-03-02"))
    assert vcn.cost_per_unit("2017-03-03") == 30.1035
    assert vcn.cost_per_unit("2017-03-04") == 30.1035
    assert vcn.cost_per_unit("2017-03-08") is None

    assert vcn.current_price("2017-03-01") is None
    assert vcn.current_price("2017-03-02") == 30.00
    assert vcn.current_price("2017-03-03") == 30.10
    assert vcn.current_price("2017-03-06") == 29.85
    assert vcn.current_price("2017-03-08") is None

    assert vcn.market_value("2017-03-01") is None
    assert vcn.market_value("2017-03-02") == 0
    assert vcn.market_value("2017-03-03") == 3010.0
    assert vcn.market_value("2017-03-06") == 2985.0
    assert vcn.market_value("2017-03-08") is None

    assert vcn.open_profit("2017-03-01") is None
    assert vcn.open_profit("2017-03-02") == 0
    assert vcn.open_profit("2017-03-03") == approx(-0.35)
    assert vcn.open_profit("2017-03-06") == approx(-25.35)
    assert vcn.open_profit("2017-03-08") is None

    assert vcn.distributions("2017-03-01") is None
    assert vcn.distributions("2017-03-02") == 0
    assert vcn.distributions("2017-03-03") == 10.1
    assert vcn.distributions("2017-03-06") == 20.0
    assert vcn.distributions("2017-03-08") is None

    assert vcn.distribution_returns("2017-03-01") is None
    assert math.isnan(vcn.distribution_returns("2017-03-02"))
    assert vcn.distribution_returns("2017-03-03") == 10.1 / 3010.35
    assert vcn.distribution_returns("2017-03-06") == 20.0 / 3010.35
    assert vcn.distribution_returns("2017-03-08") is None

    assert vcn.appreciation_returns("2017-03-01") is None
    assert math.isnan(vcn.appreciation_returns("2017-03-02"))
    assert vcn.appreciation_returns("2017-03-03") == (3010.0 -
                                                      3010.35) / 3010.35
    assert vcn.appreciation_returns("2017-03-06") == (2985.0 -
                                                      3010.35) / 3010.35
    assert vcn.appreciation_returns("2017-03-08") is None

    assert vcn.total_returns("2017-03-01") is None
    assert vcn.total_returns("2017-03-02") == 0
    assert vcn.total_returns("2017-03-03") == approx(
        (3010.0 + 10.1 - 3010.35) / 3010.35)
    assert vcn.total_returns("2017-03-06") == approx(
        (2985.0 + 20.0 - 3010.35) / 3010.35)
    assert vcn.total_returns("2017-03-08") is None

    assert vcn.weight("2017-03-01") is None
    assert vcn.weight("2017-03-02") == 0
    assert vcn.weight("2017-03-06") == 1
    simple_fixture_teardown()
Esempio n. 18
0
def test_position_class_instantiates():
    vcn = Position("VCN.TO", data=Data())
    assert vcn is not None
    assert vcn.ticker_name == "VCN.TO"
    assert "Empty" in vcn.__repr__()
    assert "Empty" in str(vcn)
Esempio n. 19
0
def test_tickers_class_instantiates():
    t = Tickers(data=Data())
    assert t is not None
    assert t.__repr__() == "{}"
    assert str(t) == "{}"
Esempio n. 20
0
 def test_create(self):
     d = Data()
Esempio n. 21
0
def test_positions_class_instantiates():
    p = Positions(data=Data())
    assert p is not None