Archive

Archive for July 19, 2020

Python: Shares Speculation System – Part 4

July 19, 2020 3 comments


Learning : Python, DataBase, SQL, SQlite3
Subject: Plan, Design and Build a Shares Speculation System

Project Card:
Project Name: Shares Speculation System
By: Ali
Date: 2.7.2020
Version: V01-2.7.2020

Last time we worked on Budgets Managment for our system, now before we start Buying and Selling shares we need to create list of shares name so we can select from it, we have a table called shares_name this Table have the following fields :
s_id: as a PRIMARY KEY.
full_name: To hold the full name of the share.
abb_name: To hold the Abbreviation of a share name.
The Share Managment section will have four main Functions to Add, Edit, Delete and Show shares. .. Let’s start with it’s Main Menu ..



First Function we will work on will be to Add New Share to the system, we will call it def add_share(): , we will ask the user to input the Share Full Name and its Abbreviation. With Abbreviation we will apply the .upper() function to convert the user input to Upper-case, and for the Full Name we will will apply this code [“ “.join([word.capitalize() for word in user-input] to upper case each first character of the Name. The we will check if the Share Name exist in the DataBase If not we will add it.

 # Add New Share
def add_share() :
        
    os.system('clear')
    print('\n   ====== Add New Share ======\n\n')
    while True :
            
        abb_name = input("   Enter the Abbreviation Name of the Share.. > ").upper() 
        full_name = " ".join([word.capitalize() for word in input('     Enter the Full Name of the Share.. > ').split(" ")])

        c.execute ("select * from shares_name where full_name ='{}' or abb_name = '{}'".format(full_name, abb_name)) 
        share_exist = c.fetchone ()
        if share_exist[0] > 0 :
            print('\n   It seams that the Share Exist in the Database, Name or Abbreviation CAN NOT be Duplicated.')
            print('   Try another Name. ')
        else :      
            c.execute ("INSERT INTO shares_name (full_name , abb_name)  VALUES(:full_name , :abb_name)",{"full_name":full_name, "abb_name":abb_name}) 
            db_conn.commit()
            
        if input ('\n   Do you want to Enter another Share Name?.  [Y,N]. >  ')  not in ['Y','y']: 
            input ('\n    .... Press any key to  Exit.  ')
            return



After adding shares we want to see them and to make sure if every things are fine, so we will write the show_share function, and it will take one argument as inside with default value = ‘Yes’, if we call the function from inside another function the value will be Yes, otherwise it will be ‘No’. Here is the code ..


Next is Editing Share Name, in this function we will call the show_share (‘Yes’) so the user will select the ID of the Share Name to be Edit, then with each attribute we will ask the user ether to Enter the new Edit of the attribute or to press Enter to keep the exist variable. .. Here is a copy of the code ..


Last Function in this Artical is Delete def delete_share() : again we will call the Shoe function to display all the Shares Name on the screen and let the user to select the one to be Deleted by Entering it’s ID.

 # Delete Function.    
def delete_share() :
    os.system('clear')
    print('\n   ====== Delete a Share  ======\n\n')
    
    print('   Here is a list of Shares we have in the System .. \n')
    show_share('Yes')
    
    del_share = input('\n\n   Enter the ID for the Share Name you want to Delete. [Q to Exit] >  ') 

    if del_share in ['q', 'Q'] :
        input ('\n\n    .... Press any key to  Exit.')
        return    
    elif del_share.isnumeric() :
        if (input ('   Are you SURE you want to DELETE Share ID {} ? [Y,N] >  '.format(del_share))) in ['y','Y'] :   
            c.execute ("delete from shares_name where s_id ={}".format(int(del_share))) 
            db_conn.commit()
            input ('\n    ....One Record DELETED. Press any key to  Exit.')
            return
        else :
            input ('\n    ....Share will NOT be Deleted. Press any key to  Exit.')
            return
    else :
        input ('\n    ....Invalid Inpout. Press any key to  Exit.')
        return



Now we can create or Enter a Budget and Enter the Shares Name we have in our Stock Market and we are ready to write the functions to make the transactions.


Coming Up: In Next part we will write the Function to submit/Save Buying and Selling Transactions to our system.


[NOTES]
1. Part-1 has no code file.
2. We are applying some basic Validations on some part of the code, and assuming that the user will not enter a messy data.


:: Shares Speculation System ::

Part 1 Part 2 Part 3 Part 4
Part5



To Download my Python code (.py) files Click-Here




Follow me on Twitter..




By: Ali Radwani