Skip to content

Commit

Permalink
💄 PnL code structure alignement
Browse files Browse the repository at this point in the history
  • Loading branch information
mraniki committed Apr 12, 2024
1 parent 63bcfdf commit e9e0a61
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
23 changes: 6 additions & 17 deletions cefi/handler/capitalcom.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"""

import asyncio
from datetime import datetime, timedelta

from capitalcom.client import Client, DirectionType
from capitalcom.client_demo import Client as DemoClient
Expand Down Expand Up @@ -194,34 +193,24 @@ async def get_account_position(self):
except Exception as e:
logger.error(f"{self.name} Error {e}")

async def get_account_pnl(self, period=None):
async def calculate_pnl(self, period=None):
"""
Return account pnl.
no pnl info available via openapi endpoint
Args:
None
Returns:
pnl
"""
today = datetime.now().date()
if period is None:
start_date = today
elif period == "W":
start_date = today - timedelta(days=today.weekday())
elif period == "M":
start_date = today.replace(day=1)
elif period == "Y":
start_date = today.replace(month=1, day=1)

end_date = datetime.now()
formatted_end_date = end_date.strftime("%Y-%m-%dT%H:%M:%S")
logger.debug("{} {}", start_date, formatted_end_date)
# end_date = datetime.now()
# formatted_end_date = end_date.strftime("%Y-%m-%dT%H:%M:%S")
# logger.debug("{} {}", start_date, formatted_end_date)
# history = self.client.account_activity_history(
# fr=start_date, to=formatted_end_date, detailed=True
# )
# logger.debug("History: {}", history)
# no pnl info available via openapi endpoint

return 0

async def pre_order_checks(self, order_params):
Expand Down
43 changes: 43 additions & 0 deletions cefi/handler/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"""

from datetime import datetime, timedelta

from loguru import logger


Expand All @@ -22,6 +24,23 @@ class CexClient:
Returns:
None
Methods:
get_quote(self, instrument)
get_offer(self, instrument)
get_bid(self, instrument)
get_account_balance(self)
get_account_position(self)
calculate_pnl(self, period=None)
get_account_pnl(self, start_date)
execute_order(self, order_params)
get_trading_asset_balance(self)
get_order_amount(self, quantity, instrument, is_percentage)
pre_order_checks(self, order_params)
replace_instrument(self, instrument)
get_instrument_decimals(self, instrument)
get_trade_confirmation(self, order_params)
"""

def __init__(self, **kwargs):
Expand Down Expand Up @@ -61,6 +80,7 @@ def __init__(self, **kwargs):
self.mapping = kwargs.get("mapping", None)
self.balance_limit = kwargs.get("balance_limit", True)
self.balance_limit_value = kwargs.get("balance_limit_value", 10)
self.is_pnl_active = kwargs.get("is_pnl_active", False)

except Exception as error:
logger.error("Client initialization error {}", error)
Expand Down Expand Up @@ -130,6 +150,29 @@ async def get_account_pnl(self, period=None):
Returns:
pnl
"""
today = datetime.date.today()
if period is None:
start_date = today
elif period == "W":
start_date = today - timedelta(days=today.isoweekday() - 1)
elif period == "M":
start_date = today.replace(day=1)
elif period == "Y":
start_date = today.replace(month=1, day=1)
else:
return 0
return self.calculate_pnl(start_date) if self.is_pnl_active else 0

async def calculate_pnl(self, period=None):
"""
Calculate the PnL for a given period.
Parameters:
period (str): The period for which to calculate PnL ('W', 'M', 'Y', or None).
Returns:
pnl: The calculated PnL value.
"""

async def execute_order(self, order_params):
"""
Expand Down

0 comments on commit e9e0a61

Please sign in to comment.