Skip to content

Commit 5374c57

Browse files
author
Tusamma Sal Sabil
committed
Add Online Stock Brokerage System Related Demo Codes
1 parent 885cf8d commit 5374c57

File tree

4 files changed

+155
-0
lines changed

4 files changed

+155
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from enum import Enum
2+
3+
4+
class ReturnStatus(Enum):
5+
SUCCESS, FAIL, INSUFFICIENT_FUNDS, INSUFFICIENT_QUANTITY, NO_STOCK_POSITION = 1, 2, 3, 4, 5, 6
6+
7+
8+
class OrderStatus(Enum):
9+
OPEN, FILLED, PARTIALLY_FILLED, CANCELLED = 1, 2, 3, 4
10+
11+
12+
class TimeEnforcementType(Enum):
13+
GOOD_TILL_CANCELLED, FILL_OR_KILL, IMMEDIATE_OR_CANCEL, ON_THE_OPEN, ON_THE_CLOSE = 1, 2, 3, 4, 5
14+
15+
16+
class AccountStatus(Enum):
17+
ACTIVE, CLOSED, CANCELED, BLACKLISTED, NONE = 1, 2, 3, 5
18+
19+
20+
class Location:
21+
def __init__(self, street, city, state, zip_code, country):
22+
self.__street_address = street
23+
self.__city = city
24+
self.__state = state
25+
self.__zip_code = zip_code
26+
self.__country = country
27+
28+
29+
class Constants:
30+
def __init__(self):
31+
self.__MONEY_TRANSFER_LIMIT = 100000
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from abc import ABC
2+
from datetime import datetime
3+
from .constants import OrderStatus, TimeEnforcementType
4+
5+
6+
class Order(ABC):
7+
def __init__(self, id):
8+
self.__order_id = id
9+
self.__is_buy_order = False
10+
self.__status = OrderStatus.OPEN
11+
self.__time_enforcement = TimeEnforcementType.ON_THE_OPEN
12+
self.__creation_time = datetime.now()
13+
14+
self.__parts = {}
15+
16+
def set_status(self, status):
17+
self.status = status
18+
19+
def save_in_DB(self):
20+
None
21+
22+
# save in the database
23+
24+
def add_order_parts(self, parts):
25+
for part in parts:
26+
self.parts[part.get_id()] = part
27+
28+
29+
class LimitOrder(Order):
30+
def __init__(self):
31+
self.__price_limit = 0.0
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from .order import Order
2+
3+
4+
class StockExchange:
5+
# singleton, used for restricting to create only one instance
6+
instance = None
7+
8+
class __OnlyOne:
9+
def __init__(self):
10+
None
11+
12+
def __init__(self):
13+
if not StockExchange.instance:
14+
StockExchange.instance = StockExchange.__OnlyOne()
15+
16+
def place_order(self, order):
17+
return_status = self.get_instance().submit_order(Order)
18+
return return_status

0 commit comments

Comments
 (0)