Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,61 @@ Add the following dependency to your `pom.xml`:
| [margins](./margins/) | Order margin calculations |
| [charges](./charges/) | Brokerage charge calculations |
| [trade-profit-and-loss](./trade-profit-and-loss/) | Trade-wise profit and loss reports |
| [**strategies/**](strategies/) | Ready-to-run options strategy examples for Nifty 50 (bullish, bearish, neutral, others). |

### Options Strategies

Each strategy file searches for the required Nifty 50 option legs using the Instruments API and places market orders via the v3 Order API.

#### [Bullish](strategies/bullish/)

| File | Strategy | Legs |
|------|----------|------|
| [BuyCall.java](strategies/bullish/code/BuyCall.java) | [**Buy Call**](strategies/bullish/README.md#buy-call--codebuycalljava) | BUY ATM CE |
| [SellPut.java](strategies/bullish/code/SellPut.java) | [**Sell Put**](strategies/bullish/README.md#sell-put--codesellputjava) | SELL ATM PE |
| [BullCallSpread.java](strategies/bullish/code/BullCallSpread.java) | [**Bull Call Spread**](strategies/bullish/README.md#bull-call-spread--codebullcallspreadjava) | BUY ATM CE + SELL ATM+1 CE |
| [BullPutSpread.java](strategies/bullish/code/BullPutSpread.java) | [**Bull Put Spread**](strategies/bullish/README.md#bull-put-spread--codebullputspreadjava) | SELL ATM PE + BUY ATM-1 PE |
| [BullButterfly.java](strategies/bullish/code/BullButterfly.java) | [**Bull Butterfly**](strategies/bullish/README.md#bull-butterfly--codebullbutterflyjava) | BUY ATM CE + SELL 2× ATM+1 CE + BUY ATM+2 CE |
| [BullCondor.java](strategies/bullish/code/BullCondor.java) | [**Bull Condor**](strategies/bullish/README.md#bull-condor--codebullcondorjava) | BUY ATM CE + SELL ATM+1 CE + SELL ATM+2 CE + BUY ATM+3 CE |
| [LongCalendarCall.java](strategies/bullish/code/LongCalendarCall.java) | [**Long Calendar with Calls**](strategies/bullish/README.md#long-calendar-with-calls--codelongcalendarcalljava) | SELL current-week ATM CE + BUY next-week ATM CE |
| [LongSyntheticFuture.java](strategies/bullish/code/LongSyntheticFuture.java) | [**Long Synthetic Future**](strategies/bullish/README.md#long-synthetic-future--codelongsyntheticfuturejava) | BUY ATM CE + SELL ATM PE |
| [CallRatioBackSpread.java](strategies/bullish/code/CallRatioBackSpread.java) | [**Call Ratio Back Spread**](strategies/bullish/README.md#call-ratio-back-spread--codecallratiobackspreadjava) | SELL 1× ATM CE + BUY 2× ATM+1 CE |
| [RangeForward.java](strategies/bullish/code/RangeForward.java) | [**Range Forward**](strategies/bullish/README.md#range-forward--coderangeforwardjava) | SELL ATM-1 PE + BUY ATM+1 CE |

#### [Bearish](strategies/bearish/)

| File | Strategy | Legs |
|------|----------|------|
| [BuyPut.java](strategies/bearish/code/BuyPut.java) | [**Buy Put**](strategies/bearish/README.md#buy-put--codebuyputjava) | BUY ATM PE |
| [SellCall.java](strategies/bearish/code/SellCall.java) | [**Sell Call**](strategies/bearish/README.md#sell-call--codesellcalljava) | SELL ATM CE |
| [BearCallSpread.java](strategies/bearish/code/BearCallSpread.java) | [**Bear Call Spread**](strategies/bearish/README.md#bear-call-spread--codebearcallspreadjava) | SELL ATM CE + BUY ATM+1 CE |
| [BearPutSpread.java](strategies/bearish/code/BearPutSpread.java) | [**Bear Put Spread**](strategies/bearish/README.md#bear-put-spread--codebearputspreadjava) | BUY ATM PE + SELL ATM-1 PE |
| [BearButterfly.java](strategies/bearish/code/BearButterfly.java) | [**Bear Butterfly**](strategies/bearish/README.md#bear-butterfly--codebearbutterflyjava) | BUY ATM PE + SELL 2× ATM-1 PE + BUY ATM-2 PE |
| [BearCondor.java](strategies/bearish/code/BearCondor.java) | [**Bear Condor**](strategies/bearish/README.md#bear-condor--codebearcondorjava) | BUY ATM PE + SELL ATM-1 PE + SELL ATM-2 PE + BUY ATM-3 PE |
| [LongCalendarPut.java](strategies/bearish/code/LongCalendarPut.java) | [**Long Calendar with Puts**](strategies/bearish/README.md#long-calendar-with-puts--codelongcalendarputjava) | SELL current-week ATM PE + BUY next-week ATM PE |
| [ShortSyntheticFuture.java](strategies/bearish/code/ShortSyntheticFuture.java) | [**Short Synthetic Future**](strategies/bearish/README.md#short-synthetic-future--codeshortsyntheticfuturejava) | SELL ATM CE + BUY ATM PE |
| [PutRatioBackSpread.java](strategies/bearish/code/PutRatioBackSpread.java) | [**Put Ratio Back Spread**](strategies/bearish/README.md#put-ratio-back-spread--codeputratiobackspreadjava) | SELL 1× ATM PE + BUY 2× ATM-1 PE |
| [RiskReversal.java](strategies/bearish/code/RiskReversal.java) | [**Risk Reversal**](strategies/bearish/README.md#risk-reversal--coderiskreversaljava) | SELL ATM+1 CE + BUY ATM-1 PE |

#### [Neutral](strategies/neutral/)

| File | Strategy | Legs |
|------|----------|------|
| [ShortStraddle.java](strategies/neutral/code/ShortStraddle.java) | [**Short Straddle**](strategies/neutral/README.md#short-straddle--codeshortstraddlejava) | SELL ATM CE + SELL ATM PE |
| [ShortStrangle.java](strategies/neutral/code/ShortStrangle.java) | [**Short Strangle**](strategies/neutral/README.md#short-strangle--codeshortstranglejava) | SELL ATM+1 CE + SELL ATM-1 PE |
| [IronButterfly.java](strategies/neutral/code/IronButterfly.java) | [**Iron Butterfly**](strategies/neutral/README.md#iron-butterfly--codeironbutterflyjava) | SELL ATM CE + SELL ATM PE + BUY ATM+2 CE + BUY ATM-2 PE |
| [Batman.java](strategies/neutral/code/Batman.java) | [**Batman**](strategies/neutral/README.md#batman--codebatmanjava) | BUY ATM CE + SELL 2× ATM+1 CE + BUY ATM+2 CE + BUY ATM PE + SELL 2× ATM-1 PE + BUY ATM-2 PE |
| [ShortIronCondor.java](strategies/neutral/code/ShortIronCondor.java) | [**Short Iron Condor**](strategies/neutral/README.md#short-iron-condor--codeshortironcondorjava) | SELL ATM+1 CE + BUY ATM+2 CE + SELL ATM-1 PE + BUY ATM-2 PE |

#### [Others](strategies/others/)

| File | Strategy | Legs |
|------|----------|------|
| [LongStraddle.java](strategies/others/code/LongStraddle.java) | [**Long Straddle**](strategies/others/README.md#long-straddle--codelongstraddlejava) | BUY ATM CE + BUY ATM PE |
| [LongStrangle.java](strategies/others/code/LongStrangle.java) | [**Long Strangle**](strategies/others/README.md#long-strangle--codelongstranglejava) | BUY ATM+1 CE + BUY ATM-1 PE |
| [CallRatioSpread.java](strategies/others/code/CallRatioSpread.java) | [**Call Ratio Spread**](strategies/others/README.md#call-ratio-spread--codecallratiospreadjava) | BUY 1× ATM CE + SELL 2× ATM+1 CE |
| [PutRatioSpread.java](strategies/others/code/PutRatioSpread.java) | [**Put Ratio Spread**](strategies/others/README.md#put-ratio-spread--codeputratiospreadjava) | BUY 1× ATM PE + SELL 2× ATM-1 PE |
| [LongIronButterfly.java](strategies/others/code/LongIronButterfly.java) | [**Long Iron Butterfly**](strategies/others/README.md#long-iron-butterfly--codelongironbutterflyjava) | BUY ATM CE + BUY ATM PE + SELL ATM+2 CE + SELL ATM-2 PE |
| [LongIronCondor.java](strategies/others/code/LongIronCondor.java) | [**Long Iron Condor**](strategies/others/README.md#long-iron-condor--codelongironcondorjava) | BUY ATM+1 CE + SELL ATM+2 CE + BUY ATM-1 PE + SELL ATM-2 PE |
| [Strip.java](strategies/others/code/Strip.java) | [**Strip**](strategies/others/README.md#strip--codestripjava) | BUY 1× ATM CE + BUY 2× ATM PE |
| [Strap.java](strategies/others/code/Strap.java) | [**Strap**](strategies/others/README.md#strap--codestrapjava) | BUY 2× ATM CE + BUY 1× ATM PE |
76 changes: 76 additions & 0 deletions examples/strategies/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Options Trading Strategies

Ready-to-run Java examples for common Nifty 50 options strategies using the [Upstox Java SDK](https://github.com/upstox/upstox-java).

Each file finds the required option legs via `InstrumentsApi.searchInstrument()` and places market orders via `OrderApiV3.placeOrder()`. Replace `your_access_token_here` with your token before running.

> **Note:** All strategies default to **Nifty 50** but work with any index. To switch, change the `query` argument in `searchInstrument()` — for example, use `"Nifty Bank"` for Bank Nifty, `"Nifty Fin Service"` for FinNifty, or `"SENSEX"` for BSE Sensex.

## Strategy Categories

### [Bullish](bullish/)

Strategies that profit when the market moves **up**. Use these when you expect Nifty 50 to rise.

| Strategy | Description |
|----------|-------------|
| [Buy Call](bullish/) | Simplest bullish bet — buy an ATM call, profit increases as market rises. |
| [Sell Put](bullish/) | Collect premium by selling an ATM put — profitable if market stays flat or rises. |
| [Bull Call Spread](bullish/) | Buy ATM call, sell OTM call to reduce cost — capped profit, capped loss. |
| [Bull Put Spread](bullish/) | Sell ATM put, buy lower OTM put — profit from premium if market stays above short strike. |
| [Bull Butterfly](bullish/) | Buy ATM call, sell 2× ATM+1 call, buy ATM+2 call — low cost, peak profit at middle strike. |
| [Bull Condor](bullish/) | Buy ATM call, sell ATM+1 and ATM+2, buy ATM+3 — wider profit zone than butterfly at slightly higher cost. |
| [Long Calendar with Calls](bullish/) | Sell current-week ATM call, buy next-week ATM call — profits from near-term time decay then upside. |
| [Long Synthetic Future](bullish/) | Buy ATM call, sell ATM put — replicates a long futures payoff using options. |
| [Call Ratio Back Spread](bullish/) | Sell ATM call, buy 2× OTM call — low-cost entry, profits accelerate on a large rally. |
| [Range Forward](bullish/) | Sell OTM put, buy OTM call — near-zero cost bullish position, unlimited risk on both sides. |

---

### [Bearish](bearish/)

Strategies that profit when the market moves **down**. Use these when you expect Nifty 50 to fall.

| Strategy | Description |
|----------|-------------|
| [Buy Put](bearish/) | Simplest bearish bet — buy an ATM put, profit increases as market falls. |
| [Sell Call](bearish/) | Collect premium by selling an ATM call — profitable if market stays flat or falls. |
| [Bear Call Spread](bearish/) | Sell ATM call, buy higher OTM call to cap risk — profit from premium if market stays below short strike. |
| [Bear Put Spread](bearish/) | Buy ATM put, sell lower OTM put to reduce cost — capped profit, capped loss. |
| [Bear Butterfly](bearish/) | Buy ATM put, sell 2× ATM-1 put, buy ATM-2 put — low cost, peak profit at middle strike. |
| [Bear Condor](bearish/) | Buy ATM put, sell ATM-1 and ATM-2, buy ATM-3 — wider profit zone than butterfly at slightly higher cost. |
| [Long Calendar with Puts](bearish/) | Sell current-week ATM put, buy next-week ATM put — profits from near-term time decay then downside. |
| [Short Synthetic Future](bearish/) | Sell ATM call, buy ATM put — replicates a short futures payoff using options. |
| [Put Ratio Back Spread](bearish/) | Sell ATM put, buy 2× OTM put — low-cost entry, profits accelerate on a large fall. |
| [Risk Reversal](bearish/) | Sell OTM call, buy OTM put — near-zero cost bearish position, unlimited risk on both sides. |

---

### [Neutral](neutral/)

Strategies that profit when the market moves **sideways** or stays range-bound. Use these when you expect low volatility.

| Strategy | Description |
|----------|-------------|
| [Short Straddle](neutral/) | Sell ATM call and ATM put at the same strike — maximum premium collected, profits if market barely moves. |
| [Short Strangle](neutral/) | Sell OTM call and OTM put — wider breakeven range than straddle, lower premium collected. |
| [Iron Butterfly](neutral/) | Sell ATM call and put, buy OTM call and put as wings — limited risk version of short straddle. |
| [Short Iron Condor](neutral/) | Sell OTM call and put, buy further OTM wings — wider range than iron butterfly, fully capped risk. |
| [Batman](neutral/) | Double butterfly (call + put side) — profits in a narrow range, defined risk on both sides. |

---

### [Others](others/)

Strategies that combine directional and volatility views, or are primarily volatility plays regardless of market direction.

| Strategy | Description |
|----------|-------------|
| [Call Ratio Spread](others/) | Buy ATM call, sell 2× OTM call — near-zero cost, profits at short strike, unlimited upside risk. |
| [Put Ratio Spread](others/) | Buy ATM put, sell 2× OTM put — near-zero cost, profits at short strike, large downside risk. |
| [Long Straddle](others/) | Buy ATM call and ATM put — profits from a big move in either direction. |
| [Long Strangle](others/) | Buy OTM call and OTM put — cheaper than straddle, needs a larger move to profit. |
| [Long Iron Butterfly](others/) | Buy ATM call and put, sell OTM wings — defined-risk volatility play, profits outside the short strikes. |
| [Long Iron Condor](others/) | Buy OTM call and put, sell further OTM wings — cheaper breakout play with fully capped risk. |
| [Strip](others/) | Buy ATM call and 2× ATM put — volatility play with bearish tilt, downside pays twice. |
| [Strap](others/) | Buy 2× ATM call and ATM put — volatility play with bullish tilt, upside pays twice. |
Loading
Loading