Skip to content

Instantly share code, notes, and snippets.

@tokhi
Last active January 7, 2022 19:22
Show Gist options
  • Save tokhi/ba4df1c5d653256d8f4728f635a93a37 to your computer and use it in GitHub Desktop.
Save tokhi/ba4df1c5d653256d8f4728f635a93a37 to your computer and use it in GitHub Desktop.
Stock spread option maximum loss and profit calculation
def break_even(long_sp, short_sp, ndp)
if long_sp > short_sp # bear option
long_sp - ndp
else # bull option
long_sp + ndp
end
end
def max_profit(long_sp, short_sp, ndp)
if(long_sp > short_sp) # bear option
bear_diff = long_sp - short_sp
else # bull option
bear_diff = short_sp - long_sp
end
(bear_diff - ndp).round(2) * 100
end
def calc(long_strike, short_strike, long_p, short_p)
bear_diff = long_strike - short_strike
bull_diff = short_strike - long_strike
net_debit_paid = long_p - short_p
breakeve_price = break_even(long_strike, short_strike,net_debit_paid)
#max_profit = (bear_diff - net_debit_paid).round(2) * 100
max_profit = max_profit(long_strike, short_strike, net_debit_paid)
max_loss = (net_debit_paid * 100).round(2)
puts "*******"
puts "Breakeven price: #{breakeve_price}\n Max Profit: #{max_profit}\n Max Loss: #{max_loss}"
end
def help
"ruby spread.rb long_strike short_strike long_price short_price"
end
# formula: https://youtu.be/Npx2j_bHwnM?t=685
puts "USAGE: #{help}"
calc(ARGV[0].to_f, ARGV[1].to_f, ARGV[2].to_f, ARGV[3].to_f)
@tokhi
Copy link
Author

tokhi commented Jan 7, 2022

ruby spread.rb 128 132 4.93 2.13

USAGE: ruby spread.rb long_strike short_strike long_strike_price short_strike_price
*******
Breakeven price: 130.8
 Max Profit: 120.0
 Max Loss: 280.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment