|  | 
|  | 1 | +from datetime import datetime | 
|  | 2 | +from abc import ABC | 
|  | 3 | +from .constants import OrderStatus, AccountStatus, ReturnStatus | 
|  | 4 | +from .order import LimitOrder | 
|  | 5 | +from .stock_exchange import StockExchange | 
|  | 6 | + | 
|  | 7 | + | 
|  | 8 | +class Account(ABC): | 
|  | 9 | + def __init__(self, id, password, name, address, email, phone, status=AccountStatus.NONE): | 
|  | 10 | + self.__id = id | 
|  | 11 | + self.__password = password | 
|  | 12 | + self.__name = name | 
|  | 13 | + self.__address = address | 
|  | 14 | + self.__email = email | 
|  | 15 | + self.__phone = phone | 
|  | 16 | + self.__status = AccountStatus.NONE | 
|  | 17 | + | 
|  | 18 | + def reset_password(self): | 
|  | 19 | + None | 
|  | 20 | + | 
|  | 21 | + | 
|  | 22 | +class Member(Account): | 
|  | 23 | + def __init__(self): | 
|  | 24 | + self.__available_funds_for_trading = 0.0 | 
|  | 25 | + self.__date_of_membership = datetime.date.today() | 
|  | 26 | + self.__stock_positions = {} | 
|  | 27 | + self.__active_orders = {} | 
|  | 28 | + | 
|  | 29 | + def place_sell_limit_order(self, stock_id, quantity, limit_price, enforcement_type): | 
|  | 30 | + # check if member has this stock position | 
|  | 31 | + if stock_id not in self.__stock_positions: | 
|  | 32 | + return ReturnStatus.NO_STOCK_POSITION | 
|  | 33 | + | 
|  | 34 | + stock_position = self.__stock_positions[stock_id] | 
|  | 35 | + # check if the member has enough quantity available to sell | 
|  | 36 | + if stock_position.get_quantity() < quantity: | 
|  | 37 | + return ReturnStatus.INSUFFICIENT_QUANTITY | 
|  | 38 | + | 
|  | 39 | + order = LimitOrder(stock_id, quantity, limit_price, enforcement_type) | 
|  | 40 | + order.is_buy_order = False | 
|  | 41 | + order.save_in_DB() | 
|  | 42 | + success = StockExchange.place_order(order) | 
|  | 43 | + if success: | 
|  | 44 | + order.set_status(OrderStatus.FAILED) | 
|  | 45 | + order.save_in_DB() | 
|  | 46 | + else: | 
|  | 47 | + self.active_orders.add(order.get_order_id(), order) | 
|  | 48 | + return success | 
|  | 49 | + | 
|  | 50 | + def place_buy_limit_order(self, stock_id, quantity, limit_price, enforcement_type): | 
|  | 51 | + # check if the member has enough funds to buy this stock | 
|  | 52 | + if self.__available_funds_for_trading < quantity * limit_price: | 
|  | 53 | + return ReturnStatus.INSUFFICIENT_FUNDS | 
|  | 54 | + | 
|  | 55 | + order = LimitOrder(stock_id, quantity, limit_price, enforcement_type) | 
|  | 56 | + order.is_buy_order = True | 
|  | 57 | + order.save_in_DB() | 
|  | 58 | + success = StockExchange.place_order(order) | 
|  | 59 | + if not success: | 
|  | 60 | + order.set_status(OrderStatus.FAILED) | 
|  | 61 | + order.save_in_DB() | 
|  | 62 | + else: | 
|  | 63 | + self.active_orders.add(order.get_order_id(), order) | 
|  | 64 | + return success | 
|  | 65 | + | 
|  | 66 | + # this function will be invoked whenever there is an update from | 
|  | 67 | + # stock exchange against an order | 
|  | 68 | + def callback_stock_exchange(self, order_id, order_parts, status): | 
|  | 69 | + order = self.active_orders[order_id] | 
|  | 70 | + order.add_order_parts(order_parts) | 
|  | 71 | + order.set_status(status) | 
|  | 72 | + order.update_in_DB() | 
|  | 73 | + | 
|  | 74 | + if status == OrderStatus.FILLED or status == OrderStatus.CANCELLEd: | 
|  | 75 | + self.active_orders.remove(order_id) | 
0 commit comments