-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbot.py
120 lines (105 loc) · 4.56 KB
/
bot.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
from strategies.strategy import Strategy
from etrade.etrade import ETrade
from execute.execute import Execute
class Bot:
"""
:description: This class is the main strategy class that will be used to run the strategy.
:param consumer_key: Your E-Trade API consumer key
:type consumer_key: str, required
:param consumer_secret: Your E-Trade API consumer secret
:type consumer_secret: str, required
:param web_username: Your E-Trade web username
:type web_username: str, required
:param web_password: Your E-Trade web password
:type web_password: str, required
:param account_id: Your E-Trade account ID
:type account_id: str, required
:param etrade_cookie: Your E-Trade cookie
:type etrade_cookie: str, required
:param account_id_key: Your E-Trade account ID key
:type account_id_key: str, required
:param strategy_name: The name of the strategy to run
:type strategy_name: str, required
:param sandbox_key: Your E-Trade sandbox API key, defaults to None
:type sandbox_key: str, optional
:param sandbox_secret: Your E-Trade sandbox API secret, defaults to None
:type sandbox_secret: str, optional
:param dev: Whether to use the E-Trade sandbox, defaults to True
:type dev: bool, optional
:param headless: Whether to run the browser in headless mode, defaults to True
:type headless: bool, optional
:param browser: The browser to use, defaults to 'chrome'
:type browser: str, optional
:param preview: Whether to preview the order, defaults to True
:type preview: bool, optional
:param prints: Whether to print the order, defaults to True
:type prints: bool, optional
"""
def __init__(
self, consumer_key, consumer_secret, web_username, web_password, account_id, etrade_cookie,
account_id_key, strategy_name, sandbox_key=None, sandbox_secret=None, dev=True, headless=True,
browser='chrome', preview=True, prints=False
):
"""
:description: This method initializes the bot.
:return: None
:rtype: None
"""
print('Starting bot...')
# Initialize objects
self.etrade = ETrade(
consumer_key, consumer_secret, web_username, web_password, account_id, etrade_cookie,
sandbox_key, sandbox_secret, dev, headless, browser
)
self.strategy = Strategy(prints, strategy_name)
self.execute = Execute(self.etrade)
# Retrieve credentials
self.consumer_key = consumer_key
self.consumer_secret = consumer_secret
self.web_username = web_username
self.web_password = web_password
self.etrade_cookie = etrade_cookie
self.account_id = account_id
self.account_id_key = account_id_key
# Run strategy
self.strategy_name = strategy_name
self.new_portfolio = self.strategy.run_strategy()
# Preview or execute trades, print information
self.preview = preview
self.prints = prints
def run(self):
"""
:description: This method runs the strategy and previews or executes the trades.
:return: The trade responses from E-Trade
:rtype: pd.DataFrame
"""
print('\nRunning strategy: {}'.format(self.strategy_name))
# Get current portfolio
current_portfolio = self.etrade.get_portfolio_data(self.account_id_key)[[
'pctOfPortfolio', 'quantity', 'positionType'
]]
# Get buying power
buying_power = self.etrade.get_buying_power(self.account_id_key, prints=self.prints)
# Calculate new portfolio shares
new_portfolio_shares = self.execute.calculate_shares(self.new_portfolio, buying_power, prints=self.prints)
# Execute trades
print('')
if self.preview:
print('Previewing Trades...\n')
place_order = self.execute.execute_trades(
current_portfolio, new_portfolio_shares, self.account_id_key, self.preview, self.prints
)
else:
# Preview trades first
print('Previewing Trades...\n')
self.preview = True
self.execute.execute_trades(
current_portfolio, new_portfolio_shares, self.account_id_key, self.preview, self.prints
)
# Execute trades
print('\nExecuting Trades...\n')
self.preview = False
place_order = self.execute.execute_trades(
current_portfolio, new_portfolio_shares, self.account_id_key, self.preview, self.prints
)
return place_order