Пример #1
0
def link_validity_port(port_link, p_id):
    # format : "port_name+?pid=id"

    port_name = ""
    port_id = ""
    output = {'name': port_name, 'id': port_id}

    # grabs the name of the portfolio
    # if not exists, return empty
    regex = re.search(r'^(.+)\+$', port_link)

    if regex:
        port_name = regex.group(1)
    else:
        return output

    # grab the information of the portfolio from database
    # compare it with the existing database
    # if the information matches, then return the name and id of the portfolio
    db_controller = Controller()
    get_data = db_controller.get_port_info(p_id)

    if get_data['name'] == port_name:
        output = {'name': port_name, 'id': p_id}

    return output
Пример #2
0
def check_register_account(new_id, new_password):
    # get data from database based on id (only take id)
    lower_id = new_id.lower()

    db_controller = Controller()
    get_data = db_controller.not_exist_username(lower_id)

    if not get_data:
        # return "name exists"
        return "Username already exists"

    # name too short
    if len(new_id) < 4:
        # return "name must be at least 4 characters long"
        return "Username must be at least 4 characters long"

    # check id name contains symbol or spaces
    check_id = re.search(r'([^A-Z^a-z^0-9^\_\s])+', new_id)

    if check_id:
        # return "name contains symbol"
        return "Username must not contains symbols"

    # check password length
    if len(new_password) < 6:
        # return "password is less than 6 characters"
        return "Password must be at least 6 characters long"

    return "True"
Пример #3
0
def get_user_portfolio(id):
    # get all portfolio from a user

    db_controller = Controller()
    get_data = db_controller.get_PortIDs(id)

    return get_data
Пример #4
0
def sign_up():

    message = ""
    if 'message' in request.args:
        if request.args['message'] != "True":
            message = request.args['message']

    if request.method == "POST":
        input_id = request.form['username']
        input_pw = request.form['password']
        input_cfm = request.form['confirm']

        # Make sure that password and confirm password fields are matching
        if input_pw != input_cfm:
            return render_template("signuppage.html",
                                   message="Password does not match")

        # Checks if a user can be registered
        check_validity = check_register_account(input_id, input_pw)

        if check_validity == "True":
            db_controller = Controller()
            get_data = db_controller.register_user(input_id, input_pw)

            return redirect(url_for("index", message="Sign up successful!"))

        else:
            # Error messages
            return render_template("signuppage.html", message=check_validity)

    return render_template("signuppage.html", message=message)
Пример #5
0
def del_symbol_var(long_link):
    # format : (symbol_name)+markt=(market)

    symbol_name = ""
    market = ""
    output = {'symbol': symbol_name, 'market': market}

    # Grab the information by the format
    regex = re.search(r'^(.+)\+markt\=(.+)$', long_link)

    # If not exists, return empty
    if regex:
        symbol_name = regex.group(1)
        market = regex.group(2)
    else:
        return output

    # From the database, match the information
    # If symbol exists in the database and by the market it is assigned to, return the name and the market
    # Else, empty
    db_controller = Controller()
    get_data = db_controller.get_symbol_by_market(market)

    if symbol_name in get_data:
        output = {'symbol': symbol_name, 'market': market}

    return output
Пример #6
0
def create_new_portfolio(id, port_name):

    # Check if name is empty
    check_port_name = re.search('^\s*$', port_name)

    # If empty, return "Port name cant be empty"
    if check_port_name:
        return "Name for portfolio must not be empty"

    # If name contains symbol
    for each_char in port_name:
        if (ord(each_char) >= 128):
            return "Portfolio must not contain symbols"
        if (not each_char.isdigit() and not each_char.isalpha()
                and each_char != '_' and each_char != ' '):
            return "Portfolio must not contain symbols"

    # Get the time of creation
    calltime = datetime.datetime.now()
    today = str(calltime.hour) + ":" + str(calltime.minute) + " " + str(
        calltime.day) + "/" + str(calltime.month) + "/" + str(calltime.year)

    # Add new portfolio into the database
    # Return true if portfolio can be created,
    # False otherwise
    db_controller = Controller()
    valid = db_controller.add_portfolio(id, port_name, today, [])

    return valid
