Exemplo n.º 1
0
    def dbinit(self):
        # Currencies
        currencies = {}
        currencies["BTC"] = Currency(key_name="BTC",
                                     name="Bitcoin",
                                     symbol="BTC")
        currencies["USD"] = Currency(key_name="USD",
                                     name="US Dollar",
                                     symbol=u"$")
        currencies["EUR"] = Currency(key_name="EUR", name="Euro", symbol=u"€")
        currencies["SEK"] = Currency(key_name="SEK",
                                     name="Swedish Kronor",
                                     symbol="kr")
        for currency in currencies:
            currencies[currency].put()

        # Markets
        markets = {}
        markets["KapitonSEK"] = Market(key_name="KapitonSEK",
                                       name="Kapiton",
                                       url="http://kapiton.se/",
                                       currency=currencies["SEK"],
                                       fee=0.0135)
        markets["MtGoxEUR"] = Market(key_name="MtGoxEUR",
                                     name="MtGox",
                                     url="http://mtgox.com/",
                                     currency=currencies["EUR"],
                                     fee=0.006)
        markets["MtGoxUSD"] = Market(key_name="MtGoxUSD",
                                     name="MtGox",
                                     url="http://mtgox.com/",
                                     currency=currencies["USD"],
                                     fee=0.006)
        for market in markets:
            markets[market].put()
Exemplo n.º 2
0
def parse_cyb2(
    url='http://www.szse.cn/szseWeb/FrontController.szse?randnum=0.5328349224291742'
):
    payload = {
        'ACTIONID': 7,
        'AJAX': 'AJAX-TRUE',
        'CATALOGID': '1898_nm',
        'TABKEY': 'tab1',
        'txtQueryDate': '2016-01-15',
        'REPORT_ACTION': 'reach'
    }
    res = requests.post(url, data=payload)
    print res.text
    # read html <table> to list of DataFrame
    dfs = pd.read_html(res.text, flavor='lxml')
    # dfs = pd.read_html(etree.tostring(r), flavor='bs4')
    if len(dfs) >= 1:
        df = dfs[0]
        print df
        tradable_shares = df.iloc[4][1]
        total_market = df.iloc[5][1]
        volume_money = df.iloc[7][1]
        volume = df.iloc[8][1]
        pe = df.iloc[10][1]
        high_pe = df.iloc[10][3]
        value = df.iloc[13][1]

        if isinstance(tradable_shares, type(pd.NaT)):
            tradable_shares = 0
        if type(total_market) == type(pd.NaT):
            total_market = 0
        if isinstance(volume_money, type(pd.NaT)):
            volume_money = 0
        if isinstance(volume, type(pd.NaT)):
            volume = 0
        if isinstance(pe, type(pd.NaT)):
            pe = 0
        if type(value) != float:
            value = 0.0

        # 换手率=成交量÷当日实际流通量
        if tradable_shares == 0:
            turnover = 0
        else:
            turnover = float(volume) / float(tradable_shares)
        print 'name:{} total_market:{} volume:{} turnover:{} pe:{} value:{}'.format(
            name, total_market, volume_money, turnover, pe, value)
        market = Market(name,
                        float(total_market) / 100000000,
                        float(volume_money) / 100000000, turnover, pe, value)
        print market
        return market
Exemplo n.º 3
0
 def scrape(self, limit=None):
     soup = BeautifulSoup(self.text, 'lxml')
     table = soup.find('table', attrs={'id': TABLE_ID})
     coins = []
     for row in table.find_all('tr')[1:]:
         tds = row.find_all('td')
         props = [td.get_text().strip() for td in tds]
         icon = tds[1].find('img')
         props.append(icon['src'] if icon else '')
         coins.append(Coin(props))
         if limit and len(coins) == limit:
             break
     return Market(coins)
