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 __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())
class fifty_vs_two_hund_mving_avg:

    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())
		

    def algorithm(self, username):
	owned_stocks = self.t.get_user_stock_list(username)
	for moving in self.moving_list:
	    symbol = moving.get_symbol()
	    if owned_stocks:
	        if symbol in owned_stocks:
		    if moving.get_fifty_day_ask() > moving.get_two_hundred_day_ask():
		        transaction(username, 1, symbol, "sell", "3")
		if symbol not in owned_stocks:	
		    if moving.get_fifty_day_ask() < moving.get_two_hundred_day_ask():
		        transaction(username, 1, symbol, "buy", "3")
	    else:
		if moving.get_fifty_day_ask() < moving.get_two_hundred_day_ask():
		        transaction(username, 1, symbol, "buy", "3")
class fifty_vs_two_hund_mving_avg:
    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())

    def algorithm(self, username):
        owned_stocks = self.t.get_user_stock_list(username)
        for moving in self.moving_list:
            symbol = moving.get_symbol()
            if owned_stocks:
                if symbol in owned_stocks:
                    if moving.get_fifty_day_ask(
                    ) > moving.get_two_hundred_day_ask():
                        transaction(username, 1, symbol, "sell", "3")
                if symbol not in owned_stocks:
                    if moving.get_fifty_day_ask(
                    ) < moving.get_two_hundred_day_ask():
                        transaction(username, 1, symbol, "buy", "3")
            else:
                if moving.get_fifty_day_ask() < moving.get_two_hundred_day_ask(
                ):
                    transaction(username, 1, symbol, "buy", "3")
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("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)
    l = tdao.get_only_user_stock_list(username)
else:
    t = hdao.select_algo_trades(username, filter_flag)
sys.path.insert(0, str(LINK_HEADERS.DAO_LINK))
from history_dao import History_dao
from company_dao import Company_dao
from algorithms_dao import Algorithms_dao
from transaction_dao import Transaction_dao

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

form = cgi.FieldStorage()

if form.getvalue("username") != None:
    username = form.getvalue("username")
#username='******'

hdao = History_dao()
cdao = Company_dao()
adao = Algorithms_dao()
tdao = Transaction_dao()
result = []
profit = 0
stock_model_dict = {}
stock_ask_dict = {}
volume_dict = {}
distinct_stocks = hdao.get_distinct_traded_stocks(username)
algorithm_details_list = hdao.get_algorithm_buy_sell_volume(
    username
)  #len(result) =  number of algo_ids list of algorithm details models
symbols_string = ""

if distinct_stocks:
    for i in range(len(distinct_stocks)):
#!/usr/bin/python

import cgi, datetime, sys, LINK_HEADERS
import simplejson as json
sys.path.insert(0, str(LINK_HEADERS.DAO_LINK))
sys.path.insert(0, str(LINK_HEADERS.MODELS_LINK))
sys.path.insert(0, str(LINK_HEADERS.SCRIPTS_LINK))
from company_dao import Company_dao
import quick_sort_companyinfo

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

cdao = Company_dao()


def main():

    result = cdao.get_all_companies_percentchange()
    quick_sort_companyinfo.iterative(result, 0,
                                     len(result) - 1, "PercentChange")
    #result.sort(key=lambda x: x.get_percent_change(), reverse=True)

    json_object = []

    worst_changes = []
    for i in range(0, 25):  #gets top 5 worst changes
        worst_changes.append({
            "symbol": result[i].get_symbol(),
            "PercentChange": result[i].get_percent_change()
        })
    json_object.append(worst_changes)
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)
    l = tdao.get_only_user_stock_list(username)
else:
    t = hdao.select_algo_trades(username, filter_flag)
Example #13
0
#!/usr/bin/python

import cgi, datetime, LINK_HEADERS, sys
import simplejson as json
sys.path.insert(0, str(LINK_HEADERS.DAO_LINK))
from company_dao import Company_dao

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


cdao=Company_dao()
company_stock_list=[]
company_stock_symbols=cdao.get_all_companies_stock()
for company in company_stock_symbols:
    company_stock_list.append(company.get_symbol())
print json.dumps(company_stock_list)
sys.path.insert(0, str(LINK_HEADERS.DAO_LINK))
from history_dao import History_dao
from company_dao import Company_dao
from algorithms_dao import Algorithms_dao
from transaction_dao import Transaction_dao

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

form = cgi.FieldStorage()

if form.getvalue("username") != None:
    username = form.getvalue("username")
#username='******'

hdao = History_dao()
cdao = Company_dao()
adao = Algorithms_dao()
tdao = Transaction_dao() 
result=[]
profit=0
stock_model_dict={}
stock_ask_dict={}    
volume_dict={}
distinct_stocks=hdao.get_distinct_traded_stocks(username)
algorithm_details_list = hdao.get_algorithm_buy_sell_volume(username) #len(result) =  number of algo_ids list of algorithm details models
symbols_string=""

if distinct_stocks:
    for i in range(len(distinct_stocks)):
        symbols_string+="'" + distinct_stocks[i].get_name() + "',"
    symbols_string=symbols_string[:-1]
Example #15
0
#!/usr/bin/python

import cgi, datetime, LINK_HEADERS, sys
import simplejson as json
sys.path.insert(0, str(LINK_HEADERS.DAO_LINK))
from company_dao import Company_dao

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

cdao = Company_dao()
company_stock_list = []
company_stock_symbols = cdao.get_all_companies_stock()
for company in company_stock_symbols:
    company_stock_list.append(company.get_symbol())
print json.dumps(company_stock_list)
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("")