Пример #7
0
def del_port_var(long_link):
    # format : "port_name+id=id"

    port_name = ""
    port_id = ""
    output = {'name': port_name, 'id': port_id}

    # Grab the information by the format
    regex = re.search(r'^(.+)\+id\=([0-9]+)$', long_link)

    # If not exists, return empty
    if regex:
        port_name = regex.group(1)
        port_id = regex.group(2)
    else:
        return output

    # Grab the information from the database
    # If data matches, return the name and id
    # Else empty
    db_controller = Controller()
    get_data = db_controller.get_port_info(port_id)

    if get_data['name'] == port_name:
        output = {'name': port_name, 'id': port_id}

    return output
Пример #8
0
def port_user_validity(id, port_id):

    db_controller = Controller()
    get_data = db_controller.get_PortIDs(id)

    if port_id in get_data:
        return True

    return False
Пример #9
0
def delete_stock(port_id, symbol, market):

    # Get portfolio's information
    db_controller = Controller()
    get_data = db_controller.get_port_info(port_id)

    # Make sure at least a stock exists
    # Make sure the specific stock exists
    if len(get_data['stock']) > 0:
        for each_stock in get_data['stock']:
            if each_stock[0] == symbol and each_stock[1] == market:
                db_controller.delete_symbol(port_id, symbol, market)
Пример #10
0
def link_validity_stock(stock_name):
    # format : name

    # Check the database for the specific stock
    # If the stock exists, return True
    # Otherwise False
    db_controller = Controller()
    get_data = db_controller.get_symbol_details(stock_name)

    if get_data['name'] == stock_name:
        return True

    return False
Пример #11
0
def load_user(id):

    # check if user exists in database (security)
    # If yes, returns the user
    # Else, empty
    lower_id = id.lower()
    db_controller = Controller()
    data = db_controller.not_exist_username(lower_id)

    if not data:
        user = users(lower_id, 0)
    else:
        user = users("", 0)
    return user
Пример #12
0
def check_password(id, password):
    # get data from database based on id

    lower_id = id.lower()

    db_controller = Controller()
    get_data = db_controller.get_password(lower_id)

    if get_data == password and get_data != "":
        # set currentUser variable as current user, and login Flask
        currentUser.set_id(lower_id)
        login_user(currentUser)
        return True

    return False
Пример #13
0
def get_all_stock():

    # initialize the dictionary (hard coded, since we will only be using 2 markets)
    db_controller = Controller()
    avail_market = ['nasdaq', 'nyse']
    data = {'all': [], 'nasdaq': [], 'nyse': []}

    # For each market used, get all symbols related to it
    for each_market in avail_market:
        get_data = db_controller.get_symbol_by_market(each_market)
        data[each_market] = get_data

    # Combine the lists
    union_list12 = union_list(data['nasdaq'], data['nyse'])

    # Add them into all
    data['all'] = union_list12

    return data
Пример #14
0
def delete_port(port_id, port_name):

    # Check the database for information of the portfolio
    db_controller = Controller()
    get_data = db_controller.get_port_info(port_id)

    # Make sure the portfolio exists
    if get_data['name'] != "" and get_data['name'] == port_name:
        for each_stock in get_data['stock']:
            db_controller.delete_symbol(port_id, each_stock[0], each_stock[1])

        # Delete the portfolio
        db_controller.delete_portfolio(port_id)
Пример #15
0
def get_portfolio_details(port_id):

    db_controller = Controller()
    get_data = db_controller.get_port_info(port_id)

    return get_data
Пример #16
0
def get_real_time_stock_for_a_port(port_id, interval):

    # Get details of the portfolio
    get_data = get_portfolio_details(port_id)
    db_controller = Controller()
    final_output = []

    # get the real time data of each stock
    # if the API returns an error, try again
    # 5 tries before returning error
    for each_stock in get_data['stock']:
        stock_data = db_controller.get_symbol_details(each_stock[0])

        retry = 0
        dict_output = {
            'name': each_stock[0],
            'market': each_stock[1],
            'open': '0.0000',
            'high': '0.0000',
            'low': '0.0000',
            'close': '0.0000',
            'volume': '0.0000',
            'time': 'Error',
            'change': 0.00,
            'percent_change': 0.00,
            'desc': 'Error',
            'sector': 'Error'
        }

        while retry < 5:
            try:
                real_time = get_real_time_stock(each_stock[0], interval)
                dict_output = {
                    'name':
                    each_stock[0],
                    'market':
                    each_stock[1],
                    'open':
                    real_time['open'],
                    'high':
                    real_time['high'],
                    'low':
                    real_time['low'],
                    'close':
                    real_time['close'],
                    'volume':
                    real_time['volume'],
                    'time':
                    real_time['Last_Refreshed'] + " " + real_time['Time_Zone'],
                    'change':
                    float(real_time['change']),
                    'percent_change':
                    float(real_time['percent_change']),
                    'desc':
                    stock_data['desc'],
                    'sector':
                    real_time['sector']
                }
                break
            except:
                retry += 1

        final_output.append(dict_output)

    return final_output
Пример #17
0
def get_real_time_stock(symbol, interval):

    # Get information if the real time data of the symbol has been previously saved
    # If it is new, 'check_database' will be empty (init value would be 0.0000 and error, which can be used as error messages)
    db_controller = Controller()
    if_already_exist = db_controller.check_real_time_data(symbol)
    check_database = db_controller.get_real_time_data(symbol)

    # Get the details of the symbol and the current time
    stock_data = db_controller.get_symbol_details(symbol)
    time_now = datetime.datetime.now()

    # update interval
    # 300 = 5 minutes
    update = 300

    # If the symbol has yet not saved previously
    if not if_already_exist:
        # INSERT INTO DATABASE
        get_data = get_realtimeData(symbol, interval)

        # Set the output
        # If the sector value is missing (which is used for error messages), replace it with the original sector (from database)
        try:
            output = {
                'name': get_data['Symbol'],
                'Last_Refreshed': get_data['Last Refreshed'],
                'Time_Zone': get_data['Time Zone'],
                'open': get_data['open'],
                'high': get_data['high'],
                'low': get_data['low'],
                'close': get_data['close'],
                'volume': get_data['volume'],
                'change': float(get_data['change']),
                'percent_change': float(get_data['percent_change']),
                'desc': stock_data['desc'],
                'sector': get_data['sector']
            }
        except:
            output = {
                'name': get_data['Symbol'],
                'Last_Refreshed': get_data['Last Refreshed'],
                'Time_Zone': get_data['Time Zone'],
                'open': get_data['open'],
                'high': get_data['high'],
                'low': get_data['low'],
                'close': get_data['close'],
                'volume': get_data['volume'],
                'change': float(get_data['change']),
                'percent_change': float(get_data['percent_change']),
                'desc': stock_data['desc'],
                'sector': stock_data['sector']
            }

        # If the output is Error (due to API error)
        # Don't add the value to the database
        if output['Last_Refreshed'] != 'Error':
            db_controller.add_real_time_data(
                get_data['Symbol'], get_data['Last Refreshed'],
                get_data['Time Zone'], get_data['open'], get_data['high'],
                get_data['low'], get_data['close'], get_data['volume'],
                get_data['change'], get_data['percent_change'],
                stock_data['desc'], time_now)

    else:
        # If the symbol real time data has been previously added in the database
        # check the time of when it was taken
        time_last = datetime.datetime.strptime(check_database['time_taken'],
                                               "%Y-%m-%d %H:%M:%S.%f")

        # get the time difference
        time_diff = time_now - time_last
        total_diff = time_diff.total_seconds()

        # If the value stored in database is not error and time difference is less that the update time (default 5 minutes)
        if total_diff <= update and check_database['Last Refreshed'] != 'Error':
            # return the values stored in the database
            output = {
                'name': check_database['Symbol'],
                'Last_Refreshed': check_database['Last Refreshed'],
                'Time_Zone': check_database['Time Zone'],
                'open': check_database['open'],
                'high': check_database['high'],
                'low': check_database['low'],
                'close': check_database['close'],
                'volume': check_database['volume'],
                'change': float(check_database['change']),
                'percent_change': float(check_database['percent_change']),
                'desc': check_database['desc'],
                'sector': stock_data['sector']
            }

        else:
            # UPDATE DATABASE
            get_data = get_realtimeData(symbol, interval)

            # Set the output
            # If the sector value is missing (which is used for error messages), replace it with the original sector (from database)
            try:
                output = {
                    'name': get_data['Symbol'],
                    'Last_Refreshed': get_data['Last Refreshed'],
                    'Time_Zone': get_data['Time Zone'],
                    'open': get_data['open'],
                    'high': get_data['high'],
                    'low': get_data['low'],
                    'close': get_data['close'],
                    'volume': get_data['volume'],
                    'change': float(get_data['change']),
                    'percent_change': float(get_data['percent_change']),
                    'desc': stock_data['desc'],
                    'sector': get_data['sector']
                }
            except:
                output = {
                    'name': get_data['Symbol'],
                    'Last_Refreshed': get_data['Last Refreshed'],
                    'Time_Zone': get_data['Time Zone'],
                    'open': get_data['open'],
                    'high': get_data['high'],
                    'low': get_data['low'],
                    'close': get_data['close'],
                    'volume': get_data['volume'],
                    'change': float(get_data['change']),
                    'percent_change': float(get_data['percent_change']),
                    'desc': stock_data['desc'],
                    'sector': stock_data['sector']
                }

            # If the value gathered does not contain error (API works fine), update the symbol from the database
            if output['Last_Refreshed'] != 'Error':
                db_controller.update_real_time_data(
                    get_data['Symbol'], get_data['Last Refreshed'],
                    get_data['Time Zone'], get_data['open'], get_data['high'],
                    get_data['low'], get_data['close'], get_data['volume'],
                    get_data['change'], get_data['percent_change'],
                    stock_data['desc'], time_now)

    return output
