/
commands.py
795 lines (607 loc) · 27.4 KB
/
commands.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
"""
This file contains the implementations for all transaction server commands.
"""
from sps.database.session import get_session
from sps.database.models import User, Money, Transaction, StockPurchase, Trigger
from sps.quotes.client import get_quote_client
from sps.transactions import xml
from sps.config import config
from datetime import datetime
import eventlet
from os import path
from logging import getLogger
log = getLogger(__name__)
class CommandError(Exception):
"""
An exception that holds both a message for the user and the original cause
"""
user_message = 'System error'
@property
def message(self):
return self.user_message
class InsufficientFundsError(CommandError):
user_message = 'Insufficient funds'
class InsufficientStockError(CommandError):
user_message = 'Insufficient stock quantity'
class UnknownCommandError(CommandError):
user_message = 'Unknown command'
class UserNotFoundError(CommandError):
user_message = 'Unknown user'
class InvalidInputError(CommandError):
user_message = 'Invalid input'
class NoBuyTransactionError(CommandError):
user_message = 'No BUY transaction is pending'
class NoSellTransactionError(CommandError):
user_message = 'No SELL transaction is pending'
class NoTriggerError(CommandError):
user_message = 'No trigger is pending'
class ExpiredBuyTransactionError(CommandError):
user_message = 'BUY transaction has expired'
class ExpiredSellTransactionError(CommandError):
user_message = 'SELL transaction has expired'
class BuyTransactionActiveError(CommandError):
user_message = 'A BUY transaction is already active'
class SellTransactionActiveError(CommandError):
user_message = 'A SELL transaction is already active'
class CommandHandler(object):
# Associates command labels (e.g. BUY) to subclasses of CommandHandler
commands = {}
@classmethod
def get_handler(cls, label):
""" Finds the CommandHandler associated with label """
if label not in cls.commands:
raise UnknownCommandError(label)
return cls.commands[label]()
@classmethod
def register_command(cls, label, command):
"""
Registers command with CommandHandler so it can be looked up by label
"""
cls.commands[label] = command
###############################################################################
# Command Classes
###############################################################################
class EchoCommand(CommandHandler):
"""
Echoes a single argument back to the client
"""
def run(self, message):
return message
CommandHandler.register_command('ECHO', EchoCommand)
class UppercaseCommand(CommandHandler):
"""
Like ECHO, but returns the message in uppercase
"""
def run(self, message):
return message.upper()
CommandHandler.register_command('UPPER', UppercaseCommand)
class ADDCommand(CommandHandler):
"""
Add the given amount of money to the user's account
"""
def run(self, username, amount):
session = get_session()
user = session.query(User).filter_by(username=username).first()
if not user:
xml.log_error('ADD', 'invalid username')
raise UserNotFoundError(username)
amount = Money.from_string(amount)
user.account_balance += amount
session.commit()
#log event
xml.log_event('ADD', username, amount=str(amount))
return xml.ResultResponse('success')
class QUOTECommand(CommandHandler):
"""
Get the current quote for the stock for the specified user
"""
def run(self, username, stock_symbol):
session = get_session()
user = session.query(User).filter_by(username=username).first()
if not user:
raise UserNotFoundError(username)
xml.log_error('QUOTE','username not found')
if len(stock_symbol) > 4:
raise InvalidInputError('stock symbol too long: %d' % \
len(stock_symbol))
xml.log_error('QUOTE','stock symbol too long')
quote_client = get_quote_client()
quote = quote_client.get_quote(stock_symbol, username)
#create log
xml.log_event('QUOTE', username, stock_symbol)
return str(quote)
class BUYCommand(CommandHandler):
"""
Buy the dollar amount of the stock for the specified user at the current
price.
"""
def run(self, username, stock_symbol, amount):
session = get_session()
user = session.query(User).filter_by(username=username).first()
if not user:
raise UserNotFoundError(username)
xml.log_error('BUY','username not found')
# Check for existing uncommitted transaction
if session.query(Transaction).filter_by(
user=user, operation='BUY', committed=False).count() > 0:
raise BuyTransactionActiveError()
xml.log_error('BUY','Outstanding Buy Exists')
# Getting stock quote
quote_client = get_quote_client()
quote = quote_client.get_quote(stock_symbol, username)
# Work out quantity of stock to buy, fail if not enough for one stock
amount = Money.from_string(amount)
quantity = amount_to_quantity(quote, amount)
if quantity == 0:
raise InsufficientFundsError()
price = quote * quantity
if user.account_balance < price:
raise InsufficientFundsError()
transaction = Transaction(user=user, quantity=quantity, operation='BUY',
stock_symbol=stock_symbol, stock_value=quote, committed=False)
session.add(transaction)
session.commit()
xml.log_transaction('BUY', transaction)
return xml.QuoteResponse(quantity=quantity, price=price)
class COMMIT_BUYCommand(CommandHandler):
"""
Commits the most recently executed BUY command
"""
def run(self, username):
session = get_session()
user = session.query(User).filter_by(username=username).first()
if not user:
xml.log_error('COMMIT_BUY','username not found')
raise UserNotFoundError(username)
transaction = session.query(Transaction).filter_by(
username=user.username, operation='BUY', committed=False
).first()
if not transaction:
xml.log_error('COMMIT_BUY','no buy tranaction found')
raise NoBuyTransactionError(username)
if (datetime.now() - transaction.creation_time) > config.TRANSACTION_TIMEOUT:
session.delete(transaction)
session.commit()
raise ExpiredBuyTransactionError(username)
price = transaction.stock_value * transaction.quantity
user.account_balance -= price
transaction.committed = True
# create or update the StockPurchase for this stock symbol
stock = session.query(StockPurchase).filter_by(
user=user, stock_symbol=transaction.stock_symbol
).first()
if not stock:
stock = StockPurchase(user=user,
stock_symbol=transaction.stock_symbol,
quantity=transaction.quantity)
else:
stock.quantity = stock.quantity + transaction.quantity
session.commit()
xml.log_transaction('COMMIT_BUY', transaction, status_message='success')
return xml.ResultResponse('success')
class CANCEL_BUYCommand(CommandHandler):
"""
Cancels the most recently executed BUY Command
"""
def run(self, username):
session = get_session()
user = session.query(User).filter_by(username=username).first()
if not user:
xml.log_error('CANCEL_BUY', 'username not found')
raise UserNotFoundError(username)
transaction = session.query(Transaction).filter_by(
username=user.username, operation='BUY', committed=False
).first()
if not transaction:
raise NoBuyTransactionError(username)
session.delete(transaction)
session.commit()
xml.log_transaction('CANCEL_BUY', transaction, status_message='success')
return xml.ResultResponse('success')
class SELLCommand(CommandHandler):
"""
Sell the specified dollar amount of the stock currently held by the
specified user at the current price.
"""
def run(self, username, stock_symbol, money_amount):
# see if user exists
session = get_session()
user = session.query(User).filter_by(username=username).first()
if not user:
raise UserNotFoundError(username)
# Check for existing uncommitted transaction
if session.query(Transaction).filter_by(
user=user, operation='SELL', committed=False).count() > 0:
raise SellTransactionActiveError()
#set up client to get quote
quote_client = get_quote_client()
quoted_stock_value = quote_client.get_quote(stock_symbol, username)
# Work out quantity of stock to sell, fail if not enough for one stock
money_amount = Money.from_string(money_amount)
quantity_to_sell = amount_to_quantity(quoted_stock_value, money_amount)
if quantity_to_sell == 0:
raise InsufficientFundsError()
# see if the user owns the requested stock and has enough for request
records = session.query(StockPurchase).filter_by(
username=user.username, stock_symbol=stock_symbol).all()
if(len(records) > 1):
raise UnknownCommandError('Multiple StockPurchase for user %s: %d',
username, len(records))
if len(records) != 1 or records[0].quantity < quantity_to_sell:
raise InsufficientStockError()
price = quoted_stock_value * quantity_to_sell
# make transaction
self.trans = Transaction(username=user.username,
stock_symbol=stock_symbol, operation='SELL', committed=False,
quantity=quantity_to_sell, stock_value=quoted_stock_value)
# commit transaction after all actions for atomicity
session.add(self.trans)
session.commit()
xml.log_transaction('SELL', self.trans, status_message='success')
return xml.QuoteResponse(quantity=quantity_to_sell, price=price)
class COMMIT_SELLCommand(CommandHandler):
"""
Commits the most recently executed SELL command
"""
def run(self, username):
session = get_session()
user = session.query(User).filter_by(username=username).first()
if not user:
raise UserNotFoundError(username)
transaction = session.query(Transaction).filter_by(
username=user.username, operation='SELL', committed=False
).first()
if not transaction:
raise NoSellTransactionError(username)
if (datetime.now() - transaction.creation_time) > config.TRANSACTION_TIMEOUT:
session.delete(transaction)
session.commit()
raise ExpiredSellTransactionError(username)
price = transaction.stock_value * transaction.quantity
user.account_balance += price
# update the StockPurchase for this stock symbol
stock = session.query(StockPurchase).filter_by(
user=user, stock_symbol=transaction.stock_symbol
).one()
stock.quantity = stock.quantity - transaction.quantity
transaction.committed = True
session.commit()
xml.log_transaction('COMMIT_SELL', transaction, status_message='success')
return xml.ResultResponse('success')
class CANCEL_SELLCommand(CommandHandler):
"""
Cancels the most recently executed SELL Command
"""
def run(self, username):
session = get_session()
user = session.query(User).filter_by(username=username).first()
if not user:
raise UserNotFoundError(username)
transaction = session.query(Transaction).filter_by(
username=user.username, operation='SELL', committed=False
).first()
if not transaction:
raise NoSellTransactionError(username)
session.delete(transaction)
session.commit()
xml.log_transaction('CANCEL_SELL', transaction, status_message='success')
return xml.ResultResponse('success')
class SET_BUY_AMOUNTCommand(CommandHandler):
"""
Sets a defined amount of the given stock to buy when the current stock
price is less than or equal to the BUY_TRIGGER
"""
def run(self, username, stock_symbol, amount):
session = get_session()
user = session.query(User).filter_by(username=username).first()
if not user:
raise UserNotFoundError(username)
# Work out quantity of stock to buy, fail if user has insufficient funds
amount = Money.from_string(amount)
if user.account_balance < amount:
raise InsufficientFundsError()
# Create inactive Trigger
set_transaction = Trigger(user=user, amount=amount,
operation='BUY', stock_symbol=stock_symbol, state=Trigger.State.INACTIVE)
session.add(set_transaction)
user.account_balance = user.account_balance - amount
user.reserve_balance += amount
session.commit()
xml.log_trigger('SET_BUY_AMOUNT', set_transaction, status_message='success')
return xml.ResultResponse('success')
class SET_SELL_AMOUNTCommand(CommandHandler):
"""
Sets a defined amount of the specified stock to sell when the current stock
price is equal or greater than the sell trigger point
"""
def run(self, username, stock_symbol, quantity):
session = get_session()
user = session.query(User).filter_by(username=username).first()
if not user:
raise UserNotFoundError(username)
quantity = int(quantity)
# see if the user owns the requested stock and has enough for request
records = session.query(StockPurchase).filter_by(
username=user.username, stock_symbol=stock_symbol).all()
if(len(records) > 1):
raise UnknownCommandError('Multiple StockPurchase for user %s: %d',
username, len(records))
if len(records) == 0 or records[0].quantity < quantity:
raise InsufficientStockError()
# Create inactive Trigger
set_transaction = Trigger(user=user, quantity=quantity,
operation='SELL', stock_symbol=stock_symbol, state=Trigger.State.INACTIVE)
session.add(set_transaction)
session.commit()
xml.log_trigger('SET_SELL_AMOUNT', set_transaction, status_message='success')
return xml.ResultResponse('success')
class CANCEL_SET_BUYCommand(CommandHandler):
"""
Cancels a SET_BUY command issued for the given stock
"""
def run(self, username, stock_symbol):
session = get_session()
user = session.query(User).filter_by(username=username).first()
if not user:
raise UserNotFoundError(username)
trigger = session.query(Trigger).filter_by(
username=user.username, operation='BUY', stock_symbol=stock_symbol,
).filter(
Trigger.state != Trigger.State.CANCELLED,
).first()
if not trigger:
raise NoTriggerError(username, stock_symbol)
trigger.state = Trigger.State.CANCELLED
session.commit()
xml.log_trigger('CANCEL_SET_BUY', trigger, status_message='success')
return xml.ResultResponse('trigger cancelled')
class SET_BUY_TRIGGERCommand(CommandHandler):
"""
Sets the trigger point base on the current stock price when any SET_BUY
will execute.
"""
def run(self, username, stock_symbol, amount):
session = get_session()
user = session.query(User).filter_by(username=username).first()
if not user:
raise UserNotFoundError(username)
amount = Money.from_string(amount)
trigger = session.query(Trigger).filter_by(
username=user.username, operation='BUY', stock_symbol=stock_symbol,
state=Trigger.State.INACTIVE
).first()
if not trigger:
raise NoTriggerError(username, stock_symbol)
trigger.state = Trigger.State.RUNNING
trigger.trigger_value = amount
session.commit()
self.session = session
eventlet.spawn(self.check_trigger, trigger)
xml.log_trigger('SET_BUY_TRIGGER', trigger, status_message='trigger set')
return xml.ResultResponse('trigger activated')
def check_trigger(self, trigger):
while True:
log.debug('Trigger %d checking for stock %s < %s',
trigger.id, trigger.stock_symbol, trigger.trigger_value)
# TODO: why is commit required here just to refresh an attribute?
self.session.refresh(trigger)
self.session.commit()
if trigger.state == Trigger.State.CANCELLED:
log.debug('Trigger %d cancelled!', trigger.id)
return
# Get a new quote for the stock
quote_client = get_quote_client()
quote = quote_client.get_quote(trigger.stock_symbol,
trigger.username)
log.debug('Trigger %d: %s => %s',
trigger.id, trigger.stock_symbol, quote)
# If quote is less than trigger value, buy stock and remove trigger
if quote < trigger.trigger_value:
# buy the stock and update reserve balance
log.debug("Trigger %d activated: %s < %s",
trigger.id, quote, trigger.trigger_value)
return self.process_transaction(quote, trigger)
eventlet.sleep(config.TRIGGER_INTERVAL)
def process_transaction(self, quote, trigger):
# Calculate real price of stock purchase based on current quote
user = trigger.user
quantity = amount_to_quantity(quote, trigger.amount)
real_amount = quote * quantity
log.debug('Trigger %d: Buying %d units of %s (%s total) for %s',
trigger.id, quantity, trigger.stock_symbol, real_amount, user.username)
# Use reserve balance to buy stock
user.reserve_balance -= trigger.amount
# Put extra money back in users account
extra = trigger.amount - real_amount
log.debug('Trigger %d: Extra money left over after purchase: %s', trigger.id, extra)
user.account_balance += extra
# create or update the StockPurchase for this stock symbol
stock = self.session.query(StockPurchase).filter_by(
user=user, stock_symbol=trigger.stock_symbol
).first()
if not stock:
stock = StockPurchase(user=user,
stock_symbol=trigger.stock_symbol,
quantity=quantity)
else:
stock.quantity = stock.quantity + quantity
self.session.delete(trigger)
self.session.commit()
xml.log_trigger('SET_BUY_TRIGGER', trigger, status_message='Trigger item bought')
class SET_SELL_TRIGGERCommand(CommandHandler):
"""
Sets the stock price trigger point for executing any SET_SELL triggers
associated with the given stock and user
"""
def run(self, username, stock_symbol, amount):
session = get_session()
user = session.query(User).filter_by(username=username).first()
if not user:
raise UserNotFoundError(username)
amount = Money.from_string(amount)
trigger = session.query(Trigger).filter_by(
username=user.username, operation='SELL', stock_symbol=stock_symbol,
state=Trigger.State.INACTIVE
).first()
if not trigger:
raise NoTriggerError(username, stock_symbol)
trigger.state = Trigger.State.RUNNING
trigger.trigger_value = amount
session.commit()
self.session = session
eventlet.spawn(self.check_trigger, trigger)
xml.log_trigger('SET_SELL_TRIGGER', trigger, status_message='trigger set')
return xml.ResultResponse('trigger activated')
def check_trigger(self, trigger):
while True:
log.debug('Trigger %d checking for stock %s > %s',
trigger.id, trigger.stock_symbol, trigger.trigger_value)
# TODO: why is commit required here just to refresh an attribute?
self.session.refresh(trigger)
self.session.commit()
if trigger.state == Trigger.State.CANCELLED:
log.debug('Trigger %d cancelled!', trigger.id)
return
# Get a new quote for the stock
quote_client = get_quote_client()
quote = quote_client.get_quote(trigger.stock_symbol,
trigger.username)
log.debug('Trigger %d: %s => %s',
trigger.id, trigger.stock_symbol, quote)
# If quote is greater than trigger value, buy stock and remove trigger
if quote > trigger.trigger_value:
# buy the stock and update reserve balance
log.debug("Trigger %d activated: %s > %s",
trigger.id, quote, trigger.trigger_value)
return self.process_transaction(quote, trigger)
eventlet.sleep(config.TRIGGER_INTERVAL)
def process_transaction(self, quote, trigger):
# Calculate real price of stock purchase based on current quote
user = trigger.user
price = quote * trigger.quantity
log.debug('Trigger %d: Selling %d units of %s (%s total) for %s',
trigger.id, trigger.quantity, trigger.stock_symbol, price, user.username)
user.account_balance += price
# update the StockPurchase for this stock symbol
stock = self.session.query(StockPurchase).filter_by(
user=user, stock_symbol=trigger.stock_symbol
).one()
stock.quantity = stock.quantity - trigger.quantity
self.session.delete(trigger)
self.session.commit()
xml.log_trigger('SET_SELL_TRIGGER', trigger, status_message='Trigger item sold')
class CANCEL_SET_SELLCommand(CommandHandler):
"""
Cancels the SET_SELL associated with the given stock and user
"""
def run(self, username, stock_symbol):
session = get_session()
user = session.query(User).filter_by(username=username).first()
if not user:
raise UserNotFoundError(username)
trigger = session.query(Trigger).filter_by(
username=user.username, operation='SELL', stock_symbol=stock_symbol,
).filter(
Trigger.state != Trigger.State.CANCELLED,
).first()
if not trigger:
raise NoTriggerError(username, stock_symbol)
trigger.state = Trigger.State.CANCELLED
session.commit()
xml.log_trigger('CANCEL_SET_SELL', trigger, status_message='success')
return xml.ResultResponse('trigger cancelled')
class DUMPLOG_USERCommand(CommandHandler):
"""
Print out the history of the users transactions to the user specified file
"""
def run(self, username, filename):
xml.log_event('DUMPLOG_USER', username, status_message= 'to file %s' % filename)
return xml.ResultResponse('success')
class DISPLAY_SUMMARYCommand(CommandHandler):
"""
Provides a summary to the client of the given user's transaction history
and the current status of their accounts as well as any set buy or sell
triggers and their parameters
"""
def run(self, username):
session = get_session()
user = session.query(User).filter_by(username=username).first()
if not user:
raise UserNotFoundError(username)
# Get this users transactions
transactions = session.query(Transaction).filter_by(user=user).all()
# Get this users triggers
triggers = session.query(Trigger).filter_by(user=user).all()
xml.log_event('DISPLAY_SUMMARY', username)
return xml.SummaryResponse(
transactions=transactions, triggers=triggers,
account_balance=user.account_balance,
reserve_balance=user.reserve_balance)
class DUMPLOGCommand(CommandHandler):
"""
Provides a summary to the client of the given user's transaction history
and the current status of their accounts as well as any set buy or sell
triggers and their parameters
"""
def run(self, *args):
if len(args) == 2:
username, filename = args
return self.dumplog_user(username, filename)
elif len(args) == 1:
filename = args[0]
return self.dumplog_admin(filename)
else:
raise TypeError('Incorrect arguments for command DUMPLOG')
def dumplog_user(self, username, filename):
session = get_session()
user = session.query(User).filter_by(username=username).first()
if not user:
raise UserNotFoundError(username)
# Get this users transactions
transactions = session.query(Transaction).filter_by(user=user).all()
# Note: this is not secure against directory traversal
full_path = path.join(config.DUMPLOG_DIR, filename)
with open(full_path, 'w') as f:
res = xml.DumplogResponse(transactions)
f.write(str(res))
xml.log_event('DUMPLOG', username, status_message='to file %s' % filename)
return xml.ResultResponse('Wrote transactions to "%s"' % full_path)
def dumplog_admin(self, filename):
# TODO: implement security for admin dumplog
session = get_session()
transactions = session.query(Transaction).all()
# Note: this is not secure against directory traversal
full_path = path.join(config.DUMPLOG_DIR, filename)
with open(full_path, 'w') as f:
res = xml.DumplogResponse(transactions)
f.write(str(res))
xml.log_event('DUMPLOG', username='Admin', status_message='to file %s' % filename)
return xml.ResultResponse('Wrote transactions to "%s"' % full_path)
def amount_to_quantity(price, amount):
""" Given the price of a stock and an maximum dollar value to buy, returns
the quantity of stock that can be bought. """
q = 0
while(True):
amount = amount - price
if (amount.dollars < 0) or (amount.cents < 0):
break
else:
q += 1
return q
CommandHandler.register_command('ADD', ADDCommand)
CommandHandler.register_command('QUOTE', QUOTECommand)
CommandHandler.register_command('BUY', BUYCommand)
CommandHandler.register_command('COMMIT_BUY', COMMIT_BUYCommand)
CommandHandler.register_command('CANCEL_BUY', CANCEL_BUYCommand)
CommandHandler.register_command('SELL', SELLCommand)
CommandHandler.register_command('COMMIT_SELL', COMMIT_SELLCommand)
CommandHandler.register_command('CANCEL_SELL', CANCEL_SELLCommand)
CommandHandler.register_command('SET_BUY_AMOUNT', SET_BUY_AMOUNTCommand)
CommandHandler.register_command('CANCEL_SET_BUY', CANCEL_SET_BUYCommand)
CommandHandler.register_command('SET_BUY_TRIGGER', SET_BUY_TRIGGERCommand)
CommandHandler.register_command('SET_SELL_AMOUNT', SET_SELL_AMOUNTCommand)
CommandHandler.register_command('SET_SELL_TRIGGER', SET_SELL_TRIGGERCommand)
CommandHandler.register_command('CANCEL_SET_SELL', CANCEL_SET_SELLCommand)
CommandHandler.register_command('DUMPLOG_USER', DUMPLOG_USERCommand)
CommandHandler.register_command('DUMPLOG', DUMPLOGCommand)
CommandHandler.register_command('DISPLAY_SUMMARY', DISPLAY_SUMMARYCommand)