Skip to content

Commit 6d740e7

Browse files
author
Edward Williams
committed
fixed bug in server.py and stock.py
1 parent 5c60a1a commit 6d740e7

File tree

3 files changed

+91
-87
lines changed

3 files changed

+91
-87
lines changed

publicBot.py

Lines changed: 84 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,103 @@
11
import random
22
#I'll need to change this to incorporate
3-
from traderbot import traderbot
3+
from traderbot import TraderBot
44

55
robot = TraderBot()
66

77
stockArray = robot.getSymbols()
88

99
#initial loop
10-
for ticker in stockArray
11-
portfolio = robot.getPortfolio()
10+
for ticker in stockArray:
11+
portfolio = robot.getPortfolio()
1212

13-
if (portfolio[ticker] != 0)
13+
if (portfolio[ticker] != 0):
1414

15-
#selling distribution:
16-
#25 percent at market price
17-
#10 percent at 10 percent above market price
18-
#10 percent at 10 percent below market price
19-
#5 percent at 5 percent below market price
15+
#selling distribution:
16+
#25 percent at market price
17+
#10 percent at 10 percent above market price
18+
#10 percent at 10 percent below market price
19+
#5 percent at 5 percent below market price
2020

21-
marketFraction = 0.25
22-
aboveMarket = 0.10
23-
tenBelowMarket = 0.10
24-
fiveBelowMarket = 0.05
25-
26-
price = robot.getPrice(ticker)
21+
marketFraction = 0.25
22+
aboveMarket = 0.10
23+
tenBelowMarket = 0.10
24+
fiveBelowMarket = 0.05
25+
26+
price = robot.getPrice(ticker)
2727

28-
result = robot.ask((int)portfolio[ticker] *marketFraction , price, ticker)
29-
if result == 'Accepted':
30-
print "trade accepted"
31-
elif result == 'Unsuccesful':
32-
print "trade declined"
33-
elif result == 'Pending':
34-
print "trade pending"
35-
else:
36-
print "not a recognizable order state"
37-
result = robot.ask((int)portfolio[ticker]*aboveMarket, price*1.10, ticker)
38-
if result == 'Accepted':
39-
print "trade accepted"
40-
elif result == 'Unsuccesful':
41-
print "trade declined"
42-
elif result == 'Pending':
43-
print "trade pending"
44-
else:
45-
print "not a recognizable order state"
46-
result = robot.ask((int)portfolio[ticker]*tenBelowMarket, price*0.9, ticker)
47-
if result == 'Accepted':
48-
print "trade accepted"
49-
elif result == 'Unsuccesful':
50-
print "trade declined"
51-
elif result == 'Pending':
52-
print "trade pending"
53-
else:
54-
print "not a recognizable order state"
55-
result = robot.ask((int)portfolio[ticker]*fiveBelowMarket, price*0.95, ticker)
56-
if result == 'Accepted':
57-
print "trade accepted"
58-
elif result == 'Unsuccesful':
59-
print "trade declined"
60-
elif result == 'Pending':
61-
print "trade pending"
62-
else:
63-
print "not a recognizable order state"
28+
result = robot.ask((int)(portfolio[ticker] *marketFraction) , price, ticker)
29+
if result == 'Accepted':
30+
print "trade accepted"
31+
elif result == 'Unsuccesful':
32+
print "trade declined"
33+
elif result == 'Pending':
34+
print "trade pending"
35+
else:
36+
print "not a recognizable order state"
37+
result = robot.ask((int)(portfolio[ticker]*aboveMarket), price*1.10, ticker)
38+
if result == 'Accepted':
39+
print "trade accepted"
40+
elif result == 'Unsuccesful':
41+
print "trade declined"
42+
elif result == 'Pending':
43+
print "trade pending"
44+
else:
45+
print "not a recognizable order state"
46+
result = robot.ask((int)(portfolio[ticker]*tenBelowMarket), price*0.9, ticker)
47+
if result == 'Accepted':
48+
print "trade accepted"
49+
elif result == 'Unsuccesful':
50+
print "trade declined"
51+
elif result == 'Pending':
52+
print "trade pending"
53+
else:
54+
print "not a recognizable order state"
55+
result = robot.ask((int)(portfolio[ticker]*fiveBelowMarket), price*0.95, ticker)
56+
if result == 'Accepted':
57+
print "trade accepted"
58+
elif result == 'Unsuccesful':
59+
print "trade declined"
60+
elif result == 'Pending':
61+
print "trade pending"
62+
else:
63+
print "not a recognizable order state"
6464

6565

6666
while True:
6767

68-
for ticker in stockArray:
69-
portfolio = robot.getPortfolio()
70-
if (portfolio[ticker] != 0):
71-
trueValue = robot.getTrueValue(ticker)
72-
price = robot.getPrice(ticker)
73-
increment = abs(trueValue - price)/3
68+
for ticker in stockArray:
69+
portfolio = robot.getPortfolio()
70+
if (portfolio[ticker] != 0):
71+
trueValue = robot.getTrueValue(ticker)
72+
price = robot.getPrice(ticker)
73+
increment = abs(trueValue - price)/3
7474

