Search code examples
pythonfunctionvariablesfinance

Attempting to change variable values for different securities when same function is run


list below defines securities to be evaluated in below PVGO screen

Securities = [ 'AAPL' , 'TSLA' , 'AMZN' ]
for 'AAPL':
    PV = 120
    EPS = 8.31
    r = 0.0519
    K=.86
    D=.88
for 'TSLA':
    PV = 244.73
    EPS = -5
    r = 0.2
    K=.83
    D=.90
for 'AMZN':
    PV = 808.33
    EPS = 4.46
    r = 0.087
    K=.86
    D=.90

the below function defines how the basic screener will evaluate each security

def PVGOscreen( ):
      PVGO = PV - EPS/r
      ratio = PVGO/PV
    print (ratio)
    if (ratio < 0.6) :
        print("stable security")
    else:
         print("volatile security")
    if (K < 0.2) and (K > D) :
         print ("Technical Buy")
    elif (K > 0.8) and (K < D) :
        print ("Technical Sell")
    else:
        print ("Not Actionable")

print (PVGOscreen( Securities ))

The output I am getting is a below, and is only the expected output for AAPL.

0.829059829059829
volatile security
Technical Buy

How do I get the output for TSLA and AMZN as well; which is to ask, how do I change the variable value for each security in the list of Securities and have the PVGOscreen output change for each of those securities, instead of only getting AAPL?


Solution

  • First, as several people have mentioned in the comments, the syntax for 'AAPL': is not valid Python syntax.

    Second, there are much better ways in Python to keep track of the data you have above. Using built-in Python, a nested dict object works nicely:

    securities = ['AAPL', 'TSLA' , 'AMZN']
    
    # Initialize "empty" nested `dict`s to better store financial data
    securities_data = {sec: {} for sec in securities}
    
    for sec in securities_data:
        if sec == 'AAPL':
            PV = 120
            EPS = 8.31
            r = 0.0519
            K=.86
            D=.88
        elif sec == 'TSLA':
            PV = 244.73
            EPS = -5
            r = 0.2
            K=.83
            D=.90
        elif sec == 'AMZN':
            PV = 808.33
            EPS = 4.46
            r = 0.087
            K=.86
            D=.90
        else:
            PV = None
            EPS = None
            r = None
            K = None
            D = None
    
        # Nested dictionary to better keep track of each security's data
        securities_data[sec]['PV'] = PV
        securities_data[sec]['EPS'] = EPS
        securities_data[sec]['r'] = r    
        securities_data[sec]['K'] = K
        securities_data[sec]['D'] = D        
    

    There are much better ways of doing this work, but I hope this example shows how easily your code can be adapted to make your life a bit easier from a programmer's perspective.

    pandas is another package that would be very well-suited for this type of problem, but that doesn't ship with base Python.

    Third, passing the name of a ticker to your PVGOscreen function will make your code more flexible, because you can use the nested dictionary from above to dynamically reference different values for each ticket, e.g. PV and EPS:

    def PVGOscreen(sec):
        PV = securities_data[sec]['PV']
        EPS = securities_data[sec]['EPS']
        r = securities_data[sec]['r']
    
        PVGO = PV - EPS/r
        ratio = PVGO/PV
    
        print(ratio)
    
        if ratio < 0.6:
            print("stable security")
        else:
             print("volatile security")
    
        if K < 0.2 and K > D:
             print("Technical Buy")
        elif K > 0.8 and K < D:
            print("Technical Sell")
        else:
            print("Not Actionable")
    

    Testing your function out now:

    >>> PVGOscreen('AAPL')
    -0.33429672447013487
    stable security
    Technical Sell
    

    And if you want the output for each of your tickers, you can try this:

    >>> for s in securities:
    ...     print(s)
    ...     PVGOscreen(s)
    ... 
    AAPL
    -0.33429672447013487
    stable security
    Technical Sell
    TSLA
    1.1021533935357333
    volatile security
    Technical Sell
    AMZN
    0.9365799020003068
    volatile security
    Technical Sell