-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPortfolioManagementSystem.py
More file actions
55 lines (47 loc) · 1.79 KB
/
PortfolioManagementSystem.py
File metadata and controls
55 lines (47 loc) · 1.79 KB
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
class PortfolioManagementSystem(TradingSystem):
def __init__(self):
super().__init__(AlpacaPaperSocket(), 'IBM', 86400, 1, 'AI_PM')
self.AI = PortfolioManagementModel()
def place_buy_order(self):
self.api.submit_order(
symbol='IBM',
qty=1,
side='buy',
type='market',
time_in_force='day',
)
def place_sell_order(self):
self.api.submit_order(
symbol='IBM',
qty=1,
side='sell',
type='market',
time_in_force='day',
)
def system_loop(self):
# Variables for weekly close
this_weeks_close = 0
last_weeks_close = 0
delta = 0
day_count = 0
while(True):
# Wait a day to request more data
time.sleep(1440)
# Request EoD data for IBM
data_req = self.api.get_barset('IBM', timeframe='1D', limit=1).df
# Construct dataframe to predict
x = pd.DataFrame(
data=[[
data_req['IBM']['close'][0]]], columns='Close'.split()
)
if(day_count == 7):
day_count = 0
last_weeks_close = this_weeks_close
this_weeks_close = x['Close']
delta = this_weeks_close - last_weeks_close
# AI choosing to buy, sell, or hold
if np.around(self.AI.network.predict([delta])) <= -.5:
self.place_sell_order()
elif np.around(self.AI.network.predict([delta]) >= .5):
self.place_buy_order()
PortfolioManagementSystem()