-
Notifications
You must be signed in to change notification settings - Fork 0
/
trader.py
109 lines (85 loc) · 3.88 KB
/
trader.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
import uuid
import random
from order import Order, SideOrder
from account import Account
import logging
logger = logging.getLogger(__name__)
class SideJob:
def __init__(self, income, period):
self.income = income
self.period = period
class Trader:
def __init__(self, money=1000000, risk_aversion=10, side_job=None):
self.id = uuid.uuid4()
# wallet
self.account = Account(money, 0)
# the trader has external source of income
self.risk_aversion = risk_aversion
# side job
self.side_job = side_job
# operations
self.created_orders = []
self.open_orders = []
# knowledge of the market
self.first_buyer = None
self.first_seller = None
def getId(self):
return self.id
def observe(self, market):
self.first_seller = market.sellers[0] # TODO: replace by getter checking length
self.first_buyer = market.buyer[0] # TODO: replace by getter checking length
def take_action(self):
self.observe()
random.randind(0, 10)
def create_order(self, ticker: str, side: SideOrder, price: float, volume: float) -> Order:
logger.info("Creating order for ticker " + ticker)
# print("Creating order for ticker",ticker,"side",side,"price",price,"volume",volume)
# TODO: Check money to buy, and check assets to sell
if self.account.enough_money(price, volume):
if side is SideOrder.BUY:
# hold money to buy
if not self.account.lock_money(price, volume):
logger.warning("Not enough money to create buy order")
return None
if side is SideOrder.SELL:
# hold assets for sell
if not self.account.hold_assets(price, volume):
logger.warning("Not enough assets to create sell order")
return None
# at this point the order can be created
order = Order(self.id, ticker, side, price, volume)
# create the order
self.created_orders.append(order)
# print("created order",order)
return order
logger.warning("Not enough money")
return None
def cancel_order(self, order_id: Order) -> bool:
for created_order in self.created_orders:
if order_id == created_order.id:
self.created_orders.remove(created_order)
if created_order.side == SideOrder.BUY:
if self.account.release_nomey(created_order.price, created_order.volume):
logger.info("Buy order was canceled OK - money released")
return True
if created_order.side == SideOrder.SELL:
if self.account.release_assets(created_order.ticker, created_order.volume):
logger.info("Sell order was canceled OK - assets released")
return True
logger.warning("Order was not cancelled")
return False
def add_open_order(self, order: Order) -> bool:
# move order from created order to open orders
for created_order in self.created_orders:
if order.id == created_order.id:
self.open_orders.append(created_order)
self.created_orders.remove(created_order)
logger.info("Order opened in the market with success")
return True
logger.warning("A problem was found opening current order")
return False
def __repr__(self):
rep = ["Trader{id: ", str(self.id), ",money: ", str(self.account.money), ",money_hold: ",
"{0:10d} ".format(self.account.money_hold), ", # created orders: ",
"".join(str(len(self.created_orders))), ", # open orders: ", "".join(str(len(self.open_orders))), "}"]
return "".join(rep)