Last active
January 7, 2022 19:22
-
-
Save tokhi/ba4df1c5d653256d8f4728f635a93a37 to your computer and use it in GitHub Desktop.
Stock spread option maximum loss and profit calculation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ruby spread.rb 128 132 4.93 2.13