-
Notifications
You must be signed in to change notification settings - Fork 1
/
agent.py
98 lines (90 loc) · 3.33 KB
/
agent.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
from Strategy.strategy import Strategy
from Strategy.position import Long, Short
class InvestorAgent(object):
def __init__(self, startingCapital: float, strat: Strategy, dateIndex: int):
self.capital_0 = startingCapital
self.capital_t = startingCapital
self.strategy = strat
self.gains = 0
self.positions = []
self.dateIndex = dateIndex
self.profits = []
self.capitalHistory = []
self.totalAssetHistory = []
def check_price(self, date):
"""
Gets the daily average price for a given date.
:param date: string of the data YYYY-MM-DD
:return: daily average price
"""
return self.strategy.daily_avg_price(date)
def signal(self, T, k=5):
"""
Whether to open a long position, short position,
or no position at all.
:param T: indicator variable
:param k: number of days
:return: signal
"""
threshold = k * 0.5 * self.strategy.p
if abs(T) > threshold:
if T > 0:
return 1
return -1
return 0
def buying_power(self, date):
"""
The maximum amount of shares the agent can buy.
:param date: current date YYYY-MM-DD
:return:
"""
price = self.check_price(date)
trueCapital = self.capital_t
for pos in self.positions:
if type(pos) == Short:
trueCapital -= pos.initialInvestment
power = int(trueCapital / price)
if power <= 0:
return 0
return power
def long(self, shareNum, date, stopLoss, posNum):
"""
Opens a long position
:param shareNum: number of shares
:param date: date of position opening
:param stopLoss: stop loss
:param posNum: position ID number
:return: string for logging the position
"""
price = self.strategy.daily_avg_price(date)
investment = shareNum * price
goal = price + (price * self.strategy.p)
position = Long(date, self.strategy.ticker, investment, price, goal, self.strategy.patience, stopLoss, shareNum,
posNum)
self.positions.append(position)
self.capital_t -= investment
return f'long,{price},{shareNum},{investment},,{self.capital_t}'
def short(self, shareNum, date, stopLoss, posNum):
"""
Opens a short position
:param shareNum: number of shares
:param date: date of position opening
:param stopLoss: stop loss
:param posNum: position ID number
:return: string for loggin the position
"""
borrowPrice = self.strategy.daily_avg_price(date)
investment = shareNum * borrowPrice
goalPrice = (1 - self.strategy.p) * borrowPrice
position = Short(date, self.strategy.ticker, investment, borrowPrice,
goalPrice, self.strategy.patience, stopLoss, shareNum, posNum)
self.positions.append(position)
self.capital_t += investment
return f'short,{borrowPrice},{shareNum},{investment},,{self.capital_t}'
def update_assets(self, update):
"""
Updates the current assets of the agent.
:param update: thing to update
:return: None
"""
self.totalAssetHistory.append(update)