Skip to content

Commit

Permalink
Merge pull request #478 from mraniki/dev
Browse files Browse the repository at this point in the history
🥚 update capitalcom.py with _fetch_account_data
  • Loading branch information
mraniki authored Jul 11, 2024
2 parents 4675b86 + 9b43143 commit 85b8193
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 53 deletions.
4 changes: 4 additions & 0 deletions cefi/default_settings.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ port = 7497
secret= ""
password = ""
testmode = true
default_account = ""
broker_client_id = ""
broker_account_number = ""
broker_gateway = true
Expand Down Expand Up @@ -60,6 +61,7 @@ balance_limit_value = 10
# secret= ""
# password = ""
# testmode = true
# default_account = ""
# defaulttype = "spot"
# ordertype = "market"
# leverage_type = "isolated"
Expand All @@ -85,6 +87,7 @@ balance_limit_value = 10
# api_key= "myapi_DEADBEEF"
# password = "12345678"
# testmode = false
# default_account = ""
# defaulttype = ""
# ordertype = ""
# leverage_type = ""
Expand Down Expand Up @@ -120,6 +123,7 @@ balance_limit_value = 10
# secret= ""
# password = ""
# testmode = true
# default_account = ""
# broker_client_id = ""
# broker_account_number = ""
# broker_gateway = true
Expand Down
126 changes: 73 additions & 53 deletions cefi/handler/capitalcom.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,45 +49,65 @@ def __init__(
"""
super().__init__(**kwargs)
self._build_client()
if self.client:
self.accounts_data = self.client.all_accounts()
logger.debug("Account data: {}", self.accounts_data)
self.account_number = self.accounts_data["accounts"][0]["accountId"]
logger.debug("Account number: {}", self.account_number)
logger.debug("Session details: {}", self.client.get_sesion_details())
else:
logger.warning("No capital.com client. Verify settings.")

def _build_client(self):
"""
Builds and sets the client based on the testmode flag.
Capital.com session last only 10 minutes
"""
self.client = DemoClient if self.testmode else Client
self.client = self.client(
log=self.user_id,
pas=self.password,
api_key=self.api_key,
)
logger.debug("Client: {}", self.client)
if self._fetch_account_data():
logger.debug("Capital.com client set with account {}", self.account_number)
return self.client
else:
self.client = None
logger.warning("No capital.com client. Verify settings.")
return False

def _fetch_account_data(self):
"""
Fetches account data from the client,
extracts the account number,
and logs account data and session details.
Parameters:
self: the CapitalHandler object
Returns:
None
"""
try:
if self.testmode:
self.client = DemoClient(
log=self.user_id,
pas=self.password,
api_key=self.api_key,
)
else:
self.client = Client(
log=self.user_id,
pas=self.password,
api_key=self.api_key,
)
logger.debug("Client: {}", self.client)
if self.client.accounts_data():
return self.client
else:
logger.warning("No valid capital.com client. Verify settings.")
self.client = None
if self.default_account:
logger.debug("Default account: {}", self.default_account)
self.switch_account(self.default_account)
self.accounts_data = self.client.all_accounts()
except Exception as e:
logger.error("{} Error {}", self.name, e)
self.client = None
return e
logger.error("Error fetching account data: {}", e)
return False
logger.debug("Account data: {}", self.accounts_data)
self.account_number = self.accounts_data["accounts"][0]["accountId"]
logger.debug("Account number: {}", self.account_number)
logger.debug("Session details: {}", self.client.get_sesion_details())

def switch_account(self, account_number):
"""
Switches to the specified account.
Parameters:
account_number: The account number to switch to.
Returns:
None
"""
self.client.switch_account(self.default_account)

async def get_quote(self, instrument):
"""
Expand All @@ -97,21 +117,21 @@ async def get_quote(self, instrument):
:param instrument: The instrument for which the quote is to be fetched.
:return: The fetched quote.
"""
self._build_client()
logger.debug("Instrument: {}", instrument)
instrument = await self.replace_instrument(instrument)
logger.debug("Changed Instrument: {}", instrument)
# search_markets = self.client.searching_market(searchTerm=instrument)
await asyncio.sleep(1) # Wait for 1 second
# logger.debug("Instrument verification: {}", search_markets)
if self._build_client():
logger.debug("Instrument: {}", instrument)
instrument = await self.replace_instrument(instrument)
logger.debug("Changed Instrument: {}", instrument)
# search_markets = self.client.searching_market(searchTerm=instrument)
await asyncio.sleep(1) # Wait for 1 second
# logger.debug("Instrument verification: {}", search_markets)

market = self.client.single_market(instrument)
# logger.debug("market: {}", market)
market = self.client.single_market(instrument)
# logger.debug("market: {}", market)

quote = market["snapshot"]["offer"]
logger.debug("Quote: {}", quote)
quote = market["snapshot"]["offer"]
logger.debug("Quote: {}", quote)

return float(quote)
return float(quote)

# Alias for get_quote
get_offer = get_quote
Expand All @@ -128,21 +148,21 @@ async def get_bid(self, instrument):
Returns:
The bid for the specified instrument.
"""
self._build_client()
logger.debug("Instrument: {}", instrument)
instrument = await self.replace_instrument(instrument)
logger.debug("Changed Instrument: {}", instrument)
# search_markets = self.client.searching_market(searchTerm=instrument)
await asyncio.sleep(1) # Wait for 1 second
# logger.debug("Instrument verification: {}", search_markets)
if self._build_client():
logger.debug("Instrument: {}", instrument)
instrument = await self.replace_instrument(instrument)
logger.debug("Changed Instrument: {}", instrument)
# search_markets = self.client.searching_market(searchTerm=instrument)
await asyncio.sleep(1) # Wait for 1 second
# logger.debug("Instrument verification: {}", search_markets)

market = self.client.single_market(instrument)
# logger.debug("market: {}", market)
market = self.client.single_market(instrument)
# logger.debug("market: {}", market)

quote = market["snapshot"]["bid"]
logger.debug("Quote: {}", quote)
quote = market["snapshot"]["bid"]
logger.debug("Quote: {}", quote)

return float(quote)
return float(quote)

async def get_account_balance(self):
"""
Expand Down
1 change: 1 addition & 0 deletions cefi/handler/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def __init__(self, **kwargs):
self.secret = kwargs.get("secret", None)
self.password = kwargs.get("password", None)
self.testmode = kwargs.get("testmode", None)
self.default_account = kwargs.get("default_account", None)
self.trading_asset = kwargs.get("trading_asset", None)
self.separator = kwargs.get("trading_asset_separator", None)
self.account_number = None
Expand Down

0 comments on commit 85b8193

Please sign in to comment.