Example #1
0
def predict(request, symbol):
	"""
	Executed when a request for /predict/SYMBOL comes in.
	Django handles parsing this string for SYMBOL.
	The default time range is 2000 to now.

	Parameters
	----------
	request: incoming request
	symbol: stock symbol

	Returns
	-------
	projection: data needed for "projection" div in index.html to show a next-day stock price via ajax
	"""
	# find the next day stock price in this time series
	start = datetime.date(2009, 12, 31)
	end = datetime.datetime.now()

	# get the stock history for that symbol with start and end dates
	stock = Stocks(symbol, start, end)
	dates, prices = stock.get_stock_history()

	# use our neural network to predict the next day closing price
	predictor = Predictors(dates, prices)
	projection = predictor.predict()
	projection = "%.2f" % projection

	return HttpResponse(projection, mimetype="text/html")
Example #2
0
def index(request):
	#csrf_token = get_token(request)
	"""
	Executed when a request for / comes in. The default symbol is 'EBAY'.
	The default time range is 2000 to now.

	Parameters
	----------
	request: incoming request

	Returns
	-------
	context: data needed for template index.html to render
	"""
	symbol = ""

	if request.method == "POST":
		symbol = request.POST.get("symbol")
		symbol = symbol.upper()

	if symbol == "":
		symbol = 'GOOG'

	start = datetime.date(1999, 12, 31)
	end = datetime.datetime.now()

	# get the stock history for that symbol with start and end dates
	stock = Stocks(symbol, start, end)
	try:
		dates, prices = stock.get_stock_history()
	except Exception as e:
		symbol = 'EBAY'
		stock = Stocks(symbol, start, end)
		dates, prices = stock.get_stock_history()

	# find the MA and MACD of this time series
	indicators = Indicators(prices)	
	ma20 = indicators.moving_average(7, type='simple')
	macd = indicators.moving_average_convergence()
	rsi = indicators.relative_strength()

	template = 'datavis/index.html'

	context = {
		'symbol': symbol,
		'dates': dates,
		'prices': prices,
		'ma20': ma20,
		'macd': macd,
		'rsi': rsi,
		#'csrf_token': csrf_token
	}
	from django.template.context import RequestContext
	return render_to_response(template, context,context_instance=RequestContext(request))
Example #3
0
def kalman(request, symbol):
	"""
	Executed when a request for /kalman/SYMBOL comes in.
	Django handles parsing this string for SYMBOL.
	The default time range is 2000 to now.

	Parameters
	----------
	request: incoming request
	symbol: stock symbol

	Returns
	-------
	context: data needed for template kalman.html to render
	"""
	# get the stock history for that symbol with start and end dates
	start = datetime.date(1999, 12, 31)
	end = datetime.datetime.now()
	stock = Stocks(symbol, start, end)
	dates, prices = stock.get_stock_history()

	#get the mean and standard deviation for this price feature
	mu = numpy.average(prices)
	sigma = numpy.std(prices)
	mu = "%.2f" % mu
	sigma = "%.2f" % sigma

	# apply the kalman filter to the time series
	filters = Filters(prices, mu, sigma)
	kalman = filters.kalmanfilter()

	template = 'datavis/kalman.html'

	context = {
		'symbol': symbol,
		'dates': dates,
		'prices': prices,
		'kalman': kalman,
		'mu': mu,
		'sigma': sigma,
	}

	return direct_to_template(request, template, context)
Example #4
0
def fft(request, symbol):
	"""
	Executed when a request for /fft/SYMBOL comes in.
	Django handles parsing this string for SYMBOL.
	The default time range is 2000 to now.

	Parameters
	----------
	request: incoming request
	symbol: stock symbol

	Returns
	-------
	context: data needed for template fft.html to render
	"""
	# get the stock history for that symbol with start and end dates
	start = datetime.date(1999, 12, 31)
	end = datetime.datetime.now()
	stock = Stocks(symbol, start, end)
	dates, prices = stock.get_stock_history()

	# transform the time-series to the frequency domain
	transform = Transforms(prices)
	period, power = transform.fft2()

	# reverse the numpy array
	period = period[::-1]
	power = power[::-1]

	template = 'datavis/fft.html'

	context = {
		'symbol': symbol,
		'period': period,
		'power': power,
	}

	return render_to_response(template, context,context_instance=RequestContext(request))