User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 427,223 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,283 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 343 | Replies: 7
Reply
Join Date: Jul 2008
Posts: 7
Reputation: StarryEyedSoy is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
StarryEyedSoy StarryEyedSoy is offline Offline
Newbie Poster

Performing calculations from an excel list

  #1  
Jul 20th, 2008
I have a list of the stock prices over 14 years and am required to calculate the daily returns as
Returns = log(today_s price/yesterday_s price)

Here's what my data looks like:

7/18/2008 19.57
7/17/2008 18.08
7/16/2008 14.27


How do I calculate this while reading the data from excel? I have split the string into a list, but don't know how to simultaneously use data from 2 different lines (I am reading one line at a time using a for loop)

Any help would be greatly appreciated, thank you!!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2008
Location: Durham, NC
Posts: 206
Reputation: jlm699 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 31
jlm699's Avatar
jlm699 jlm699 is offline Offline
Posting Whiz in Training

Re: Performing calculations from an excel list

  #2  
Jul 21st, 2008
You can simply store the data of the previous line in a variable, so that on the following iteration you can calculate with the previous line and the current one.
Let's Go Pens!

** Just because I reply to your question does not invite you to PM me. Keep discussions on the thread of topic, I will not answer your questions over PM. **
Reply With Quote  
Join Date: Jul 2008
Posts: 7
Reputation: StarryEyedSoy is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
StarryEyedSoy StarryEyedSoy is offline Offline
Newbie Poster

Re: Performing calculations from an excel list

  #3  
Jul 21st, 2008
import math

for line in inp:
    myList = line.split()
    prev_i = None
    for i in myList:
        if not prev_i:
            continue
        dailyReturns = log(i.myList[1] - prev_i.myList[1])
    myList.append(dailyReturns)
    print myList

thats what I used, but I am getting an error saying that dailyReturns is not defined. )-:
Last edited by vegaseat : Jul 21st, 2008 at 1:00 pm. Reason: corrected code tags to [/code] not [\code]
Reply With Quote  
Join Date: Jul 2008
Location: Durham, NC
Posts: 206
Reputation: jlm699 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 31
jlm699's Avatar
jlm699 jlm699 is offline Offline
Posting Whiz in Training

Re: Performing calculations from an excel list

  #4  
Jul 21st, 2008
Originally Posted by StarryEyedSoy View Post
  1. import math
  2.  
  3. for line in inp:
  4. myList = line.split()
  5. prev_i = None
  6. for i in myList:
  7. if not prev_i:
  8. continue
  9. dailyReturns = log(i.myList[1] - prev_i.myList[1])
  10. myList.append(dailyReturns)
  11. print myList

thats what I used, but I am getting an error saying that dailyReturns is not defined. )-:

You must consider that if myList is empty, dailyReturns will never be initialized (defined) so you will run into this problem. You should initialize dailyReturns to some arbitraty value (like 0) and then check to see if it has changed after the for i in myList loop. That way you only append it if it changed.
Last edited by jlm699 : Jul 21st, 2008 at 12:08 pm.
Let's Go Pens!

** Just because I reply to your question does not invite you to PM me. Keep discussions on the thread of topic, I will not answer your questions over PM. **
Reply With Quote  
Join Date: Dec 2006
Posts: 450
Reputation: woooee is on a distinguished road 
Rep Power: 2
Solved Threads: 62
woooee woooee is offline Offline
Posting Pro in Training

Re: Performing calculations from an excel list

  #5  
Jul 22nd, 2008
This might be easier to understand (not tested). This is not as efficient as storing the value as you perform one additional split() per record instead of storing it, but it may be more straight forward.
import math

data = open(filename, "r").readlines()   ##  reads entire file into list
stop=len(data)
daily_list=[]
for ctr in range(1, stop):     ## start with the second record
    current_list = data[ctr].split()
    prev_list =data[ctr-1].split()
    daily_returns = log(current_list[1] - prev_list[1])
    daily_list.append(daily_returns)
