Skip to content

Instantly share code, notes, and snippets.

@OblateSpheroid
Last active January 14, 2020 20:04
Show Gist options
  • Select an option

  • Save OblateSpheroid/2a18a7261d6e6335c0c4d8052ac13173 to your computer and use it in GitHub Desktop.

Select an option

Save OblateSpheroid/2a18a7261d6e6335c0c4d8052ac13173 to your computer and use it in GitHub Desktop.

Revisions

  1. john s-g revised this gist Jan 14, 2020. 1 changed file with 41 additions and 35 deletions.
    76 changes: 41 additions & 35 deletions parse_RH_accounts.py
    100644 → 100755
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    #!/usr/bin/env python

    '''RobinHood gives account info as string. This converts string into DataFrame'''

    import pandas as pd
    @@ -14,22 +16,20 @@ def readlines(filename):
    def read_account(filename=None, input=None):
    '''Read as string from file or from string variable'''
    if filename != None:
    l1 = readlines(filename)
    ls_raw = readlines(filename)
    else:
    l1 = input.splitlines()
    l2 = [s.replace('$','') for s in l1]
    for i in range(0,len(l2)):
    ls_raw = input.splitlines()
    ls_clean = [s.replace('$','').replace(',','') for s in ls_raw]
    for i in range(0,len(ls_clean)):
    try: # change everything that looks like a number into a number
    if type(l2[i]) == str:
    l2[i] = l2[i].replace('$','').replace(',','')
    l2[i] = float(l2[i])
    ls_clean[i] = float(ls_clean[i])
    except:
    pass
    lol = []
    lol = [] # list of lists
    start = 0
    l_len = int(len(l2)/7)
    l_len = int(len(ls_clean)/7) # number of securities
    for i in range(0, l_len): # group by 7
    lol.append(l2[start:start+7])
    lol.append(ls_clean[start:start+7])
    start += 7
    df = pd.DataFrame(lol, columns=['Name', 'Symbol', 'Shares', 'Price', 'Average Cost', 'Total Return', 'Equity'])
    df['Perc Return'] = df['Price']/df['Average Cost'] - 1
    @@ -39,32 +39,38 @@ def read_account(filename=None, input=None):
    df.loc[i, 'Total Return'] = df.loc[i, 'Total Return'] * -1
    df.index = df.Name
    df.drop(['Name'], axis=1, inplace=True)
    df = df.astype({'Shares': 'int32'})
    return df

    # Example:
    inputs = '''Fake Company
    FAKE
    100
    $10.83
    $8.32
    $250.96
    $1,082.50
    Imaginary Firm
    IMAG
    50
    $43.68
    $38.70
    $249.00
    $2,184.00
    Made-up Partners
    MUP
    7
    $33.60
    $31.17
    $17.00
    $235.20'''

    # df = read_account(input=inputs)
    df = read_account('accounts.txt')
    print(df)
    if False: # Example:
    inputs = '''Fake Company
    FAKE
    100
    $10.83
    $8.32
    $250.96
    $1,082.50
    Imaginary Firm
    IMAG
    50
    $43.68
    $38.70
    $249.00
    $2,184.00
    Made-up Partners
    MUP
    7
    $33.60
    $31.17
    $17.00
    $235.20'''

    df = read_account(input=inputs)


    if __name__ == '__main__':
    # df = read_account(input=inputs)
    df = read_account('accounts.txt')
    print(df)

  2. john s-g revised this gist Sep 24, 2019. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion parse_RH_accounts.py
    Original file line number Diff line number Diff line change
    @@ -20,6 +20,8 @@ def read_account(filename=None, input=None):
    l2 = [s.replace('$','') for s in l1]
    for i in range(0,len(l2)):
    try: # change everything that looks like a number into a number
    if type(l2[i]) == str:
    l2[i] = l2[i].replace('$','').replace(',','')
    l2[i] = float(l2[i])
    except:
    pass
    @@ -63,5 +65,6 @@ def read_account(filename=None, input=None):
    $235.20'''

    # df = read_account(input=inputs)
    print(read_account('accounts.txt'))
    df = read_account('accounts.txt')
    print(df)

  3. john s-g revised this gist Aug 7, 2019. 1 changed file with 7 additions and 2 deletions.
    9 changes: 7 additions & 2 deletions parse_RH_accounts.py
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,10 @@

    import pandas as pd

    pd.set_option('display.max_columns',10)
    pd.set_option('display.max_rows',200)
    pd.set_option('display.expand_frame_repr',False)

    def readlines(filename):
    with open(filename) as f:
    ls = [l.strip() for l in f.readlines()]
    @@ -10,7 +14,7 @@ def readlines(filename):
    def read_account(filename=None, input=None):
    '''Read as string from file or from string variable'''
    if filename != None:
    l1 = readlines('accounts.txt')
    l1 = readlines(filename)
    else:
    l1 = input.splitlines()
    l2 = [s.replace('$','') for s in l1]
    @@ -58,5 +62,6 @@ def read_account(filename=None, input=None):
    $17.00
    $235.20'''

    df = read_account(input=inputs)
    # df = read_account(input=inputs)
    print(read_account('accounts.txt'))

  4. OblateSpheroid created this gist Jun 12, 2019.
    62 changes: 62 additions & 0 deletions parse_RH_accounts.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    '''RobinHood gives account info as string. This converts string into DataFrame'''

    import pandas as pd

    def readlines(filename):
    with open(filename) as f:
    ls = [l.strip() for l in f.readlines()]
    return ls

    def read_account(filename=None, input=None):
    '''Read as string from file or from string variable'''
    if filename != None:
    l1 = readlines('accounts.txt')
    else:
    l1 = input.splitlines()
    l2 = [s.replace('$','') for s in l1]
    for i in range(0,len(l2)):
    try: # change everything that looks like a number into a number
    l2[i] = float(l2[i])
    except:
    pass
    lol = []
    start = 0
    l_len = int(len(l2)/7)
    for i in range(0, l_len): # group by 7
    lol.append(l2[start:start+7])
    start += 7
    df = pd.DataFrame(lol, columns=['Name', 'Symbol', 'Shares', 'Price', 'Average Cost', 'Total Return', 'Equity'])
    df['Perc Return'] = df['Price']/df['Average Cost'] - 1
    df.sort_values(by=['Perc Return'], ascending=False, inplace=True)
    for i in range(0,len(df)):
    if df.loc[i, 'Perc Return'] < 0:
    df.loc[i, 'Total Return'] = df.loc[i, 'Total Return'] * -1
    df.index = df.Name
    df.drop(['Name'], axis=1, inplace=True)
    return df

    # Example:
    inputs = '''Fake Company
    FAKE
    100
    $10.83
    $8.32
    $250.96
    $1,082.50
    Imaginary Firm
    IMAG
    50
    $43.68
    $38.70
    $249.00
    $2,184.00
    Made-up Partners
    MUP
    7
    $33.60
    $31.17
    $17.00
    $235.20'''

    df = read_account(input=inputs)