-
Notifications
You must be signed in to change notification settings - Fork 13
/
example.py
203 lines (173 loc) · 8.14 KB
/
example.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
from investopedia_api import InvestopediaApi, StockTrade, OptionTrade, Expiration, OrderLimit, TransactionType, OptionScope, OptionChain
import json
from datetime import datetime, timedelta
credentials = {}
with open('credentials.json') as ifh:
credentials = json.load(ifh)
# look at credentials_example.json
# credentials = {"username": "you@example.org", "password": "yourpassword" }
client = InvestopediaApi(credentials)
p = client.portfolio
print("\nPortfolio Details")
print("-------------------------------------------------")
print("Portfolio Value: %s" % p.account_value)
print("Cash: %s" % p.cash)
print("Buying Power: %s" % p.buying_power)
print("Annual Return Percent: %s" % p.annual_return_pct)
print("-------------------------------------------------")
print("\nOpen Orders:")
# To cancel a pending trade, run open_order.cancel()
for open_order in p.open_orders:
print("-------------------------------------------------")
print("Trade Type: %s" % open_order.trade_type)
print("Symbol: %s" % open_order.symbol)
print("Quantity: %s" % open_order.quantity)
print("Price: %s" % open_order.order_price)
print("-------------------------------------------------")
print("-------------------------------------------------")
print("\nStock Portfolio Details:")
print("-------------------------------------------------")
print("Market Value: %s" % p.stock_portfolio.market_value)
print("Today's Gain: %s (%s%%)" % (p.stock_portfolio.day_gain_dollar, p.stock_portfolio.day_gain_percent))
print("Total Gain: %s (%s%%)" % (p.stock_portfolio.total_gain_dollar, p.stock_portfolio.total_gain_percent))
print("-------------------------------------------------")
print("\nLong Positions:")
for position in p.stock_portfolio:
print("-------------------------------------------------")
print("Company: %s (%s)" % (position.description, position.symbol))
print("Shares: %s" % position.quantity)
print("Purchase Price: %s" % position.purchase_price)
print("Current Price: %s" % position.current_price)
print("Today's Gain: %s (%s%%)" % (position.day_gain_dollar, position.day_gain_percent))
print("Total Gain: %s (%s%%)" % (position.total_gain_dollar, position.total_gain_percent))
print("Market/Total Value: %s" % position.market_value)
print("\t------------------------------")
print("\tQuote")
print("\t------------------------------")
quote = position.quote
for k,v in quote.__dict__.items():
print("\t%s: %s" % (k,v))
print("\t------------------------------")
print("-------------------------------------------------")
print("\nShort Positions:")
for position in p.short_portfolio:
print("-------------------------------------------------")
print("Company: %s (%s)" % (position.description, position.symbol))
print("Shares: %s" % position.quantity)
print("Purchase Price: %s" % position.purchase_price)
print("Current Price: %s" % position.current_price)
print("Today's Gain: %s (%s%%)" % (position.day_gain_dollar, position.day_gain_percent))
print("Total Gain: %s (%s%%)" % (position.total_gain_dollar, position.total_gain_percent))
print("Market/Total Value: %s" % position.market_value)
print("\t------------------------------")
print("\tQuote")
print("\t------------------------------")
quote = position.quote
for k,v in quote.__dict__.items():
print("\t%s: %s" % (k,v))
print("\t------------------------------")
print("-------------------------------------------------")
print("\nOption Positions:")
for position in p.option_portfolio:
print("-------------------------------------------------")
print("Company: %s (%s)" % (position.description, position.underlying_symbol))
print("Symbol: %s" % position.symbol)
print("Contracts: %s" % position.quantity)
print("Purchase Price: %s" % position.purchase_price)
print("Current Price: %s" % position.current_price)
print("Today's Gain: %s (%s%%)" % (position.day_gain_dollar, position.day_gain_percent))
print("Total Gain: %s (%s%%)" % (position.total_gain_dollar, position.total_gain_percent))
print("Market/Total Value: %s" % position.market_value)
print("\t------------------------------")
print("\tQuote")
print("\t------------------------------")
quote = position.quote
for k,v in quote.__dict__.items():
print("\t%s: %s" % (k,v))
print("\t------------------------------")
print("-------------------------------------------------")
# Make a stock trade
# Buy 2 shares of GOOG with limit $100 and no expiration
tt1 = TransactionType.BUY
ol1 = OrderLimit.LIMIT(100)
exp1 = Expiration.GOOD_UNTIL_CANCELLED()
trade1 = StockTrade(portfolio_id=p.portfolio_id, symbol="GOOG", quantity=2, transaction_type=tt1, order_limit=ol1, expiration=exp1)
trade1.validate()
trade1.execute()
# Buy 3 shares of AAPL at market value with expiration set to end of day
# defaults order_limit to OrderLimit.MARKET() and expiration to Expiration.END_OF_DAY())
trade2 = StockTrade(portfolio_id=p.portfolio_id, symbol='AAPL', quantity=3, transaction_type=TransactionType.BUY)
trade2.validate()
trade2.execute()
# short sell 1 share of AMZN
trade3 = StockTrade(portfolio_id=p.portfolio_id, symbol='AMZN', quantity=1, transaction_type=TransactionType.SELL_SHORT)
trade3.validate()
trade3.execute()
p.refresh()
for open_order in p.open_orders:
if open_order.symbol == 'GOOG' and open_order.quantity == 2:
# cancel GOOG trade
open_order.cancel()
if open_order.symbol == 'AAPL' and open_order.quantity == 3:
# cancel AAPL trade
open_order.cancel()
if open_order.symbol == 'AMZN' and open_order.quantity == 1:
# cancel AMZN trade
open_order.cancel()
stock_portfolio = p.stock_portfolio
if len(p.stock_portfolio) > 0:
# first long position in portfolio
first_long_position = p.stock_portfolio[0]
symbol = first_long_position.symbol
quantity = first_long_position.quantity
# execute trade to sell position in portfolio
first_long_position.sell()
client.refresh_portfolio()
p = client.portfolio
for oo in p.open_orders:
if oo.symbol == symbol and oo.quantity == quantity:
# cancel trade to sell first position in portfolio
oo.cancel()
short_portfolio = p.short_portfolio
if len(p.short_portfolio) > 0:
# first short position in portfolio
first_short_position = p.short_portfolio[0]
symbol = first_short_position.symbol
quantity = first_short_position.quantity
# execute trade to cover position in portfolio
first_short_position.cover()
p.refresh()
for oo in p.open_orders:
# cancel cover trade you just made
if oo.symbol == symbol and oo.quantity == quantity:
# cancel trade to cover first position in portfolio
oo.cancel()
if len(p.option_portfolio) > 0:
first_option_contract = p.option_portfolio[0]
symbol = first_option_contract.symbol
quantity = first_option_contract.quantity
# close out first option contract in portfolio
first_option_contract.close()
p.refresh()
for oo in p.open_orders:
# cancel order to close out contract
if oo.symbol == symbol and oo.quantity == quantity:
oo.cancel()
# Gets all available option contracts for AAPL
oc = client.get_option_chain('AAPL')
all_options = oc.all()
print("There are %s available option contracts for AAPL" % len(all_options))
two_weeks_from_today = datetime.now() + timedelta(days=14)
print("AAPL in-the-money put options expiring within two weeks:")
put_options_near_expiration_itm = oc.search(before=two_weeks_from_today, puts=True, calls=False, scope=OptionScope.IN_THE_MONEY)
for option in put_options_near_expiration_itm:
print("%s:\n\tbid: %s\n\task: %s\n\tlast price: %s\n\texpires:%s" % (option.symbol, option.bid, option.ask, option.last, option.expiration.strftime("%m/%d/%Y") ))
option_to_buy = put_options_near_expiration_itm[0]
trade4 = OptionTrade(portfolio_id=p.portfolio_id, symbol=option_to_buy.symbol, quantity=1, transaction_type=TransactionType.BUY)
trade4.validate()
trade4.execute()
p.refresh()
p = client.portfolio
for oo in p.open_orders:
if oo.symbol == option_to_buy.symbol:
oo.cancel()