@@ -29,7 +29,7 @@ def __init__(self, key, username, password, account, acc_type='DEMO'):
2929 self .s = requests .Session ()
3030 self .config = Config (self .account_type )
3131 self .ls_client = None
32- self .version = '1.0.5 '
32+ self .version = '1.0.6 '
3333
3434 def getVersion (self ):
3535 return self .version
@@ -98,85 +98,57 @@ def getProfitLoss(self, account_num=''):
9898 return acc .loc [account_num , 'balance' ]['profitLoss' ]
9999
100100 def watchlists (self ):
101- try :
102- response = self .s .get (self .config .endpoint + '/watchlists' , headers = {'X-IG-API-KEY' : self .api_key , 'CST' : self .CST , 'X-SECURITY-TOKEN' : self .X_SECURITY_TOKEN , 'Content-Type' : 'application/json;' , 'Accept' : 'application/json; charset=UTF-8' , 'VERSION' : '1' })
103- data = json .loads (response .text )
104- df = pd .DataFrame (data ['watchlists' ])
105- df = df .set_index ('name' )
106- return df
107- except Exception as e :
108- logging .error (e )
109- return pd .DataFrame ()
101+ response = self .s .get (self .config .endpoint + '/watchlists' , headers = {'X-IG-API-KEY' : self .api_key , 'CST' : self .CST , 'X-SECURITY-TOKEN' : self .X_SECURITY_TOKEN , 'Content-Type' : 'application/json;' , 'Accept' : 'application/json; charset=UTF-8' , 'VERSION' : '1' })
102+ data = json .loads (response .text )
103+ df = pd .DataFrame (data ['watchlists' ])
104+ df = df .set_index ('name' )
105+ return df
110106
111107 def watchlist (self , id ):
112- try :
113- response = self .s .get (self .config .endpoint + '/watchlists/' + id , headers = {'X-IG-API-KEY' : self .api_key , 'CST' : self .CST , 'X-SECURITY-TOKEN' : self .X_SECURITY_TOKEN , 'Content-Type' : 'application/json;' , 'Accept' : 'application/json; charset=UTF-8' , 'VERSION' : '1' })
114- data = json .loads (response .text )
115- df = pd .DataFrame (data ['markets' ])
116- df = df .set_index ('epic' )
117- return df
118- except Exception as e :
119- logging .error (e )
120- return pd .DataFrame ()
108+ response = self .s .get (self .config .endpoint + '/watchlists/' + id , headers = {'X-IG-API-KEY' : self .api_key , 'CST' : self .CST , 'X-SECURITY-TOKEN' : self .X_SECURITY_TOKEN , 'Content-Type' : 'application/json;' , 'Accept' : 'application/json; charset=UTF-8' , 'VERSION' : '1' })
109+ data = json .loads (response .text )
110+ df = pd .DataFrame (data ['markets' ])
111+ df = df .set_index ('epic' )
112+ return df
121113
122114 def getPrice (self , epic , resolution = '' , numPoints = 0 ):
123- try :
124- response = self .s .get (self .config .endpoint + '/markets/' + epic , headers = {'X-IG-API-KEY' : self .api_key , 'CST' : self .CST , 'X-SECURITY-TOKEN' : self .X_SECURITY_TOKEN , 'Content-Type' : 'application/json;' , 'Accept' : 'application/json; charset=UTF-8' , 'VERSION' : '2' })
125- data = json .loads (response .text )
126- tz = pytz .timezone ('Europe/London' )
127- d = dt .now (tz ).strftime ("%Y-%m-%d" )
128- df = pd .DataFrame ([data ['snapshot' ]])
129- df ['updateTime' ] = d + ' ' + df ['updateTime' ]
130- return df
131- except Exception as e :
132- logging .error (e )
133- return pd .DataFrame ()
115+ response = self .s .get (self .config .endpoint + '/markets/' + epic , headers = {'X-IG-API-KEY' : self .api_key , 'CST' : self .CST , 'X-SECURITY-TOKEN' : self .X_SECURITY_TOKEN , 'Content-Type' : 'application/json;' , 'Accept' : 'application/json; charset=UTF-8' , 'VERSION' : '2' })
116+ data = json .loads (response .text )
117+ tz = pytz .timezone ('Europe/London' )
118+ d = dt .now (tz ).strftime ("%Y-%m-%d" )
119+ df = pd .DataFrame ([data ['snapshot' ]])
120+ df ['updateTime' ] = d + ' ' + df ['updateTime' ]
121+ return df
134122
135123 def getPrices (self , epic , resolution = '' , numPoints = 0 , start = '' , end = '' ):
136- try :
137- params = epic
138- if resolution != '' and numPoints != 0 :
139- params += '/' + resolution + '/' + str (numPoints )
140- elif resolution != '' and start != '' and end != '' :
141- params += '/' + resolution + '/' + start + '/' + end
142- response = self .s .get (self .config .endpoint + '/prices/' + params , headers = {'X-IG-API-KEY' : self .api_key , 'CST' : self .CST , 'X-SECURITY-TOKEN' : self .X_SECURITY_TOKEN , 'Content-Type' : 'application/json;' , 'Accept' : 'application/json; charset=UTF-8' , 'VERSION' : '2' })
143- data = json .loads (response .text )
144- df = pd .DataFrame (data ['prices' ])
145- return df
146- except Exception as e :
147- logging .error (e )
148- return pd .DataFrame ()
124+ params = epic
125+ if resolution != '' and numPoints != 0 :
126+ params += '/' + resolution + '/' + str (numPoints )
127+ elif resolution != '' and start != '' and end != '' :
128+ params += '/' + resolution + '/' + start + '/' + end
129+ response = self .s .get (self .config .endpoint + '/prices/' + params , headers = {'X-IG-API-KEY' : self .api_key , 'CST' : self .CST , 'X-SECURITY-TOKEN' : self .X_SECURITY_TOKEN , 'Content-Type' : 'application/json;' , 'Accept' : 'application/json; charset=UTF-8' , 'VERSION' : '2' })
130+ data = json .loads (response .text )
131+ df = pd .DataFrame (data ['prices' ])
132+ return df
149133
150134 def getOpenPosition (self , dealId = '' ):
151- try :
152- params = ''
153- if dealId != '' :
154- params = '/' + str (dealId )
155- response = self .s .get (self .config .endpoint + '/positions' + params , headers = {'X-IG-API-KEY' : self .api_key , 'CST' : self .CST , 'X-SECURITY-TOKEN' : self .X_SECURITY_TOKEN , 'Content-Type' : 'application/json;' , 'Accept' : 'application/json; charset=UTF-8' , 'VERSION' : '2' })
156- data = json .loads (response .text )
157- if dealId != '' :
158- df = pd .DataFrame (data )
159- else :
160- df = pd .DataFrame (data ['positions' ])
161- return df
162- except Exception as e :
163- logging .error (e )
164- return pd .DataFrame ()
135+ params = ''
136+ if dealId != '' :
137+ params = '/' + str (dealId )
138+ response = self .s .get (self .config .endpoint + '/positions' + params , headers = {'X-IG-API-KEY' : self .api_key , 'CST' : self .CST , 'X-SECURITY-TOKEN' : self .X_SECURITY_TOKEN , 'Content-Type' : 'application/json;' , 'Accept' : 'application/json; charset=UTF-8' , 'VERSION' : '2' })
139+ data = json .loads (response .text )
140+ if dealId != '' :
141+ df = pd .DataFrame (data )
142+ else :
143+ df = pd .DataFrame (data ['positions' ])
144+ return df
165145
166146 def closePosition (self , dealId , direction , expiry , orderType , size ):
167- try :
168- response = self .s .post (self .config .endpoint + '/positions/otc' , headers = {'X-IG-API-KEY' : self .api_key , 'CST' : self .CST , 'X-SECURITY-TOKEN' : self .X_SECURITY_TOKEN , 'Content-Type' : 'application/json;' , 'Accept' : 'application/json; charset=UTF-8' , '_method' :'DELETE' , 'VERSION' : '1' }, data = json .dumps ({"dealId" : dealId , "direction" : direction , "expiry" : expiry , "orderType" : orderType , "size" : size }))
169- data = json .loads (response .text )
170- return data ['dealReference' ]
171- except Exception as e :
172- logging .error (e )
173- return pd .DataFrame ()
147+ response = self .s .post (self .config .endpoint + '/positions/otc' , headers = {'X-IG-API-KEY' : self .api_key , 'CST' : self .CST , 'X-SECURITY-TOKEN' : self .X_SECURITY_TOKEN , 'Content-Type' : 'application/json;' , 'Accept' : 'application/json; charset=UTF-8' , '_method' :'DELETE' , 'VERSION' : '1' }, data = json .dumps ({"dealId" : dealId , "direction" : direction , "expiry" : expiry , "orderType" : orderType , "size" : size }))
148+ data = json .loads (response .text )
149+ return data ['dealReference' ]
174150
175151 def createPosition (self , currency , direction , epic , expiry , orderType , size , limitDistance = None , stopDistance = None , forceOpen = True , guaranteedStop = False ):
176- try :
177- response = self .s .post (self .config .endpoint + '/positions/otc' , headers = {'X-IG-API-KEY' : self .api_key , 'CST' : self .CST , 'X-SECURITY-TOKEN' : self .X_SECURITY_TOKEN , 'Content-Type' : 'application/json;' , 'Accept' : 'application/json; charset=UTF-8' , 'VERSION' : '2' }, data = json .dumps ({"currencyCode" : currency , "direction" : direction , "epic" : epic , "expiry" : expiry , "orderType" : orderType , "size" : size , "limitDistance" : limitDistance , "stopDistance" : stopDistance , "forceOpen" : forceOpen , "guaranteedStop" : guaranteedStop }))
178- data = json .loads (response .text )
179- return data ['dealReference' ]
180- except Exception as e :
181- logging .error (e )
182- return pd .DataFrame ()
152+ response = self .s .post (self .config .endpoint + '/positions/otc' , headers = {'X-IG-API-KEY' : self .api_key , 'CST' : self .CST , 'X-SECURITY-TOKEN' : self .X_SECURITY_TOKEN , 'Content-Type' : 'application/json;' , 'Accept' : 'application/json; charset=UTF-8' , 'VERSION' : '2' }, data = json .dumps ({"currencyCode" : currency , "direction" : direction , "epic" : epic , "expiry" : expiry , "orderType" : orderType , "size" : size , "limitDistance" : limitDistance , "stopDistance" : stopDistance , "forceOpen" : forceOpen , "guaranteedStop" : guaranteedStop }))
153+ data = json .loads (response .text )
154+ return data ['dealReference' ]
0 commit comments