def getprediction(series, rdate, elements): rdateg = mjd2g(rdate) qdate = str(rdateg[0]) + '-' + str(rdateg[1]) + '-' + str(rdateg[2]) elements = 1 #!??!?!?!? I must have copied the select statement in without considering approach, rethink it! # do we get this from econ or econsmooth?? # todo: test with element =1 adjust query to get n elements and sort to take the # top one on the list cursor.execute( """SELECT value FROM econ WHERE series=%s AND date > %s order by date desc LIMIT %s""", (series, qdate, elements)) data = cursor.fetchall() assert cursor.rowcount > 0, 'no data returned - rdate=%d (%s)' % (rdate, qdate) return data
def validmktday(series, rdate): rdateg = mjd2g(rdate) qdate = str(rdateg[0]) + '-' + str(rdateg[1]) + '-' + str(rdateg[2]) #print "validmktday: ", series, rdate, qdate cursor.execute( """SELECT value FROM econ WHERE series=%s AND date=%s""", (series, qdate)) data = cursor.fetchall() assert cursor.rowcount == 1 or cursor.rowcount == 0, 'validmktday: invalid row count %s %s' % ( series, qdate) # print "validmktday: ", cursor.rowcount if cursor.rowcount == 1: validday = 1 else: validday = 0 print validday return validday
def getseriesdata(x, rdate, datasrc): # recheck how this was done before series = x[0] elements = int(x[1]) lag = 0 # lag will be determined by doing a dictionary lookup lag=lags{series}? if nolags: lag = 0 # used for production forecasting when we want the best data avail. rdateg = mjd2g(rdate - lag) qdate = str(rdateg[0]) + '-' + str(rdateg[1]) + '-' + str(rdateg[2]) if datasrc == 'econsmooth': cursor.execute( """SELECT value FROM econsmooth WHERE series=%s AND date < %s order by date desc LIMIT %s""", (series, qdate, elements)) else: cursor.execute( """SELECT value FROM econ WHERE series=%s AND date < %s order by date desc LIMIT %s""", (series, qdate, elements)) data = cursor.fetchall() # seems to come out of here as a 2xn array, but the [0] column is used and the [1] is empty assert cursor.rowcount > 0, 'no data returned - rdate=%d series=%s' % ( rdate, series) return data
def makerecs(forecastmode): global tinputs global toutput global firstdatemjd global lastdatemjd # # value - refers to a data element that is fully prepared to go into trng file if (forecastmode == "yes"): lastdatemjd = firstdatemjd + 1 print "forecast mode is YES so first and lastmjd is the same" print firstdatemjd print lastdatemjd for rdate in range(firstdatemjd, lastdatemjd): tinputs = [] toutput = [] # todo: check for valid market day # todo op: check for valid market day in forecast rdateg = mjd2g(rdate) print 'rdate is: %d (%4d-%02d-%02d)' % (rdate, rdateg[0], rdateg[1], rdateg[2]) print 'mktseries: ', mktseries if validmktday(mktseries, rdate): # for series in for x in parm_seriesprm: # x is (series, elements, lag) valuesrc = x[0] datasrc = 'econsmooth' data = getseriesdata(x, rdate, datasrc) for value in data: trainingdata( value[0], valuesrc) # use [0] to make sure it is a simple value # for seriesma in for x in parm_seriesmaprm: valuesrc = x[0] datasrc = 'econ' value = getseriesmadata(x, rdate, datasrc) trainingdata( value, valuesrc ) # the value is simple now after going through average # do remaining items if (timelinefmjd > 0): valuesrc = '*' value = (rdate - timelinefmjd) * timeline_sfactor #print value trainingdata(value, valuesrc) if (parm_anlsaw): valuesrc = '*' value = doy(rdateg) / 366 trainingdata(value, valuesrc) if (parm_anlsin): valuesrc = '*' value = sin(doy(rdateg) / 366 * pi) / 2 + 0.5 trainingdata(value, valuesrc) if (parm_anlcos): valuesrc = '*' value = cos(doy(rdateg) / 366 * pi) / 2 + 0.5 trainingdata(value, valuesrc) # forecast mode: would skip this section, write a zero or leave the record one element short? # get prediction fcseries = 'INDEX_SPX' fcelements = 1 value = getprediction(fcseries, rdate, fcelements) valuesrc = fcseries trainingdata( value[0][0], valuesrc, "forecast") # use [0] to make sure it is a scalor value # print 'forecast value: ', value[0][0], rdate close_training_rec() else: pass print "non-market day: ", mjd2g(rdate)
titems_p = titems_p + (parm_anlsaw + parm_anlsin + parm_anlcos) if (timelinefmjd): titems_p = titems_p + 1 # print ident print 'ident: ', parm_ident print 'annual cycles(s,s,c): ', parm_anlsaw, parm_anlsin, parm_anlcos print 'series slots...' for row in parm_seriesprm: print row print 'seriesma - moving averages...' for row in parm_seriesmaprm: print row print 'seriesslp - series slope...' for row in parm_seriesslpprm: print row fdateg = mjd2g(firstdatemjd) fdate = str(fdateg[0]) + '-' + str(fdateg[1]) + '-' + str(fdateg[2]) ldateg = mjd2g(lastdatemjd) ldate = str(ldateg[0]) + '-' + str(ldateg[1]) + '-' + str(ldateg[2]) print 'rdate range: %s (%d) - %s (%d)' % (fdate, firstdatemjd, ldate, lastdatemjd) print 'elements: ', titems_p + 1 print '--- assembling records now ', time.strftime(STR_TIME_FORMAT, time.localtime()) sys.stdout.flush() # now assemble the records makerecs(forecastmode) # assert (trec_p= trec) print "ERROR: param specification of elements # different than actual number of records present:" trecs, trecs_p