Custom Python code for calculating the Probability of Profit (POP) for options trading strategies using Monte Carlo Simulations.

Related tags

Miscellaneouspoptions
Overview

poptions

Custom Python code for calculating the Probability of Profit (POP) for options trading strategies using Monte Carlo Simulations. The Monte Carlo Simulation runs thousands of individual stock price simulations and uses the data from these simulations to average out a POP number.

Unlike other calculators, poptions lets you specify a target profit, such as a percentage of maximum profit or a multiple of the debit paid, that will trigger your position to close when it's reached in the simulation. Additionally, you will specify the 'closing days', which refers to the number of calendar days that will pass until you close the position (assuming the target profit wasn't reached to trigger the closing).

In simpler words: the estimated POP from poptions refers to the probability of hitting a specified target profit within a specified number of calendar days.

Poptions lets you add MULTIPLE combinations of target profits and closing days!

Poptions also outputs an Average Days To Close (ADTC) number. This is the estimated average number of calendar days you will have to wait until you reach your target profit, assuming that the POP ended up in your favor.

Poptions can also be used to evaluate existing trades (see below).

Disclaimer: poptions has not been vetted by any certified professional or expert. The calculations do not constitute investment advice. They are for educational purposes only. Calculations may contain mistakes and are made using models with inherent limitations that are highlighted below. Use this tool at your own risk.

How does it work?

A great video explaining the underlying logic is shown here: https://www.tastytrade.com/shows/the-skinny-on-options-modeling/episodes/probability-of-50-profit-12-17-2015

In short, thousands of stock price simulations are executed in which the price change per day is modeled according to Geometric Brownian Motion. The Black-Scholes Model is then used to estimate the price of an options contract (or multiple contracts depending on the strategy used) per day in each simulation. The number of simulations in which the selected profit criteria is met (e.g. 50% of maximum profit within 20 calendar days) is divided by the total number of simulations, giving you an estimate of the POP. A similar averaging is done to acquire the ADTC.

poptions makes the following assumptions for its simulations:

  • The stock price volatility is equal to the implied volatility and remains constant.
  • Geometric Brownian Motion is used to model the stock price.
  • Risk-free interest rates remain constant.
  • The Black-Scholes Model is used to price options contracts.
  • Dividend yield is not considered.
  • Commissions are not considered.
  • Assignment risks are not considered.
  • Earnings date and stock splits are not considered.

Of course, not all of these assumptions are true in real life and so there are limitations to this approach. For example, it's highly unlikely that the stock price volatility remains constant for several days. Thus, one should take these results with a grain of salt.

How to use poptions

The requirements.txt file lists all the python packages (and their versions) that need to be installed for poptions to work.

A working example of a Call Credit Spread strategy is located in the poptions_examples.py file, as shown:

underlying = 137.31     # Current underlying price
short_strike = 145      # Short strike price
short_price = 1.13      # Short call price
long_strike = 150
long_price = 0.4
rate = 0        # Annualized risk-free rate as a percentage
sigma = 26.8        # Implied Volatility as a percentage
days_to_expiration = 45     # Calendar days left till expiration
percentage_array = [20, 30, 40]  # Percentage of maximum profit that will trigger the position to close
closing_days_array = [21, 22, 23]       # Max calendar days passed until position is closed
trials = 2000       # Number of independent trials

print("Call Credit Spread: ", poptions.callCreditSpread(underlying, sigma, rate, trials, days_to_expiration,
                                                        closing_days_array, percentage_array, short_strike,
                                                        short_price, long_strike, long_price))

The comments in the code should be self-explanatory, but the percentage_array, closing_days_array, and trials variables require some extra clarification:

  • The first elements in percentage_array and closing_days_array are 20 and 21, respectively.
    This means that our target profit is 20% of maximum profit (0.2 * (short_price - long_price) = $ 0.146). The Monte Carlo Simulation will consider each individual simulation (renamed to trial here) a success if this target profit is achieved. If this target profit is not reached within 21 calendar days, it will be considered a failure.

  • You can add multiple combinations of target profits and closing days by simply adding extra elements to percentage_array and closing_days_array! In the above example, we tell the Simulation to also evaluate 30% and 40% of maximum profits for 22 and 23 calendar days, respectively.

  • Increasing the number of trials will improve the accuracy of your estimations at the cost of a slower simulation.

Some Extra Notes:

  • Running poptions.callCreditSpread() will not output consistent results. There will always be some variance from its previous runs. This is because a new simulation is started from scratch for every run. The amount of variance depends on how high trials is set: More trials -> higher accuracy (less variance).

  • For the Long Call and Long Put strategies, percentage_array is replaced with multiple_array. This means that the target profit is now defined as a multiple of the debit that you paid to open the position. For example, if you bought a call option for $1.00, a value of [2] in multiple_array means that your target profit is 2 * $ 1.00 = $ 2.00.

  • You can evaluate existing trades with poptions! Type the net credit received into ONE of the short price variables, and leave the rest of the price variables at 0. Fill out all other variables with present data. Example: Net credit received was $0.73 for a Call Credit Spread, so short_price is 0.73 and long_price is 0. All other variables are filled with present data. For strategies where a net debit is paid like Debit Spreads, the debit paid should be in ONE of the long price variables, and leave the rest of the price variables at 0.

Entering existing trades is NOT supported for Covered Calls unless the current underlying price is the same as it was when you opened the position! This is because the underlying variable refers to the purchase price of the stock when you opened the position.

Running poptions_examples.py gives you the following output:

Call Credit Spread:  {'pop': [61.3, 57.65, 52.55], 'pop_error': [2.81, 2.85, 2.88], 'avg_dtc': [8.87, 10.3, 11.41], 'avg_dtc_error': [0.39, 0.43, 0.45]}
  • pop is the probability of reaching the target profit within the closing days. The first element in pop corresponds to the first elements in percentage_array and closing_days_array.

  • pop_error is the error range for pop. In the above example, for the first element, there is a 99% chance that the 'true' value for pop is between 58.49 (61.3 - 2.81) and 64.11 (61.3 + 2.81). Of course, this error range gets smaller as trials is increased.

  • avg_dtc refers to the Average Days To Close (ADTC).

  • avg_dtc_error is the error range for avg_dtc.

If avg_dtc falls on a weekend/holiday when the markets are closed, then you can assume that the closing date is on the following business/trading day.

SPEED BOOST with Numba!

If you're looking to potentially speed up simulations by 100x, the Numba python package can help you out! Numba translates Python functions to optimized machine code at runtime using the industry-standard LLVM compiler library. Numba-compiled numerical algorithms in Python can approach the speeds of C or FORTRAN.

Using Numba is shockingly easy. It requires making very little modifications to our code. Follow these steps to speed up poptions.callCreditSpread() in poptions_examples.py with Numba:

Open the CallCreditSpread.py file. Add the following decorator to this function:

@jit(nopython=True, cache=True)
def bsm_debit(sim_price, strikes, rate, time_fraction, sigma):
    ...

Open the MonteCarlo.py file. Add the decorator to this function:

@jit(nopython=True, cache=True)
def monteCarlo(underlying, rate, sigma, days_to_expiration, closing_days_array, trials, initial_credit,
                   min_profit, strikes, bsm_func):
    ...

Open the BlackScholes.py file. Add the decorator to the functions:

@jit(nopython=True, cache=True)
def blackScholesPut(s, k, rr, tt, sd):
    ...
    
@jit(nopython=True, cache=True)
def blackScholesCall(s, k, rr, tt, sd):
    ...

You're good to go, but you MUST account for the following: The first time you call poptions.callCreditSpread() will be slow (around a few seconds) since it triggers a compilation step for Numba. The second poptions.callCreditSpread() call is where you'll see the performance gains. Here's a comparison of the speeds between calls:

First poptions.callCreditSpread() call WITH Numba Compilation: 1.756 seconds
Second poptions.callCreditSpread() call WITHOUT Numba Compilation: 0.0064 seconds

Donations

If you like the project and feel like donating some crypto to the author(s), you can do so here:

BTC: 16xbCyVZB3x3PNFs1qQEXGsNgtTd4BKE6z

LTC: Lg1d1VEd5DMQzycZTSSeDEc59yomwDwX8j

Thank you!

License

MIT License

A description of this license can be found in the LICENSE.txt file.

Paimon is a pixie (or script) who was made for anyone from {EPITECH} who are struggling with the Coding Style.

Paimon Paimon is a pixie (or script) who was made for anyone from {EPITECH} who are struggling with the Coding Style. Her goal is to assist you in you

Lyy 2 Oct 17, 2021
Explore related sequences in the OEIS

OEIS explorer This is a tool for exploring two different kinds of relationships between sequences in the OEIS: mentions (links) of other sequences on

Alex Hall 6 Mar 15, 2022
WildHack 2021 solution by Nuclear Foxes team (public version).

