Skip to content

Commit

Permalink
fix: improving Watchlist time efficiency
Browse files Browse the repository at this point in the history
Initially Watchlist would request all instruments within the watchlist
which would require a large number of requests, slowing down the
watchlist enabled feature significantly. However now it doesn't get all
instruments, just requests them when needed in certain methods.
  • Loading branch information
hnewey7 committed Apr 8, 2024
1 parent 3245675 commit e18d202
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions src/ig_package/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ def __init__(self,id:str,IG_obj: IG) -> None:
self.id = watchlist_dict["id"]
self.name = watchlist_dict["name"]
self.IG_obj = IG_obj
self.markets = self._get_instrument_objects()
self.markets: list[Instrument] = []

def _get_instruments_IG(self) -> list | None:
""" Getting financial instruments held within the watchlist from the IG API.
Expand Down Expand Up @@ -417,11 +417,22 @@ def add_instrument(self,instrument_name:str) -> str:
response = self.IG_obj.request_handler.send_request("https://api.ig.com/gateway/deal/markets?searchTerm={}".format(instrument_name),"GET",headers=self.IG_obj.header)
instruments = json.loads(response.text)["markets"]
top_instrument_epic = instruments[0]["epic"]
# Sending request to add instrument to watchlist.
logger.info(f"Adding top market ({top_instrument_epic}) to watchlist ({self.id})")
response = self.IG_obj.request_handler.send_request("https://api.ig.com/gateway/deal/watchlists/{}".format(self.id),"PUT",headers=self.IG_obj.header,data=json.dumps({"epic":top_instrument_epic}))
# Updating markets.
self.markets = self._get_instrument_objects()

# Checking if epic is already present in watchlist.
instruments = self._get_instruments_IG()
present_check = False
for instrument in instruments:
if instrument["epic"] == top_instrument_epic:
present_check = True

if not present_check:
# Sending request to add instrument to watchlist.
logger.info(f"Adding top market ({top_instrument_epic}) to watchlist ({self.id})")
response = self.IG_obj.request_handler.send_request("https://api.ig.com/gateway/deal/watchlists/{}".format(self.id),"PUT",headers=self.IG_obj.header,data=json.dumps({"epic":top_instrument_epic}))
# Creating new instrument.
new_instrument = Instrument(top_instrument_epic,self.IG_obj)
self.markets.append(new_instrument)

return top_instrument_epic

def del_instrument(self,instrument_name:str=None,epic:str=None) -> None:
Expand All @@ -442,8 +453,11 @@ def del_instrument(self,instrument_name:str=None,epic:str=None) -> None:
# Sending request to delete instrument from watchlist.
logger.info(f"Requesting instrument to be removed ({instrument.epic}) from watchlist ({self.id}).")
response = self.IG_obj.request_handler.send_request("https://api.ig.com/gateway/deal/watchlists/{}/{}".format(self.id,instrument.epic),"DELETE",headers=self.IG_obj.header)
# Updating markets.
self.markets = self._get_instrument_objects()

# Removing instrument from markets.
for instrument in self.markets:
if instrument.epic == epic or instrument.name == instrument_name:
self.markets.remove(instrument)

def get_all_historical_data(self,resolution:str,start:str,end:str) -> dict:
""" Gets all historical data from instruments contained within the watchlist.
Expand All @@ -461,6 +475,8 @@ def get_all_historical_data(self,resolution:str,start:str,end:str) -> dict:
-------
dict
Dictionary of all dataframes with the key being the instrument name."""
# Getting all markets.
self.markets = self._get_instrument_objects()
# Creating dictionary to store all dataframes.
df_dict = {}
for instrument in self.markets:
Expand Down

0 comments on commit e18d202

Please sign in to comment.