-
Notifications
You must be signed in to change notification settings - Fork 13
/
investopedia_api.py
88 lines (67 loc) · 3.17 KB
/
investopedia_api.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
from trade_common import Expiration, OrderLimit, TransactionType, OptionTrade, StockTrade
from api_models import OptionChain, Parsers, stock_quote, OptionScope
from session_singleton import Session
import warnings
class InvestopediaApi(object):
def __init__(self, credentials,game_name=None, portfolio_id=None,game_id=None):
Session.login(credentials)
self.portfolios = Parsers.get_portfolios()
self.portfolio = self.portfolios[0]
if game_name is not None or portfolio_id is not None or game_id is not None:
self.change_portfolio(game_name,game_id,portfolio_id)
print("Initialized portfolio ID %s in game %s" % (self.portfolio.portfolio_id, self.portfolio.game_name))
#self.open_orders = self.portfolio.open_orders
def change_portfolio(self,game_name=None, game_id=None, portfolio_id=None):
if game_name is not None:
for portfolio in self.portfolios:
if portfolio.game_name == game_name:
self.portfolio = portfolio
print("changed portfolio to portfolio ID %s in game %s" % (self.portfolio.portfolio_id, self.portfolio.game_name))
return
warnings.warn("Portfolio not changed, could not find portfolio with game_name %s" % game_name)
return
elif game_id is not None:
for portfolio in self.portfolios:
if portfolio.game_id == game_id:
self.portfolio = portfolio
print("changed portfolio to portfolio ID %s in game %s" % (self.portfolio.portfolio_id, self.portfolio.game_name))
return
warnings.warn("Portfolio not changed, could not find portfolio with game_id %s" % game_id)
return
elif portfolio_id is not None:
for portfolio in self.portfolios:
if portfolio.game_id == game_id:
self.portfolio = portfolio
print("changed portfolio to portfolio ID %s in game %s" % (self.portfolio.portfolio_id, self.portfolio.game_name))
return
warnings.warn("Portfolio not changed, could not find portfolio with portfolio_id %s" % portfolio_id)
return
else:
warnings.warn("Portfolio not changed, specify a valid game_name, game_id, or portfolio_id")
def refresh_portfolio(self):
current_portfolio_id = self.portfolio.portfolio_id
self.portfolios = Parsers.get_portfolios()
for portfolio in self.portfolios:
if current_portfolio_id == portfolio.portfolio_id:
self.portfolio = portfolio
self.open_orders = self.portfolio.open_orders
@staticmethod
def get_option_chain(symbol):
return OptionChain(symbol)
@staticmethod
def get_stock_quote(symbol):
return stock_quote(symbol)
class StockTrade(StockTrade):
pass
class OptionTrade(OptionTrade):
pass
class Expiration(Expiration):
pass
class OrderLimit(OrderLimit):
pass
class TransactionType(TransactionType):
pass
class OptionChain(OptionChain):
pass
class OptionScope(OptionScope):
pass