-
Notifications
You must be signed in to change notification settings - Fork 29
/
bots.py
159 lines (137 loc) · 7.17 KB
/
bots.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
import numpy as np
class Bot:
"""
This is a general bot structure.
"""
def __init__(self, capital: float, portfolio: dict = None):
self.capital = capital
self.uninvested = capital
self.invested = 0
self.portfolio = {} if portfolio is None else portfolio
def transact_capital(self, ticker, units: int, price: float, type: str):
if type == "sell":
transaction = units * price
self.uninvested += transaction
del self.portfolio[ticker]
elif type == "buy":
transaction = units * price
self.uninvested -= transaction
self.portfolio[ticker] = {"units": units, "purchase_price": price}
else:
raise Exception("Transaction type {} not recognised. Choose between `sell` and `buy`.".format(type))
def compute_capital(self, price: dict):
self.invested = 0.0
for ticker in self.portfolio:
self.invested += self.portfolio[ticker]['units'] * price[ticker]
self.capital = self.uninvested + self.invested
class Adam(Bot):
def __init__(self, capital: float, portfolio: dict = None):
super().__init__(capital, portfolio)
self.min_rel_profit = 0.03
self.max_rel_loss = 0.1
def trade(self, info: dict):
## sell strategy
owned_stocks = list(self.portfolio.keys())
for ticker in owned_stocks:
purchase_price = self.portfolio[ticker]["purchase_price"]
rel_margin = (info[ticker]['price'] - purchase_price) / purchase_price
if rel_margin > self.min_rel_profit or rel_margin < -self.max_rel_loss:
self.transact_capital(ticker, self.portfolio[ticker]['units'], info[ticker]['price'], type="sell")
## buy strategy
for ticker in info:
if ticker not in self.portfolio.keys() and info[ticker]['rate'] == "HIGHLY BELOW TREND":
units = int(np.minimum(self.uninvested, self.capital / 30) // info[ticker]['price'])
if units >= 1:
self.transact_capital(ticker, units, info[ticker]['price'], type="buy")
class Betty(Bot):
def __init__(self, capital: float, portfolio: dict = None):
super().__init__(capital, portfolio)
self.min_rel_profit = 0.1
self.max_rel_loss = 0.03
def trade(self, info: dict):
## sell strategy
owned_stocks = list(self.portfolio.keys())
for ticker in owned_stocks:
purchase_price = self.portfolio[ticker]["purchase_price"]
rel_margin = (info[ticker]['price'] - purchase_price) / purchase_price
if rel_margin > self.min_rel_profit or rel_margin < -self.max_rel_loss:
self.transact_capital(ticker, self.portfolio[ticker]['units'], info[ticker]['price'], type="sell")
## buy strategy
for ticker in info:
if ticker not in self.portfolio.keys() and info[ticker]['rate'] == "HIGHLY ABOVE TREND":
units = int(np.minimum(self.uninvested, self.capital / 30) // info[ticker]['price'])
if units >= 1:
self.transact_capital(ticker, units, info[ticker]['price'], type="buy")
class Chris(Bot):
def __init__(self, capital: float, portfolio: dict = None):
super().__init__(capital, portfolio)
self.buy_only = ["GOOGL", "AMZN", "AAPL", "MSFT", "FB"]
def trade(self, info: dict):
## buy strategy
buy_only = [ticker for ticker in self.buy_only if ticker in info]
for ticker in buy_only:
if ticker in info:
count = np.maximum(1, len(buy_only) - len(self.portfolio))
units = int(self.uninvested / count // info[ticker]['price'])
if units >= 1:
self.transact_capital(ticker, units, info[ticker]['price'], type="buy")
class Dany(Bot):
def __init__(self, capital: float, portfolio: dict = None):
super().__init__(capital, portfolio)
self.min_rel_profit = 0.1
self.max_rel_loss = 0.2
def trade(self, info: dict):
## sell strategy
owned_stocks = list(self.portfolio.keys())
for ticker in owned_stocks:
purchase_price = self.portfolio[ticker]["purchase_price"]
rel_margin = (info[ticker]['price'] - purchase_price) / purchase_price
if rel_margin > self.min_rel_profit or rel_margin < -self.max_rel_loss:
self.transact_capital(ticker, self.portfolio[ticker]['units'], info[ticker]['price'], type="sell")
## buy strategy
for ticker in info:
if ticker not in self.portfolio.keys() and info[ticker]['rate'] in ["HIGHLY BELOW TREND", "BELOW TREND"]:
units = int(np.minimum(self.uninvested, self.capital / 30) // info[ticker]['price'])
if units >= 1:
self.transact_capital(ticker, units, info[ticker]['price'], type="buy")
class Eddy(Bot):
def __init__(self, capital: float, portfolio: dict = None):
super().__init__(capital, portfolio)
self.min_rel_profit = 0.2
self.max_rel_loss = 0.1
def trade(self, info: dict):
## sell strategy
owned_stocks = list(self.portfolio.keys())
for ticker in owned_stocks:
purchase_price = self.portfolio[ticker]["purchase_price"]
rel_margin = (info[ticker]['price'] - purchase_price) / purchase_price
if rel_margin > self.min_rel_profit or rel_margin < -self.max_rel_loss:
self.transact_capital(ticker, self.portfolio[ticker]['units'], info[ticker]['price'], type="sell")
## buy strategy
for ticker in info:
if ticker not in self.portfolio.keys() and info[ticker]['rate'] in ["HIGHLY ABOVE TREND", "ABOVE TREND"]:
units = int(np.minimum(self.uninvested, self.capital / 30) // info[ticker]['price'])
if units >= 1:
self.transact_capital(ticker, units, info[ticker]['price'], type="buy")
class Flora(Bot):
def __init__(self, capital: float, portfolio: dict = None):
super().__init__(capital, portfolio)
self.min_rel_profit = 0.1
self.max_rel_loss = 0.2
def trade(self, info: dict):
## sell strategy
owned_stocks = list(self.portfolio.keys())
for ticker in owned_stocks:
purchase_price = self.portfolio[ticker]["purchase_price"]
rel_margin = (info[ticker]['price'] - purchase_price) / purchase_price
if rel_margin > self.min_rel_profit or rel_margin < -self.max_rel_loss:
self.transact_capital(ticker, self.portfolio[ticker]['units'], info[ticker]['price'], type="sell")
## buy strategy
growths = np.array([info[ticker]['growth'] for ticker in info])
idx = np.argsort(growths)[::-1]
sorted_tickers = np.array(list(info.keys()))[idx]
for ticker in sorted_tickers:
if ticker not in self.portfolio.keys() and info[ticker]['rate'] == "ALONG TREND" and info[ticker]['growth'] >= 1:
units = int(np.minimum(self.uninvested, self.capital / 30) // info[ticker]['price'])
if units >= 1:
self.transact_capital(ticker, units, info[ticker]['price'], type="buy")