Exemplo n.º 4
0
def main():
    """
    Application that will...
    """
    gbce = Market()

    # open the market
    print("------------------------------------")
    print("Global Beverage Corporation Exchange")
    print("------------------------------------")

    # load sample stock data into the market
    for stock in SAMPLE_DATA:
        stk = Stock()
        setattr(stk, "Stock Symbol", stock[0])
        setattr(stk, "Type", stock[1])
        setattr(stk, "Last Dividend", stock[2])
        setattr(stk, "Fixed Dividend", stock[3])
        setattr(stk, "Par Value", stock[4])

        gbce.add_stock(stk)
        print("Welcome {} to the GBCE".format(getattr(stk, "Stock Symbol")))
        sleep(0.8)

    # run some trades
    for trade_no in range(20):
        t = Trade()
        setattr(t, "Stock Symbol",
                choice(list(map(lambda x: x[0], SAMPLE_DATA))))
        setattr(t, "Buy/Sell", choice(['Buy', 'Sell']))
        setattr(t, "Quantity",
                choice([500, 1000, 2000, 5000, 10000, 20000, 50000]))
        setattr(t, "Price", choice(list(range(50, 150))))

        gbce.add_trade(t)

        sto = gbce.get_stock(getattr(t, "Stock Symbol"))

        print("{sale_report} ({all_share})".\
              format(sale_report="{time}: {b} {q} shares of {s} for {p}".\
                     format(b=getattr(t,"Buy/Sell"),
                            time=t.Timestamp.time().replace(microsecond=0),
                            q=t.Quantity,p=t.Price,
                            s=getattr(t,"Stock Symbol")),
                     all_share="GBCE All-Share: {a:.2f}".\
                     format(a=gbce.all_share_index())))

        sleep(0.75 + choice([0.10, 0.15, 0.25, 0.45]))

    # close the market
    print("------------------------------------")
Exemplo n.º 5
0
def parse_sz_market_common(name, url):
    page = parse(url).getroot()
    result = etree.tostring(page)
    # print '*'*20
    # print result
    # print '*'*20

    r = page.get_element_by_id('REPORTID_tab1')
    print '*' * 20
    print etree.tostring(r)
    print '*' * 20
    # read html <table> to list of DataFrame
    dfs = pd.read_html(etree.tostring(r), flavor='lxml')
    # dfs = pd.read_html(etree.tostring(r), flavor='bs4')
    if len(dfs) >= 1:
        df = dfs[0]
        print df
        tradable_shares = df.iloc[4][1]
        total_market = df.iloc[5][1]
        volume_money = df.iloc[7][1]
        volume = df.iloc[8][1]
        pe = df.iloc[10][1]
        high_pe = df.iloc[10][3]
        value = df.iloc[13][1]

        if isinstance(tradable_shares, type(pd.NaT)):
            tradable_shares = 0
        if type(total_market) == type(pd.NaT):
            total_market = 0
        if isinstance(volume_money, type(pd.NaT)):
            volume_money = 0
        if isinstance(volume, type(pd.NaT)):
            volume = 0
        if isinstance(pe, type(pd.NaT)):
            pe = 0
        if type(value) != float:
            value = 0.0

        # 换手率=成交量÷当日实际流通量
        if tradable_shares == 0:
            turnover = 0
        else:
            turnover = float(volume) / float(tradable_shares)
        print 'name:{} total_market:{} volume:{} turnover:{} pe:{} value:{}'.format(
            name, total_market, volume_money, turnover, pe, value)
        market = Market(name,
                        float(total_market) / 100000000,
                        float(volume_money) / 100000000, turnover, pe, value)
        print market
        return market
Exemplo n.º 6
0
def parse_sh_market():
    page = parse(
        'http://www.sse.com.cn/market/stockdata/overview/day/').getroot()
    result = etree.tostring(page)
    print result

    r = page.get_element_by_id('dateList')
    statistics = r.text_content().split()
    for word in statistics:
        print word

    market = Market(name='sh',
                    total_market_cap=statistics[1],
                    volume=float(statistics[8]) / 10000,
                    turnover=statistics[12],
                    pe=statistics[14],
                    date=statistics[2])
    print market
    return market
