Open pixcelo opened 1 year ago
def calculate_pl(self, symbol, position, lot_size, entry_rate, exit_rate, spread, usd_jpy_rate=146):
if position == "long":
entry_rate_with_spread = entry_rate + spread
exit_rate_with_spread = exit_rate
diff = exit_rate - entry_rate_with_spread
elif position == "short":
entry_rate_with_spread = entry_rate
exit_rate_with_spread = exit_rate + spread
diff = entry_rate - exit_rate_with_spread
else:
raise ValueError("Invalid position type. Choose 'long' or 'short'.")
# For pairs like EURUSD
if symbol[-3:] != "JPY":
return lot_size * diff * usd_jpy_rate
# For pairs like USDJPY
elif symbol[:3] == "USD":
return lot_size * diff
# For cross currency pairs like EURJPY
else:
return lot_size * diff * entry_rate_with_spread
version2
def calculate_pl(self, symbol, position, lot_size, entry_rate, exit_rate, spread_points, usd_jpy_rate=146):
# Convert spread from points to pips
if symbol[-3:] == "JPY" or symbol[:3] == "JPY":
spread_pips = spread_points / 100 # e.g., for pairs like USDJPY
else:
spread_pips = spread_points / 10000 # e.g., for pairs like EURUSD (5=>0.0005)
if position == "long":
entry_rate_with_spread = entry_rate + spread_pips
exit_rate_with_spread = exit_rate
diff = exit_rate - entry_rate_with_spread
elif position == "short":
entry_rate_with_spread = entry_rate
exit_rate_with_spread = exit_rate + spread_pips
diff = entry_rate - exit_rate_with_spread
else:
raise ValueError("Invalid position type. Choose 'long' or 'short'.")
# For pairs like EURUSD
if symbol[-3:] != "JPY":
return lot_size * diff * usd_jpy_rate
# For pairs like USDJPY
elif symbol[:3] == "USD":
return lot_size * diff
# For cross currency pairs like EURJPY
else:
return lot_size * diff * entry_rate_with_spread
note https://note.com/whakamarie/n/ne45f60d1b9bf