Skip to content

Instantly share code, notes, and snippets.

@rlapz
Last active July 26, 2026 05:26
Show Gist options
  • Select an option

  • Save rlapz/87f442761263fbacb177ec4382474a5c to your computer and use it in GitHub Desktop.

Select an option

Save rlapz/87f442761263fbacb177ec4382474a5c to your computer and use it in GitHub Desktop.
Futures Trading Calculators
#!/bin/env python
import math
round_fn = math.floor
#round_fn = math.ceil
def calculate():
try:
balance = float(input('Balance (default: 10_000_000) : ') or 10_000_000)
risk = float(input('Risk per Trade (%) (default: 1) : ') or 1.0)
sl = float(input('Stop Loss (%) (default: 1) : ') or 1.0)
tp = float(input('Take Profit (%) (default: 2) : ') or 2.0)
except ValueError:
print('Error: Invalid input')
return
if balance <= 0 or risk <= 0 or sl <= 0 or tp <= 0:
print('Error: All values must be greater than 0')
return
risk_amount = balance * (risk / 100)
position_size = risk_amount / (sl / 100)
leverage = position_size / balance
# Round leverage sensibly
leverage_rounded = max(round_fn(leverage), 1);
# Recalculate with chosen leverage
actual_position = balance * leverage_rounded
actual_loss = actual_position * (sl / 100)
actual_profit = actual_position * (tp / 100)
# Risk : Reward ratio
rr_ratio = tp / sl
no_lev = ''
if leverage_rounded == 1:
no_lev = '(None)'
print('\n' + '-'*50)
print(f'Required Leverage : {leverage_rounded:>15}x {no_lev}')
print(f'Balance (Margin) : {balance:>15,.2f}')
print(f'Position Size : {actual_position:>15,.2f}')
print(f'Stop Loss : {sl:>15,.2f}%')
print(f'Take Profit : {tp:>15,.2f}%')
print(f'Risk Amount : {actual_loss:>15,.2f} ({(actual_loss/balance*100):>5,.2f}%)')
print(f'Potential Profit : {actual_profit:>15,.2f} ({(actual_profit/balance*100):>5,.2f}%)')
print(f'Risk:Reward : {rr_ratio:>15,.2f}')
while True:
calculate()
again = input('\n' + '-'*50 + '\nCalculate again? (y/n): ').lower().strip()
if again == 'n':
break
print('-'*50 + '\n')
#!/bin/env python
def calculate():
try:
balance = float(input('Balance (default: 10_000_000) : ') or 10_000_000)
leverage = int(input('Leverage (default: 10) : ') or 10)
risk = float(input('Risk per Trade (%) (default: 1): ') or 1.0)
sl = float(input('Stop Loss (%) (default: 1) : ') or 1.0)
tp = float(input('Take Profit (%) (default: 2) : ') or 2.0)
except ValueError:
print('Error: Invalid input')
return
if balance <= 0 or leverage <= 0 or risk <= 0 or sl <= 0 or tp <= 0:
print('Error: All values must be greater than 0')
return
# Risk amount in currency
risk_amount = balance * (risk / 100)
# Position size required so that a SL% move loses exactly the risk amount
# Loss = Position Size × (SL / 100) → Position Size = Risk Amount / (SL / 100)
position_size = risk_amount / (sl / 100)
# Required margin with fixed leverage
margin = position_size / leverage
# Potential profit if TP is hit
potential_profit = position_size * (tp / 100)
# Risk : Reward ratio
rr_ratio = tp / sl
# Profit & Loss in percent of balance
loss_percent = (margin / balance) * 100
profit_percent = (potential_profit / balance) * 100
print('\n' + '-' * 50)
print(f'Balance : {balance:>15,.2f}')
print(f'Leverage : {leverage:>15}x')
print(f'Risk per Trade : {risk:>15.2f}%')
print(f'Stop Loss : {sl:>15.2f}%')
print(f'Take Profit : {tp:>15.2f}%')
print('-' * 50)
print(f'Risk Amount : {risk_amount:>15,.2f}')
print(f'Position Size : {position_size:>15,.2f}')
print('-' * 50)
print(f'Required Margin : {margin:>15,.2f} ({loss_percent:>5,.2f}%)')
print(f'Potential Profit: {potential_profit:>15,.2f} ({profit_percent:>5,.2f}%)')
print(f'Risk:Reward : {rr_ratio:>15.2f}')
print('-' * 50)
while True:
calculate()
again = input('\n' + '-' * 50 + '\nCalculate again? (y/n): ').lower().strip()
if again == 'n':
break
print('-' * 50 + '\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment