Esempio n. 1
0
def bidmodify(request):
	date = datetime.now()
	hour = (date.time()).hour
	if hour < 0:
		message = notification(user_id=request.session['login_id'],notification = "Order cannot be processed. Market Closed")
		message.save()
		return HttpResponseRedirect('/marketwatch/')
	if request.method == 'POST':
		try:
			b = bid.objects.get(pk=request.POST['transaction_id'])
		except:
			return HttpResponseRedirect('/portfolio/')
		b.quantity=request.POST['quantity']
		b.quote = request.POST['quote']
		shareId = b.share
		min = round(shareId.day_value * 0.9, 2) 
		max = round(shareId.day_value * 1.1, 2)
		quote = round(decimal.Decimal(b.quote), 2)
		if quote < min or quote > max:
			message = notification(user_id=request.session['login_id'],notification="Quote unsuccessful. Quote price out of bounds")
			message.save()
			return HttpResponseRedirect(request.META['HTTP_REFERER'])
		b.save()
		
		data = "bid"
		dummy(shareId,request)
		checktransaction(shareId,data,request)
		return HttpResponseRedirect(request.META['HTTP_REFERER'])
	else:
		return HttpResponseRedirect(request.META['HTTP_REFERER'])
Esempio n. 2
0
def offercheck(request):
	date = datetime.now()
	hour = (date.time()).hour
	if hour < 0:
		message = notification(user_id=request.session['login_id'],notification = "Order cannot be processed. Market Closed")
		message.save()
		return HttpResponseRedirect('/marketwatch/')
	if request.method == 'POST':
		shareId = share.objects.get(pk=request.POST['share'])
		sellerId = login.objects.get(pk=request.session['login_id'])
		s = offer(seller = sellerId ,share = shareId , quantity=request.POST['quantity'],quote=request.POST['price'])
		userShare = user_share.objects.get(user_id=request.session['login_id'],share=shareId)
		min = round(shareId.day_value * 0.9, 2) 
		max = round(shareId.day_value * 1.1, 2)
		quote = round(decimal.Decimal(s.quote), 2)
		if quote < min or quote > max:
			message = notification(user_id=request.session['login_id'],notification="Quote unsuccessful. Quote price out of bounds")
			message.save()
			return HttpResponseRedirect(request.META['HTTP_REFERER'])
		if int(s.quantity) > userShare.quantity:
			message = notification(user_id=request.session['login_id'],notification="Quote unsuccessful. Not enough stocks to sell")
			message.save()
			return HttpResponseRedirect(request.META['HTTP_REFERER'])
			
		
		s.save()
		data = "sell"
		dummy(shareId,request)
		checktransaction(shareId,data,request)
		return HttpResponseRedirect(request.META['HTTP_REFERER'])
	else:
		return HttpResponseRedirect(request.META['HTTP_REFERER'])
Esempio n. 3
0
def bidcheck(request):
	date = datetime.now()
	hour = (date.time()).hour
	if hour > 0:
		message = notification(user_id=request.session['login_id'],notification = "Order cannot be processed. Market Closed")
		return HttpResponse(json.dumps({"server_response":"wronginput"}), mimetype='application/javascript')
		message.save()
		return HttpResponseRedirect('/marketwatch/')
# remember to change response type
	if request.method == 'POST':
	        if request.POST['quantity']=="" or request.POST['price']=="":
        	        return HttpResponse(json.dumps({"server_response":"wronginput"}), mimetype='application/javascript')
		shareId = share.objects.get(pk=request.POST['share'])
		bidderId = login.objects.get(pk=request.session['login_id'])
		b = bid(bidder = bidderId, share = shareId, quantity=request.POST['quantity'], quote=request.POST['price'])
		min = round(shareId.day_value * 0.9, 2)
		max = round(shareId.day_value * 1.1, 2)
		quote = round(decimal.Decimal(b.quote), 2)
		if quote < min or quote > max:
			message = notification(user_id=request.session['login_id'],notification = "Quote unsuccessful. Quote price out of bounds")
			message.save()
	                return HttpResponse(json.dumps({"server_response":"wronginput"}), mimetype='application/javascript')

		b.save()
		data = "bid"
		dummy(shareId,request)
		checktransaction(shareId,data,request)
#		return HttpResponseRedirect(request.META['HTTP_REFERER'])
		return HttpResponse(json.dumps({"server_response":"biddone"}), mimetype='application/javascript')
	else:
                return HttpResponseRedirect('/marketwatch/')
Esempio n. 4
0
def offermodify(request):
	date = datetime.now()
	hour = (date.time()).hour
	if hour > 0:
		message = notification(user_id=request.session['login_id'],notification = "Order cannot be processed. Market Closed")
		message.save()
		return HttpResponse(json.dumps({"server_response":"wronginput"}), mimetype='application/javascript')
		return HttpResponseRedirect('/marketwatch/')
	if request.method == 'POST':
		if request.POST['quantity']=="" or request.POST['quote']=="":
                	return HttpResponse(json.dumps({"server_response":"wronginput"}), mimetype='application/javascript')
		try:
			o = offer.objects.get(pk=request.POST['transaction_id'])
		except:
			return HttpResponseRedirect('/portfolio/')
		shareId = o.share
		o.quantity=request.POST['quantity']
		o.quote = request.POST['quote']

		userShare = user_share.objects.get(user_id=request.session['login_id'],share=shareId)
		min = round(shareId.day_value * 0.9, 2) 
		max = round(shareId.day_value * 1.1, 2)
		quote = round(decimal.Decimal(o.quote), 2)
		if quote < min or quote > max:
			message = notification(user_id=request.session['login_id'],notification="Quote unsuccessful. Quote price out of bounds")
			message.save()
	                return HttpResponse(json.dumps({"server_response":"wronginput"}), mimetype='application/javascript')

		if int(o.quantity) > userShare.quantity:
			message = notification(user_id=request.session['login_id'],notification="Quote unsuccessful. Not enough stocks to sell")
			message.save()
                	return HttpResponse(json.dumps({"server_response":"wronginput"}), mimetype='application/javascript')

		
		o.save()
		data = "sell"
		dummy(shareId,request)
		checktransaction(shareId,data,request)
                return HttpResponse(json.dumps({"server_response":"offerdone"}), mimetype='application/javascript')
	else:
		return HttpResponseRedirect(request.META['HTTP_REFERER'])
Esempio n. 5
0
def deleteoffer(request):
	date = datetime.now()
	hour = (date.time()).hour
	if hour < 0:
		message = notification(user_id=request.session['login_id'],notification = "Order cannot be processed. Market Closed")
		message.save()
		return HttpResponseRedirect('/marketwatch/')
	if request.method == 'POST':
		s = offer.objects.get(pk=request.POST['transaction_id'])
		s.delete()
		return HttpResponseRedirect(request.META['HTTP_REFERER'])
	else:
		return HttpResponseRedirect(request.META['HTTP_REFERER'])
Esempio n. 6
0
def deleteoffer(request):
	date = datetime.now()
	hour = (date.time()).hour
	if hour > 0:
		message = notification(user_id=request.session['login_id'],notification = "Order cannot be processed. Market Closed")
		message.save()
		return HttpResponse(json.dumps({"server_response":"wronginput"}), mimetype='application/javascript')
		return HttpResponseRedirect('/portfolio/')
	if request.method == 'POST':
		s = offer.objects.get(pk=request.POST['transaction_id'])
		s.delete()
		return HttpResponseRedirect(request.META['HTTP_REFERER'])
	else:
		return HttpResponseRedirect(request.META['HTTP_REFERER'])
Esempio n. 7
0
def checktransaction(share, data, request):
	bids = bid.objects.filter(share=share).order_by('-quote')
	offers = offer.objects.filter(share=share).order_by('quote')

	shareName = share.name
	dummy = login.objects.get(email='*****@*****.**')
	flag = 1
	for o in offers:
		if flag == 0:
			break
		for b in bids:						
			try:
				if (o.quote <= b.quote):
					price = (o.quote + b.quote)/2
					buyer = b.bidder
					seller = o.seller

					if o.quantity < b.quantity:
						quantity = o.quantity
					else:
						quantity = b.quantity

					transactionTotal = quantity * price
					#Check if buyer has enough money
					if buyer.money < (b.quantity * price):
						note = notification(user=buyer, notification="Your bid for " +shareName+ " got deleted due to lack of money")
						note.save()
						b.delete()
						continue
					#Check if seller has enough shares
					if (user_share.objects.filter(user=seller, share=share)):
						ss = user_share.objects.get(user=seller, share=share)
						if (ss.quantity < o.quantity):
							note = notification(user=seller, notification="Your offer for " +shareName+ " got deleted due to lack of share")
							note.save()
							o.delete()
							break
					else:
						note = notification(user=seller, notification="Your offer for " +shareName+ " got deleted due to lack of share")
						note.save()
						o.delete()
						break

					if (buyer!=seller):
						if (user_share.objects.filter(user=buyer, share=share)):
							bs = user_share.objects.get(user=buyer, share=share)														
							bs.quantity = bs.quantity + quantity
						else:
							bs = user_share(user=buyer, share=share, quantity=quantity)
						bs.save()

						if (seller != dummy):
							ss.quantity = ss.quantity - quantity
							ss.save()

						if (buyer != dummy):
							buyer.money = buyer.money - transactionTotal							
							buyer.save()

						seller.money = seller.money + transactionTotal
						seller.save()												

					try:
						t=transactions(buyer=b.bidder, seller=o.seller, share=share, quantity=quantity, price=price)
						t.save()
						noteBuyer = notification(user=buyer, notification=str(quantity)+" shares of "+share.name+" bought at "+str(price))
						noteSeller = notification(user=seller,notification=str(quantity)+" shares of "+share.name+" sold at "+str(price))
						noteBuyer.save()
						noteSeller.save()
					except IndexError:
						break

					if o.quantity < b.quantity:						
						b.quantity = b.quantity - o.quantity
						b.save()
						o.delete()
						break
					else:						
						o.quantity = o.quantity - b.quantity
						if o.quantity == 0:
							o.delete()
							break
						else:
							o.save()
						b.delete()
						continue
				elif (o.quote > b.quote):
					flag = 0
					break

			except IndexError:
				break
