-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbroker.py
61 lines (51 loc) · 2.23 KB
/
broker.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
# from portfolio import Portfolio
from portfolio import OrderStatus
from slack import WebClient
from keys import SLACK_TOKEN
from utils import send_slack_dm
class Broker():
""" Broker object. Executes orders. Contains portfolio information """
def __init__(self, commission=None, portfolio=None, send_note=False):
self.commission = commission
self.portfolio = portfolio
self.delay = 0
self.slackClient = WebClient(token=SLACK_TOKEN)
self.send_note = send_note
return
def execute_buy(self, portfolio, order, current_price):
if order.delay > 0:
order.delay -= 1
return order
order.status = OrderStatus(order.status.value + 1)
if not self.check_funds(portfolio, order, current_price):
order.status = OrderStatus(4)
return order
else:
order.status = OrderStatus(order.status.value + 1)
portfolio.cash -= order.size * current_price
order.status = OrderStatus(order.status.value + 1)
if self.send_note:
send_slack_dm(self.slackClient, "{} {} shares of {}!".format(order.ordertype, order.size, order.symbol))
print("Cash left to Trade after Buy: ", portfolio.cash)
return order
def execute_sell(self, portfolio, order, current_price):
if order.delay > 0:
order.delay -= 1
return order
order.status = OrderStatus(order.status.value + 1)
order.status = OrderStatus(order.status.value + 1)
portfolio.cash += order.size * current_price
order.status = OrderStatus(order.status.value + 1)
if self.send_note:
send_slack_dm(self.slackClient, "{} {} shares of {}!".format(order.ordertype, order.size, order.symbol))
print("Cash left to Trade after Sell: ", portfolio.cash)
return order
def check_funds(self, portfolio, order, current_price):
return portfolio.cash >= order.size * current_price
class Commission():
""" Commission object """
def __init__(self, fixed=0.0, percentage=0.0, fixed_per_share=0.0):
self.fixed = fixed
self.percentage = percentage
self.fixed_per_share = fixed_per_share
return