Exemplo n.º 7
0
    def setUp(self):
        """Create test client, add sample data."""

        User.query.delete()
        Market.query.delete()
        SavedArticle.query.delete()

        self.client = app.test_client()

        m = Market(code="en-CA", name="United States")

        db.session.add(m)
        db.session.commit()

        self.testuser = User(email="*****@*****.**",
                             username="******",
                             password="******",
                             image_url="http://www.picture.com",
                             market="en-CA")

        db.session.add(self.testuser)
        db.session.commit()
Exemplo n.º 8
0
def parse_sz_market():
    page = parse('http://www.szse.cn/main/marketdata/tjsj/jbzb/').getroot()

    r = page.get_element_by_id('REPORTID_tab1')
    print etree.tostring(r)
    # read html <table> to list of DataFrame
    dfs = pd.read_html(etree.tostring(r), flavor='lxml')
    # print dfs
    # print len(dfs)
    if len(dfs) >= 1:
        df = dfs[0]
        print df
        total_market = df.iloc[10][1]
        volume = df.iloc[12][1]
        avg_price = df.iloc[13][1]
        pe = df.iloc[14][1]
        turnover_rate = df.iloc[15][1]

        if type(total_market) == type(pd.NaT):
            total_market = 0
        if type(volume) == type(pd.NaT):
            volume = 0
        if type(turnover_rate) == type(pd.NaT):
            turnover_rate = 0
        if type(pe) == type(pd.NaT):
            pe = 0
        print 'total_market:{} volume:{} turnover_rate:{} pe:{}'.format(
            total_market, volume, turnover_rate, pe)
        market = Market('sz',
                        total_market_cap=float(total_market) / 100000000,
                        volume=float(volume) / 100000000,
                        turnover=float(turnover_rate),
                        pe=float(pe))
        print market
        # print df.index
        # print df.columns
        # print df.values
        # print df.describe()
        return market
Exemplo n.º 9
0
def get_markets(db_session, betfair_api, event):

    markets = []
    bfl_markets = betfair_api.betting.list_market_catalogue(
        filter=bfl.filters.market_filter(
            event_ids=[event.betfair_id],
            market_type_codes=['WIN'],
        ),
        max_results=25,
        market_projection=['MARKET_START_TIME', 'RUNNER_METADATA'])
    for bfl_market in bfl_markets:
        market = db_session.query(Market).filter(
            Market.event_id == event.id,
            Market.betfair_id == bfl_market.market_id).one_or_none()
        if not market:
            print(
                f"New market: {bfl_market.market_start_time}: {bfl_market.market_name} ({len(bfl_market.runners)} runners)"
            )
            market = Market(event_id=event.id,
                            betfair_id=bfl_market.market_id,
                            name=bfl_market.market_name,
                            start_time=bfl_market.market_start_time,
                            total_matched=bfl_market.total_matched)
            db_session.add(market)
        markets.append(market)
        for bfl_runner in bfl_market.runners:
            runner = db_session.query(Runner).filter(
                Runner.betfair_id == bfl_runner.selection_id).one_or_none()
            if not runner:
                runner = Runner(
                    betfair_id=bfl_runner.selection_id,
                    name=bfl_runner.runner_name,
                )
                db_session.add(runner)
            market_runner = db_session.query(MarketRunner).filter(
                MarketRunner.market_id == market.id,
                MarketRunner.runner_id == runner.id).one_or_none()
            if not market_runner:
                market_runner = MarketRunner(
                    market_id=market.id,
                    runner_id=runner.id,
                    sort_priority=bfl_runner.sort_priority)
                jockey_name = bfl_runner.metadata['JOCKEY_NAME']
                if jockey_name:
                    jockey = db_session.query(Jockey).filter(
                        Jockey.name == jockey_name).one_or_none()
                    if not jockey:
                        jockey = Jockey(name=jockey_name, )
                        db_session.add(jockey)
                    market_runner.jockey_id = jockey.id
                trainer_name = bfl_runner.metadata['TRAINER_NAME']
                if trainer_name:
                    trainer = db_session.query(Trainer).filter(
                        Trainer.name == trainer_name).one_or_none()
                    if not trainer:
                        trainer = Trainer(name=trainer_name, )
                        db_session.add(trainer)
                    market_runner.trainer_id = trainer.id
                db_session.add(market_runner)
    db_session.commit()
    return markets