print daily_list
Last edited by woooee : Jul 22nd, 2008 at 6:03 pm.
Reply With Quote  
Join Date: Jul 2008
Posts: 7
Reputation: StarryEyedSoy is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
StarryEyedSoy StarryEyedSoy is offline Offline
Newbie Poster

Re: Performing calculations from an excel list

  #6  
Jul 22nd, 2008
Thank you so much JLM and Wooee (-: I managed to work it out, here is my code:
import math
import time

prevRec = None
for i in inplis:
    rec = i.strip().split(",")
    if rec[0] == 'Date':
        continue
    if prevRec == None:
        prevRec = rec[:]
    else:
        currPrice = float(rec[-1])
        prevPrice = float (prevRec[-1])
        prevRec = rec[:]
        dailyReturns = math.log(currPrice/prevPrice)        
        rec.append(dailyReturns)
        myTime = str(rec[0])
        print myTime
       
    import string           
    newStr = string.join(map(str,rec),",")+'\n'
    outp.write(newStr)
   

outp.close()

This worked well for me. I have another quick question, though. How do I read a date from the file and read into a type of time so that I can convert it to another (strftime?)

The logic I am trying to put to work here is this:
I am trying to read the first element of rec into a string, namely myStr. I need to read this into the time format 2008-7-21 (how do I do this? I've tried so many ways, do I use mktime?) so that once Python stores it as a date I can do time.strftime(%y%m%d,myTime)

Thanks! (-:
Last edited by StarryEyedSoy : Jul 22nd, 2008 at 11:18 pm.
Reply With Quote  
Join Date: Jul 2008
Location: Durham, NC
Posts: 206
Reputation: jlm699 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 31
jlm699's Avatar
jlm699 jlm699 is offline Offline
Posting Whiz in Training

Re: Performing calculations from an excel list

  #7  
Jul 23rd, 2008
You should use strptime to translate the time into a time structure. This works exactly like strftime only backwards.. you supply it with the format that the time will appear in and it will give you a time struct instead of giving it a time struct and telling the function how to format the output!
An example:
  1. >>> import time
  2. >>> my_time = '2008-7-21'
  3. >>> time.strptime( my_time, '%Y-%m-%d' )
  4. (2008, 7, 21, 0, 0, 0, 0, 203, -1)
  5. >>> new_time = time.strptime( my_time, '%Y-%m-%d' )
  6. >>> time.strftime( '%y%m%d', new_time )
  7. '080721'
  8. >>>

Refer to this page for more information on the time formatting strings for strftime and strptime.
Let's Go Pens!

** Just because I reply to your question does not invite you to PM me. Keep discussions on the thread of topic, I will not answer your questions over PM. **
Reply With Quote  
Join Date: Jul 2008
Posts: 7
Reputation: StarryEyedSoy is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
StarryEyedSoy StarryEyedSoy is offline Offline
Newbie Poster

Re: Performing calculations from an excel list

  #8  
Jul 23rd, 2008
Originally Posted by jlm699 View Post
You should use strptime to translate the time into a time structure. This works exactly like strftime only backwards.. you supply it with the format that the time will appear in and it will give you a time struct instead of giving it a time struct and telling the function how to format the output!
An example:
  1. >>> import time
  2. >>> my_time = '2008-7-21'
  3. >>> time.strptime( my_time, '%Y-%m-%d' )
  4. (2008, 7, 21, 0, 0, 0, 0, 203, -1)
  5. >>> new_time = time.strptime( my_time, '%Y-%m-%d' )
  6. >>> time.strftime( '%y%m%d', new_time )
  7. '080721'
  8. >>>

Refer to this page for more information on the time formatting strings for strftime and strptime.



Jlm, thank you so much, that worked like a charm! My code now works perfectly! (-: (-: I really appreciate your taking the time to answer my questions!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Python Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the Python Forum

All times are GMT -4. The time now is 11:30 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC