-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrade_operator.py
93 lines (74 loc) · 2.41 KB
/
trade_operator.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
from portfolio_manager import PortfolioManager
import alpaca_trade_api as tradeapi
import schedule
import os, json
class PortfolioTrader():
def __init__(self,
funding=4000,
barTimeInterval = "minute",
strategy="sharpe",
symbolsList = None):
BASE_URL = 'https://paper-api.alpaca.markets'
try:
with open('./secrets.json') as secrets:
data = json.load(secrets)
self.api = tradeapi.REST(
key_id=data['KeyID'],
secret_key=data['SecretKey'],
base_url='https://paper-api.alpaca.markets', api_version='v2'
)
except RuntimeError as e:
raise SystemExit("Not found secrets API key")
self.strategy = strategy
self.symbols_list = symbolsList
self.manager = PortfolioManager(totalFunding=funding, symbolList=symbolsList)
self.manager.setStockPrices()
self.manager.optimizePortfolio(strategy)
self.allocate = self.manager.allocateFunding()[0]
def monitor(self):
self.allocate = self.manager.allocateFunding()[0]
for symbol in self.symbols_list:
self.manager.calculateEMA(symbol)
order = self.manager.trade_signal(symbol)
if order and order is True:
self.place_buy_order(symbol, self.allocate[symbol])
elif order and order is False:
self.place_sell_order(symbol, self.allocate[symbol])
def place_buy_order(self, symbol, num_shares):
print(symbol, num_shares)
api.submit_order(
symbol=symbol,
qty=num_shares,
side='buy',
type='market',
time_in_force='gtc'
)
def place_sell_order(self, symbol, num_shares):
print(symbol, num_shares)
api.submit_order(
symbol=symbol,
qty=num_shares,
side='sell',
type='market',
time_in_force='gtc'
)
def system_loop(self):
pass
if __name__ == "__main__":
trader = PortfolioTrader(
funding=4000,
strategy='volatility',
symbolsList=[
"MDB",
"PYPL",
"DDOG",
"NVDA",
"V",
"MA",
"TEAM",
"FB",
"AMZN",
"AAPL",
"NFLX",
"GOOG",
])