Exemplo n.º 10
0
from models import Market
import datetime

from lxml import html
import requests

from models import db

raw = requests.get("https://loanbase.com/stats")
tree = html.fromstring(raw.text)

active_loans = float(
    tree.xpath('//div[@class="statistic"]/p//text()')[2].split(" ")[1])

m = Market(lbactive=active_loans, created=datetime.datetime.utcnow())
db.session.add(m)
db.session.commit()
Exemplo n.º 11
0
def forecast():
    #tam & sas
    sales = request.form['sales']
    price = request.form['price']
    sas = request.form['sas']
    growth = request.form['growth']
    total = Market(sales, price, sas, growth)
    calc_tam = total.tam()
    calc_tam_c = '{:,.2f}'.format(calc_tam)
    calc_sas = total.sas()
    calc_sas_c = '{:,.2f}'.format(calc_sas)
    #forecast P&L inputs
    salesm1 = request.form['salesm1']
    pricem1 = request.form['pricem1']
    sgmom = request.form['sgmom']
    cmgrowth = request.form['cmgrowth']
    pindex = request.form['pindex']
    cogs = request.form['cogs']
    opex = request.form['opex']
    fixed = request.form['fixed']
    #forecast P&L class constructor
    fpl = Forecastpl(salesm1, pricem1, sgmom, cmgrowth, pindex, cogs, opex,
                     fixed)
    #x chart axis
    x = np.arange(1, 13)
    #forecast P&L growth and percentages
    fpl.sales_growth()
    fpl.cm_growth()
    fpl.price_growth()
    fpl.cogs_pc()
    fpl.opex_pc()
    fpl.fixed_pc()
    #forecast P&L pre-plot calculations
    fpl.units_mom()
    fpl.price_mom()
    fpl_rev = fpl.revenue_profile()
    fpl.cm_calc_initial()
    fpl.cm_adjusted()
    fpl_cmadj = fpl.cm_calc()
    fpl.fixed_calc()
    fpl_op = fpl.fpl_op_profit()
    #forecast P&L cumulative
    fpl_rev_cum = np.cumsum(fpl_rev)
    fpl_cmadj_cum = np.cumsum(fpl_cmadj)
    fpl_op_cum = np.cumsum(fpl_op)
    #sas and fp&l comparison
    fpl_sas = round(np.sum(fpl_rev, dtype=np.float64), 2)
    fpl_sas_c = '{:,.2f}'.format(fpl_sas)
    fpl_sas_delta = round(float(fpl_sas - calc_sas), 2)
    fpl_sas_delta_c = '{:,.2f}'.format(fpl_sas_delta)
    fpl_sas_delta_pc = round(float((fpl_sas / calc_tam) * 100), 2)
    #plot parameters
    plt.plot(x, fpl_rev_cum, label='revenue')
    plt.plot(x, fpl_cmadj_cum, label='cm')
    plt.plot(x, fpl_op_cum, label='op profit')
    plt.xlabel('month')
    plt.ylabel('£')
    plt.title('Forecast P&L cumulative ')
    plt.legend()
    plt.xticks(x)
    plt.savefig(img_path)
    plt.close()
    return render_template('forecast.html',
                           sales=sales,
                           price=price,
                           sas=sas,
                           growth=growth,
                           calc_tam_c=calc_tam_c,
                           calc_sas_c=calc_sas_c,
                           name='Forecast P&L cumulative',
                           fpl_sas_c=fpl_sas_c,
                           fpl_sas_delta_c=fpl_sas_delta_c,
                           fpl_sas_delta_pc=fpl_sas_delta_pc,
                           fileName=fileName,
                           fileExt=fileExt)