WildHack 2021 Nuclear Foxes Team This repo contains our project for the Wildberries Hackathon 2021. Task 2: Searching tags Implement an algorithm of r

Sergey Zakharov 1 Apr 18, 2022
The-White-Noise-Project - The project creates noise intentionally

The-White-Noise-Project High quality audio matters everywhere, even in noise. Be

Ali Hakim Taşkıran 1 Jan 02, 2022
Tools for analyzing Java JVM gc log files

gc_log This package consists of two separate utilities useful for : gc_log_visualizer.py regionsize.py GC Log Visualizer This was updated to run under

Brad Schoening 0 Jan 04, 2022
This script provides LIVE feedback for On-The-Fly data collection with RELION

README This script provides LIVE feedback for On-The-Fly data collection with RELION (very useful to explore already processed datasets too!) Creating

cryoEM CNIO 6 Jul 14, 2022
Slientruss3d : Python for stable truss analysis tool

slientruss3d : Python for stable truss analysis tool Desciption slientruss3d is a python package which can solve the resistances, internal forces and

3 Dec 26, 2022
Werkzeug has a debug console that requires a pin. It's possible to bypass this with an LFI vulnerability or use it as a local privilege escalation vector.

Werkzeug Debug Console Pin Bypass Werkzeug has a debug console that requires a pin by default. It's possible to bypass this with an LFI vulnerability

Wyatt Dahlenburg 23 Dec 17, 2022
basic tool for NFT. let's spam, this is the easiest way to generate a hell lotta image

NFT generator this is the easiest way to generate a hell lotta image buckle up and follow me! how to first have your image in .png (transparent backgr

34 Nov 18, 2022
Socorro is the Mozilla crash ingestion pipeline. It accepts and processes Breakpad-style crash reports. It provides analysis tools.

Socorro Socorro is a Mozilla-centric ingestion pipeline and analysis tools for crash reports using the Breakpad libraries. Support This is a Mozilla-s

Mozilla Services 552 Dec 19, 2022
Yet another Airflow plugin using CLI command as RESTful api, supports Airflow v2.X.

中文版文档 Airflow Extended API Plugin Airflow Extended API, which export airflow CLI command as REST-ful API to extend the ability of airflow official API

Eric Cao 106 Nov 09, 2022
A beacon generator using Cobalt Strike and a variety of tools.

Beaconator is an aggressor script for Cobalt Strike used to generate either staged or stageless shellcode and packing the generated shellcode using your tool of choice.

Capt. Meelo 441 Dec 17, 2022
It is Keqin Wang first project in CMU, trying to use DRL(PPO) to control a 5-dof manipulator to draw line in space.

5dof-robot-writing this project aim to use PPO control a 5 dof manipulator to draw lines in 3d space. Introduction to the files the pybullet environme

Keqin Wang 4 Aug 22, 2022
Airplane reservation system python 2

airplane-reservation-system-python-2 Announcement 🔊 : 🔴 IMPORTANT 🔴 : Few new things have been added into the code [16/05/2021] different names is

voyager2005 1 Dec 06, 2021
Uproot - A script to bring deeply nested files or directories to the surface

UPROOT Bring deeply nested files or folders to the surface Uproot helps convert

Ted 2 Jan 15, 2022
GDIT: Geometry Dash Info Tool

GDIT: Geometry Dash Info Tool This is the first large script that allows you to quickly get information from the Geometry Dash server

dezz0xY 2 Jan 09, 2022
Collection of script & resources for Foundry's Nuke software.

Author: Liam Collod. Collections of scripting stuff I wrote for Foundry's Nuke software. Utilisation You can have a look at the README.md file in each

Liam Collod 1 May 14, 2022
MatroSka Mod Compiler for ts4scripts

MMC Current Version: 0.2 MatroSka Mod Compiler for .ts4script files Requirements Have Python 3.7 installed and set as default. Running from Source pip

MatroSka 1 Dec 13, 2021
MODSKIN-LOLPRO-updater: The mod is fkn 10y old and has'nt a self-updater

The mod is fkn 10y old and has'nt a self-updater. To use it just run the exec, wait some seconds, and it will run the new modsk

Shiro Amurha 3 Apr 23, 2022
A python script for osu!lazer rulesets auto update.

osu-lazer-rulesets-autoupdater A python script for osu!lazer rulesets auto update. How to use: 如何使用: You can refer to the python script. The begining

3 Jul 26, 2022