Пример #18
0
def symbol_into_port(port_id, symbol, market):

    # Grab information from a port for validity check
    db_controller = Controller()
    get_data = db_controller.get_port_info(port_id)
    valid = []

    # If 'ALL' is chosen when adding symbol
    if market.lower() == "all":
        # Available market
        all_market = ['nasdaq', 'nyse']
        avail_market = []

        # Find any market that offers the symbol in check
        for each_market in all_market:
            check_stock_in_market = db_controller.get_symbol_by_market(
                each_market)
            if symbol in check_stock_in_market:
                avail_market.append(each_market)

        # If no market offers this symbol, returns an error message
        if len(avail_market) > 0:
            pass
        else:
            return symbol + " does not exist"

    else:
        # Check if the market exists
        # If true, use it as a combination to be added
        # Else, return an error message
        check_market_valid = db_controller.get_symbol_by_market(market)
        if len(check_market_valid) > 0:
            valid = [symbol, market]
        else:
            return "Market does not exist"

    # Make sure port exists before adding
    if get_data['name'] != "":

        # If market is "all"
        if market.lower() == "all":

            # Try for each (symbol, market) combination and check if the portfolio contained these combination
            # Use the combination if it is not contained in the portfolio
            for each_test in avail_market:
                check_valid = [symbol, each_test]
                if check_valid not in get_data['stock']:
                    market = each_test
                    valid = [symbol, market]
                    break

        # if the stock to be added does not exist in the portfolio and not empty
        if valid not in get_data['stock'] and market.lower() != "all" and len(
                valid) > 0:

            # Make sure the symbol exists in the market
            # If so, add it to the portfolio
            check_stock_in_market = db_controller.get_symbol_by_market(market)
            if symbol in check_stock_in_market:
                db_controller.add_symbol(port_id, symbol, market)
                return True

    return symbol + " already exists"