•
•
•
•
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
![]() |
•
•
Join Date: Jul 2008
Posts: 7
Reputation:
Rep Power: 0
Solved Threads: 0
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!!
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!!
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. **
** 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. **
•
•
Join Date: Jul 2008
Posts: 7
Reputation:
Rep Power: 0
Solved Threads: 0
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 myListthats 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]
•
•
•
•
python Syntax (Toggle Plain Text)
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. )-:
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. **
** 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. **
•
•
Join Date: Dec 2006
Posts: 450
Reputation:
Rep Power: 2
Solved Threads: 62
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.
•
•
Join Date: Jul 2008
Posts: 7
Reputation:
Rep Power: 0
Solved Threads: 0
Thank you so much JLM and Wooee (-: I managed to work it out, here is my code:
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! (-:
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.
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:
Refer to this page for more information on the time formatting strings for strftime and strptime.
An example:
python Syntax (Toggle Plain Text)
>>> import time >>> my_time = '2008-7-21' >>> time.strptime( my_time, '%Y-%m-%d' ) (2008, 7, 21, 0, 0, 0, 0, 203, -1) >>> new_time = time.strptime( my_time, '%Y-%m-%d' ) >>> time.strftime( '%y%m%d', new_time ) '080721' >>>
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. **
** 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. **
•
•
Join Date: Jul 2008
Posts: 7
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
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:
python Syntax (Toggle Plain Text)
>>> import time >>> my_time = '2008-7-21' >>> time.strptime( my_time, '%Y-%m-%d' ) (2008, 7, 21, 0, 0, 0, 0, 203, -1) >>> new_time = time.strptime( my_time, '%Y-%m-%d' ) >>> time.strftime( '%y%m%d', new_time ) '080721' >>>
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!
![]() |
•
•
•
•
•
•
•
•
DaniWeb Python Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Other Threads in the Python Forum
- Previous Thread: Move Scollbars in controls after Freezing them
- Next Thread: new. need help


Linear Mode