Python Forum

Full Version: Python Error (Traceback & ValueError) Help!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Anyone here able to help with some python error messages? 

1. Traceback (most recent call last):
2. ValueError: time data '' does not match format '%Y%m%d'

The code seems to run & execute based on the conditions stated below, up till printing 'we are complete. here is total profit:'.

PLEASE HELP!


this is the code I'm using:

import time
from datetime import datetime
from time import mktime


def backTest():

    stance = 'none'
    buyPrice = 0
    sellPrice = 0
    previousPrice = 0

    totalProfit = 0
    
    bigDataFile = open('AAPL.txt','r')
    readFile = bigDataFile.read()
    lineSplit = readFile.split('\n')

    for everyLine in lineSplit:
        dividedLine = everyLine.split(',')
        initialDate = dividedLine[0]
        unixStamp = mktime(datetime.strptime(initialDate, '%Y%m%d').timetuple())
        dateStamp = time.strftime('%d/%m/%Y',time.localtime(unixStamp))
        stockPrice = float(dividedLine[4])
        reformatted = unixStamp,dateStamp,stockPrice

        if stance == 'none':
            if stockPrice < previousPrice:
                print 'buy triggered!'
                buyPrice = stockPrice
                print 'bought stock for',buyPrice
                stance = 'holding'

        elif stance == 'holding':
            if stockPrice > buyPrice * .002 + buyPrice:
                print 'sell triggered!'
                sellPrice = stockPrice
                print 'finished trade, sold for:',sellPrice
                stance = 'none'
                tradeProfit = sellPrice - buyPrice
                totalProfit += tradeProfit
                print totalProfit

        previousPrice = stockPrice

    print 'we are complete. here is total profit:'
    print totalProfit
    time.sleep(555)

backTest()
In the future, please use code tags (I added them this time)

I'm thinking that dividedLine[0] is not a timestamp.
try printing the contents before the conversion
(Dec-19-2016, 02:19 AM)Larz60+ Wrote: [ -> ]In the future, please use code tags (I added them this time)

I'm thinking that dividedLine[0] is not a timestamp.
try printing the contents before the conversion

My apologies. I am new to this. Thanks for your help!

It does print & initialDate is a timestamp. Would you be able to explain, in words, what the two error messages actually mean & how I am to interpret them? Maybe, that way, I could troubleshoot it myself?

Thanks!
Please post the entire traceback, it contains valuable information
that shows the context of the error.
please show print result
Traceback is followed by "the stack" (ie, the state of nested function calls...). One end of the stack is where your code has crashed (which is why providing the whole traceback when you ask for help is a good idea).

The "ValueError" is an usual answer in Python to an invalid argument in a function call. Given the message it is likely the strptime() call telling you that it cannot parse its input with the format you provided.