411 REFERENCE · Wikibooks
Scientific Calculations with Julia/Investing in options
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
Scientific Calculations with Julia Investing in options Exercises in Julia Analyze your genome
An option is a financial contract that gives you the right (not the obligation) to buy (Call) or sell (Put) shares at the strike price at expiration. For example, if the strike price of the corresponding call option is €100, if the option price is €5, you pay €5 immediately. If the stock is worth €120 at expiration, you buy the shares at €100 and sell them at €120, so your profit is €120 - €100 - €15 = €15. If, however, the stock is worth €90 at expiration, I only lose the option price and do not exercise it. If the strike price of the corresponding put option is €100, if the option price is €5, you pay €5 immediately. If the stock is worth €80 at expiration, I resell the shares at the €100 strike price, so the profit is 100 - 80 - 5 = €15. If, however, the stock is worth €120 at expiration, I only lose the option price and do not exercise it.
Implementation in Julia
CALL Options
Install the libraries:
using Pkg
Pkg.add(["YFinance","DataFrames","CSV"])
Download the Google call options:
using DataFrames, YFinance, CSV
df = get_Options("GOOG")["calls"] |> DataFrame
Sometimes when downloading data I get the error: "Too many requests" So if you can download them, it's best to save them in a CSV file:
CSV.write("calls_goog.csv", df)
Load the data:
using DataFrames, YFinance, CSV
df = CSV.read("calls_goog.csv", DataFrame)
Select the columns I'm interested in:
df1 = select(df,:contractSymbol,:strike,:lastPrice,:volume,:expiration,:impliedVolatility,:inTheMoney)
At 1:00 PM on March 31, 2026, Google shares were listed S = 273.14. The first 13 options are "in the money" because the strike is lower than the value of the stock. At the April 2, 2026, expiration of the calls shown in the expiration column, if S = 280, I would obtain a profit equal to max(S - strike, 0) - lastPrice, exercising the right to buy the stock at the strike and resell it immediately thereafter:
S = 280
df1.profit_S_280 = max.(S.-df1.strike, 0).-df1.lastPrice
If, on the other hand, at the April 2, 2026, expiration of the calls, S = 160, I would obtain a loss on all calls equal to -lastPrice:
S = 160
df1.loss_S_160 = max.(S.-df1.strike,0).-df1.lastPrice
If at the April 2, 2026, call expiration, S = 240, some low-strike calls may be profitable, but most will be losing money:
S = 240
df1.loss_S_240 = max.(S.-df1.strike,0).-df1.lastPrice
df1
contractSymbol strike lastPrice volume expiration impliedVolatility inTheMoney profit_S_280 loss_S_160 loss_S_240 GOOG260402C00170000 170.0 105.08 1 2026-04-02T00:00:00.0 1.0000000000000003e-5 true 4.920000000000002 -105.08 -35.08 GOOG260402C00180000 180.0 119.64 9 2026-04-02T00:00:00.0 1.0000000000000003e-5 true -19.64 -119.64 -59.64 GOOG260402C00200000 200.0 76.77 5 2026-04-02T00:00:00.0 1.0000000000000003e-5 true 3.230000000000004 -76.77 -36.769999999999996 GOOG260402C00205000 205.0 71.63 5 2026-04-02T00:00:00.0 1.0000000000000003e-5 true 3.3700000000000045 -71.63 -36.629999999999995 GOOG260402C00210000 210.0 64.51 1 2026-04-02T00:00:00.0 1.0000000000000003e-5 true 5.489999999999995 -64.51 -34.510000000000005 GOOG260402C00225000 225.0 48.89 50 2026-04-02T00:00:00.0 1.0000000000000003e-5 true 6.109999999999999 -48.89 -33.89 GOOG260402C00230000 230.0 44.3 2 2026-04-02T00:00:00.0 1.0000000000000003e-5 true 5.700000000000003 -44.3 -34.3 GOOG260402C00235000 235.0 71.32 2026-04-02T00:00:00.0 1.0000000000000003e-5 true -26.319999999999993 -71.32 -66.32 GOOG260402C00240000 240.0 32.73 138 2026-04-02T00:00:00.0 1.0000000000000003e-5 true 7.270000000000003 -32.73 -32.73 GOOG260402C00245000 245.0 28.0 2 2026-04-02T00:00:00.0 1.0000000000000003e-5 true 7.0 -28.0 -28.0 GOOG260402C00250000 250.0 11.61pm 27 2026-04-02T00:00:00.0 1.0000000000000003e-5 true 6.390000000000001 -23.61 -23.61 GOOG260402C00255000 255.0 19.69 1 2026-04-02T00:00:00.0 1.0000000000000003e-5 true 5.309999999999999 -19.69 -19.69 GOOG260402C00260000 260.0 1.14pm 90 2026-04-02T00:00:00.0 1.0000000000000003e-5 true 6.859999999999999 -13.14 -13.14 -------------------- GOOG260402C00385000 385.0 0.1 1 2026-04-02T00:00:00.0 0.500005 false -0.1 -0.1 -0.1 GOOG260402C00390000 390.0 0.01 2 2026-04-02T00:00:00.0 0.500005 false -0.01 -0.01 -0.01 GOOG260402C00395000 395.0 0.1 1 2026-04-02T00:00:00.0 0.500005 false -0.1 -0.1 -0.1 GOOG260402C00400000 400.0 0.01 10 2026-04-02T00:00:00.0 0.500005 false -0.01 -0.01 -0.01 GOOG260402C00405000 405.0 0.01 2 2026-04-02T00:00:00.0 0.500005 false -0.01 -0.01 -0.01 GOOG260402C00410000 410.0 0.1 3 2026-04-02T00:00:00.0 0.500005 false -0.1 -0.1 -0.1 GOOG260402C00415000 415.0 0.05 4 2026-04-02T00:00:00.0 0.500005 false -0.05 -0.05 -0.05 GOOG260402C00420000 420.0 0.1 1 2026-04-02T00:00:00.0 0.500005 false -0.1 -0.1 -0.1 GOOG260402C00425000 425.0 0.1 1 2026-04-02T00:00:00.0 0.500005 false -0.1 -0.1 -0.1 GOOG260402C00430000 430.0 0.1 4 2026-04-02T00:00:00.0 0.500005 false -0.1 -0.1 -0.1 GOOG260402C00435000 435.0 0.05 2 2026-04-02T00:00:00.0 0.500005 false -0.05 -0.05 -0.05 GOOG260402C00440000 440.0 0.1 4 2026-04-02T00:00:00.0 0.500005 false -0.1 -0.1 -0.1 GOOG260402C00445000 445.0 0.1 1 2026-04-02T00:00:00.0 0.500005 false -0.1 -0.1 -0.1 GOOG260402C00450000 450.0 0.06 10 2026-04-02T00:00:00.0 0.500005 false -0.06 -0.06 -0.06
PUT Options
df_puts = get_Options("GOOG")["puts"] |> DataFrame
CSV.write("puts_goog.csv", df_puts)
using DataFrames, YFinance, CSV
df = CSV.read("puts_goog.csv", DataFrame)
df2 = select(df,:contractSymbol,:strike,:lastPrice,:volume,:expiration,:impliedVolatility,:inTheMoney)
At 1:00 PM on March 31, 2026, Google shares are trading at S = 273.14. The last put options are "in the money" because the strike price is greater than the stock price. At the expiration date of April 2, 2026, corresponding to the expiration column of the dataset, if S = 280, only the puts with strike prices > 280 will be in the money. 280 generate a profit (equal to max(strike-S,0) - lastPrice), while the others expire worthless with a loss equal to -lastPrice:
S = 280
df2.profitto_S_280 = max.(df2.strike.-S,0).-df2.lastPrice
S = 220
df2.profitto_S_220 = max.(df2.strike.-S,0).-df2.lastPrice
If at the expiration of April 2, 2026, of the puts visible in the expiration column of the dataset, S = 460, I would obtain a loss on all the puts (the values in the column will all be negative). :
S = 460
df2.perdita_S_460 = max.(df2.strike.-S,0).-df2.lastPrice
df2
contractSymbol strike lastPrice volume expiration impliedVolatility inTheMoney profit_S_280 profit_S_220 loss_S_460 GOOG260402P00175000 175.0 0.3 1 2026-04-02T00:00:00.0 0.500005 false -0.3 -0.3 -0.3 GOOG260402P00180000 180.0 0.02 2026-04-02T00:00:00.0 0.500005 false -0.02 -0.02 -0.02 GOOG260402P00190000 190.0 0.1 1 2026-04-02T00:00:00.0 0.500005 false -0.1 -0.1 -0.1 GOOG260402P00195000 195.0 0.03 10 2026-04-02T00:00:00.0 0.500005 false -0.03 -0.03 -0.03 GOOG260402P00200000 200.0 0.09 1 2026-04-02T00:00:00.0 0.500005 false -0.09 -0.09 -0.09 GOOG260402P00205000 205.0 0.1 1 2026-04-02T00:00:00.0 0.500005 false -0.1 -0.1 -0.1 GOOG260402P00210000 210.0 0.04 4 2026-04-02T00:00:00.0 0.500005 false -0.04 -0.04 -0.04 GOOG260402P00215000 215.0 0.03 1 2026-04-02T00:00:00.0 0.500005 false -0.03 -0.03 -0.03 GOOG260402P00220000 220.0 0.02 34 2026-04-02T00:00:00.0 0.500005 false -0.02 -0.02 -0.02 GOOG260402P00225000 225.0 0.02 2 2026-04-02T00:00:00.0 0.500005 false -0.02 4.98 -0.02 GOOG260402P00230000 230.0 0.01 5 2026-04-02T00:00:00.0 0.500005 false -0.01 9.99 -0.01 GOOG260402P00235000 235.0 0.02 20 2026-04-02T00:00:00.0 0.2500075 false -0.02 14.98 -0.02 GOOG260402P00240000 240.0 0.02 265 2026-04-02T00:00:00.0 0.2500075 false -0.02 19.98 -0.02 -------------------- GOOG260402P00312500 312.5 31.05 310 2026-04-02T00:00:00.0 1.0000000000000003e-5 true 1.4499999999999993 61.45 -31.05 GOOG260402P00315000 315.0 25.6 4 2026-04-02T00:00:00.0 1.0000000000000003e-5 true 9.399999999999999 69.4 -25.6 GOOG260402P00317500 317.5 38.6 1 2026-04-02T00:00:00.0 1.0000000000000003e-5 true -1.1000000000000014 58.9 -38.6 GOOG260402P00320000 320.0 45.26 1 2026-04-02T00:00:00.0 1.0000000000000003e-5 true -5.259999999999998 54.74 -45.26 GOOG260402P00322500 322.5 21.0 1 2026-04-02T00:00:00.0 1.0000000000000003e-5 true 9.5pm 81.5 -21.0 GOOG260402P00325000 325.0 32.0 1 2026-04-02T00:00:00.0 1.0000000000000003e-5 true 13.0 73.0 -32.0 GOOG260402P00330000 330.0 41.35 4 2026-04-02T00:00:00.0 1.0000000000000003e-5 true 8.649999999999999 68.65 -41.35 GOOG260402P00335000 335.0 46.25 1 2026-04-02T00:00:00.0 1.0000000000000003e-5 true 8.75 68.75 -46.25 GOOG260402P00340000 340.0 36.6 1 2026-04-02T00:00:00.0 1.0000000000000003e-5 true 23.4 83.4 -36.6 GOOG260402P00345000 345.0 47.56 2026-04-02T00:00:00.0 1.0000000000000003e-5 true 17.439999999999998 77.44 -47.56 GOOG260402P00350000 350.0 47.58 30 2026-04-02T00:00:00.0 1.0000000000000003e-5 true 10.42pm 82.42 -47.58 GOOG260402P00360000 360.0 45.03 1 2026-04-02T00:00:00.0 1.0000000000000003e-5 true 34.97 94.97 -45.03 GOOG260402P00450000 450.0 162.3 2026-04-02T00:00:00.0 1.0000000000000003e-5 true 7.699999999999989 67.69999999999999 -162.3