Exemplo n.º 1
0
    def group_by_sector(self, username):

        t_dao = Transaction_dao()
        c_dao = Company_dao()
        distinct_stocks = t_dao.get_user_stock_list(username)

        sector_volume = {}

        for symbol in distinct_stocks:
            company_info = c_dao.get_company_model(symbol)
            volume = t_dao.get_owned_stock_model(
                username, symbol, company_info.get_ask()).get_volume()

            try:
                sector = self.get_sector_by_symbol(symbol)
                if (sector.strip() == ''): sector = "Other"
            except:
                sector = "Other"

            if (sector not in sector_volume):
                sector_volume[sector] = volume
            else:
                sector_volume[sector] += volume

        return sector_volume
Exemplo n.º 2
0
def transaction(username, volume, company, trans_type, algo_id):

    udao1 = User_stock_value_dao()
    udao2 = User_portfolio_dao()
    cdao = Company_dao()
    tdao = Transaction_dao()
    hdao = History_dao()

    c = cdao.get_company_model(company)

    ask = Decimal(c.get_ask())
    #time = datetime.datetime.utcnow()

    final = calculate_price(ask, volume)

    if trans_type == 'buy':
        u = udao2.get_user_portfolio_model(username)
        if final <= u.get_available_funds():
            tdao.buy(username, company, volume, ask, algo_id)
            hdao.insert(username, 'buy', company, ask, final, volume, algo_id)

            o = tdao.get_user_stock_value_model(username)

            if o.get_total_stock_values():
                udao1.update_total_stock_values(username,
                                                o.get_total_stock_values())

            if o.get_profit():
                udao1.update_profit(username, o.get_profit())

            up = udao2.get_user_portfolio_model(username)
            udao2.update_available_funds(
                username,
                Decimal(up.get_available_funds()) - final)

    elif trans_type == 'sell':

        o = tdao.get_owned_stock_model(username, company, ask)

        if o.get_volume() >= volume:
            tdao.sell(username, company, volume, ask, algo_id)
            hdao.insert(username, 'sell', company, ask, final, volume, algo_id)

            u = tdao.get_user_stock_value_model(username)
            udao1.update_total_stock_values(username,
                                            u.get_total_stock_values())
            udao1.update_profit(username, u.get_profit())

            portfolio = udao2.get_user_portfolio_model(username)
            udao2.update_available_funds(
                username,
                Decimal(final) + Decimal(portfolio.get_available_funds()))
            udao2.update_total_portfolio(
                username,
                Decimal(final) + Decimal(portfolio.get_available_funds()) +
                Decimal(u.get_total_stock_values()))
 def __init__(self, active_algorithms_list):
     self.moving_list = None
     self.t = Transaction_dao()
     self.c = Company_dao()
     for aam in active_algorithms_list:
         if aam.get_algo_id() == "3":
             if self.moving_list == None:
                 self.moving_list = self.c.get_moving_average()
             if self.moving_list:
                 self.algorithm(aam.get_user())
if form.getvalue("username") != None:
    username = form.getvalue("username")
if form.getvalue("filter") != None:
    portfolio_filter = form.getvalue("filter")

    if portfolio_filter == '1':
        filter_flag = "ALL"
    elif portfolio_filter == '2':
        filter_flag = "ALGOS"
    elif portfolio_filter == '0':
        filter_flag = "USER"
    else:
        filter_flag = portfolio_filter

tdao = Transaction_dao()
u2 = User_stock_value_dao()
u1 = User_portfolio_dao()
cdao = Company_dao()
hdao = History_dao()

data = {}

if filter_flag == "ALL":
    t = hdao.select_all(username)
    l = tdao.get_user_stock_list(username)
elif filter_flag == "ALGOS":
    t = hdao.select_all_algo_trades(username)
    l = tdao.get_all_algo_stock_list(username)
elif filter_flag == "USER":
    t = hdao.select_all_user_trades(username)