Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Python Error (Traceback & ValueError) Help!
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:'.


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 =
    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

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?

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.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs:

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  ValueError: invalid literal for int() with base 10: '' Jay123 7 214 Aug-05-2019, 02:43 PM
Last Post: Jay123
  [SOLVED ]ValueError: View function did not return a response Hadad 0 101 Jul-24-2019, 04:01 PM
Last Post: Hadad
  Changing a traceback message without a 2nd raise Clunk_Head 1 125 Jul-14-2019, 12:45 AM
Last Post: Gribouillis
  Highlight/Underline a string | ValueError: zero length field name in format searching1 1 200 Jul-01-2019, 03:06 AM
Last Post: metulburr
  ValueError: could not convert string to float michaelbs007 1 916 Jun-08-2019, 08:44 PM
Last Post: micseydel
  ValueError: invalid rectstyle object fen1c5 1 347 Jun-05-2019, 02:51 PM
Last Post: heiner55
  ValueError: dictionary update sequence element #0 has length 1; 2 Jmekubo 4 461 Apr-28-2019, 07:25 PM
Last Post: Jmekubo
  ValueError: invalid literal for int() with base 10: '' ivinjjunior 6 1,720 Apr-20-2019, 05:37 PM
Last Post: keames
  Help with ValueError using ranint from random. FWendeburg 2 266 Feb-14-2019, 04:22 AM
Last Post: ichabod801
  I have Traceback error and a type error and i dont know how to fix it coltron1282 2 235 Feb-07-2019, 03:15 PM
Last Post: coltron1282

Forum Jump:

Users browsing this thread: 1 Guest(s)