Esempio n. 8
0
def checktransaction(shareId,data,request):
	shareBids = bid.objects.filter(share=shareId).order_by('-quote')
	shareOffers = offer.objects.filter(share=shareId).order_by('quote')
	shareName = shareId.name
	dummy = login.objects.get(email='*****@*****.**')
	if(data == "bid"):
			for i in shareOffers:
				try:
					if (i.quote < shareBids[0].quote) or (i.quote == shareBids[0].quote):
						if i.quantity < shareBids[0].quantity:


							userId=login.objects.get(pk=shareBids[0].bidder_id)
							transactionPrice = i.quote +(shareBids[0].quote - i.quote)/2
							transactionTotal = i.quantity*transactionPrice
							if userId != dummy:
								if userId.money < transactionTotal:
									shareBids[0].delete()
									for shareBid in shareBids:
										shareBid.save()
									note = notification(user=userId,notification="Your Bid for "+shareName+" got deleted due to lack of money")
									note.save()
									break
								
							temp = shareBids[0]
							temp.quantity = temp.quantity - i.quantity
							temp.save()	
							try:
								t=transactions(buyer=shareBids[0].bidder,seller = i.seller , share = shareId , quantity = i.quantity , price = transactionPrice)
								noteBuyer = notification(user=t.buyer,notification=str(t.quantity)+" shares of "+shareId.name+" bought at "+str(t.price))
								noteSeller = notification(user=t.seller,notification=str(t.quantity)+" shares of "+shareId.name+" sold at "+str(t.price))
								noteBuyer.save()
								noteSeller.save()
							except IndexError:
								break
							buyer = t.buyer
							seller = t.seller
							if (buyer!=seller):
								if buyer != dummy:
									buyer.money = buyer.money - transactionTotal
								if (user_share.objects.filter(user=buyer,share=shareId)):
									bs = user_share.objects.get(user=buyer,share=shareId)
									bs.quantity = bs.quantity + t.quantity
								else:
									bs = user_share(user=buyer,share=shareId,quantity=t.quantity)
								bs.save()
								ss=user_share.objects.get(user=seller,share=shareId)
								if seller != dummy:
									ss.quantity = ss.quantity - t.quantity
								ss.save()
								seller = i.seller
								seller.money = seller.money + transactionTotal
								buyer.save()
								seller.save()
							t.save()						

							i.delete()
						else:

							transactionPrice =i.quote + (shareBids[0].quote - i.quote)/2
							transactionTotal = shareBids[0].quantity*transactionPrice

							userId=login.objects.get(pk=shareBids[0].bidder_id)
							money = userId.money
							if userId != dummy:
								if userId.money < transactionTotal:
									i.delete()
									for shareBid in shareBids:
										shareBid.save()
									note = notification(user=userId,notification="Your Bid for "+shareName+" got deleted due to lack of money")
									note.save()
									break
								
							i.quantity = i.quantity - shareBids[0].quantity 	
							try:
								t=transactions(buyer=shareBids[0].bidder,seller = i.seller , share = shareId , quantity = shareBids[0].quantity , price = transactionPrice)
								noteBuyer = notification(user=t.buyer,notification=str(t.quantity)+" shares of "+shareId.name+" bought at "+str(t.price))
								noteSeller = notification(user=t.seller,notification=str(t.quantity)+" shares of "+shareId.name+" sold at "+str(t.price))
								noteBuyer.save()
								noteSeller.save()
							except IndexError:
								break
							
							buyer = t.buyer
							seller = t.seller
							if (buyer!=seller):
								if buyer != dummy:
									buyer.money = buyer.money - transactionTotal
								if (user_share.objects.filter(user=buyer,share=shareId)):
									bs = user_share.objects.get(user=buyer,share=shareId)
									bs.quantity = bs.quantity + t.quantity
								else:
									bs = user_share(user=buyer,share=shareId,quantity=t.quantity)
								bs.save()
								
								ss=user_share.objects.get(user=seller,share=shareId)
								if seller != dummy:
									ss.quantity = ss.quantity - t.quantity
								ss.save()
								seller.money = seller.money + transactionTotal
								buyer.save()
								seller.save()		
							t.save()	
							shareBids[0].delete()
							for shareBid in shareBids:
								shareBid.save()
							i.save()
							if i.quantity == 0:
								i.delete()
				except IndexError:
					break
	else:
			for i in shareBids:
				try:
					if (shareOffers[0].quote < i.quote) or (shareOffers[0].quote == i.quote):
						if shareOffers[0].quantity < i.quantity:
							transactionPrice = shareOffers[0].quote + (i.quote - shareOffers[0].quote)/2
							transactionTotal = transactionPrice*shareOffers[0].quantity
							userId = login.objects.get(pk=i.bidder_id)
							money = userId.money
							if userId != dummy:
								if userId.money < transactionTotal:
									i.delete()
									note = notification(user=userId,notification="Your Bid for "+shareName+" got deleted due to lack of money")
									note.save()
									break
							i.quantity = i.quantity - shareOffers[0].quantity
							i.save()
							try:
								t=transactions(buyer=i.bidder,seller = shareOffers[0].seller , share = shareId , quantity = shareOffers[0].quantity , price = transactionPrice)
								noteBuyer = notification(user=t.buyer,notification=str(t.quantity)+" shares of "+shareId.name+" bought at "+str(t.price))
								noteSeller = notification(user=t.seller,notification=str(t.quantity)+" shares of "+shareId.name+" sold at "+str(t.price))
								noteBuyer.save()
								noteSeller.save()
							except IndexError:
								break
							buyer = t.buyer
							seller = t.seller
							if (buyer!=seller):
								if buyer != dummy:
									buyer.money = buyer.money - transactionTotal
								if (user_share.objects.filter(user=buyer,share=shareId)):
									bs = user_share.objects.get(user=buyer,share=shareId)
									bs.quantity = bs.quantity + t.quantity
								else:
									bs = user_share(user=buyer,share=shareId,quantity=t.quantity)
								bs.save()
								
								ss=user_share.objects.get(user=seller,share=shareId)
								if seller != dummy:
									ss.quantity = ss.quantity - t.quantity
								ss.save()
				
								seller.money = seller.money + transactionTotal
								buyer.save()
								seller.save()		
							
							
							t.save()	
							
							
							
							
							
							temp = shareOffers[0]
							temp.delete()
						else:
							transactionPrice = shareOffers[0].quote + (i.quote - shareOffers[0].quote)/2
							transactionTotal = transactionPrice*i.quantity
							
							userId=login.objects.get(pk=shareBids[0].bidder_id)
							if userId != dummy:
								if userId.money < transactionTotal:
									i.delete()
									note = notification(user=userId,notification="Your Bid for "+shareName+" got deleted due to lack of money")
									note.save()
									break
							
							temp = shareOffers[0]
							temp.quantity = temp.quantity - i.quantity
							temp.save()
							try:
								t=transactions(buyer=i.bidder,seller = shareOffers[0].seller , share = shareId , quantity = i.quantity , price = transactionPrice)
								noteBuyer = notification(user=t.buyer,notification=str(t.quantity)+" shares of "+shareId.name+" bought at "+str(t.price))
								noteSeller = notification(user=t.seller,notification=str(t.quantity)+" shares of "+shareId.name+" sold at "+str(t.price))
								noteBuyer.save()
								noteSeller.save()
							except IndexError:
								break
							buyer = t.buyer
							seller = t.seller
							if (buyer!=seller):
								if buyer != dummy:
									buyer.money = buyer.money - transactionTotal
								
								if (user_share.objects.filter(user=buyer,share=shareId)):
									bs = user_share.objects.get(user=buyer,share=shareId)
									bs.quantity = bs.quantity + t.quantity
								else:
									bs = user_share(user=buyer,share=shareId,quantity=t.quantity)
								bs.save()
								
								ss=user_share.objects.get(user=seller,share=shareId)
								if seller != dummy:
									ss.quantity = ss.quantity - t.quantity
								ss.save()

								seller.money = seller.money + transactionTotal
								buyer.save()
								seller.save()		
							
							t.save()	
							
							i.delete()
							
							if temp.quantity == 0:
								temp.delete()
								break
				except IndexError:
					break