Skip to content

Commit

Permalink
added params for brokerage commission
Browse files Browse the repository at this point in the history
  • Loading branch information
prithviraj-gotepatil-algobulls committed Jun 29, 2023
1 parent 36effa6 commit 86e8d71
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions pyalgotrading/algobulls/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ def __init__(self):
self.papertrade_pnl_data = None
self.realtrade_pnl_data = None



@staticmethod
def get_authorization_url():
"""
Expand Down Expand Up @@ -296,14 +294,16 @@ def get_report(self, strategy_code, trading_type, report_type, render_as_datafra
else:
print('Report not available yet. Please retry in sometime')

def get_pnl_report_table(self, strategy_code, trading_type, location):
def get_pnl_report_table(self, strategy_code, trading_type, location, broker_commission_percentage=0, broker_commission_price=None):
"""
Fetch BT/PT/RT Profit & Loss details
Args:
strategy_code: strategy code
trading_type: type of trades : Backtesting, Papertrading, Realtrading
location: Location of the exchange
broker_commission_percentage: Percentage of broker commission per trade
broker_commission_price: Broker fee per trade
Returns:
Report details
Expand Down Expand Up @@ -341,9 +341,16 @@ def get_pnl_report_table(self, strategy_code, trading_type, location):
_df['exit_transaction_type'] = _df['exit_transaction_type'].apply(lambda _: 'BUY' if _ else 'SELL')
_df["pnl_cumulative_absolute"] = _df["pnl_absolute"].cumsum(axis=0, skipna=True)

# add brokerage
_df['brokerage'] = ((_df['entry_price'] * _df['entry_quantity']) + (_df['exit_price'] * _df['exit_quantity'])) * (broker_commission_percentage / 100)
if broker_commission_price is not None:
_df["brokerage"].loc[_df["brokerage"] > broker_commission_price] = broker_commission_price
_df['net_pnl'] = _df['pnl_absolute'] - _df['brokerage']

else:
# No data available, send back an empty dataframe
_df = pd.DataFrame(columns=list(column_rename_map.values()))
_df['net_pnl'] = None

return _df

Expand All @@ -364,7 +371,7 @@ def get_report_statistics(self, strategy_code, initial_funds, report, html_dump,
order_report = None

# get pnl data and cleanup as per quantstats format
_returns_df = pnl_df[['entry_timestamp', 'pnl_absolute']]
_returns_df = pnl_df[['entry_timestamp', 'net_pnl']]
_returns_df['entry_timestamp'] = _returns_df['entry_timestamp'].dt.tz_localize(None) # Note: Quantstats has a bug. It doesn't accept the df index, which is set below, with timezone. Hence, we have to drop the timezone info
_returns_df = _returns_df.set_index('entry_timestamp')
_returns_df["total_funds"] = _returns_df.pnl_absolute.cumsum() + initial_funds
Expand Down Expand Up @@ -621,7 +628,7 @@ def get_backtesting_logs(self, strategy_code):

return self.get_logs(strategy_code, trading_type=TradingType.BACKTESTING)

def get_backtesting_report_pnl_table(self, strategy_code, location=None, show_all_rows=False, force_fetch=False):
def get_backtesting_report_pnl_table(self, strategy_code, location=None, show_all_rows=False, force_fetch=False, broker_commission_percentage=0, broker_commission_price=None):
"""
Fetch Back Testing Profit & Loss details
Expand All @@ -630,13 +637,15 @@ def get_backtesting_report_pnl_table(self, strategy_code, location=None, show_al
location: Location of Exchange
show_all_rows: True or False
force_fetch: Forcefully fetch PnL data
broker_commission_percentage: Percentage of broker commission per trade
broker_commission_price: Broker fee per trade
Returns:
Report details
"""

if self.backtesting_pnl_data is None or location is not None or force_fetch:
self.backtesting_pnl_data = self.get_pnl_report_table(strategy_code, TradingType.BACKTESTING, location)
self.backtesting_pnl_data = self.get_pnl_report_table(strategy_code, TradingType.BACKTESTING, location, broker_commission_percentage, broker_commission_price)

return self.backtesting_pnl_data

Expand Down Expand Up @@ -767,7 +776,7 @@ def get_papertrading_logs(self, strategy_code):

return self.get_logs(strategy_code=strategy_code, trading_type=TradingType.PAPERTRADING)

def get_papertrading_report_pnl_table(self, strategy_code, location=None, show_all_rows=False, force_fetch=False):
def get_papertrading_report_pnl_table(self, strategy_code, location=None, show_all_rows=False, force_fetch=False, broker_commission_percentage=0, broker_commission_price=None):
"""
Fetch Paper Trading Profit & Loss details
Expand All @@ -776,13 +785,15 @@ def get_papertrading_report_pnl_table(self, strategy_code, location=None, show_a
location: Location of the exchange
show_all_rows: True or False
force_fetch: Forcefully fetch PnL data
broker_commission_percentage: Percentage of broker commission per trade
broker_commission_price: Broker fee per trade
Returns:
Report details
"""

if self.papertrade_pnl_data is None or location is not None or force_fetch:
self.papertrade_pnl_data = self.get_pnl_report_table(strategy_code, TradingType.PAPERTRADING, location)
self.papertrade_pnl_data = self.get_pnl_report_table(strategy_code, TradingType.PAPERTRADING, location, broker_commission_percentage, broker_commission_price)

return self.papertrade_pnl_data

Expand Down Expand Up @@ -912,7 +923,7 @@ def get_realtrading_logs(self, strategy_code):

return self.get_logs(strategy_code=strategy_code, trading_type=TradingType.REALTRADING)

def get_realtrading_report_pnl_table(self, strategy_code, location=None, show_all_rows=False, force_fetch=False):
def get_realtrading_report_pnl_table(self, strategy_code, location=None, show_all_rows=False, force_fetch=False, broker_commission_percentage=0, broker_commission_price=None):
"""
Fetch Real Trading Profit & Loss details
Expand All @@ -921,13 +932,15 @@ def get_realtrading_report_pnl_table(self, strategy_code, location=None, show_al
location: Location of the Exchange
show_all_rows: True or False
force_fetch: Forcefully fetch PnL data
broker_commission_percentage: Percentage of broker commission per trade
broker_commission_price: Broker fee per trade
Returns:
Report details
"""

if self.realtrade_pnl_data is None or location is not None or force_fetch:
self.realtrade_pnl_data = self.get_pnl_report_table(strategy_code, TradingType.REALTRADING, location)
self.realtrade_pnl_data = self.get_pnl_report_table(strategy_code, TradingType.REALTRADING, location, broker_commission_percentage, broker_commission_price)

return self.realtrade_pnl_data

Expand Down

0 comments on commit 86e8d71

Please sign in to comment.