33
44
55class Equity (CoreModule ):
6- def __init__ (self , email , password , reality , browserPath , headless = False , timeout = 2 ):
6+ def __init__ (self , email , password , reality , browserPath , headless = False , timeout = 2 , loadSymbols = False ):
77 super ().__init__ (email , password , TradingType .Equity ,
88 reality , headless , browserPath , timeout )
9- self .reality = reality
9+ self .reality = Reality .Real
10+
11+ if (loadSymbols ):
12+ self .saveAllInstruments ()
13+ else :
14+ self .loadedInstruments = False
1015
1116 # Format url on whether is live
1217 def urlf (self , url ):
@@ -21,13 +26,134 @@ def urlf(self, url):
2126 # Not sure what sellThreshold is
2227 # The sell parameters only appear for UK stocks for some reason
2328 def getMinMax (self , code ):
24- return super (). get (self .urlf ("/rest/v1/equity/value-order/min-max?instrumentCode=" + code ), cookies = self .cookiePayload )
29+ return get (self .urlf ("/rest/v1/equity/value-order/min-max?instrumentCode=" + code ), cookies = self .cookiePayload )
2530
2631 # Takes instrument code, returns maxBuy and maxSell in shares for the account and max buy and sell that is technically
2732 # possible on the exchange, also has minTrade and if suspended
2833 def getSettings (self , code ):
29- return super (). post (self .urlf ("/rest/v2/account/instruments/settings" ), cookies = self .cookiePayload , payload = [code ])
34+ return post (self .urlf ("/rest/v2/account/instruments/settings" ), cookies = self .cookiePayload , payload = [code ])
3035
3136 # Gets the account performance graph data
3237 def getPortfolioPerformance (self , historyPeriod ):
33- return super ().get (self .urlf ("/rest/v2/portfolio?period=" + historyPeriod ), cookies = self .cookiePayload )
38+ return get (self .urlf ("/rest/v2/portfolio?period=" + historyPeriod ), cookies = self .cookiePayload )
39+
40+ # Get all instruments on Trading212!
41+ def getAllInstruments (self ):
42+ return get (self .urlf ("/rest/v2/instruments/" ))
43+
44+ # Gets company ISINs and tickers for graphs etc
45+ def getTickers (self ):
46+ return get (self .urlf ("/rest/companies" ))
47+
48+ # Saves all instruments in a dictionary useful for symbol lookup
49+ def saveAllInstruments (self ):
50+ logging .info ("Loading all instruments, this will take a while..." )
51+ self .instruments = self .getAllInstruments ()
52+ # Don't think getting tickers is actually required
53+ #self.tickerISINs = self.getTickers()
54+ self .loadedInstruments = True
55+
56+ # Search the instrument dictionary
57+ def instrumentSearch (self , searchUsing , searchKey , resultUsing ):
58+ if (not self .loadedInstruments ):
59+ self .saveAllInstruments ()
60+ for i in self .instruments :
61+ try :
62+ if (i [searchUsing ] == searchKey ):
63+ return i [resultUsing ]
64+ except :
65+ pass
66+
67+ # Gets a symbols name
68+ def getName (self , isin = "" , prettyName = "" , code = "" , id = "" ):
69+ if (isin != "" ):
70+ return self .instrumentSearch ("isin" , isin , "name" )
71+ if (prettyName != "" ):
72+ return self .instrumentSearch ("prettyName" , prettyName , "name" )
73+ if (code != "" ):
74+ return self .instrumentSearch ("code" , code , "name" )
75+ if (id != "" ):
76+ return self .instrumentSearch ("id" , id , "name" )
77+ else :
78+ raise Exception (
79+ "ISIN, prettyName, code, or id is required to find name" )
80+
81+ # Gets a symbols prettyName
82+ def getPrettyName (self , isin = "" , name = "" , code = "" , id = "" ):
83+ if (isin != "" ):
84+ return self .instrumentSearch ("isin" , isin , "prettyName" )
85+ if (name != "" ):
86+ return self .instrumentSearch ("name" , name , "prettyName" )
87+ if (code != "" ):
88+ return self .instrumentSearch ("code" , code , "prettyName" )
89+ if (id != "" ):
90+ return self .instrumentSearch ("id" , id , "prettyName" )
91+ else :
92+ raise Exception (
93+ "ISIN, name, code, or id is required to find prettyName" )
94+
95+ # Gets a symbols code
96+ def getCode (self , isin = "" , name = "" , prettyName = "" , id = "" ):
97+ if (isin != "" ):
98+ return self .instrumentSearch ("isin" , isin , "code" )
99+ if (name != "" ):
100+ return self .instrumentSearch ("name" , name , "code" )
101+ if (prettyName != "" ):
102+ return self .instrumentSearch ("prettyName" , prettyName , "code" )
103+ if (id != "" ):
104+ return self .instrumentSearch ("id" , id , "code" )
105+ else :
106+ raise Exception (
107+ "ISIN, name, prettyName, or id is required to find code" )
108+
109+ # Gets a symbols ISIN
110+ def getISIN (self , name = "" , prettyName = "" , code = "" , id = "" ):
111+ if (name != "" ):
112+ return self .instrumentSearch ("name" , name , "isin" )
113+ if (prettyName != "" ):
114+ return self .instrumentSearch ("prettyName" , prettyName , "isin" )
115+ if (code != "" ):
116+ return self .instrumentSearch ("code" , code , "isin" )
117+ if (id != "" ):
118+ return self .instrumentSearch ("id" , id , "isin" )
119+ else :
120+ raise Exception (
121+ "Name, prettyName, code, or id is required to find ISIN" )
122+
123+ # Gets a symbols ID
124+ def getID (self , isin = "" , name = "" , prettyName = "" , code = "" ):
125+ if (isin != "" ):
126+ return self .instrumentSearch ("isin" , isin , "id" )
127+ if (name != "" ):
128+ return self .instrumentSearch ("name" , name , "id" )
129+ if (prettyName != "" ):
130+ return self .instrumentSearch ("prettyName" , prettyName , "id" )
131+ if (code != "" ):
132+ return self .instrumentSearch ("code" , code , "id" )
133+ else :
134+ raise Exception (
135+ "ISIN, name, prettyName, or code is required to find id" )
136+
137+ # Get instrument details position code from the secret API
138+ def getInstrument (self , code ):
139+ return get (self .urlf ("/rest/v2/instruments/" + code ))
140+
141+ # Get instrument details by ISIN
142+ # If language is not available, Trading212 seems to return English
143+ def getFundamentals (self , isin , langCode = "en" ):
144+ return get (self .urlf ("/rest/companies/fundamentals?languageCode=" + langCode + "&isin=" + isin ))
145+
146+ # Gets chart data for a particular ticker
147+ # When getting chart data, the ticker returned is the code not what is on the stock exchange!
148+ def getChartData (self , code , chartPeriod , size , includeFake = False ):
149+ payload = {
150+ "candles" : [
151+ {
152+ "ticker" : code ,
153+ "period" : chartPeriod ,
154+ "size" : size ,
155+ "includeFake" : includeFake
156+ }
157+ ]
158+ }
159+ return post (url = self .urlf ("/charting/v2/batch" ), payload = payload )
0 commit comments