[PYTHON] I have problem determining stop loss and take profit if i am not using all my capital

 

Hello, this is my first post, my following function behaves as expected if i let it determine my position size, but when i choose that position size with another value, it gives me wrong result, for this problem i'm not using tick size yet.

>>> def sltp_calculator(account, entry, position_size=None, risk_percentage=0.01, reward_multiplier=2):
        """Stop Loss/Take Profit Calculator"""
        account_risk = account * risk_percentage

        if position_size is None:
            position_size = account / entry

        # To calculate the stop loss we assume that
        # -> position_size * (entry - stop_loss) = account_risk
        # -> entry - stop_loss = account_risk / position_size
        # -> -stop_loss = (account_risk / position_size) - entry
        # -> stop_loss = entry - (account_risk / position_size)
        stop_loss = entry - (account_risk / position_size)

        risk_per_share = entry - stop_loss
        take_profit = entry + (risk_per_share * reward_multiplier)

        return position_size, stop_loss, take_profit

This is an example input/output

>>> sltp_calculator(54.28690198, 0.00019)
>>> (285720.5367368421, 0.00018810000000000002, 0.0001938)
>>>
>>> # 285720.5367368421 * 0.00018810000000000002 = 53.74403296020001  ≈ 54.28690198 * (1 - 0.01)  (Stop Loss OK)
>>> # 285720.5367368421 * 0.0001938              = 55.372640019600006 ≈ 54.28690198 * (1 + 0.02)  (Take Profit OK)

Everything is ok, it gives me the desired stop loss to risk the 1% of my account, and the take profit to gain 2x the risked amount, but it fails when i choose different position size

>>> sltp_calculator(54.28690198, 0.00019, 280000)
>>> (280000, 0.00018806118207214287, 0.0001938776358557143)
>>>
>>> 280000 * 0.00018806118207214287 = 52.6571309802 =/= 54.28690198 * (1 - 0.01)  (Stop Loss FAIL)
>>> 280000 * 0.0001938776358557143  = 54.2857380396 =/= 54.28690198 * (1 + 0.02)  (Take Profit FAIL)

I will appreciate if you help me with my calculations, thanks in advance

Documentation on MQL5: Constants, Enumerations and Structures / Input/Output Constants
Documentation on MQL5: Constants, Enumerations and Structures / Input/Output Constants
  • www.mql5.com
Input/Output Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Alver Lopez:

Hello, this is my first post, my following function behaves as expected if i let it determine my position size, but when i choose that position size with another value, it gives me wrong result, for this problem i'm not using tick size yet.

This is an example input/output

Everything is ok, it gives me the desired stop loss to risk the 1% of my account, and the take profit to gain 2x the risked amount, but it fails when i choose different position size

I will appreciate if you help me with my calculations, thanks in advance

Well, i found a solution, i will share in case you find useful.

>>> def sltp_calculator(account, entry, position_size=None, risk_percentage=0.01, reward_multiplier=2):
        """Stop Loss / Take Profit Calculator

        RESTRICTION
        -----------
        `position_size * entry` must be greater than `account * (1 - risk_percentage)`, 
        otherwise calculation of stop loss / take profit will fail.
        """
        if position_size is None:
            position_size = account / entry

        # position_size * stop_loss = account * (1 - risk_percentage)
        # position_size * (entry - delta) = account * (1 - risk_percentage)
        # entry - delta = (account * (1 - risk_percentage)) / position_size
        delta = entry - ((account * (1 - risk_percentage)) / position_size)

        stop_loss = entry - delta

        reward_percentage = risk_percentage * reward_multiplier
        # position_size * take_profit = account * (1 + reward_percentage)
        # position_size * (entry + delta) = account * (1 + reward_percentage)
        # entry + delta = (account * (1 + reward_percentage)) / position_size
        delta = ((account * (1 + reward_percentage)) / position_size) - entry

        take_profit = entry + delta

        return position_size, stop_loss, take_profit