75-
#bid for 10 percent of the total stock @ market value
76-
#bid for 4 percent of total stock @ below market value
77-
#bid for 3 percent of total stock @ above market value
78-
#bid for 2 percent of total stock @ significantly above market value
79-
#bid for 1 percent of total stock @ true value
80-
if trueValue/price >1.05:
81-
stockToBuy = 0.2 * robot.getVolume(ticker)
82-
robot.bid((int)stockToBuy/2, price, ticker)
83-
stockToBuy/= 2
84-
robot.bid((int)stockToBuy*0.4, price - increment, ticker)
85-
robot.bid((int)stockToBuy*0.3, price + increment, ticker)
86-
robot.bid((int)stockToBuy*0.2, price + 2 * increment, ticker)
87-
robot.bid((int)stockToBuy*0.1, price + 3 * increment, ticker)
75+
#bid for 10 percent of the total stock @ market value
76+
#bid for 4 percent of total stock @ below market value
77+
#bid for 3 percent of total stock @ above market value
78+
#bid for 2 percent of total stock @ significantly above market value
79+
#bid for 1 percent of total stock @ true value
80+
if trueValue/price >1.05:
81+
stockToBuy = 0.2 * robot.getVolume(ticker)
82+
robot.bid((int)(stockToBuy/2), price, ticker)
83+
stockToBuy/= 2
84+
robot.bid((int)(stockToBuy*0.4), price - increment, ticker)
85+
robot.bid((int)(stockToBuy*0.3), price + increment, ticker)
86+
robot.bid((int)(stockToBuy*0.2), price + 2 * increment, ticker)
87+
robot.bid((int)(stockToBuy*0.1), price + 3 * increment, ticker)
8888

89-
#ask for 10 percent of the total stock @ market value
90-
#ask for 4 percent of total stock @ above market value
91-
#ask for 3 percent of total stock @ below market value
92-
#ask for 2 percent of total stock @ significantly below market value
93-
#ask for 1 percent of total stock @ true value
94-
if trueValue/price < 0.95:
95-
stockToSell = 0.3 * portfolio[ticker]
96-
robot.ask((int)stockToSell/2, price, ticker)
97-
stockToBuy/= 2
98-
robot.ask((int)stockToSell*0.4, price + increment, ticker)
99-
robot.ask((int)stockToSell*0.3, price - increment, ticker)
100-
robot.ask((int)stockToSell*0.2, price - 2 * increment, ticker)
101-
robot.ask((int)stockToSell*0.1, price - 3 * increment, ticker)
89+
#ask for 10 percent of the total stock @ market value
90+
#ask for 4 percent of total stock @ above market value
91+
#ask for 3 percent of total stock @ below market value
92+
#ask for 2 percent of total stock @ significantly below market value
93+
#ask for 1 percent of total stock @ true value
94+
if trueValue/price < 0.95:
95+
stockToSell = 0.3 * portfolio[ticker]
96+
robot.ask((int)(stockToSell/2), price, ticker)
97+
stockToBuy/= 2
98+
robot.ask((int)(stockToSell*0.4), price + increment, ticker)
99+
robot.ask((int)(stockToSell*0.3), price - increment, ticker)
100+
robot.ask((int)(stockToSell*0.2), price - 2 * increment, ticker)
101+
robot.ask((int)(stockToSell*0.1), price - 3 * increment, ticker)
102102

103-
103+

server.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ def ask(account_id, ticker, price, quantity):
3030
quantityAvailable = account.portfolio[ticker]
3131
if quantity > quantityAvailable:
3232
return 0
33+
34+
print "Account ID:", account_id
3335
stock.sellAt(quantity, price, account_id)
36+
3437
stocks[ticker]=stock
3538
return 1
3639

@@ -41,6 +44,7 @@ def bid(account_id, ticker, price, quantity):
4144
account = accounts[account_id]
4245
if price * quantity > account.availableFunds:
4346
return 0
47+
print "Account ID:", account_id
4448
stock.buyAt(quantity, price, account_id)
4549
stocks[ticker]=stock
4650
return 1

stock.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,14 @@ def buyAt(self, quantity, price, account_id):
187187
order = Order(account_id, quantity, price, self.ticker)
188188
self.buyQueue.append(order)
189189
self.buyQueue.sort(key = lambda order: order.price, reverse = True)
190-
self.checkQueue()
190+
self.checkQueues()
191191
#by price in descending
192192
#O(n*logn)
193193

194-
def sellAt(self, quantity, price, acccount_id):
194+
def sellAt(self, quantity, price, account_id):
195195
order = Order(account_id, quantity, price, self.ticker)
196196
self.sellQueue.append(order)
197197
self.sellQueue.sort(key = lambda order: order.price)
198-
self.checkQueue()
198+
self.checkQueues()
199199
#by price in ascending
200200
#O(n*logn)

0 commit comments

Comments
 (0)