Example #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;
Example #2
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
Example #3
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 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()))
# OWNED STOCKS
sector_dao = Sector_info_dao()
data['sector_volume'] = {}
if l:

    data['owned_stocks'] = {}
    #total_stock_value = 0

    #    for i in range(len(l)):
    #        c = cdao.get_company_model(l[i])

    c = cdao.get_list_of_company_models(l)
    if c:
        for i in range(len(c)):
            try:
                o = tdao.get_owned_stock_model(username, c[i].get_symbol(),
                                               c[i].get_ask())
            except:
                continue

            data['owned_stocks'][i] = {}
            data['owned_stocks'][i]['name'] = c[i].get_name()
            data['owned_stocks'][i]['stock'] = c[i].get_symbol()
            data['owned_stocks'][i]['current_shares'] = o.get_volume()
            data['owned_stocks'][i]['current_price'] = c[i].get_ask()
            data['owned_stocks'][i]['total_worth'] = o.get_total_worth()
            data['owned_stocks'][i]['profit'] = o.get_profit()
            #total_stock_value = Decimal(total_stock_value) + Decimal(o.get_total_worth())

            #--------Code for chart - sector_volume:---
            volume = o.get_volume()
            symbol = c[i].get_symbol()
# OWNED STOCKS
sector_dao=Sector_info_dao()
data['sector_volume']={}
if l:
    
    data['owned_stocks']={}
    #total_stock_value = 0
    
#    for i in range(len(l)):
#        c = cdao.get_company_model(l[i])
    
    c = cdao.get_list_of_company_models(l)
    if c:
        for i in range(len(c)):
            try:
                o = tdao.get_owned_stock_model(username, c[i].get_symbol(), c[i].get_ask()) 
            except:
                continue
            
            data['owned_stocks'][i]={}
            data['owned_stocks'][i]['stock'] = c[i].get_symbol()
            data['owned_stocks'][i]['current_shares'] = o.get_volume()
            data['owned_stocks'][i]['current_price'] = c[i].get_ask()
            data['owned_stocks'][i]['total_worth'] = o.get_total_worth()
            data['owned_stocks'][i]['profit'] = o.get_profit()
            #total_stock_value = Decimal(total_stock_value) + Decimal(o.get_total_worth())

            #--------Code for chart - sector_volume:---
            volume=o.get_volume()
            symbol=c[i].get_symbol()
            try:
import cgi, LINK_HEADERS, sys
import simplejson as json
sys.path.insert(0, str(LINK_HEADERS.MODELS_LINK))
sys.path.insert(0, str(LINK_HEADERS.DAO_LINK))
from transaction_dao import Transaction_dao
from company_dao import Company_dao

print "Content-Type: text/html\r\n\r\n"

form=cgi.FieldStorage()

if form.getvalue("user_name") != None:
    username=form.getvalue("user_name")

#test
#username='******'
pie_chart_list=[]
tdao=Transaction_dao()
user_stock_list=tdao.get_user_stock_list(username)
if user_stock_list != None:
    for stock in user_stock_list:
        cdao=Company_dao()
        company_model=cdao.get_company_model(stock)
        price=company_model.get_ask()
        owned_stock_model=tdao.get_owned_stock_model(username, stock, price)
        total_worth=owned_stock_model.get_total_worth()
        pie_chart_list.append([stock,float(total_worth)])   
    print json.dumps(pie_chart_list)
else:
    print json.dumps("")