diff --git a/examples/README.md b/examples/README.md index 96d6482..83d859f 100644 --- a/examples/README.md +++ b/examples/README.md @@ -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 | diff --git a/examples/strategies/README.md b/examples/strategies/README.md new file mode 100644 index 0000000..0ffff85 --- /dev/null +++ b/examples/strategies/README.md @@ -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. | diff --git a/examples/strategies/bearish/README.md b/examples/strategies/bearish/README.md new file mode 100644 index 0000000..d287311 --- /dev/null +++ b/examples/strategies/bearish/README.md @@ -0,0 +1,287 @@ +# Bearish Strategies + +These files place bearish options trades on Nifty 50 using the Upstox Java SDK. Use them when you think the market is going to go **down**. + +**Before running:** Replace `your_access_token_here` in each file with your Upstox access token. + +> **Works on any index:** Change `"Nifty 50"` in `searchInstrument()` to trade a different index — e.g. `"Nifty Bank"` for Bank Nifty or `"SENSEX"` for BSE Sensex. + +--- + +## Buy Put — [code/BuyPut.java](code/BuyPut.java) + +**When to use:** You strongly believe Nifty will fall in the next few days. + +**What it does:** Buys one ATM put option. A put option gains value as the market falls. If the market rises instead, you only lose the premium you paid — nothing more. + +Think of it like the mirror image of buying a call — same fixed-risk structure, but you profit from a fall instead of a rise. + +**Example:** +- Nifty is at 23,100. You buy the 23100 PE for ₹130. +- Nifty falls to 22,800 by expiry → you make **₹170 profit** (300 points gain minus ₹130 premium) +- Nifty rises to 23,300 by expiry → option expires worthless, you lose **₹130** (your premium) + +``` +You profit when : Nifty closes below 22,970 (strike − premium) +Max profit : Grows as Nifty falls further +Max loss : ₹130 per unit — the premium you paid +``` + +**Run:** +```bash +javac code/BuyPut.java && java -cp . BuyPut +``` + +--- + +## Sell Call — [code/SellCall.java](code/SellCall.java) + +**When to use:** You think Nifty will stay flat or drift slightly lower — not necessarily a big crash. + +**What it does:** Sells one ATM call option and collects premium upfront. As long as the market doesn't rise much, you keep the premium as profit. If the market rallies sharply, losses can be significant. + +Think of it like the bearish version of selling a put — you collect a fee and hope nothing unexpected happens on the upside. + +**Example:** +- Nifty is at 23,100. You sell the 23100 CE and collect ₹150. +- Nifty stays at 23,100 or goes down by expiry → call expires worthless, you keep **₹150** +- Nifty rises to 23,400 by expiry → you lose ₹300 but already collected ₹150, net loss = **₹150** + +``` +You profit when : Nifty closes below 23,250 (strike + premium) +Max profit : ₹150 per unit — the premium you collected +Max loss : Large if Nifty rallies sharply above the strike +``` + +**Run:** +```bash +javac code/SellCall.java && java -cp . SellCall +``` + +--- + +## Bear Call Spread — [code/BearCallSpread.java](code/BearCallSpread.java) + +**When to use:** You expect Nifty to stay flat or fall moderately. You want to earn premium but with a safety net on the upside. + +**What it does:** Sells an ATM call (collects premium) and buys an OTM call one strike above (pays a smaller premium as protection). The bought call limits your loss if the market rises unexpectedly. You keep the net credit as profit if Nifty stays below the sold strike. + +**Example:** +- Nifty is at 23,100. Sell 23100 CE for ₹150, Buy 23150 CE for ₹110. Net credit = ₹40. +- Nifty stays below 23,100 at expiry → both calls expire worthless, you keep **₹40** +- Nifty rises to 23,300 at expiry → max loss = ₹50 (spread width) − ₹40 = **₹10 per unit** + +``` +You profit when : Nifty closes below 23,140 (short strike + net credit) +Max profit : ₹40 per unit — the net premium collected +Max loss : ₹10 per unit — capped by the bought call +``` + +> **Why use this over a plain Sell Call?** The bought call acts as a safety net — your loss is capped no matter how far the market rises. + +**Run:** +```bash +javac code/BearCallSpread.java && java -cp . BearCallSpread +``` + +--- + +## Bear Put Spread — [code/BearPutSpread.java](code/BearPutSpread.java) + +**When to use:** You expect Nifty to fall moderately — not a huge crash, but a noticeable move down. You want cheaper entry than buying a put outright. + +**What it does:** Buys an ATM put (higher strike) and sells a lower OTM put. The premium received from the sold put reduces the net cost. The trade-off: your profit is capped — you won't benefit if Nifty crashes far below the lower strike. + +**Example:** +- Nifty is at 23,100. Buy 23100 PE for ₹130, Sell 23050 PE for ₹95. Net cost = ₹35. +- Nifty falls to 22,900 by expiry → max profit = **₹15 per unit** (50 point spread minus ₹35 cost) +- Nifty rises to 23,300 by expiry → both options expire worthless, you lose **₹35** + +``` +You profit when : Nifty closes below 23,065 (higher strike − net cost) +Max profit : ₹15 per unit — if Nifty closes at or below 23,050 +Max loss : ₹35 per unit — the net premium you paid +``` + +> **Why use this over a plain Buy Put?** It's cheaper. You give up profits from a large crash but pay less upfront. + +**Run:** +```bash +javac code/BearPutSpread.java && java -cp . BearPutSpread +``` + +--- + +## Bear Butterfly — [code/BearButterfly.java](code/BearButterfly.java) + +**When to use:** You expect Nifty to fall to a specific level by expiry — not too much, not too little. You want a low-cost trade with a sharp profit peak exactly at your target price. + +**What it does:** Uses three put strikes — buy one ATM put, sell two ATM-1 puts, and buy one ATM-2 put. The two sold puts bring in premium that makes this significantly cheaper than a plain bear put spread. Maximum profit is earned when Nifty closes exactly at ATM-1 (the middle strike) at expiry. Both max profit and max loss are fully capped. + +Think of it as a sniper trade on the downside — very low cost, defined risk, but Nifty needs to land close to your middle strike for the best payout. + +**Example:** +- Nifty is at 23,100. Buy 23100 PE (₹130), Sell 2× 23050 PE (₹95 each), Buy 23000 PE (₹65). +- Net cost = (130 + 65) − (95 × 2) = **₹5 per unit** +- Nifty closes at 23,050 at expiry → max profit = spread width − net cost = 50 − 5 = **₹45 per unit** +- Nifty stays at 23,100 or above → all puts expire worthless, you lose **₹5** +- Nifty falls to 23,000 or below → gains and losses cancel out, you lose **₹5** + +``` +You profit when : Nifty closes between 23,005 and 23,095 (approx) +Max profit : ₹45 per unit — if Nifty closes exactly at 23,050 (ATM-1) +Max loss : ₹5 per unit — the small net premium you paid +``` + +> **Why use this over a Bear Put Spread?** Much cheaper entry. The trade-off is that your profit is concentrated at one specific price — the middle strike. + +**Run:** +```bash +javac code/BearButterfly.java && java -cp . BearButterfly +``` + +--- + +## Bear Condor — [code/BearCondor.java](code/BearCondor.java) + +**When to use:** You expect Nifty to fall moderately into a specific range — not just a tiny dip, but not a full crash either. You want defined risk at a lower cost than a simple put spread. + +**What it does:** Uses four put strikes in descending order — buy ATM put, sell ATM-1 and ATM-2 puts, buy ATM-3 put. The two short puts in the middle bring in premium that reduces your net cost. Maximum profit is earned when Nifty closes anywhere between the two short strikes (ATM-1 and ATM-2) at expiry. Both max profit and max loss are fully capped. + +Think of it as a bear put spread with an extra layer — a wider profit zone than the butterfly, at a slightly higher cost. + +**Example:** +- Nifty is at 23,100. Buy 23100 PE (₹130), Sell 23050 PE (₹95), Sell 23000 PE (₹65), Buy 22950 PE (₹40). +- Net cost = (130 + 40) − (95 + 65) = **₹10 per unit** +- Nifty closes between 23,050 and 23,000 at expiry → max profit = spread width − net cost = 50 − 10 = **₹40 per unit** +- Nifty stays at 23,100 or above → all puts expire worthless, you lose **₹10** +- Nifty falls to 22,950 or below → all spreads cancel out, you lose **₹10** + +``` +You profit when : Nifty closes between 23,090 and 22,960 (approx) +Max profit : ₹40 per unit — if Nifty closes between 23,050 and 23,000 +Max loss : ₹10 per unit — the net premium you paid +``` + +> **Why use this over a Bear Butterfly?** Wider profit zone — you don't need Nifty to land on one exact strike. You sacrifice a little profit but get more room for the trade to work. + +**Run:** +```bash +javac code/BearCondor.java && java -cp . BearCondor +``` + +--- + +## Long Calendar with Puts — [code/LongCalendarPut.java](code/LongCalendarPut.java) + +**When to use:** You expect Nifty to stay near its current level in the short term but fall over the coming week. You want to use time decay to your advantage. + +**What it does:** Sells a current-week ATM put and buys a next-week ATM put at the same strike. The near-term put loses value faster (time decay accelerates closer to expiry), so you profit from that decay while still holding a longer-dated put that benefits if Nifty falls after the near-term expiry. + +Think of it as a two-phase trade — first earn from the near-term option expiring worthless, then ride any downside through the longer-dated put you still own. + +**Example:** +- Nifty is at 23,100. Sell current-week 23100 PE for ₹60, Buy next-week 23100 PE for ₹130. Net cost = ₹70. +- Nifty stays near 23,100 through current-week expiry → near-term put expires worthless, you keep ₹60. You still own the next-week put worth ₹130 (or more if Nifty falls). +- Nifty falls to 22,800 after current-week expiry → next-week put gains value significantly, overall profit grows. +- Nifty rises sharply → both puts lose value, max loss is the ₹70 net cost you paid. + +``` +You profit when : Nifty stays near ATM through near-term expiry, then falls +Max profit : Varies — depends on how much the far-term put gains after near-term expires +Max loss : ₹70 per unit — the net premium you paid (difference between the two puts) +``` + +> **Key difference from other strategies:** This trade spans two expiries. You are not just betting on direction — you are also betting on *when* the move happens. + +**Run:** +```bash +javac code/LongCalendarPut.java && java -cp . LongCalendarPut +``` + +--- + +## Short Synthetic Future — [code/ShortSyntheticFuture.java](code/ShortSyntheticFuture.java) + +**When to use:** You have a strong bearish view and want the same profit/loss profile as selling a futures contract, but using options instead. + +**What it does:** Sells an ATM call and buys an ATM put at the same strike and expiry. The premium collected from the sold call partially offsets the cost of the bought put, making this a near-zero-cost position. From this point, the trade behaves exactly like a short futures position — you profit point-for-point as Nifty falls, and lose point-for-point as it rises. + +Think of it as a short futures position built from options. You get the same unlimited downside profit and unlimited upside loss, but without daily MTM settlement like futures. + +**Example:** +- Nifty is at 23,100. Sell 23100 CE for ₹150, Buy 23100 PE for ₹130. Net credit = ₹20. +- Nifty falls to 22,700 at expiry → put pays ₹400, call expires worthless, profit = 400 + 20 = **₹420 per unit** +- Nifty rises to 23,500 at expiry → call is exercised against you, put expires worthless, loss = 400 − 20 = **₹380 per unit** +- Nifty closes exactly at 23,100 → both expire worthless, you keep the **₹20** net credit + +``` +You profit when : Nifty closes below 23,120 (strike + net credit) +Max profit : Grows point-for-point as Nifty falls — no cap +Max loss : Grows point-for-point as Nifty rises — no cap +``` + +> **How is this different from just shorting a futures contract?** The payoff is identical, but you avoid daily mark-to-market settlements. However, the risk is just as large — always use a clear stop-loss plan before entering. + +**Run:** +```bash +javac code/ShortSyntheticFuture.java && java -cp . ShortSyntheticFuture +``` + +--- + +## Put Ratio Back Spread — [code/PutRatioBackSpread.java](code/PutRatioBackSpread.java) + +**When to use:** You expect a sharp, significant fall in Nifty. You want to benefit from a big downside move while keeping entry cost low — ideally entering for free or a small credit. + +**What it does:** Sells one ATM put and uses that premium to buy two OTM puts (ATM-1). The sold ATM put finances most or all of the cost of the two bought puts. The trade has a zone of maximum loss around the short strike but profits substantially if Nifty falls sharply below the long put strikes. If Nifty stays flat or rises, you keep the small net credit (if any). + +Think of it as paying for a big downside move with the premium collected from selling a closer put. The more Nifty falls, the more you profit — because you hold two long puts. + +**Example:** +- Nifty is at 23,100. Sell 23100 PE (₹130), Buy 2× 23050 PE (₹95 each). Net credit = ₹130 − ₹190 = **−₹60 net debit** (or credit depending on premiums). +- Nifty closes at 23,050 at expiry → short put loses ₹50, long puts expire worthless, loss = 50 + 60 = **₹110** (max loss zone) +- Nifty falls to 22,800 at expiry → short put loses ₹300, 2 long puts gain 2×250 = ₹500, net = 500 − 300 − 60 = **₹140 profit** +- Nifty stays above 23,100 at expiry → all puts expire worthless, loss = **₹60** (net debit paid) + +``` +You profit when : Nifty falls significantly below ATM-1 strike +Max profit : Grows as Nifty falls sharply — 2 long puts accelerate gains +Max loss : At the long put strike (ATM-1) — limited, predictable zone +``` + +> **Put Ratio Back Spread vs Buy Put:** Back spread profits much more on a large crash due to 2× long puts, at a lower entry cost. The trade-off is a loss zone near the short strike if Nifty falls only slightly. + +**Run:** +```bash +javac code/PutRatioBackSpread.java && java -cp . PutRatioBackSpread +``` + +--- + +## Risk Reversal — [code/RiskReversal.java](code/RiskReversal.java) + +**When to use:** You have a clear bearish view and want downside exposure at very low cost — or even for free — by funding the put with a sold call. + +**What it does:** Sells an OTM call (ATM+1) and uses that premium to buy an OTM put (ATM-1). Since both options are out of the money, the premiums are often close, making this a near-zero-cost trade. You profit as Nifty falls below the put strike, and lose as it rises above the call strike. Between the two strikes, the trade is roughly flat. + +Think of it as a directional bet where you fund the downside protection by giving up upside participation. You don't pay much to enter, but your loss is unlimited if Nifty rallies. + +**Example:** +- Nifty is at 23,100. Sell 23150 CE (₹110), Buy 23050 PE (₹95). Net credit = ₹15. +- Nifty falls to 22,800 at expiry → put gains ₹250, call expires worthless, profit = 250 + 15 = **₹265 per unit** +- Nifty rises to 23,400 at expiry → call loses ₹250, put expires worthless, loss = 250 − 15 = **₹235 per unit** +- Nifty closes between 23,050 and 23,150 at expiry → both expire worthless, profit = **₹15** (net credit kept) + +``` +You profit when : Nifty closes below 23,035 (put strike − net credit) +Max profit : Grows as Nifty falls below the put strike — no cap +Max loss : Grows as Nifty rises above the call strike — no cap +``` + +> **Risk Reversal vs Buy Put:** Risk reversal costs far less (often near zero) but gives up upside if the market rallies sharply. Buy put has capped loss at the premium paid but costs more upfront. + +**Run:** +```bash +javac code/RiskReversal.java && java -cp . RiskReversal +``` diff --git a/examples/strategies/bearish/code/BearButterfly.java b/examples/strategies/bearish/code/BearButterfly.java new file mode 100644 index 0000000..5efb095 --- /dev/null +++ b/examples/strategies/bearish/code/BearButterfly.java @@ -0,0 +1,97 @@ +import com.upstox.ApiClient; +import com.upstox.ApiException; +import com.upstox.Configuration; +import com.upstox.api.PlaceOrderV3Request; +import com.upstox.api.PlaceOrderV3Response; +import io.swagger.client.api.InstrumentsApi; +import io.swagger.client.api.OrderApiV3; +import java.util.List; +import java.util.Map; + +public class BearButterfly { + public static void main(String[] args) { + // Replace with your access token + ApiClient defaultClient = Configuration.getDefaultApiClient(); + defaultClient.setAccessToken("your_access_token_here"); + + try { + InstrumentsApi instrumentsApi = new InstrumentsApi(); + OrderApiV3 orderApi = new OrderApiV3(); + + // Step 1: Find the ATM put option (upper long) + List> upperInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "next_week", 0, null, null).getData(); + Map upperLong = upperInstruments.get(0); + System.out.println("Upper long (ATM) - Trading symbol : " + upperLong.get("trading_symbol")); + System.out.println("Upper long (ATM) - Instrument key : " + upperLong.get("instrument_key")); + + // Step 2: Find the ATM-1 put option (middle short) + List> middleInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "next_week", -1, null, null).getData(); + Map middleShort = middleInstruments.get(0); + System.out.println("Middle short (ATM-1) - Trading symbol : " + middleShort.get("trading_symbol")); + System.out.println("Middle short (ATM-1) - Instrument key : " + middleShort.get("instrument_key")); + + // Step 3: Find the ATM-2 put option (lower long) + List> lowerInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "next_week", -2, null, null).getData(); + Map lowerLong = lowerInstruments.get(0); + System.out.println("Lower long (ATM-2) - Trading symbol : " + lowerLong.get("trading_symbol")); + System.out.println("Lower long (ATM-2) - Instrument key : " + lowerLong.get("instrument_key")); + + // Step 4: Buy the ATM put (upper long) + PlaceOrderV3Request buyUpperOrder = new PlaceOrderV3Request(); + buyUpperOrder.setInstrumentToken((String) upperLong.get("instrument_key")); + buyUpperOrder.setQuantity(((Double) upperLong.get("lot_size")).intValue()); + buyUpperOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyUpperOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyUpperOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyUpperOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyUpperOrder.setPrice(0F); + buyUpperOrder.setDisclosedQuantity(0); + buyUpperOrder.setTriggerPrice(0F); + buyUpperOrder.setMarketProtection(-1F); + buyUpperOrder.setIsAmo(false); + + PlaceOrderV3Response buyUpperResult = orderApi.placeOrder(buyUpperOrder); + System.out.println("Buy ATM PE order placed. Order ID: " + buyUpperResult); + + // Step 5: Sell 2x the ATM-1 put (middle short) + PlaceOrderV3Request sellMiddleOrder = new PlaceOrderV3Request(); + sellMiddleOrder.setInstrumentToken((String) middleShort.get("instrument_key")); + sellMiddleOrder.setQuantity(((Double) middleShort.get("lot_size")).intValue() * 2); + sellMiddleOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.SELL); + sellMiddleOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + sellMiddleOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + sellMiddleOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + sellMiddleOrder.setPrice(0F); + sellMiddleOrder.setDisclosedQuantity(0); + sellMiddleOrder.setTriggerPrice(0F); + sellMiddleOrder.setMarketProtection(-1F); + sellMiddleOrder.setIsAmo(false); + + PlaceOrderV3Response sellMiddleResult = orderApi.placeOrder(sellMiddleOrder); + System.out.println("Sell 2x ATM-1 PE order placed. Order ID: " + sellMiddleResult); + + // Step 6: Buy the ATM-2 put (lower long) + PlaceOrderV3Request buyLowerOrder = new PlaceOrderV3Request(); + buyLowerOrder.setInstrumentToken((String) lowerLong.get("instrument_key")); + buyLowerOrder.setQuantity(((Double) lowerLong.get("lot_size")).intValue()); + buyLowerOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyLowerOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyLowerOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyLowerOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyLowerOrder.setPrice(0F); + buyLowerOrder.setDisclosedQuantity(0); + buyLowerOrder.setTriggerPrice(0F); + buyLowerOrder.setMarketProtection(-1F); + buyLowerOrder.setIsAmo(false); + + PlaceOrderV3Response buyLowerResult = orderApi.placeOrder(buyLowerOrder); + System.out.println("Buy ATM-2 PE order placed. Order ID: " + buyLowerResult); + + } catch (ApiException e) { + System.err.println("API error: " + e.getMessage()); + } + } +} diff --git a/examples/strategies/bearish/code/BearCallSpread.java b/examples/strategies/bearish/code/BearCallSpread.java new file mode 100644 index 0000000..07ebeab --- /dev/null +++ b/examples/strategies/bearish/code/BearCallSpread.java @@ -0,0 +1,73 @@ +import com.upstox.ApiClient; +import com.upstox.ApiException; +import com.upstox.Configuration; +import com.upstox.api.PlaceOrderV3Request; +import com.upstox.api.PlaceOrderV3Response; +import io.swagger.client.api.InstrumentsApi; +import io.swagger.client.api.OrderApiV3; +import java.util.List; +import java.util.Map; + +public class BearCallSpread { + public static void main(String[] args) { + // Replace with your access token + ApiClient defaultClient = Configuration.getDefaultApiClient(); + defaultClient.setAccessToken("your_access_token_here"); + + try { + InstrumentsApi instrumentsApi = new InstrumentsApi(); + OrderApiV3 orderApi = new OrderApiV3(); + + // Step 1: Find the ATM call option (short call) + List> shortInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "next_week", 0, null, null).getData(); + Map shortCall = shortInstruments.get(0); + System.out.println("Short call (ATM) - Trading symbol : " + shortCall.get("trading_symbol")); + System.out.println("Short call (ATM) - Instrument key : " + shortCall.get("instrument_key")); + + // Step 2: Find the ATM+1 call option (long call) + List> longInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "next_week", 1, null, null).getData(); + Map longCall = longInstruments.get(0); + System.out.println("Long call (ATM+1) - Trading symbol : " + longCall.get("trading_symbol")); + System.out.println("Long call (ATM+1) - Instrument key : " + longCall.get("instrument_key")); + + // Step 3: Sell the ATM call + PlaceOrderV3Request sellOrder = new PlaceOrderV3Request(); + sellOrder.setInstrumentToken((String) shortCall.get("instrument_key")); + sellOrder.setQuantity(((Double) shortCall.get("lot_size")).intValue()); + sellOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.SELL); + sellOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + sellOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + sellOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + sellOrder.setPrice(0F); + sellOrder.setDisclosedQuantity(0); + sellOrder.setTriggerPrice(0F); + sellOrder.setMarketProtection(-1F); + sellOrder.setIsAmo(false); + + PlaceOrderV3Response sellResult = orderApi.placeOrder(sellOrder); + System.out.println("Sell ATM CE order placed. Order ID: " + sellResult); + + // Step 4: Buy the ATM+1 call + PlaceOrderV3Request buyOrder = new PlaceOrderV3Request(); + buyOrder.setInstrumentToken((String) longCall.get("instrument_key")); + buyOrder.setQuantity(((Double) longCall.get("lot_size")).intValue()); + buyOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyOrder.setPrice(0F); + buyOrder.setDisclosedQuantity(0); + buyOrder.setTriggerPrice(0F); + buyOrder.setMarketProtection(-1F); + buyOrder.setIsAmo(false); + + PlaceOrderV3Response buyResult = orderApi.placeOrder(buyOrder); + System.out.println("Buy ATM+1 CE order placed. Order ID: " + buyResult); + + } catch (ApiException e) { + System.err.println("API error: " + e.getMessage()); + } + } +} diff --git a/examples/strategies/bearish/code/BearCondor.java b/examples/strategies/bearish/code/BearCondor.java new file mode 100644 index 0000000..c0b9ab1 --- /dev/null +++ b/examples/strategies/bearish/code/BearCondor.java @@ -0,0 +1,118 @@ +import com.upstox.ApiClient; +import com.upstox.ApiException; +import com.upstox.Configuration; +import com.upstox.api.PlaceOrderV3Request; +import com.upstox.api.PlaceOrderV3Response; +import io.swagger.client.api.InstrumentsApi; +import io.swagger.client.api.OrderApiV3; +import java.util.List; +import java.util.Map; + +public class BearCondor { + public static void main(String[] args) { + // Replace with your access token + ApiClient defaultClient = Configuration.getDefaultApiClient(); + defaultClient.setAccessToken("your_access_token_here"); + + try { + InstrumentsApi instrumentsApi = new InstrumentsApi(); + OrderApiV3 orderApi = new OrderApiV3(); + + // Step 1: Find all four put option legs + List> atmInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "next_week", 0, null, null).getData(); + Map atmLeg = atmInstruments.get(0); + System.out.println("ATM (offset 0) - Trading symbol : " + atmLeg.get("trading_symbol")); + System.out.println("ATM (offset 0) - Instrument key : " + atmLeg.get("instrument_key")); + + List> atm1Instruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "next_week", -1, null, null).getData(); + Map atm1Leg = atm1Instruments.get(0); + System.out.println("ATM-1 (offset -1) - Trading symbol : " + atm1Leg.get("trading_symbol")); + System.out.println("ATM-1 (offset -1) - Instrument key : " + atm1Leg.get("instrument_key")); + + List> atm2Instruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "next_week", -2, null, null).getData(); + Map atm2Leg = atm2Instruments.get(0); + System.out.println("ATM-2 (offset -2) - Trading symbol : " + atm2Leg.get("trading_symbol")); + System.out.println("ATM-2 (offset -2) - Instrument key : " + atm2Leg.get("instrument_key")); + + List> atm3Instruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "next_week", -3, null, null).getData(); + Map atm3Leg = atm3Instruments.get(0); + System.out.println("ATM-3 (offset -3) - Trading symbol : " + atm3Leg.get("trading_symbol")); + System.out.println("ATM-3 (offset -3) - Instrument key : " + atm3Leg.get("instrument_key")); + + // Step 2: Buy the ATM put + PlaceOrderV3Request buyAtmOrder = new PlaceOrderV3Request(); + buyAtmOrder.setInstrumentToken((String) atmLeg.get("instrument_key")); + buyAtmOrder.setQuantity(((Double) atmLeg.get("lot_size")).intValue()); + buyAtmOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyAtmOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyAtmOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyAtmOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyAtmOrder.setPrice(0F); + buyAtmOrder.setDisclosedQuantity(0); + buyAtmOrder.setTriggerPrice(0F); + buyAtmOrder.setMarketProtection(-1F); + buyAtmOrder.setIsAmo(false); + + PlaceOrderV3Response buyAtmResult = orderApi.placeOrder(buyAtmOrder); + System.out.println("Buy ATM PE order placed. Order ID: " + buyAtmResult); + + // Step 3: Sell the ATM-1 put + PlaceOrderV3Request sellAtm1Order = new PlaceOrderV3Request(); + sellAtm1Order.setInstrumentToken((String) atm1Leg.get("instrument_key")); + sellAtm1Order.setQuantity(((Double) atm1Leg.get("lot_size")).intValue()); + sellAtm1Order.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.SELL); + sellAtm1Order.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + sellAtm1Order.setProduct(PlaceOrderV3Request.ProductEnum.D); + sellAtm1Order.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + sellAtm1Order.setPrice(0F); + sellAtm1Order.setDisclosedQuantity(0); + sellAtm1Order.setTriggerPrice(0F); + sellAtm1Order.setMarketProtection(-1F); + sellAtm1Order.setIsAmo(false); + + PlaceOrderV3Response sellAtm1Result = orderApi.placeOrder(sellAtm1Order); + System.out.println("Sell ATM-1 PE order placed. Order ID: " + sellAtm1Result); + + // Step 4: Sell the ATM-2 put + PlaceOrderV3Request sellAtm2Order = new PlaceOrderV3Request(); + sellAtm2Order.setInstrumentToken((String) atm2Leg.get("instrument_key")); + sellAtm2Order.setQuantity(((Double) atm2Leg.get("lot_size")).intValue()); + sellAtm2Order.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.SELL); + sellAtm2Order.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + sellAtm2Order.setProduct(PlaceOrderV3Request.ProductEnum.D); + sellAtm2Order.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + sellAtm2Order.setPrice(0F); + sellAtm2Order.setDisclosedQuantity(0); + sellAtm2Order.setTriggerPrice(0F); + sellAtm2Order.setMarketProtection(-1F); + sellAtm2Order.setIsAmo(false); + + PlaceOrderV3Response sellAtm2Result = orderApi.placeOrder(sellAtm2Order); + System.out.println("Sell ATM-2 PE order placed. Order ID: " + sellAtm2Result); + + // Step 5: Buy the ATM-3 put + PlaceOrderV3Request buyAtm3Order = new PlaceOrderV3Request(); + buyAtm3Order.setInstrumentToken((String) atm3Leg.get("instrument_key")); + buyAtm3Order.setQuantity(((Double) atm3Leg.get("lot_size")).intValue()); + buyAtm3Order.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyAtm3Order.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyAtm3Order.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyAtm3Order.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyAtm3Order.setPrice(0F); + buyAtm3Order.setDisclosedQuantity(0); + buyAtm3Order.setTriggerPrice(0F); + buyAtm3Order.setMarketProtection(-1F); + buyAtm3Order.setIsAmo(false); + + PlaceOrderV3Response buyAtm3Result = orderApi.placeOrder(buyAtm3Order); + System.out.println("Buy ATM-3 PE order placed. Order ID: " + buyAtm3Result); + + } catch (ApiException e) { + System.err.println("API error: " + e.getMessage()); + } + } +} diff --git a/examples/strategies/bearish/code/BearPutSpread.java b/examples/strategies/bearish/code/BearPutSpread.java new file mode 100644 index 0000000..e0a94af --- /dev/null +++ b/examples/strategies/bearish/code/BearPutSpread.java @@ -0,0 +1,73 @@ +import com.upstox.ApiClient; +import com.upstox.ApiException; +import com.upstox.Configuration; +import com.upstox.api.PlaceOrderV3Request; +import com.upstox.api.PlaceOrderV3Response; +import io.swagger.client.api.InstrumentsApi; +import io.swagger.client.api.OrderApiV3; +import java.util.List; +import java.util.Map; + +public class BearPutSpread { + public static void main(String[] args) { + // Replace with your access token + ApiClient defaultClient = Configuration.getDefaultApiClient(); + defaultClient.setAccessToken("your_access_token_here"); + + try { + InstrumentsApi instrumentsApi = new InstrumentsApi(); + OrderApiV3 orderApi = new OrderApiV3(); + + // Step 1: Find the ATM put option (upper long) + List> upperInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "next_week", 0, null, null).getData(); + Map upperLong = upperInstruments.get(0); + System.out.println("Upper long (ATM) - Trading symbol : " + upperLong.get("trading_symbol")); + System.out.println("Upper long (ATM) - Instrument key : " + upperLong.get("instrument_key")); + + // Step 2: Find the ATM-1 put option (lower short) + List> lowerInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "next_week", -1, null, null).getData(); + Map lowerShort = lowerInstruments.get(0); + System.out.println("Lower short (ATM-1) - Trading symbol : " + lowerShort.get("trading_symbol")); + System.out.println("Lower short (ATM-1) - Instrument key : " + lowerShort.get("instrument_key")); + + // Step 3: Buy the ATM put + PlaceOrderV3Request buyOrder = new PlaceOrderV3Request(); + buyOrder.setInstrumentToken((String) upperLong.get("instrument_key")); + buyOrder.setQuantity(((Double) upperLong.get("lot_size")).intValue()); + buyOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyOrder.setPrice(0F); + buyOrder.setDisclosedQuantity(0); + buyOrder.setTriggerPrice(0F); + buyOrder.setMarketProtection(-1F); + buyOrder.setIsAmo(false); + + PlaceOrderV3Response buyResult = orderApi.placeOrder(buyOrder); + System.out.println("Buy ATM PE order placed. Order ID: " + buyResult); + + // Step 4: Sell the ATM-1 put + PlaceOrderV3Request sellOrder = new PlaceOrderV3Request(); + sellOrder.setInstrumentToken((String) lowerShort.get("instrument_key")); + sellOrder.setQuantity(((Double) lowerShort.get("lot_size")).intValue()); + sellOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.SELL); + sellOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + sellOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + sellOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + sellOrder.setPrice(0F); + sellOrder.setDisclosedQuantity(0); + sellOrder.setTriggerPrice(0F); + sellOrder.setMarketProtection(-1F); + sellOrder.setIsAmo(false); + + PlaceOrderV3Response sellResult = orderApi.placeOrder(sellOrder); + System.out.println("Sell ATM-1 PE order placed. Order ID: " + sellResult); + + } catch (ApiException e) { + System.err.println("API error: " + e.getMessage()); + } + } +} diff --git a/examples/strategies/bearish/code/BuyPut.java b/examples/strategies/bearish/code/BuyPut.java new file mode 100644 index 0000000..de7eda0 --- /dev/null +++ b/examples/strategies/bearish/code/BuyPut.java @@ -0,0 +1,48 @@ +import com.upstox.ApiClient; +import com.upstox.ApiException; +import com.upstox.Configuration; +import com.upstox.api.PlaceOrderV3Request; +import com.upstox.api.PlaceOrderV3Response; +import io.swagger.client.api.InstrumentsApi; +import io.swagger.client.api.OrderApiV3; +import java.util.List; +import java.util.Map; + +public class BuyPut { + public static void main(String[] args) { + // Replace with your access token + ApiClient defaultClient = Configuration.getDefaultApiClient(); + defaultClient.setAccessToken("your_access_token_here"); + + try { + // Step 1: Find the ATM put option + InstrumentsApi instrumentsApi = new InstrumentsApi(); + List> instruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "next_week", 0, null, null).getData(); + Map instrument = instruments.get(0); + System.out.println("Trading symbol : " + instrument.get("trading_symbol")); + System.out.println("Instrument key : " + instrument.get("instrument_key")); + + // Step 2: Place a buy order + OrderApiV3 orderApi = new OrderApiV3(); + PlaceOrderV3Request body = new PlaceOrderV3Request(); + body.setInstrumentToken((String) instrument.get("instrument_key")); + body.setQuantity(((Double) instrument.get("lot_size")).intValue()); + body.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + body.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + body.setProduct(PlaceOrderV3Request.ProductEnum.D); + body.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + body.setPrice(0F); + body.setDisclosedQuantity(0); + body.setTriggerPrice(0F); + body.setMarketProtection(-1F); + body.setIsAmo(false); + + PlaceOrderV3Response result = orderApi.placeOrder(body); + System.out.println("Order placed successfully. Order ID: " + result); + + } catch (ApiException e) { + System.err.println("API error: " + e.getMessage()); + } + } +} diff --git a/examples/strategies/bearish/code/LongCalendarPut.java b/examples/strategies/bearish/code/LongCalendarPut.java new file mode 100644 index 0000000..163b6cd --- /dev/null +++ b/examples/strategies/bearish/code/LongCalendarPut.java @@ -0,0 +1,73 @@ +import com.upstox.ApiClient; +import com.upstox.ApiException; +import com.upstox.Configuration; +import com.upstox.api.PlaceOrderV3Request; +import com.upstox.api.PlaceOrderV3Response; +import io.swagger.client.api.InstrumentsApi; +import io.swagger.client.api.OrderApiV3; +import java.util.List; +import java.util.Map; + +public class LongCalendarPut { + public static void main(String[] args) { + // Replace with your access token + ApiClient defaultClient = Configuration.getDefaultApiClient(); + defaultClient.setAccessToken("your_access_token_here"); + + try { + InstrumentsApi instrumentsApi = new InstrumentsApi(); + OrderApiV3 orderApi = new OrderApiV3(); + + // Step 1: Find the current-week ATM put (near-term) + List> nearInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "current_week", 0, null, null).getData(); + Map nearPut = nearInstruments.get(0); + System.out.println("Near-term put (current week) - Trading symbol : " + nearPut.get("trading_symbol")); + System.out.println("Near-term put (current week) - Instrument key : " + nearPut.get("instrument_key")); + + // Step 2: Find the next-week ATM put (far-term) + List> farInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "next_week", 0, null, null).getData(); + Map farPut = farInstruments.get(0); + System.out.println("Far-term put (next week) - Trading symbol : " + farPut.get("trading_symbol")); + System.out.println("Far-term put (next week) - Instrument key : " + farPut.get("instrument_key")); + + // Step 3: Sell the near-term (current-week) put + PlaceOrderV3Request sellOrder = new PlaceOrderV3Request(); + sellOrder.setInstrumentToken((String) nearPut.get("instrument_key")); + sellOrder.setQuantity(((Double) nearPut.get("lot_size")).intValue()); + sellOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.SELL); + sellOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + sellOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + sellOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + sellOrder.setPrice(0F); + sellOrder.setDisclosedQuantity(0); + sellOrder.setTriggerPrice(0F); + sellOrder.setMarketProtection(-1F); + sellOrder.setIsAmo(false); + + PlaceOrderV3Response sellResult = orderApi.placeOrder(sellOrder); + System.out.println("Sell current-week PE order placed. Order ID: " + sellResult); + + // Step 4: Buy the far-term (next-week) put + PlaceOrderV3Request buyOrder = new PlaceOrderV3Request(); + buyOrder.setInstrumentToken((String) farPut.get("instrument_key")); + buyOrder.setQuantity(((Double) farPut.get("lot_size")).intValue()); + buyOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyOrder.setPrice(0F); + buyOrder.setDisclosedQuantity(0); + buyOrder.setTriggerPrice(0F); + buyOrder.setMarketProtection(-1F); + buyOrder.setIsAmo(false); + + PlaceOrderV3Response buyResult = orderApi.placeOrder(buyOrder); + System.out.println("Buy next-week PE order placed. Order ID: " + buyResult); + + } catch (ApiException e) { + System.err.println("API error: " + e.getMessage()); + } + } +} diff --git a/examples/strategies/bearish/code/PutRatioBackSpread.java b/examples/strategies/bearish/code/PutRatioBackSpread.java new file mode 100644 index 0000000..d1ad106 --- /dev/null +++ b/examples/strategies/bearish/code/PutRatioBackSpread.java @@ -0,0 +1,73 @@ +import com.upstox.ApiClient; +import com.upstox.ApiException; +import com.upstox.Configuration; +import com.upstox.api.PlaceOrderV3Request; +import com.upstox.api.PlaceOrderV3Response; +import io.swagger.client.api.InstrumentsApi; +import io.swagger.client.api.OrderApiV3; +import java.util.List; +import java.util.Map; + +public class PutRatioBackSpread { + public static void main(String[] args) { + // Replace with your access token + ApiClient defaultClient = Configuration.getDefaultApiClient(); + defaultClient.setAccessToken("your_access_token_here"); + + try { + InstrumentsApi instrumentsApi = new InstrumentsApi(); + OrderApiV3 orderApi = new OrderApiV3(); + + // Step 1: Find the ATM put option (short put) + List> shortInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "next_week", 0, null, null).getData(); + Map shortPut = shortInstruments.get(0); + System.out.println("Short put (ATM) - Trading symbol : " + shortPut.get("trading_symbol")); + System.out.println("Short put (ATM) - Instrument key : " + shortPut.get("instrument_key")); + + // Step 2: Find the ATM-1 put option (long put) + List> longInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "next_week", -1, null, null).getData(); + Map longPut = longInstruments.get(0); + System.out.println("Long put (ATM-1) - Trading symbol : " + longPut.get("trading_symbol")); + System.out.println("Long put (ATM-1) - Instrument key : " + longPut.get("instrument_key")); + + // Step 3: Sell 1x the ATM put + PlaceOrderV3Request sellOrder = new PlaceOrderV3Request(); + sellOrder.setInstrumentToken((String) shortPut.get("instrument_key")); + sellOrder.setQuantity(((Double) shortPut.get("lot_size")).intValue()); + sellOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.SELL); + sellOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + sellOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + sellOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + sellOrder.setPrice(0F); + sellOrder.setDisclosedQuantity(0); + sellOrder.setTriggerPrice(0F); + sellOrder.setMarketProtection(-1F); + sellOrder.setIsAmo(false); + + PlaceOrderV3Response sellResult = orderApi.placeOrder(sellOrder); + System.out.println("Sell 1x ATM PE order placed. Order ID: " + sellResult); + + // Step 4: Buy 2x the ATM-1 put + PlaceOrderV3Request buyOrder = new PlaceOrderV3Request(); + buyOrder.setInstrumentToken((String) longPut.get("instrument_key")); + buyOrder.setQuantity(((Double) longPut.get("lot_size")).intValue() * 2); + buyOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyOrder.setPrice(0F); + buyOrder.setDisclosedQuantity(0); + buyOrder.setTriggerPrice(0F); + buyOrder.setMarketProtection(-1F); + buyOrder.setIsAmo(false); + + PlaceOrderV3Response buyResult = orderApi.placeOrder(buyOrder); + System.out.println("Buy 2x ATM-1 PE order placed. Order ID: " + buyResult); + + } catch (ApiException e) { + System.err.println("API error: " + e.getMessage()); + } + } +} diff --git a/examples/strategies/bearish/code/RiskReversal.java b/examples/strategies/bearish/code/RiskReversal.java new file mode 100644 index 0000000..f499170 --- /dev/null +++ b/examples/strategies/bearish/code/RiskReversal.java @@ -0,0 +1,73 @@ +import com.upstox.ApiClient; +import com.upstox.ApiException; +import com.upstox.Configuration; +import com.upstox.api.PlaceOrderV3Request; +import com.upstox.api.PlaceOrderV3Response; +import io.swagger.client.api.InstrumentsApi; +import io.swagger.client.api.OrderApiV3; +import java.util.List; +import java.util.Map; + +public class RiskReversal { + public static void main(String[] args) { + // Replace with your access token + ApiClient defaultClient = Configuration.getDefaultApiClient(); + defaultClient.setAccessToken("your_access_token_here"); + + try { + InstrumentsApi instrumentsApi = new InstrumentsApi(); + OrderApiV3 orderApi = new OrderApiV3(); + + // Step 1: Find the ATM+1 call option (short call) + List> callInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "next_week", 1, null, null).getData(); + Map shortCall = callInstruments.get(0); + System.out.println("Short call (ATM+1) - Trading symbol : " + shortCall.get("trading_symbol")); + System.out.println("Short call (ATM+1) - Instrument key : " + shortCall.get("instrument_key")); + + // Step 2: Find the ATM-1 put option (long put) + List> putInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "next_week", -1, null, null).getData(); + Map longPut = putInstruments.get(0); + System.out.println("Long put (ATM-1) - Trading symbol : " + longPut.get("trading_symbol")); + System.out.println("Long put (ATM-1) - Instrument key : " + longPut.get("instrument_key")); + + // Step 3: Sell the ATM+1 call + PlaceOrderV3Request sellOrder = new PlaceOrderV3Request(); + sellOrder.setInstrumentToken((String) shortCall.get("instrument_key")); + sellOrder.setQuantity(((Double) shortCall.get("lot_size")).intValue()); + sellOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.SELL); + sellOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + sellOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + sellOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + sellOrder.setPrice(0F); + sellOrder.setDisclosedQuantity(0); + sellOrder.setTriggerPrice(0F); + sellOrder.setMarketProtection(-1F); + sellOrder.setIsAmo(false); + + PlaceOrderV3Response sellResult = orderApi.placeOrder(sellOrder); + System.out.println("Sell ATM+1 CE order placed. Order ID: " + sellResult); + + // Step 4: Buy the ATM-1 put + PlaceOrderV3Request buyOrder = new PlaceOrderV3Request(); + buyOrder.setInstrumentToken((String) longPut.get("instrument_key")); + buyOrder.setQuantity(((Double) longPut.get("lot_size")).intValue()); + buyOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyOrder.setPrice(0F); + buyOrder.setDisclosedQuantity(0); + buyOrder.setTriggerPrice(0F); + buyOrder.setMarketProtection(-1F); + buyOrder.setIsAmo(false); + + PlaceOrderV3Response buyResult = orderApi.placeOrder(buyOrder); + System.out.println("Buy ATM-1 PE order placed. Order ID: " + buyResult); + + } catch (ApiException e) { + System.err.println("API error: " + e.getMessage()); + } + } +} diff --git a/examples/strategies/bearish/code/SellCall.java b/examples/strategies/bearish/code/SellCall.java new file mode 100644 index 0000000..396c86c --- /dev/null +++ b/examples/strategies/bearish/code/SellCall.java @@ -0,0 +1,48 @@ +import com.upstox.ApiClient; +import com.upstox.ApiException; +import com.upstox.Configuration; +import com.upstox.api.PlaceOrderV3Request; +import com.upstox.api.PlaceOrderV3Response; +import io.swagger.client.api.InstrumentsApi; +import io.swagger.client.api.OrderApiV3; +import java.util.List; +import java.util.Map; + +public class SellCall { + public static void main(String[] args) { + // Replace with your access token + ApiClient defaultClient = Configuration.getDefaultApiClient(); + defaultClient.setAccessToken("your_access_token_here"); + + try { + // Step 1: Find the ATM call option + InstrumentsApi instrumentsApi = new InstrumentsApi(); + List> instruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "next_week", 0, null, null).getData(); + Map instrument = instruments.get(0); + System.out.println("Trading symbol : " + instrument.get("trading_symbol")); + System.out.println("Instrument key : " + instrument.get("instrument_key")); + + // Step 2: Place a sell order + OrderApiV3 orderApi = new OrderApiV3(); + PlaceOrderV3Request body = new PlaceOrderV3Request(); + body.setInstrumentToken((String) instrument.get("instrument_key")); + body.setQuantity(((Double) instrument.get("lot_size")).intValue()); + body.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.SELL); + body.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + body.setProduct(PlaceOrderV3Request.ProductEnum.D); + body.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + body.setPrice(0F); + body.setDisclosedQuantity(0); + body.setTriggerPrice(0F); + body.setMarketProtection(-1F); + body.setIsAmo(false); + + PlaceOrderV3Response result = orderApi.placeOrder(body); + System.out.println("Order placed successfully. Order ID: " + result); + + } catch (ApiException e) { + System.err.println("API error: " + e.getMessage()); + } + } +} diff --git a/examples/strategies/bearish/code/ShortSyntheticFuture.java b/examples/strategies/bearish/code/ShortSyntheticFuture.java new file mode 100644 index 0000000..85c0808 --- /dev/null +++ b/examples/strategies/bearish/code/ShortSyntheticFuture.java @@ -0,0 +1,73 @@ +import com.upstox.ApiClient; +import com.upstox.ApiException; +import com.upstox.Configuration; +import com.upstox.api.PlaceOrderV3Request; +import com.upstox.api.PlaceOrderV3Response; +import io.swagger.client.api.InstrumentsApi; +import io.swagger.client.api.OrderApiV3; +import java.util.List; +import java.util.Map; + +public class ShortSyntheticFuture { + public static void main(String[] args) { + // Replace with your access token + ApiClient defaultClient = Configuration.getDefaultApiClient(); + defaultClient.setAccessToken("your_access_token_here"); + + try { + InstrumentsApi instrumentsApi = new InstrumentsApi(); + OrderApiV3 orderApi = new OrderApiV3(); + + // Step 1: Find the ATM call option (short call) + List> callInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "next_week", 0, null, null).getData(); + Map shortCall = callInstruments.get(0); + System.out.println("Short call - Trading symbol : " + shortCall.get("trading_symbol")); + System.out.println("Short call - Instrument key : " + shortCall.get("instrument_key")); + + // Step 2: Find the ATM put option (long put) + List> putInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "next_week", 0, null, null).getData(); + Map longPut = putInstruments.get(0); + System.out.println("Long put - Trading symbol : " + longPut.get("trading_symbol")); + System.out.println("Long put - Instrument key : " + longPut.get("instrument_key")); + + // Step 3: Sell the ATM call + PlaceOrderV3Request sellOrder = new PlaceOrderV3Request(); + sellOrder.setInstrumentToken((String) shortCall.get("instrument_key")); + sellOrder.setQuantity(((Double) shortCall.get("lot_size")).intValue()); + sellOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.SELL); + sellOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + sellOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + sellOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + sellOrder.setPrice(0F); + sellOrder.setDisclosedQuantity(0); + sellOrder.setTriggerPrice(0F); + sellOrder.setMarketProtection(-1F); + sellOrder.setIsAmo(false); + + PlaceOrderV3Response sellResult = orderApi.placeOrder(sellOrder); + System.out.println("Sell ATM CE order placed. Order ID: " + sellResult); + + // Step 4: Buy the ATM put + PlaceOrderV3Request buyOrder = new PlaceOrderV3Request(); + buyOrder.setInstrumentToken((String) longPut.get("instrument_key")); + buyOrder.setQuantity(((Double) longPut.get("lot_size")).intValue()); + buyOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyOrder.setPrice(0F); + buyOrder.setDisclosedQuantity(0); + buyOrder.setTriggerPrice(0F); + buyOrder.setMarketProtection(-1F); + buyOrder.setIsAmo(false); + + PlaceOrderV3Response buyResult = orderApi.placeOrder(buyOrder); + System.out.println("Buy ATM PE order placed. Order ID: " + buyResult); + + } catch (ApiException e) { + System.err.println("API error: " + e.getMessage()); + } + } +} diff --git a/examples/strategies/bullish/README.md b/examples/strategies/bullish/README.md new file mode 100644 index 0000000..31037b4 --- /dev/null +++ b/examples/strategies/bullish/README.md @@ -0,0 +1,287 @@ +# Bullish Strategies + +These files place bullish options trades on Nifty 50 using the Upstox Java SDK. Use them when you think the market is going to go **up**. + +**Before running:** Replace `your_access_token_here` in each file with your Upstox access token. + +> **Works on any index:** Change `"Nifty 50"` in `searchInstrument()` to trade a different index — e.g. `"Nifty Bank"` for Bank Nifty or `"SENSEX"` for BSE Sensex. + +--- + +## Buy Call — [code/BuyCall.java](code/BuyCall.java) + +**When to use:** You strongly believe Nifty will go up in the next few days. + +**What it does:** Buys one ATM call option. A call option gains value as the market rises. If the market falls instead, you only lose the premium you paid — nothing more. + +Think of it like placing a bet: you pay a fixed amount upfront, and if the market moves in your favour, you profit. If it doesn't, your loss is capped at what you paid. + +**Example:** +- Nifty is at 23,100. You buy the 23100 CE for ₹150. +- Nifty goes up to 23,400 by expiry → you make **₹150 profit** (300 points gain minus ₹150 premium) +- Nifty falls to 23,000 by expiry → option expires worthless, you lose **₹150** (your premium) + +``` +You profit when : Nifty closes above 23,250 (strike + premium) +Max profit : Unlimited — grows as Nifty rises +Max loss : ₹150 per unit — the premium you paid +``` + +**Run:** +```bash +javac code/BuyCall.java && java -cp . BuyCall +``` + +--- + +## Sell Put — [code/SellPut.java](code/SellPut.java) + +**When to use:** You think Nifty will stay flat or go slightly up — not necessarily a big rally. + +**What it does:** Sells one ATM put option and collects premium upfront. As long as the market doesn't fall much, you keep the premium as profit. If the market falls sharply, losses can be significant. + +Think of it like being an insurance seller — you collect a fee, and the trade is fine unless something goes badly wrong. + +**Example:** +- Nifty is at 23,100. You sell the 23100 PE and collect ₹130. +- Nifty stays at 23,100 or goes up by expiry → put expires worthless, you keep **₹130** +- Nifty falls to 22,800 by expiry → you lose ₹300 but already collected ₹130, net loss = **₹170** + +``` +You profit when : Nifty closes above 22,970 (strike − premium) +Max profit : ₹130 per unit — the premium you collected +Max loss : Large if Nifty falls sharply below the strike +``` + +**Run:** +```bash +javac code/SellPut.java && java -cp . SellPut +``` + +--- + +## Bull Call Spread — [code/BullCallSpread.java](code/BullCallSpread.java) + +**When to use:** You expect Nifty to go up moderately — not a big move, but a steady rise. You want to reduce the cost compared to simply buying a call. + +**What it does:** Buys an ATM call (lower strike) and sells an OTM call (one strike above). The premium received from the sold call reduces how much you pay. The trade-off: your profit is capped — you won't benefit if Nifty shoots up beyond the upper strike. + +**Example:** +- Nifty is at 23,100. Buy 23100 CE for ₹150, Sell 23150 CE for ₹110. You pay a net ₹40. +- Nifty goes to 23,200 by expiry → max profit = **₹10 per unit** (50 point spread minus ₹40 cost) +- Nifty falls to 23,000 by expiry → both options expire worthless, you lose **₹40** + +``` +You profit when : Nifty closes above 23,140 (lower strike + net cost) +Max profit : ₹10 per unit — if Nifty closes at or above 23,150 +Max loss : ₹40 per unit — the net premium you paid +``` + +> **Why use this over a plain Buy Call?** It's cheaper. You sacrifice some upside profit but pay less upfront. + +**Run:** +```bash +javac code/BullCallSpread.java && java -cp . BullCallSpread +``` + +--- + +## Bull Put Spread — [code/BullPutSpread.java](code/BullPutSpread.java) + +**When to use:** You expect Nifty to stay above a certain level. You want to earn premium without taking on large downside risk. + +**What it does:** Sells an ATM put (collects premium) and buys a lower OTM put (pays a smaller premium as protection). The bought put limits how much you can lose if the market falls. You keep the difference — the net credit — as profit if the market stays above the sold strike. + +**Example:** +- Nifty is at 23,100. Sell 23100 PE for ₹130, Buy 23050 PE for ₹95. Net credit = ₹35. +- Nifty stays above 23,100 at expiry → both puts expire worthless, you keep **₹35** +- Nifty falls to 22,900 at expiry → max loss = ₹50 (spread width) − ₹35 = **₹15 per unit** + +``` +You profit when : Nifty closes above 23,065 (short strike − net credit) +Max profit : ₹35 per unit — the net premium collected +Max loss : ₹15 per unit — capped by the bought put +``` + +> **Why use this over a plain Sell Put?** The bought put acts as a safety net — your loss is limited no matter how far the market falls. + +**Run:** +```bash +javac code/BullPutSpread.java && java -cp . BullPutSpread +``` + +--- + +## Bull Butterfly — [code/BullButterfly.java](code/BullButterfly.java) + +**When to use:** You expect Nifty to rise to a specific level by expiry — not too much, not too little. You want a low-cost trade with a sharp profit peak right at your target price. + +**What it does:** Uses three call strikes — buy one ATM call, sell two ATM+1 calls, and buy one ATM+2 call. The two sold calls bring in premium that makes this cheaper than a plain bull call spread. The sweet spot is Nifty closing exactly at ATM+1 (the middle strike) at expiry. If Nifty goes too far above or falls below, both max profit and max loss are fully capped. + +Think of it as a sniper trade — low cost, defined risk, but you need Nifty to land close to your target strike to get the best payout. + +**Example:** +- Nifty is at 23,100. Buy 23100 CE (₹150), Sell 2× 23150 CE (₹110 each), Buy 23200 CE (₹75). +- Net cost = (150 + 75) − (110 × 2) = **₹5 per unit** +- Nifty closes at 23,150 at expiry → max profit = spread width − net cost = 50 − 5 = **₹45 per unit** +- Nifty closes at 23,100 or below → all calls expire worthless, you lose **₹5** +- Nifty closes at 23,200 or above → gains and losses cancel out, you lose **₹5** + +``` +You profit when : Nifty closes between 23,105 and 23,195 (approx) +Max profit : ₹45 per unit — if Nifty closes exactly at 23,150 (ATM+1) +Max loss : ₹5 per unit — the small net premium you paid +``` + +> **Why use this over a Bull Call Spread?** Much cheaper entry. The trade-off is that your profit is concentrated at one specific price — the middle strike. + +**Run:** +```bash +javac code/BullButterfly.java && java -cp . BullButterfly +``` + +--- + +## Bull Condor — [code/BullCondor.java](code/BullCondor.java) + +**When to use:** You expect Nifty to rise moderately into a specific range — not too little, not too much. You want defined risk at a lower cost than a simple call spread. + +**What it does:** Uses four call strikes in order — buy ATM, sell ATM+1, sell ATM+2, buy ATM+3. The two short calls in the middle bring in premium that reduces your net cost. Maximum profit is earned when Nifty closes anywhere between the two short strikes (ATM+1 and ATM+2) at expiry. Both max profit and max loss are fully capped. + +Think of it as a bull call spread with an extra layer — you give up some profit potential to make the trade cheaper to enter. + +**Example:** +- Nifty is at 23,100. Buy 23100 CE (₹150), Sell 23150 CE (₹110), Sell 23200 CE (₹75), Buy 23250 CE (₹45). +- Net cost = (150 + 45) − (110 + 75) = **₹10 per unit** +- Nifty closes between 23,150 and 23,200 at expiry → max profit = spread width − net cost = 50 − 10 = **₹40 per unit** +- Nifty closes at 23,100 or below → all calls expire worthless, you lose **₹10** +- Nifty closes at 23,250 or above → all spreads cancel out, you lose **₹10** + +``` +You profit when : Nifty closes between 23,110 and 23,240 (approx) +Max profit : ₹40 per unit — if Nifty closes between 23,150 and 23,200 +Max loss : ₹10 per unit — the net premium you paid +``` + +> **Why use this over a Bull Call Spread?** Lower upfront cost. You cap your upside further but pay significantly less to enter the trade. + +**Run:** +```bash +javac code/BullCondor.java && java -cp . BullCondor +``` + +--- + +## Long Calendar with Calls — [code/LongCalendarCall.java](code/LongCalendarCall.java) + +**When to use:** You expect Nifty to stay near its current level in the short term but move up over the coming week. You want to use time decay to your advantage. + +**What it does:** Sells a current-week ATM call and buys a next-week ATM call at the same strike. The near-term call loses value faster (time decay is quicker closer to expiry), so you profit from that decay while still holding a longer-dated call that benefits if Nifty rises after the near-term expiry. + +Think of it as a two-phase trade — first earn from the near-term option expiring, then ride any upside through the longer-dated call you still own. + +**Example:** +- Nifty is at 23,100. Sell current-week 23100 CE for ₹80, Buy next-week 23100 CE for ₹150. Net cost = ₹70. +- Nifty stays near 23,100 through current-week expiry → near-term call expires worthless, you keep ₹80. You still own the next-week call worth ₹150 (or more if Nifty rises). +- Nifty rises to 23,400 after current-week expiry → next-week call gains value significantly, overall profit grows. +- Nifty falls sharply → both calls lose value, max loss is the ₹70 net cost you paid. + +``` +You profit when : Nifty stays near ATM through near-term expiry, then rises +Max profit : Varies — depends on how much the far-term call gains after near-term expires +Max loss : ₹70 per unit — the net premium you paid (difference between the two calls) +``` + +> **Key difference from other strategies:** This trade spans two expiries. You are not just betting on direction — you are also betting on *when* the move happens. + +**Run:** +```bash +javac code/LongCalendarCall.java && java -cp . LongCalendarCall +``` + +--- + +## Long Synthetic Future — [code/LongSyntheticFuture.java](code/LongSyntheticFuture.java) + +**When to use:** You have a strong bullish view and want the same profit/loss profile as buying a futures contract, but using options instead. + +**What it does:** Buys an ATM call and sells an ATM put at the same strike and expiry. The premium collected from the sold put largely offsets the cost of the bought call, making this a near-zero-cost position. From this point, the trade behaves exactly like a long futures position — you profit point-for-point as Nifty rises, and lose point-for-point as it falls. + +Think of it as a futures position built from options. You get the same unlimited upside and unlimited downside, but with the flexibility of options (no daily MTM settlement like futures). + +**Example:** +- Nifty is at 23,100. Buy 23100 CE for ₹150, Sell 23100 PE for ₹130. Net cost = ₹20. +- Nifty rises to 23,500 at expiry → call pays ₹400, put expires worthless, profit = 400 − 20 = **₹380 per unit** +- Nifty falls to 22,700 at expiry → call expires worthless, put is exercised against you, loss = 400 + 20 = **₹420 per unit** +- Nifty closes exactly at 23,100 → both expire worthless, you lose only **₹20** (net cost paid) + +``` +You profit when : Nifty closes above 23,120 (strike + net cost) +Max profit : Unlimited — grows point-for-point as Nifty rises +Max loss : Unlimited — grows point-for-point as Nifty falls +``` + +> **How is this different from just buying a futures contract?** The payoff is identical, but you avoid daily mark-to-market settlements. However, the risk is just as large — do not use this strategy without a clear stop-loss plan. + +**Run:** +```bash +javac code/LongSyntheticFuture.java && java -cp . LongSyntheticFuture +``` + +--- + +## Call Ratio Back Spread — [code/CallRatioBackSpread.java](code/CallRatioBackSpread.java) + +**When to use:** You expect a sharp, significant rally in Nifty. You want to benefit from a big upside move while keeping entry cost low — ideally entering for free or a small credit. + +**What it does:** Sells one ATM call and uses that premium to buy two OTM calls (ATM+1). The sold ATM call finances most or all of the cost of the two bought calls. The trade has a zone of maximum loss around the short strike but profits substantially if Nifty rallies sharply above the long call strikes. If Nifty stays flat or falls, you keep the small net credit (if any). + +Think of it as paying for a big upside move with the premium collected from selling a closer call. The more Nifty rises, the more you profit — because you hold two long calls. + +**Example:** +- Nifty is at 23,100. Sell 23100 CE (₹150), Buy 2× 23150 CE (₹110 each). Net credit = ₹150 − ₹220 = **−₹70 net debit**. +- Nifty closes at 23,150 at expiry → short call loses ₹50, long calls expire worthless, loss = 50 + 70 = **₹120** (max loss zone) +- Nifty rises to 23,400 at expiry → short call loses ₹300, 2 long calls gain 2×250 = ₹500, net = 500 − 300 − 70 = **₹130 profit** +- Nifty stays below 23,100 at expiry → all calls expire worthless, loss = **₹70** (net debit paid) + +``` +You profit when : Nifty rallies significantly above ATM+1 strike +Max profit : Grows as Nifty rises sharply — 2 long calls accelerate gains +Max loss : At the long call strike (ATM+1) — limited, predictable zone +``` + +> **Call Ratio Back Spread vs Buy Call:** Back spread profits much more on a large rally due to 2× long calls, at a lower entry cost. The trade-off is a loss zone near the short strike if Nifty rises only slightly. + +**Run:** +```bash +javac code/CallRatioBackSpread.java && java -cp . CallRatioBackSpread +``` + +--- + +## Range Forward — [code/RangeForward.java](code/RangeForward.java) + +**When to use:** You have a clear bullish view and want upside exposure at very low cost — or even for free — by funding the call with a sold put. + +**What it does:** Sells an OTM put (ATM-1) and uses that premium to buy an OTM call (ATM+1). Since both options are out of the money, the premiums are often close, making this a near-zero-cost trade. You profit as Nifty rises above the call strike, and lose as it falls below the put strike. Between the two strikes, the trade is roughly flat. + +Think of it as a directional bet where you fund the upside participation by giving up downside protection. You don't pay much to enter, but your loss is unlimited if Nifty falls sharply. + +**Example:** +- Nifty is at 23,100. Sell 23050 PE (₹95), Buy 23150 CE (₹110). Net debit = ₹15. +- Nifty rises to 23,400 at expiry → call gains ₹250, put expires worthless, profit = 250 − 15 = **₹235 per unit** +- Nifty falls to 22,800 at expiry → call expires worthless, put loses ₹250, loss = 250 + 15 = **₹265 per unit** +- Nifty closes between 23,050 and 23,150 at expiry → both expire worthless, loss = **₹15** (net debit paid) + +``` +You profit when : Nifty closes above 23,165 (call strike + net debit) +Max profit : Grows as Nifty rises above the call strike — no cap +Max loss : Grows as Nifty falls below the put strike — no cap +``` + +> **Range Forward vs Buy Call:** Range forward costs far less (often near zero) but creates downside risk if the market falls sharply. Buy call has capped loss at the premium paid but costs more upfront. + +**Run:** +```bash +javac code/RangeForward.java && java -cp . RangeForward +``` diff --git a/examples/strategies/bullish/code/BullButterfly.java b/examples/strategies/bullish/code/BullButterfly.java new file mode 100644 index 0000000..ca37387 --- /dev/null +++ b/examples/strategies/bullish/code/BullButterfly.java @@ -0,0 +1,97 @@ +import com.upstox.ApiClient; +import com.upstox.ApiException; +import com.upstox.Configuration; +import com.upstox.api.PlaceOrderV3Request; +import com.upstox.api.PlaceOrderV3Response; +import io.swagger.client.api.InstrumentsApi; +import io.swagger.client.api.OrderApiV3; +import java.util.List; +import java.util.Map; + +public class BullButterfly { + public static void main(String[] args) { + // Replace with your access token + ApiClient defaultClient = Configuration.getDefaultApiClient(); + defaultClient.setAccessToken("your_access_token_here"); + + try { + InstrumentsApi instrumentsApi = new InstrumentsApi(); + OrderApiV3 orderApi = new OrderApiV3(); + + // Step 1: Find the ATM call option (lower long) + List> lowerInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "next_week", 0, null, null).getData(); + Map lowerLong = lowerInstruments.get(0); + System.out.println("Lower long (ATM) - Trading symbol : " + lowerLong.get("trading_symbol")); + System.out.println("Lower long (ATM) - Instrument key : " + lowerLong.get("instrument_key")); + + // Step 2: Find the ATM+1 call option (middle short) + List> middleInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "next_week", 1, null, null).getData(); + Map middleShort = middleInstruments.get(0); + System.out.println("Middle short (ATM+1) - Trading symbol : " + middleShort.get("trading_symbol")); + System.out.println("Middle short (ATM+1) - Instrument key : " + middleShort.get("instrument_key")); + + // Step 3: Find the ATM+2 call option (upper long) + List> upperInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "next_week", 2, null, null).getData(); + Map upperLong = upperInstruments.get(0); + System.out.println("Upper long (ATM+2) - Trading symbol : " + upperLong.get("trading_symbol")); + System.out.println("Upper long (ATM+2) - Instrument key : " + upperLong.get("instrument_key")); + + // Step 4: Buy the ATM call (lower long) + PlaceOrderV3Request buyLowerOrder = new PlaceOrderV3Request(); + buyLowerOrder.setInstrumentToken((String) lowerLong.get("instrument_key")); + buyLowerOrder.setQuantity(((Double) lowerLong.get("lot_size")).intValue()); + buyLowerOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyLowerOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyLowerOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyLowerOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyLowerOrder.setPrice(0F); + buyLowerOrder.setDisclosedQuantity(0); + buyLowerOrder.setTriggerPrice(0F); + buyLowerOrder.setMarketProtection(-1F); + buyLowerOrder.setIsAmo(false); + + PlaceOrderV3Response buyLowerResult = orderApi.placeOrder(buyLowerOrder); + System.out.println("Buy ATM CE order placed. Order ID: " + buyLowerResult); + + // Step 5: Sell 2x the ATM+1 call (middle short) + PlaceOrderV3Request sellMiddleOrder = new PlaceOrderV3Request(); + sellMiddleOrder.setInstrumentToken((String) middleShort.get("instrument_key")); + sellMiddleOrder.setQuantity(((Double) middleShort.get("lot_size")).intValue() * 2); + sellMiddleOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.SELL); + sellMiddleOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + sellMiddleOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + sellMiddleOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + sellMiddleOrder.setPrice(0F); + sellMiddleOrder.setDisclosedQuantity(0); + sellMiddleOrder.setTriggerPrice(0F); + sellMiddleOrder.setMarketProtection(-1F); + sellMiddleOrder.setIsAmo(false); + + PlaceOrderV3Response sellMiddleResult = orderApi.placeOrder(sellMiddleOrder); + System.out.println("Sell 2x ATM+1 CE order placed. Order ID: " + sellMiddleResult); + + // Step 6: Buy the ATM+2 call (upper long) + PlaceOrderV3Request buyUpperOrder = new PlaceOrderV3Request(); + buyUpperOrder.setInstrumentToken((String) upperLong.get("instrument_key")); + buyUpperOrder.setQuantity(((Double) upperLong.get("lot_size")).intValue()); + buyUpperOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyUpperOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyUpperOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyUpperOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyUpperOrder.setPrice(0F); + buyUpperOrder.setDisclosedQuantity(0); + buyUpperOrder.setTriggerPrice(0F); + buyUpperOrder.setMarketProtection(-1F); + buyUpperOrder.setIsAmo(false); + + PlaceOrderV3Response buyUpperResult = orderApi.placeOrder(buyUpperOrder); + System.out.println("Buy ATM+2 CE order placed. Order ID: " + buyUpperResult); + + } catch (ApiException e) { + System.err.println("API error: " + e.getMessage()); + } + } +} diff --git a/examples/strategies/bullish/code/BullCallSpread.java b/examples/strategies/bullish/code/BullCallSpread.java new file mode 100644 index 0000000..ca37441 --- /dev/null +++ b/examples/strategies/bullish/code/BullCallSpread.java @@ -0,0 +1,73 @@ +import com.upstox.ApiClient; +import com.upstox.ApiException; +import com.upstox.Configuration; +import com.upstox.api.PlaceOrderV3Request; +import com.upstox.api.PlaceOrderV3Response; +import io.swagger.client.api.InstrumentsApi; +import io.swagger.client.api.OrderApiV3; +import java.util.List; +import java.util.Map; + +public class BullCallSpread { + public static void main(String[] args) { + // Replace with your access token + ApiClient defaultClient = Configuration.getDefaultApiClient(); + defaultClient.setAccessToken("your_access_token_here"); + + try { + InstrumentsApi instrumentsApi = new InstrumentsApi(); + OrderApiV3 orderApi = new OrderApiV3(); + + // Step 1: Find the ATM call option (lower long) + List> lowerInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "next_week", 0, null, null).getData(); + Map lowerLong = lowerInstruments.get(0); + System.out.println("Lower long (ATM) - Trading symbol : " + lowerLong.get("trading_symbol")); + System.out.println("Lower long (ATM) - Instrument key : " + lowerLong.get("instrument_key")); + + // Step 2: Find the ATM+1 call option (upper short) + List> upperInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "next_week", 1, null, null).getData(); + Map upperShort = upperInstruments.get(0); + System.out.println("Upper short (ATM+1) - Trading symbol : " + upperShort.get("trading_symbol")); + System.out.println("Upper short (ATM+1) - Instrument key : " + upperShort.get("instrument_key")); + + // Step 3: Buy the ATM call (lower long) + PlaceOrderV3Request buyOrder = new PlaceOrderV3Request(); + buyOrder.setInstrumentToken((String) lowerLong.get("instrument_key")); + buyOrder.setQuantity(((Double) lowerLong.get("lot_size")).intValue()); + buyOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyOrder.setPrice(0F); + buyOrder.setDisclosedQuantity(0); + buyOrder.setTriggerPrice(0F); + buyOrder.setMarketProtection(-1F); + buyOrder.setIsAmo(false); + + PlaceOrderV3Response buyResult = orderApi.placeOrder(buyOrder); + System.out.println("Buy ATM CE order placed. Order ID: " + buyResult); + + // Step 4: Sell the ATM+1 call (upper short) + PlaceOrderV3Request sellOrder = new PlaceOrderV3Request(); + sellOrder.setInstrumentToken((String) upperShort.get("instrument_key")); + sellOrder.setQuantity(((Double) upperShort.get("lot_size")).intValue()); + sellOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.SELL); + sellOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + sellOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + sellOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + sellOrder.setPrice(0F); + sellOrder.setDisclosedQuantity(0); + sellOrder.setTriggerPrice(0F); + sellOrder.setMarketProtection(-1F); + sellOrder.setIsAmo(false); + + PlaceOrderV3Response sellResult = orderApi.placeOrder(sellOrder); + System.out.println("Sell ATM+1 CE order placed. Order ID: " + sellResult); + + } catch (ApiException e) { + System.err.println("API error: " + e.getMessage()); + } + } +} diff --git a/examples/strategies/bullish/code/BullCondor.java b/examples/strategies/bullish/code/BullCondor.java new file mode 100644 index 0000000..4e373a0 --- /dev/null +++ b/examples/strategies/bullish/code/BullCondor.java @@ -0,0 +1,118 @@ +import com.upstox.ApiClient; +import com.upstox.ApiException; +import com.upstox.Configuration; +import com.upstox.api.PlaceOrderV3Request; +import com.upstox.api.PlaceOrderV3Response; +import io.swagger.client.api.InstrumentsApi; +import io.swagger.client.api.OrderApiV3; +import java.util.List; +import java.util.Map; + +public class BullCondor { + public static void main(String[] args) { + // Replace with your access token + ApiClient defaultClient = Configuration.getDefaultApiClient(); + defaultClient.setAccessToken("your_access_token_here"); + + try { + InstrumentsApi instrumentsApi = new InstrumentsApi(); + OrderApiV3 orderApi = new OrderApiV3(); + + // Step 1: Find all four call option legs + List> atmInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "next_week", 0, null, null).getData(); + Map atmLeg = atmInstruments.get(0); + System.out.println("ATM (offset 0) - Trading symbol : " + atmLeg.get("trading_symbol")); + System.out.println("ATM (offset 0) - Instrument key : " + atmLeg.get("instrument_key")); + + List> atm1Instruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "next_week", 1, null, null).getData(); + Map atm1Leg = atm1Instruments.get(0); + System.out.println("ATM+1 (offset 1) - Trading symbol : " + atm1Leg.get("trading_symbol")); + System.out.println("ATM+1 (offset 1) - Instrument key : " + atm1Leg.get("instrument_key")); + + List> atm2Instruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "next_week", 2, null, null).getData(); + Map atm2Leg = atm2Instruments.get(0); + System.out.println("ATM+2 (offset 2) - Trading symbol : " + atm2Leg.get("trading_symbol")); + System.out.println("ATM+2 (offset 2) - Instrument key : " + atm2Leg.get("instrument_key")); + + List> atm3Instruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "next_week", 3, null, null).getData(); + Map atm3Leg = atm3Instruments.get(0); + System.out.println("ATM+3 (offset 3) - Trading symbol : " + atm3Leg.get("trading_symbol")); + System.out.println("ATM+3 (offset 3) - Instrument key : " + atm3Leg.get("instrument_key")); + + // Step 2: Buy the ATM call + PlaceOrderV3Request buyAtmOrder = new PlaceOrderV3Request(); + buyAtmOrder.setInstrumentToken((String) atmLeg.get("instrument_key")); + buyAtmOrder.setQuantity(((Double) atmLeg.get("lot_size")).intValue()); + buyAtmOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyAtmOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyAtmOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyAtmOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyAtmOrder.setPrice(0F); + buyAtmOrder.setDisclosedQuantity(0); + buyAtmOrder.setTriggerPrice(0F); + buyAtmOrder.setMarketProtection(-1F); + buyAtmOrder.setIsAmo(false); + + PlaceOrderV3Response buyAtmResult = orderApi.placeOrder(buyAtmOrder); + System.out.println("Buy ATM CE order placed. Order ID: " + buyAtmResult); + + // Step 3: Sell the ATM+1 call + PlaceOrderV3Request sellAtm1Order = new PlaceOrderV3Request(); + sellAtm1Order.setInstrumentToken((String) atm1Leg.get("instrument_key")); + sellAtm1Order.setQuantity(((Double) atm1Leg.get("lot_size")).intValue()); + sellAtm1Order.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.SELL); + sellAtm1Order.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + sellAtm1Order.setProduct(PlaceOrderV3Request.ProductEnum.D); + sellAtm1Order.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + sellAtm1Order.setPrice(0F); + sellAtm1Order.setDisclosedQuantity(0); + sellAtm1Order.setTriggerPrice(0F); + sellAtm1Order.setMarketProtection(-1F); + sellAtm1Order.setIsAmo(false); + + PlaceOrderV3Response sellAtm1Result = orderApi.placeOrder(sellAtm1Order); + System.out.println("Sell ATM+1 CE order placed. Order ID: " + sellAtm1Result); + + // Step 4: Sell the ATM+2 call + PlaceOrderV3Request sellAtm2Order = new PlaceOrderV3Request(); + sellAtm2Order.setInstrumentToken((String) atm2Leg.get("instrument_key")); + sellAtm2Order.setQuantity(((Double) atm2Leg.get("lot_size")).intValue()); + sellAtm2Order.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.SELL); + sellAtm2Order.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + sellAtm2Order.setProduct(PlaceOrderV3Request.ProductEnum.D); + sellAtm2Order.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + sellAtm2Order.setPrice(0F); + sellAtm2Order.setDisclosedQuantity(0); + sellAtm2Order.setTriggerPrice(0F); + sellAtm2Order.setMarketProtection(-1F); + sellAtm2Order.setIsAmo(false); + + PlaceOrderV3Response sellAtm2Result = orderApi.placeOrder(sellAtm2Order); + System.out.println("Sell ATM+2 CE order placed. Order ID: " + sellAtm2Result); + + // Step 5: Buy the ATM+3 call + PlaceOrderV3Request buyAtm3Order = new PlaceOrderV3Request(); + buyAtm3Order.setInstrumentToken((String) atm3Leg.get("instrument_key")); + buyAtm3Order.setQuantity(((Double) atm3Leg.get("lot_size")).intValue()); + buyAtm3Order.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyAtm3Order.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyAtm3Order.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyAtm3Order.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyAtm3Order.setPrice(0F); + buyAtm3Order.setDisclosedQuantity(0); + buyAtm3Order.setTriggerPrice(0F); + buyAtm3Order.setMarketProtection(-1F); + buyAtm3Order.setIsAmo(false); + + PlaceOrderV3Response buyAtm3Result = orderApi.placeOrder(buyAtm3Order); + System.out.println("Buy ATM+3 CE order placed. Order ID: " + buyAtm3Result); + + } catch (ApiException e) { + System.err.println("API error: " + e.getMessage()); + } + } +} diff --git a/examples/strategies/bullish/code/BullPutSpread.java b/examples/strategies/bullish/code/BullPutSpread.java new file mode 100644 index 0000000..397fc19 --- /dev/null +++ b/examples/strategies/bullish/code/BullPutSpread.java @@ -0,0 +1,73 @@ +import com.upstox.ApiClient; +import com.upstox.ApiException; +import com.upstox.Configuration; +import com.upstox.api.PlaceOrderV3Request; +import com.upstox.api.PlaceOrderV3Response; +import io.swagger.client.api.InstrumentsApi; +import io.swagger.client.api.OrderApiV3; +import java.util.List; +import java.util.Map; + +public class BullPutSpread { + public static void main(String[] args) { + // Replace with your access token + ApiClient defaultClient = Configuration.getDefaultApiClient(); + defaultClient.setAccessToken("your_access_token_here"); + + try { + InstrumentsApi instrumentsApi = new InstrumentsApi(); + OrderApiV3 orderApi = new OrderApiV3(); + + // Step 1: Find the ATM put option (upper short) + List> upperInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "next_week", 0, null, null).getData(); + Map upperShort = upperInstruments.get(0); + System.out.println("Upper short (ATM) - Trading symbol : " + upperShort.get("trading_symbol")); + System.out.println("Upper short (ATM) - Instrument key : " + upperShort.get("instrument_key")); + + // Step 2: Find the ATM-1 put option (lower long) + List> lowerInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "next_week", -1, null, null).getData(); + Map lowerLong = lowerInstruments.get(0); + System.out.println("Lower long (ATM-1) - Trading symbol : " + lowerLong.get("trading_symbol")); + System.out.println("Lower long (ATM-1) - Instrument key : " + lowerLong.get("instrument_key")); + + // Step 3: Sell the ATM put (upper short) + PlaceOrderV3Request sellOrder = new PlaceOrderV3Request(); + sellOrder.setInstrumentToken((String) upperShort.get("instrument_key")); + sellOrder.setQuantity(((Double) upperShort.get("lot_size")).intValue()); + sellOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.SELL); + sellOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + sellOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + sellOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + sellOrder.setPrice(0F); + sellOrder.setDisclosedQuantity(0); + sellOrder.setTriggerPrice(0F); + sellOrder.setMarketProtection(-1F); + sellOrder.setIsAmo(false); + + PlaceOrderV3Response sellResult = orderApi.placeOrder(sellOrder); + System.out.println("Sell ATM PE order placed. Order ID: " + sellResult); + + // Step 4: Buy the ATM-1 put (lower long) + PlaceOrderV3Request buyOrder = new PlaceOrderV3Request(); + buyOrder.setInstrumentToken((String) lowerLong.get("instrument_key")); + buyOrder.setQuantity(((Double) lowerLong.get("lot_size")).intValue()); + buyOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyOrder.setPrice(0F); + buyOrder.setDisclosedQuantity(0); + buyOrder.setTriggerPrice(0F); + buyOrder.setMarketProtection(-1F); + buyOrder.setIsAmo(false); + + PlaceOrderV3Response buyResult = orderApi.placeOrder(buyOrder); + System.out.println("Buy ATM-1 PE order placed. Order ID: " + buyResult); + + } catch (ApiException e) { + System.err.println("API error: " + e.getMessage()); + } + } +} diff --git a/examples/strategies/bullish/code/BuyCall.java b/examples/strategies/bullish/code/BuyCall.java new file mode 100644 index 0000000..3072980 --- /dev/null +++ b/examples/strategies/bullish/code/BuyCall.java @@ -0,0 +1,48 @@ +import com.upstox.ApiClient; +import com.upstox.ApiException; +import com.upstox.Configuration; +import com.upstox.api.PlaceOrderV3Request; +import com.upstox.api.PlaceOrderV3Response; +import io.swagger.client.api.InstrumentsApi; +import io.swagger.client.api.OrderApiV3; +import java.util.List; +import java.util.Map; + +public class BuyCall { + public static void main(String[] args) { + // Replace with your access token + ApiClient defaultClient = Configuration.getDefaultApiClient(); + defaultClient.setAccessToken("your_access_token_here"); + + try { + // Step 1: Find the ATM call option + InstrumentsApi instrumentsApi = new InstrumentsApi(); + List> instruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "next_week", 0, null, null).getData(); + Map instrument = instruments.get(0); + System.out.println("Trading symbol : " + instrument.get("trading_symbol")); + System.out.println("Instrument key : " + instrument.get("instrument_key")); + + // Step 2: Place a buy order + OrderApiV3 orderApi = new OrderApiV3(); + PlaceOrderV3Request body = new PlaceOrderV3Request(); + body.setInstrumentToken((String) instrument.get("instrument_key")); + body.setQuantity(((Double) instrument.get("lot_size")).intValue()); + body.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + body.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + body.setProduct(PlaceOrderV3Request.ProductEnum.D); + body.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + body.setPrice(0F); + body.setDisclosedQuantity(0); + body.setTriggerPrice(0F); + body.setMarketProtection(-1F); + body.setIsAmo(false); + + PlaceOrderV3Response result = orderApi.placeOrder(body); + System.out.println("Order placed successfully. Order ID: " + result); + + } catch (ApiException e) { + System.err.println("API error: " + e.getMessage()); + } + } +} diff --git a/examples/strategies/bullish/code/CallRatioBackSpread.java b/examples/strategies/bullish/code/CallRatioBackSpread.java new file mode 100644 index 0000000..cbf7272 --- /dev/null +++ b/examples/strategies/bullish/code/CallRatioBackSpread.java @@ -0,0 +1,73 @@ +import com.upstox.ApiClient; +import com.upstox.ApiException; +import com.upstox.Configuration; +import com.upstox.api.PlaceOrderV3Request; +import com.upstox.api.PlaceOrderV3Response; +import io.swagger.client.api.InstrumentsApi; +import io.swagger.client.api.OrderApiV3; +import java.util.List; +import java.util.Map; + +public class CallRatioBackSpread { + public static void main(String[] args) { + // Replace with your access token + ApiClient defaultClient = Configuration.getDefaultApiClient(); + defaultClient.setAccessToken("your_access_token_here"); + + try { + InstrumentsApi instrumentsApi = new InstrumentsApi(); + OrderApiV3 orderApi = new OrderApiV3(); + + // Step 1: Find the ATM call option (short call) + List> shortInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "next_week", 0, null, null).getData(); + Map shortCall = shortInstruments.get(0); + System.out.println("Short call (ATM) - Trading symbol : " + shortCall.get("trading_symbol")); + System.out.println("Short call (ATM) - Instrument key : " + shortCall.get("instrument_key")); + + // Step 2: Find the ATM+1 call option (long call) + List> longInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "next_week", 1, null, null).getData(); + Map longCall = longInstruments.get(0); + System.out.println("Long call (ATM+1) - Trading symbol : " + longCall.get("trading_symbol")); + System.out.println("Long call (ATM+1) - Instrument key : " + longCall.get("instrument_key")); + + // Step 3: Sell 1x the ATM call + PlaceOrderV3Request sellOrder = new PlaceOrderV3Request(); + sellOrder.setInstrumentToken((String) shortCall.get("instrument_key")); + sellOrder.setQuantity(((Double) shortCall.get("lot_size")).intValue()); + sellOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.SELL); + sellOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + sellOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + sellOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + sellOrder.setPrice(0F); + sellOrder.setDisclosedQuantity(0); + sellOrder.setTriggerPrice(0F); + sellOrder.setMarketProtection(-1F); + sellOrder.setIsAmo(false); + + PlaceOrderV3Response sellResult = orderApi.placeOrder(sellOrder); + System.out.println("Sell 1x ATM CE order placed. Order ID: " + sellResult); + + // Step 4: Buy 2x the ATM+1 call + PlaceOrderV3Request buyOrder = new PlaceOrderV3Request(); + buyOrder.setInstrumentToken((String) longCall.get("instrument_key")); + buyOrder.setQuantity(((Double) longCall.get("lot_size")).intValue() * 2); + buyOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyOrder.setPrice(0F); + buyOrder.setDisclosedQuantity(0); + buyOrder.setTriggerPrice(0F); + buyOrder.setMarketProtection(-1F); + buyOrder.setIsAmo(false); + + PlaceOrderV3Response buyResult = orderApi.placeOrder(buyOrder); + System.out.println("Buy 2x ATM+1 CE order placed. Order ID: " + buyResult); + + } catch (ApiException e) { + System.err.println("API error: " + e.getMessage()); + } + } +} diff --git a/examples/strategies/bullish/code/LongCalendarCall.java b/examples/strategies/bullish/code/LongCalendarCall.java new file mode 100644 index 0000000..a2bb7b8 --- /dev/null +++ b/examples/strategies/bullish/code/LongCalendarCall.java @@ -0,0 +1,73 @@ +import com.upstox.ApiClient; +import com.upstox.ApiException; +import com.upstox.Configuration; +import com.upstox.api.PlaceOrderV3Request; +import com.upstox.api.PlaceOrderV3Response; +import io.swagger.client.api.InstrumentsApi; +import io.swagger.client.api.OrderApiV3; +import java.util.List; +import java.util.Map; + +public class LongCalendarCall { + public static void main(String[] args) { + // Replace with your access token + ApiClient defaultClient = Configuration.getDefaultApiClient(); + defaultClient.setAccessToken("your_access_token_here"); + + try { + InstrumentsApi instrumentsApi = new InstrumentsApi(); + OrderApiV3 orderApi = new OrderApiV3(); + + // Step 1: Find the current-week ATM call (near-term) + List> nearInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "current_week", 0, null, null).getData(); + Map nearCall = nearInstruments.get(0); + System.out.println("Near-term call (current week) - Trading symbol : " + nearCall.get("trading_symbol")); + System.out.println("Near-term call (current week) - Instrument key : " + nearCall.get("instrument_key")); + + // Step 2: Find the next-week ATM call (far-term) + List> farInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "next_week", 0, null, null).getData(); + Map farCall = farInstruments.get(0); + System.out.println("Far-term call (next week) - Trading symbol : " + farCall.get("trading_symbol")); + System.out.println("Far-term call (next week) - Instrument key : " + farCall.get("instrument_key")); + + // Step 3: Sell the near-term (current-week) call + PlaceOrderV3Request sellOrder = new PlaceOrderV3Request(); + sellOrder.setInstrumentToken((String) nearCall.get("instrument_key")); + sellOrder.setQuantity(((Double) nearCall.get("lot_size")).intValue()); + sellOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.SELL); + sellOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + sellOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + sellOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + sellOrder.setPrice(0F); + sellOrder.setDisclosedQuantity(0); + sellOrder.setTriggerPrice(0F); + sellOrder.setMarketProtection(-1F); + sellOrder.setIsAmo(false); + + PlaceOrderV3Response sellResult = orderApi.placeOrder(sellOrder); + System.out.println("Sell current-week CE order placed. Order ID: " + sellResult); + + // Step 4: Buy the far-term (next-week) call + PlaceOrderV3Request buyOrder = new PlaceOrderV3Request(); + buyOrder.setInstrumentToken((String) farCall.get("instrument_key")); + buyOrder.setQuantity(((Double) farCall.get("lot_size")).intValue()); + buyOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyOrder.setPrice(0F); + buyOrder.setDisclosedQuantity(0); + buyOrder.setTriggerPrice(0F); + buyOrder.setMarketProtection(-1F); + buyOrder.setIsAmo(false); + + PlaceOrderV3Response buyResult = orderApi.placeOrder(buyOrder); + System.out.println("Buy next-week CE order placed. Order ID: " + buyResult); + + } catch (ApiException e) { + System.err.println("API error: " + e.getMessage()); + } + } +} diff --git a/examples/strategies/bullish/code/LongSyntheticFuture.java b/examples/strategies/bullish/code/LongSyntheticFuture.java new file mode 100644 index 0000000..fedfaa5 --- /dev/null +++ b/examples/strategies/bullish/code/LongSyntheticFuture.java @@ -0,0 +1,73 @@ +import com.upstox.ApiClient; +import com.upstox.ApiException; +import com.upstox.Configuration; +import com.upstox.api.PlaceOrderV3Request; +import com.upstox.api.PlaceOrderV3Response; +import io.swagger.client.api.InstrumentsApi; +import io.swagger.client.api.OrderApiV3; +import java.util.List; +import java.util.Map; + +public class LongSyntheticFuture { + public static void main(String[] args) { + // Replace with your access token + ApiClient defaultClient = Configuration.getDefaultApiClient(); + defaultClient.setAccessToken("your_access_token_here"); + + try { + InstrumentsApi instrumentsApi = new InstrumentsApi(); + OrderApiV3 orderApi = new OrderApiV3(); + + // Step 1: Find the ATM call option + List> callInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "next_week", 0, null, null).getData(); + Map atmCall = callInstruments.get(0); + System.out.println("ATM call - Trading symbol : " + atmCall.get("trading_symbol")); + System.out.println("ATM call - Instrument key : " + atmCall.get("instrument_key")); + + // Step 2: Find the ATM put option + List> putInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "next_week", 0, null, null).getData(); + Map atmPut = putInstruments.get(0); + System.out.println("ATM put - Trading symbol : " + atmPut.get("trading_symbol")); + System.out.println("ATM put - Instrument key : " + atmPut.get("instrument_key")); + + // Step 3: Buy the ATM call + PlaceOrderV3Request buyOrder = new PlaceOrderV3Request(); + buyOrder.setInstrumentToken((String) atmCall.get("instrument_key")); + buyOrder.setQuantity(((Double) atmCall.get("lot_size")).intValue()); + buyOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyOrder.setPrice(0F); + buyOrder.setDisclosedQuantity(0); + buyOrder.setTriggerPrice(0F); + buyOrder.setMarketProtection(-1F); + buyOrder.setIsAmo(false); + + PlaceOrderV3Response buyResult = orderApi.placeOrder(buyOrder); + System.out.println("Buy ATM CE order placed. Order ID: " + buyResult); + + // Step 4: Sell the ATM put + PlaceOrderV3Request sellOrder = new PlaceOrderV3Request(); + sellOrder.setInstrumentToken((String) atmPut.get("instrument_key")); + sellOrder.setQuantity(((Double) atmPut.get("lot_size")).intValue()); + sellOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.SELL); + sellOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + sellOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + sellOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + sellOrder.setPrice(0F); + sellOrder.setDisclosedQuantity(0); + sellOrder.setTriggerPrice(0F); + sellOrder.setMarketProtection(-1F); + sellOrder.setIsAmo(false); + + PlaceOrderV3Response sellResult = orderApi.placeOrder(sellOrder); + System.out.println("Sell ATM PE order placed. Order ID: " + sellResult); + + } catch (ApiException e) { + System.err.println("API error: " + e.getMessage()); + } + } +} diff --git a/examples/strategies/bullish/code/RangeForward.java b/examples/strategies/bullish/code/RangeForward.java new file mode 100644 index 0000000..a99443b --- /dev/null +++ b/examples/strategies/bullish/code/RangeForward.java @@ -0,0 +1,73 @@ +import com.upstox.ApiClient; +import com.upstox.ApiException; +import com.upstox.Configuration; +import com.upstox.api.PlaceOrderV3Request; +import com.upstox.api.PlaceOrderV3Response; +import io.swagger.client.api.InstrumentsApi; +import io.swagger.client.api.OrderApiV3; +import java.util.List; +import java.util.Map; + +public class RangeForward { + public static void main(String[] args) { + // Replace with your access token + ApiClient defaultClient = Configuration.getDefaultApiClient(); + defaultClient.setAccessToken("your_access_token_here"); + + try { + InstrumentsApi instrumentsApi = new InstrumentsApi(); + OrderApiV3 orderApi = new OrderApiV3(); + + // Step 1: Find the ATM-1 put option (short put) + List> putInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "next_week", -1, null, null).getData(); + Map shortPut = putInstruments.get(0); + System.out.println("Short put (ATM-1) - Trading symbol : " + shortPut.get("trading_symbol")); + System.out.println("Short put (ATM-1) - Instrument key : " + shortPut.get("instrument_key")); + + // Step 2: Find the ATM+1 call option (long call) + List> callInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "next_week", 1, null, null).getData(); + Map longCall = callInstruments.get(0); + System.out.println("Long call (ATM+1) - Trading symbol : " + longCall.get("trading_symbol")); + System.out.println("Long call (ATM+1) - Instrument key : " + longCall.get("instrument_key")); + + // Step 3: Sell the ATM-1 put + PlaceOrderV3Request sellOrder = new PlaceOrderV3Request(); + sellOrder.setInstrumentToken((String) shortPut.get("instrument_key")); + sellOrder.setQuantity(((Double) shortPut.get("lot_size")).intValue()); + sellOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.SELL); + sellOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + sellOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + sellOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + sellOrder.setPrice(0F); + sellOrder.setDisclosedQuantity(0); + sellOrder.setTriggerPrice(0F); + sellOrder.setMarketProtection(-1F); + sellOrder.setIsAmo(false); + + PlaceOrderV3Response sellResult = orderApi.placeOrder(sellOrder); + System.out.println("Sell ATM-1 PE order placed. Order ID: " + sellResult); + + // Step 4: Buy the ATM+1 call + PlaceOrderV3Request buyOrder = new PlaceOrderV3Request(); + buyOrder.setInstrumentToken((String) longCall.get("instrument_key")); + buyOrder.setQuantity(((Double) longCall.get("lot_size")).intValue()); + buyOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyOrder.setPrice(0F); + buyOrder.setDisclosedQuantity(0); + buyOrder.setTriggerPrice(0F); + buyOrder.setMarketProtection(-1F); + buyOrder.setIsAmo(false); + + PlaceOrderV3Response buyResult = orderApi.placeOrder(buyOrder); + System.out.println("Buy ATM+1 CE order placed. Order ID: " + buyResult); + + } catch (ApiException e) { + System.err.println("API error: " + e.getMessage()); + } + } +} diff --git a/examples/strategies/bullish/code/SellPut.java b/examples/strategies/bullish/code/SellPut.java new file mode 100644 index 0000000..caa2b99 --- /dev/null +++ b/examples/strategies/bullish/code/SellPut.java @@ -0,0 +1,48 @@ +import com.upstox.ApiClient; +import com.upstox.ApiException; +import com.upstox.Configuration; +import com.upstox.api.PlaceOrderV3Request; +import com.upstox.api.PlaceOrderV3Response; +import io.swagger.client.api.InstrumentsApi; +import io.swagger.client.api.OrderApiV3; +import java.util.List; +import java.util.Map; + +public class SellPut { + public static void main(String[] args) { + // Replace with your access token + ApiClient defaultClient = Configuration.getDefaultApiClient(); + defaultClient.setAccessToken("your_access_token_here"); + + try { + // Step 1: Find the ATM put option + InstrumentsApi instrumentsApi = new InstrumentsApi(); + List> instruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "next_week", 0, null, null).getData(); + Map instrument = instruments.get(0); + System.out.println("Trading symbol : " + instrument.get("trading_symbol")); + System.out.println("Instrument key : " + instrument.get("instrument_key")); + + // Step 2: Place a sell order + OrderApiV3 orderApi = new OrderApiV3(); + PlaceOrderV3Request body = new PlaceOrderV3Request(); + body.setInstrumentToken((String) instrument.get("instrument_key")); + body.setQuantity(((Double) instrument.get("lot_size")).intValue()); + body.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.SELL); + body.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + body.setProduct(PlaceOrderV3Request.ProductEnum.D); + body.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + body.setPrice(0F); + body.setDisclosedQuantity(0); + body.setTriggerPrice(0F); + body.setMarketProtection(-1F); + body.setIsAmo(false); + + PlaceOrderV3Response result = orderApi.placeOrder(body); + System.out.println("Order placed successfully. Order ID: " + result); + + } catch (ApiException e) { + System.err.println("API error: " + e.getMessage()); + } + } +} diff --git a/examples/strategies/neutral/README.md b/examples/strategies/neutral/README.md new file mode 100644 index 0000000..490871b --- /dev/null +++ b/examples/strategies/neutral/README.md @@ -0,0 +1,158 @@ +# Neutral Strategies + +These files place neutral options trades on Nifty 50 using the Upstox Java SDK. Use them when you think the market is going to **stay in a range** — not move up or down significantly. + +**Before running:** Replace `your_access_token_here` in each file with your Upstox access token. + +> **Works on any index:** Change `"Nifty 50"` in `searchInstrument()` to trade a different index — e.g. `"Nifty Bank"` for Bank Nifty or `"SENSEX"` for BSE Sensex. + +--- + +## Short Straddle — [code/ShortStraddle.java](code/ShortStraddle.java) + +**When to use:** You expect Nifty to barely move from its current level — low volatility, sideways market. + +**What it does:** Sells both an ATM call and an ATM put at the same strike. You collect premium from both sides. As long as the market stays close to that strike, both options lose value and you profit. If the market moves sharply in either direction, losses can be large. + +Think of it as betting that "nothing big will happen." You earn money from time decay as long as the market stays calm. + +**Example:** +- Nifty is at 23,100. Sell 23100 CE for ₹150, Sell 23100 PE for ₹130. Total collected = ₹280. +- Nifty closes at 23,100 at expiry → both options expire worthless, you keep **₹280** +- Nifty rises to 23,500 at expiry → call is exercised against you, net loss = 400 − 280 = **₹120** +- Nifty falls to 22,700 at expiry → put is exercised against you, net loss = 400 − 280 = **₹120** + +``` +You profit when : Nifty closes between 22,820 and 23,380 +Max profit : ₹280 per unit — if Nifty closes exactly at 23,100 +Max loss : Grows if Nifty moves far beyond either breakeven — no hard cap +``` + +> **Note:** This strategy has unlimited risk on both sides. Use it only when you are confident the market will stay range-bound, or consider Iron Butterfly for a safer version. + +**Run:** +```bash +javac code/ShortStraddle.java && java -cp . ShortStraddle +``` + +--- + +## Short Strangle — [code/ShortStrangle.java](code/ShortStrangle.java) + +**When to use:** Similar to the straddle, but you are okay giving the market a bit more room to move. You expect Nifty to stay within a wider range. + +**What it does:** Sells an OTM call (one strike above ATM) and an OTM put (one strike below ATM). Since both options are out-of-the-money, less premium is collected compared to a straddle — but the market has more breathing room before the trade starts losing. + +**Example:** +- Nifty is at 23,100. Sell 23150 CE for ₹110, Sell 23050 PE for ₹95. Total collected = ₹205. +- Nifty closes anywhere between 23,050 and 23,150 at expiry → both expire worthless, you keep **₹205** +- Nifty rises to 23,500 at expiry → net loss = (23500 − 23150) − 205 = **₹145** +- Nifty falls to 22,700 at expiry → net loss = (23050 − 22700) − 205 = **₹145** + +``` +You profit when : Nifty closes between 22,845 and 23,355 +Max profit : ₹205 per unit — if Nifty stays between the two sold strikes +Max loss : Grows if Nifty moves far beyond either breakeven — no hard cap +``` + +> **Straddle vs Strangle:** Strangle gives more room for the market to move but earns less premium. Straddle earns more but needs the market to stay very close to ATM. + +**Run:** +```bash +javac code/ShortStrangle.java && java -cp . ShortStrangle +``` + +--- + +## Iron Butterfly — [code/IronButterfly.java](code/IronButterfly.java) + +**When to use:** You expect Nifty to stay near its current level, but you want your losses to be capped — unlike the straddle where losses are unlimited. + +**What it does:** Sells an ATM call and an ATM put (just like a straddle) to collect premium, but also buys an OTM call and an OTM put further away as protection. The bought options limit how much you can lose if the market moves sharply. You collect less premium overall, but your risk is fully defined. + +**Example:** +- Nifty is at 23,100. +- Sell 23100 CE (₹150) + Sell 23100 PE (₹130) = ₹280 collected +- Buy 23200 CE (₹60) + Buy 23000 PE (₹55) = ₹115 paid as protection +- Net premium in hand = ₹165. Protection kicks in 100 points away from ATM. +- Nifty closes at 23,100 at expiry → max profit = **₹165 per unit** +- Nifty moves to 23,200 or 23,000 at expiry → loss is fully capped at the wing boundary + +``` +You profit when : Nifty closes between 22,935 and 23,265 +Max profit : ₹165 per unit — if Nifty closes at 23,100 +Max loss : Capped — cannot lose more than (wing width − net credit) +``` + +> **Why use this over a Straddle?** The Iron Butterfly is safer — your loss is always limited. Great for beginners stepping into neutral strategies. + +**Run:** +```bash +javac code/IronButterfly.java && java -cp . IronButterfly +``` + +--- + +## Batman — [code/Batman.java](code/Batman.java) + +**When to use:** You expect Nifty to stay range-bound but want two profit zones instead of one — one above and one below the current price. All risk is defined. + +**What it does:** Combines a call butterfly and a put butterfly around the ATM strike. A butterfly spread makes money when the market closes near the middle strike of that butterfly. By building one on the call side and one on the put side, you create two peaks of profit — one slightly above ATM and one slightly below. The profit and loss zone resembles the Batman logo, giving the strategy its name. + +You pay a small net premium to enter. Your loss cannot exceed that premium, no matter what the market does. + +**Legs breakdown:** +- Call butterfly: BUY ATM CE → SELL 2× ATM+1 CE → BUY ATM+2 CE +- Put butterfly: BUY ATM PE → SELL 2× ATM-1 PE → BUY ATM-2 PE + +**Example:** +- Nifty is at 23,100. Strikes used: 23000 / 23050 / 23100 / 23150 / 23200. +- Call butterfly net cost ≈ ₹10. Put butterfly net cost ≈ ₹5. Total net cost ≈ ₹15. +- Nifty closes near 23,150 at expiry → call butterfly pays off, profit ≈ **₹35 per unit** +- Nifty closes near 23,050 at expiry → put butterfly pays off, profit ≈ **₹30 per unit** +- Nifty closes at 22,800 or 23,400 at expiry → all options expire at max loss, you lose **≈ ₹15** (your net cost) + +``` +Profit zones : Near ATM+1 (call butterfly peak) and ATM-1 (put butterfly peak) +Max profit : Varies — roughly 2–4× the net cost, at each inner strike +Max loss : Net premium paid ≈ ₹15 per unit — fully defined, cannot lose more +``` + +> **Why Batman?** It's a low-cost, defined-risk strategy with two profit opportunities. Good when you expect the market to be slightly volatile but within a band. + +**Run:** +```bash +javac code/Batman.java && java -cp . Batman +``` + +--- + +## Short Iron Condor — [code/ShortIronCondor.java](code/ShortIronCondor.java) + +**When to use:** You expect Nifty to stay within a range — wider than a straddle, with fully capped risk on both sides. One of the most popular neutral strategies among options traders. + +**What it does:** Sells an OTM call (ATM+1) and an OTM put (ATM-1) to collect premium, then buys a further OTM call (ATM+2) and put (ATM-2) as wings to cap the maximum loss. You collect net premium upfront. As long as Nifty stays between the two short strikes, both short options expire worthless and you keep the full premium. If Nifty breaks out, the long wings limit how much you can lose. + +Think of it as a short strangle with insurance. You give up a little premium to buy the wings, but in return your loss is always capped — no matter how far the market moves. + +**Example:** +- Nifty is at 23,100. +- Sell 23150 CE (₹110) + Sell 23050 PE (₹95) = ₹205 collected +- Buy 23200 CE (₹60) + Buy 23000 PE (₹55) = ₹115 paid for wings +- Net credit = ₹90. Wing width = 50 points on each side. +- Nifty closes between 23,050 and 23,150 at expiry → all short options expire worthless, profit = **₹90 per unit** +- Nifty rises to 23,300 at expiry → call spread fully against you, loss = 50 − 90 → net **still profit ₹40** (within wing) +- Nifty rises beyond 23,200 → max loss kicks in = wing width − net credit = 50 − 90 → since credit > wing, net profit still positive here; adjust wing width for realistic scenarios + +``` +You profit when : Nifty closes between 22,960 and 23,240 (short strikes ± net credit) +Max profit : ₹90 per unit — if Nifty closes between 23,050 and 23,150 +Max loss : Wing width − Net credit = 50 − 90 → capped at wing boundary +``` + +> **Iron Condor vs Short Strangle:** The condor collects less premium but gives you defined, capped risk. The strangle earns more but has unlimited loss potential. For most traders, the condor is the safer, more manageable choice. + +**Run:** +```bash +javac code/ShortIronCondor.java && java -cp . ShortIronCondor +``` diff --git a/examples/strategies/neutral/code/Batman.java b/examples/strategies/neutral/code/Batman.java new file mode 100644 index 0000000..55c8e4a --- /dev/null +++ b/examples/strategies/neutral/code/Batman.java @@ -0,0 +1,164 @@ +import com.upstox.ApiClient; +import com.upstox.ApiException; +import com.upstox.Configuration; +import com.upstox.api.PlaceOrderV3Request; +import com.upstox.api.PlaceOrderV3Response; +import io.swagger.client.api.InstrumentsApi; +import io.swagger.client.api.OrderApiV3; +import java.util.List; +import java.util.Map; + +public class Batman { + public static void main(String[] args) { + // Replace with your access token + ApiClient defaultClient = Configuration.getDefaultApiClient(); + defaultClient.setAccessToken("your_access_token_here"); + + try { + InstrumentsApi instrumentsApi = new InstrumentsApi(); + OrderApiV3 orderApi = new OrderApiV3(); + + // Step 1: Find all six option legs + List> atmCallInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "next_week", 0, null, null).getData(); + Map atmCall = atmCallInstruments.get(0); + System.out.println("ATM call (CE offset 0) - Trading symbol : " + atmCall.get("trading_symbol")); + System.out.println("ATM call (CE offset 0) - Instrument key : " + atmCall.get("instrument_key")); + + List> otm1CallInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "next_week", 1, null, null).getData(); + Map otm1Call = otm1CallInstruments.get(0); + System.out.println("OTM+1 call (CE offset 1) - Trading symbol : " + otm1Call.get("trading_symbol")); + System.out.println("OTM+1 call (CE offset 1) - Instrument key : " + otm1Call.get("instrument_key")); + + List> otm2CallInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "next_week", 2, null, null).getData(); + Map otm2Call = otm2CallInstruments.get(0); + System.out.println("OTM+2 call (CE offset 2) - Trading symbol : " + otm2Call.get("trading_symbol")); + System.out.println("OTM+2 call (CE offset 2) - Instrument key : " + otm2Call.get("instrument_key")); + + List> atmPutInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "next_week", 0, null, null).getData(); + Map atmPut = atmPutInstruments.get(0); + System.out.println("ATM put (PE offset 0) - Trading symbol : " + atmPut.get("trading_symbol")); + System.out.println("ATM put (PE offset 0) - Instrument key : " + atmPut.get("instrument_key")); + + List> otm1PutInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "next_week", -1, null, null).getData(); + Map otm1Put = otm1PutInstruments.get(0); + System.out.println("OTM-1 put (PE offset -1) - Trading symbol : " + otm1Put.get("trading_symbol")); + System.out.println("OTM-1 put (PE offset -1) - Instrument key : " + otm1Put.get("instrument_key")); + + List> otm2PutInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "next_week", -2, null, null).getData(); + Map otm2Put = otm2PutInstruments.get(0); + System.out.println("OTM-2 put (PE offset -2) - Trading symbol : " + otm2Put.get("trading_symbol")); + System.out.println("OTM-2 put (PE offset -2) - Instrument key : " + otm2Put.get("instrument_key")); + + // Step 2: Buy ATM call + PlaceOrderV3Request buyAtmCallOrder = new PlaceOrderV3Request(); + buyAtmCallOrder.setInstrumentToken((String) atmCall.get("instrument_key")); + buyAtmCallOrder.setQuantity(((Double) atmCall.get("lot_size")).intValue()); + buyAtmCallOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyAtmCallOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyAtmCallOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyAtmCallOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyAtmCallOrder.setPrice(0F); + buyAtmCallOrder.setDisclosedQuantity(0); + buyAtmCallOrder.setTriggerPrice(0F); + buyAtmCallOrder.setMarketProtection(-1F); + buyAtmCallOrder.setIsAmo(false); + + PlaceOrderV3Response buyAtmCallResult = orderApi.placeOrder(buyAtmCallOrder); + System.out.println("Buy ATM CE order placed. Order ID: " + buyAtmCallResult); + + // Step 3: Sell 2x OTM+1 call + PlaceOrderV3Request sellOtm1CallOrder = new PlaceOrderV3Request(); + sellOtm1CallOrder.setInstrumentToken((String) otm1Call.get("instrument_key")); + sellOtm1CallOrder.setQuantity(((Double) otm1Call.get("lot_size")).intValue() * 2); + sellOtm1CallOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.SELL); + sellOtm1CallOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + sellOtm1CallOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + sellOtm1CallOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + sellOtm1CallOrder.setPrice(0F); + sellOtm1CallOrder.setDisclosedQuantity(0); + sellOtm1CallOrder.setTriggerPrice(0F); + sellOtm1CallOrder.setMarketProtection(-1F); + sellOtm1CallOrder.setIsAmo(false); + + PlaceOrderV3Response sellOtm1CallResult = orderApi.placeOrder(sellOtm1CallOrder); + System.out.println("Sell 2x OTM+1 CE order placed. Order ID: " + sellOtm1CallResult); + + // Step 4: Buy OTM+2 call + PlaceOrderV3Request buyOtm2CallOrder = new PlaceOrderV3Request(); + buyOtm2CallOrder.setInstrumentToken((String) otm2Call.get("instrument_key")); + buyOtm2CallOrder.setQuantity(((Double) otm2Call.get("lot_size")).intValue()); + buyOtm2CallOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyOtm2CallOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyOtm2CallOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyOtm2CallOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyOtm2CallOrder.setPrice(0F); + buyOtm2CallOrder.setDisclosedQuantity(0); + buyOtm2CallOrder.setTriggerPrice(0F); + buyOtm2CallOrder.setMarketProtection(-1F); + buyOtm2CallOrder.setIsAmo(false); + + PlaceOrderV3Response buyOtm2CallResult = orderApi.placeOrder(buyOtm2CallOrder); + System.out.println("Buy OTM+2 CE order placed. Order ID: " + buyOtm2CallResult); + + // Step 5: Buy ATM put + PlaceOrderV3Request buyAtmPutOrder = new PlaceOrderV3Request(); + buyAtmPutOrder.setInstrumentToken((String) atmPut.get("instrument_key")); + buyAtmPutOrder.setQuantity(((Double) atmPut.get("lot_size")).intValue()); + buyAtmPutOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyAtmPutOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyAtmPutOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyAtmPutOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyAtmPutOrder.setPrice(0F); + buyAtmPutOrder.setDisclosedQuantity(0); + buyAtmPutOrder.setTriggerPrice(0F); + buyAtmPutOrder.setMarketProtection(-1F); + buyAtmPutOrder.setIsAmo(false); + + PlaceOrderV3Response buyAtmPutResult = orderApi.placeOrder(buyAtmPutOrder); + System.out.println("Buy ATM PE order placed. Order ID: " + buyAtmPutResult); + + // Step 6: Sell 2x OTM-1 put + PlaceOrderV3Request sellOtm1PutOrder = new PlaceOrderV3Request(); + sellOtm1PutOrder.setInstrumentToken((String) otm1Put.get("instrument_key")); + sellOtm1PutOrder.setQuantity(((Double) otm1Put.get("lot_size")).intValue() * 2); + sellOtm1PutOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.SELL); + sellOtm1PutOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + sellOtm1PutOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + sellOtm1PutOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + sellOtm1PutOrder.setPrice(0F); + sellOtm1PutOrder.setDisclosedQuantity(0); + sellOtm1PutOrder.setTriggerPrice(0F); + sellOtm1PutOrder.setMarketProtection(-1F); + sellOtm1PutOrder.setIsAmo(false); + + PlaceOrderV3Response sellOtm1PutResult = orderApi.placeOrder(sellOtm1PutOrder); + System.out.println("Sell 2x OTM-1 PE order placed. Order ID: " + sellOtm1PutResult); + + // Step 7: Buy OTM-2 put + PlaceOrderV3Request buyOtm2PutOrder = new PlaceOrderV3Request(); + buyOtm2PutOrder.setInstrumentToken((String) otm2Put.get("instrument_key")); + buyOtm2PutOrder.setQuantity(((Double) otm2Put.get("lot_size")).intValue()); + buyOtm2PutOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyOtm2PutOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyOtm2PutOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyOtm2PutOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyOtm2PutOrder.setPrice(0F); + buyOtm2PutOrder.setDisclosedQuantity(0); + buyOtm2PutOrder.setTriggerPrice(0F); + buyOtm2PutOrder.setMarketProtection(-1F); + buyOtm2PutOrder.setIsAmo(false); + + PlaceOrderV3Response buyOtm2PutResult = orderApi.placeOrder(buyOtm2PutOrder); + System.out.println("Buy OTM-2 PE order placed. Order ID: " + buyOtm2PutResult); + + } catch (ApiException e) { + System.err.println("API error: " + e.getMessage()); + } + } +} diff --git a/examples/strategies/neutral/code/IronButterfly.java b/examples/strategies/neutral/code/IronButterfly.java new file mode 100644 index 0000000..b179abb --- /dev/null +++ b/examples/strategies/neutral/code/IronButterfly.java @@ -0,0 +1,121 @@ +import com.upstox.ApiClient; +import com.upstox.ApiException; +import com.upstox.Configuration; +import com.upstox.api.PlaceOrderV3Request; +import com.upstox.api.PlaceOrderV3Response; +import io.swagger.client.api.InstrumentsApi; +import io.swagger.client.api.OrderApiV3; +import java.util.List; +import java.util.Map; + +public class IronButterfly { + public static void main(String[] args) { + // Replace with your access token + ApiClient defaultClient = Configuration.getDefaultApiClient(); + defaultClient.setAccessToken("your_access_token_here"); + + try { + InstrumentsApi instrumentsApi = new InstrumentsApi(); + OrderApiV3 orderApi = new OrderApiV3(); + + // Step 1: Find the ATM call option (short call) + List> shortCallInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "next_week", 0, null, null).getData(); + Map shortCall = shortCallInstruments.get(0); + System.out.println("Short call (ATM) - Trading symbol : " + shortCall.get("trading_symbol")); + System.out.println("Short call (ATM) - Instrument key : " + shortCall.get("instrument_key")); + + // Step 2: Find the ATM put option (short put) + List> shortPutInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "next_week", 0, null, null).getData(); + Map shortPut = shortPutInstruments.get(0); + System.out.println("Short put (ATM) - Trading symbol : " + shortPut.get("trading_symbol")); + System.out.println("Short put (ATM) - Instrument key : " + shortPut.get("instrument_key")); + + // Step 3: Find the ATM+2 call option (long call wing) + List> longCallInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "next_week", 2, null, null).getData(); + Map longCall = longCallInstruments.get(0); + System.out.println("Long call (ATM+2) - Trading symbol : " + longCall.get("trading_symbol")); + System.out.println("Long call (ATM+2) - Instrument key : " + longCall.get("instrument_key")); + + // Step 4: Find the ATM-2 put option (long put wing) + List> longPutInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "next_week", -2, null, null).getData(); + Map longPut = longPutInstruments.get(0); + System.out.println("Long put (ATM-2) - Trading symbol : " + longPut.get("trading_symbol")); + System.out.println("Long put (ATM-2) - Instrument key : " + longPut.get("instrument_key")); + + // Step 5: Sell the ATM call + PlaceOrderV3Request sellCallOrder = new PlaceOrderV3Request(); + sellCallOrder.setInstrumentToken((String) shortCall.get("instrument_key")); + sellCallOrder.setQuantity(((Double) shortCall.get("lot_size")).intValue()); + sellCallOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.SELL); + sellCallOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + sellCallOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + sellCallOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + sellCallOrder.setPrice(0F); + sellCallOrder.setDisclosedQuantity(0); + sellCallOrder.setTriggerPrice(0F); + sellCallOrder.setMarketProtection(-1F); + sellCallOrder.setIsAmo(false); + + PlaceOrderV3Response sellCallResult = orderApi.placeOrder(sellCallOrder); + System.out.println("Sell ATM CE order placed. Order ID: " + sellCallResult); + + // Step 6: Sell the ATM put + PlaceOrderV3Request sellPutOrder = new PlaceOrderV3Request(); + sellPutOrder.setInstrumentToken((String) shortPut.get("instrument_key")); + sellPutOrder.setQuantity(((Double) shortPut.get("lot_size")).intValue()); + sellPutOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.SELL); + sellPutOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + sellPutOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + sellPutOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + sellPutOrder.setPrice(0F); + sellPutOrder.setDisclosedQuantity(0); + sellPutOrder.setTriggerPrice(0F); + sellPutOrder.setMarketProtection(-1F); + sellPutOrder.setIsAmo(false); + + PlaceOrderV3Response sellPutResult = orderApi.placeOrder(sellPutOrder); + System.out.println("Sell ATM PE order placed. Order ID: " + sellPutResult); + + // Step 7: Buy the ATM+2 call (wing) + PlaceOrderV3Request buyCallOrder = new PlaceOrderV3Request(); + buyCallOrder.setInstrumentToken((String) longCall.get("instrument_key")); + buyCallOrder.setQuantity(((Double) longCall.get("lot_size")).intValue()); + buyCallOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyCallOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyCallOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyCallOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyCallOrder.setPrice(0F); + buyCallOrder.setDisclosedQuantity(0); + buyCallOrder.setTriggerPrice(0F); + buyCallOrder.setMarketProtection(-1F); + buyCallOrder.setIsAmo(false); + + PlaceOrderV3Response buyCallResult = orderApi.placeOrder(buyCallOrder); + System.out.println("Buy ATM+2 CE order placed. Order ID: " + buyCallResult); + + // Step 8: Buy the ATM-2 put (wing) + PlaceOrderV3Request buyPutOrder = new PlaceOrderV3Request(); + buyPutOrder.setInstrumentToken((String) longPut.get("instrument_key")); + buyPutOrder.setQuantity(((Double) longPut.get("lot_size")).intValue()); + buyPutOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyPutOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyPutOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyPutOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyPutOrder.setPrice(0F); + buyPutOrder.setDisclosedQuantity(0); + buyPutOrder.setTriggerPrice(0F); + buyPutOrder.setMarketProtection(-1F); + buyPutOrder.setIsAmo(false); + + PlaceOrderV3Response buyPutResult = orderApi.placeOrder(buyPutOrder); + System.out.println("Buy ATM-2 PE order placed. Order ID: " + buyPutResult); + + } catch (ApiException e) { + System.err.println("API error: " + e.getMessage()); + } + } +} diff --git a/examples/strategies/neutral/code/ShortIronCondor.java b/examples/strategies/neutral/code/ShortIronCondor.java new file mode 100644 index 0000000..cb9c888 --- /dev/null +++ b/examples/strategies/neutral/code/ShortIronCondor.java @@ -0,0 +1,121 @@ +import com.upstox.ApiClient; +import com.upstox.ApiException; +import com.upstox.Configuration; +import com.upstox.api.PlaceOrderV3Request; +import com.upstox.api.PlaceOrderV3Response; +import io.swagger.client.api.InstrumentsApi; +import io.swagger.client.api.OrderApiV3; +import java.util.List; +import java.util.Map; + +public class ShortIronCondor { + public static void main(String[] args) { + // Replace with your access token + ApiClient defaultClient = Configuration.getDefaultApiClient(); + defaultClient.setAccessToken("your_access_token_here"); + + try { + InstrumentsApi instrumentsApi = new InstrumentsApi(); + OrderApiV3 orderApi = new OrderApiV3(); + + // Step 1: Find the ATM+1 call option (short call) + List> shortCallInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "next_week", 1, null, null).getData(); + Map shortCall = shortCallInstruments.get(0); + System.out.println("Short call (ATM+1) - Trading symbol : " + shortCall.get("trading_symbol")); + System.out.println("Short call (ATM+1) - Instrument key : " + shortCall.get("instrument_key")); + + // Step 2: Find the ATM-1 put option (short put) + List> shortPutInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "next_week", -1, null, null).getData(); + Map shortPut = shortPutInstruments.get(0); + System.out.println("Short put (ATM-1) - Trading symbol : " + shortPut.get("trading_symbol")); + System.out.println("Short put (ATM-1) - Instrument key : " + shortPut.get("instrument_key")); + + // Step 3: Find the ATM+2 call option (long call wing) + List> longCallInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "next_week", 2, null, null).getData(); + Map longCall = longCallInstruments.get(0); + System.out.println("Long call (ATM+2) - Trading symbol : " + longCall.get("trading_symbol")); + System.out.println("Long call (ATM+2) - Instrument key : " + longCall.get("instrument_key")); + + // Step 4: Find the ATM-2 put option (long put wing) + List> longPutInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "next_week", -2, null, null).getData(); + Map longPut = longPutInstruments.get(0); + System.out.println("Long put (ATM-2) - Trading symbol : " + longPut.get("trading_symbol")); + System.out.println("Long put (ATM-2) - Instrument key : " + longPut.get("instrument_key")); + + // Step 5: Sell the ATM+1 call + PlaceOrderV3Request sellCallOrder = new PlaceOrderV3Request(); + sellCallOrder.setInstrumentToken((String) shortCall.get("instrument_key")); + sellCallOrder.setQuantity(((Double) shortCall.get("lot_size")).intValue()); + sellCallOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.SELL); + sellCallOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + sellCallOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + sellCallOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + sellCallOrder.setPrice(0F); + sellCallOrder.setDisclosedQuantity(0); + sellCallOrder.setTriggerPrice(0F); + sellCallOrder.setMarketProtection(-1F); + sellCallOrder.setIsAmo(false); + + PlaceOrderV3Response sellCallResult = orderApi.placeOrder(sellCallOrder); + System.out.println("Sell ATM+1 CE order placed. Order ID: " + sellCallResult); + + // Step 6: Sell the ATM-1 put + PlaceOrderV3Request sellPutOrder = new PlaceOrderV3Request(); + sellPutOrder.setInstrumentToken((String) shortPut.get("instrument_key")); + sellPutOrder.setQuantity(((Double) shortPut.get("lot_size")).intValue()); + sellPutOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.SELL); + sellPutOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + sellPutOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + sellPutOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + sellPutOrder.setPrice(0F); + sellPutOrder.setDisclosedQuantity(0); + sellPutOrder.setTriggerPrice(0F); + sellPutOrder.setMarketProtection(-1F); + sellPutOrder.setIsAmo(false); + + PlaceOrderV3Response sellPutResult = orderApi.placeOrder(sellPutOrder); + System.out.println("Sell ATM-1 PE order placed. Order ID: " + sellPutResult); + + // Step 7: Buy the ATM+2 call (wing) + PlaceOrderV3Request buyCallOrder = new PlaceOrderV3Request(); + buyCallOrder.setInstrumentToken((String) longCall.get("instrument_key")); + buyCallOrder.setQuantity(((Double) longCall.get("lot_size")).intValue()); + buyCallOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyCallOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyCallOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyCallOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyCallOrder.setPrice(0F); + buyCallOrder.setDisclosedQuantity(0); + buyCallOrder.setTriggerPrice(0F); + buyCallOrder.setMarketProtection(-1F); + buyCallOrder.setIsAmo(false); + + PlaceOrderV3Response buyCallResult = orderApi.placeOrder(buyCallOrder); + System.out.println("Buy ATM+2 CE order placed. Order ID: " + buyCallResult); + + // Step 8: Buy the ATM-2 put (wing) + PlaceOrderV3Request buyPutOrder = new PlaceOrderV3Request(); + buyPutOrder.setInstrumentToken((String) longPut.get("instrument_key")); + buyPutOrder.setQuantity(((Double) longPut.get("lot_size")).intValue()); + buyPutOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyPutOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyPutOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyPutOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyPutOrder.setPrice(0F); + buyPutOrder.setDisclosedQuantity(0); + buyPutOrder.setTriggerPrice(0F); + buyPutOrder.setMarketProtection(-1F); + buyPutOrder.setIsAmo(false); + + PlaceOrderV3Response buyPutResult = orderApi.placeOrder(buyPutOrder); + System.out.println("Buy ATM-2 PE order placed. Order ID: " + buyPutResult); + + } catch (ApiException e) { + System.err.println("API error: " + e.getMessage()); + } + } +} diff --git a/examples/strategies/neutral/code/ShortStraddle.java b/examples/strategies/neutral/code/ShortStraddle.java new file mode 100644 index 0000000..9e63392 --- /dev/null +++ b/examples/strategies/neutral/code/ShortStraddle.java @@ -0,0 +1,73 @@ +import com.upstox.ApiClient; +import com.upstox.ApiException; +import com.upstox.Configuration; +import com.upstox.api.PlaceOrderV3Request; +import com.upstox.api.PlaceOrderV3Response; +import io.swagger.client.api.InstrumentsApi; +import io.swagger.client.api.OrderApiV3; +import java.util.List; +import java.util.Map; + +public class ShortStraddle { + public static void main(String[] args) { + // Replace with your access token + ApiClient defaultClient = Configuration.getDefaultApiClient(); + defaultClient.setAccessToken("your_access_token_here"); + + try { + InstrumentsApi instrumentsApi = new InstrumentsApi(); + OrderApiV3 orderApi = new OrderApiV3(); + + // Step 1: Find the ATM call option (short call) + List> callInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "next_week", 0, null, null).getData(); + Map shortCall = callInstruments.get(0); + System.out.println("Short call - Trading symbol : " + shortCall.get("trading_symbol")); + System.out.println("Short call - Instrument key : " + shortCall.get("instrument_key")); + + // Step 2: Find the ATM put option (short put) + List> putInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "next_week", 0, null, null).getData(); + Map shortPut = putInstruments.get(0); + System.out.println("Short put - Trading symbol : " + shortPut.get("trading_symbol")); + System.out.println("Short put - Instrument key : " + shortPut.get("instrument_key")); + + // Step 3: Sell the ATM call + PlaceOrderV3Request sellCallOrder = new PlaceOrderV3Request(); + sellCallOrder.setInstrumentToken((String) shortCall.get("instrument_key")); + sellCallOrder.setQuantity(((Double) shortCall.get("lot_size")).intValue()); + sellCallOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.SELL); + sellCallOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + sellCallOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + sellCallOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + sellCallOrder.setPrice(0F); + sellCallOrder.setDisclosedQuantity(0); + sellCallOrder.setTriggerPrice(0F); + sellCallOrder.setMarketProtection(-1F); + sellCallOrder.setIsAmo(false); + + PlaceOrderV3Response sellCallResult = orderApi.placeOrder(sellCallOrder); + System.out.println("Sell ATM CE order placed. Order ID: " + sellCallResult); + + // Step 4: Sell the ATM put + PlaceOrderV3Request sellPutOrder = new PlaceOrderV3Request(); + sellPutOrder.setInstrumentToken((String) shortPut.get("instrument_key")); + sellPutOrder.setQuantity(((Double) shortPut.get("lot_size")).intValue()); + sellPutOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.SELL); + sellPutOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + sellPutOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + sellPutOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + sellPutOrder.setPrice(0F); + sellPutOrder.setDisclosedQuantity(0); + sellPutOrder.setTriggerPrice(0F); + sellPutOrder.setMarketProtection(-1F); + sellPutOrder.setIsAmo(false); + + PlaceOrderV3Response sellPutResult = orderApi.placeOrder(sellPutOrder); + System.out.println("Sell ATM PE order placed. Order ID: " + sellPutResult); + + } catch (ApiException e) { + System.err.println("API error: " + e.getMessage()); + } + } +} diff --git a/examples/strategies/neutral/code/ShortStrangle.java b/examples/strategies/neutral/code/ShortStrangle.java new file mode 100644 index 0000000..77094e7 --- /dev/null +++ b/examples/strategies/neutral/code/ShortStrangle.java @@ -0,0 +1,73 @@ +import com.upstox.ApiClient; +import com.upstox.ApiException; +import com.upstox.Configuration; +import com.upstox.api.PlaceOrderV3Request; +import com.upstox.api.PlaceOrderV3Response; +import io.swagger.client.api.InstrumentsApi; +import io.swagger.client.api.OrderApiV3; +import java.util.List; +import java.util.Map; + +public class ShortStrangle { + public static void main(String[] args) { + // Replace with your access token + ApiClient defaultClient = Configuration.getDefaultApiClient(); + defaultClient.setAccessToken("your_access_token_here"); + + try { + InstrumentsApi instrumentsApi = new InstrumentsApi(); + OrderApiV3 orderApi = new OrderApiV3(); + + // Step 1: Find the ATM+1 call option (short call) + List> callInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "next_week", 1, null, null).getData(); + Map shortCall = callInstruments.get(0); + System.out.println("Short call (ATM+1) - Trading symbol : " + shortCall.get("trading_symbol")); + System.out.println("Short call (ATM+1) - Instrument key : " + shortCall.get("instrument_key")); + + // Step 2: Find the ATM-1 put option (short put) + List> putInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "next_week", -1, null, null).getData(); + Map shortPut = putInstruments.get(0); + System.out.println("Short put (ATM-1) - Trading symbol : " + shortPut.get("trading_symbol")); + System.out.println("Short put (ATM-1) - Instrument key : " + shortPut.get("instrument_key")); + + // Step 3: Sell the ATM+1 call + PlaceOrderV3Request sellCallOrder = new PlaceOrderV3Request(); + sellCallOrder.setInstrumentToken((String) shortCall.get("instrument_key")); + sellCallOrder.setQuantity(((Double) shortCall.get("lot_size")).intValue()); + sellCallOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.SELL); + sellCallOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + sellCallOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + sellCallOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + sellCallOrder.setPrice(0F); + sellCallOrder.setDisclosedQuantity(0); + sellCallOrder.setTriggerPrice(0F); + sellCallOrder.setMarketProtection(-1F); + sellCallOrder.setIsAmo(false); + + PlaceOrderV3Response sellCallResult = orderApi.placeOrder(sellCallOrder); + System.out.println("Sell ATM+1 CE order placed. Order ID: " + sellCallResult); + + // Step 4: Sell the ATM-1 put + PlaceOrderV3Request sellPutOrder = new PlaceOrderV3Request(); + sellPutOrder.setInstrumentToken((String) shortPut.get("instrument_key")); + sellPutOrder.setQuantity(((Double) shortPut.get("lot_size")).intValue()); + sellPutOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.SELL); + sellPutOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + sellPutOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + sellPutOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + sellPutOrder.setPrice(0F); + sellPutOrder.setDisclosedQuantity(0); + sellPutOrder.setTriggerPrice(0F); + sellPutOrder.setMarketProtection(-1F); + sellPutOrder.setIsAmo(false); + + PlaceOrderV3Response sellPutResult = orderApi.placeOrder(sellPutOrder); + System.out.println("Sell ATM-1 PE order placed. Order ID: " + sellPutResult); + + } catch (ApiException e) { + System.err.println("API error: " + e.getMessage()); + } + } +} diff --git a/examples/strategies/others/README.md b/examples/strategies/others/README.md new file mode 100644 index 0000000..a351c88 --- /dev/null +++ b/examples/strategies/others/README.md @@ -0,0 +1,242 @@ +# Other Strategies + +These files implement options strategies that don't fit neatly into a single bullish, bearish, or neutral category. They combine directional and volatility views, or are primarily volatility plays regardless of market direction. + +**Before running:** Replace `your_access_token_here` in each file with your Upstox access token. + +> **Works on any index:** Change `"Nifty 50"` in `searchInstrument()` to trade a different index — e.g. `"Nifty Bank"` for Bank Nifty or `"SENSEX"` for BSE Sensex. + +--- + +## Call Ratio Spread — [code/CallRatioSpread.java](code/CallRatioSpread.java) + +**When to use:** You expect a moderate upward move in Nifty but not a big rally. You want to enter at very low cost or even collect a small credit. + +**What it does:** Buys one ATM call and sells two OTM calls (ATM+1). The two sold calls bring in more premium than the one bought call costs, so this trade is often entered at zero cost or a small net credit. Maximum profit is at the short strike (ATM+1). If Nifty rises sharply beyond ATM+1, the uncovered short call starts losing — this is the key risk. + +**Example:** +- Nifty is at 23,100. Buy 23100 CE (₹150), Sell 2× 23150 CE (₹110 each). Net credit = ₹70. +- Nifty closes at 23,150 at expiry → long call gains ₹50, short calls expire worthless, profit = 50 + 70 = **₹120 per unit** +- Nifty closes at 23,000 at expiry → all calls expire worthless, profit = **₹70** (net credit kept) +- Nifty rises to 23,400 at expiry → long call gains ₹300, short calls lose 2×250 = ₹500, net = −200 + 70 = **₹130 loss** + +``` +You profit when : Nifty closes below 23,290 (approx upper breakeven) +Max profit : ₹120 per unit — if Nifty closes exactly at 23,150 +Max loss : Increases as Nifty rises sharply above the short strike — no hard cap +``` + +> **Warning:** The uncovered portion of the short call creates unlimited upside risk. Always have a stop-loss in place if Nifty rallies strongly. + +**Run:** +```bash +javac code/CallRatioSpread.java && java -cp . CallRatioSpread +``` + +--- + +## Put Ratio Spread — [code/PutRatioSpread.java](code/PutRatioSpread.java) + +**When to use:** You expect a moderate fall in Nifty but not a sharp crash. You want to enter at very low cost or collect a small credit. + +**What it does:** Buys one ATM put and sells two OTM puts (ATM-1). The two sold puts generate more premium than the one bought put costs, so this trade is often entered at zero cost or a small net credit. Maximum profit is at the short strike (ATM-1). If Nifty falls sharply below ATM-1, the uncovered short put starts losing — this is the key risk. + +**Example:** +- Nifty is at 23,100. Buy 23100 PE (₹130), Sell 2× 23050 PE (₹95 each). Net credit = ₹60. +- Nifty closes at 23,050 at expiry → long put gains ₹50, short puts expire worthless, profit = 50 + 60 = **₹110 per unit** +- Nifty stays at 23,200 at expiry → all puts expire worthless, profit = **₹60** (net credit kept) +- Nifty falls to 22,800 at expiry → long put gains ₹300, short puts lose 2×250 = ₹500, net = −200 + 60 = **₹140 loss** + +``` +You profit when : Nifty closes above 22,810 (approx lower breakeven) +Max profit : ₹110 per unit — if Nifty closes exactly at 23,050 +Max loss : Increases as Nifty falls sharply below the short strike — no hard cap +``` + +> **Warning:** The uncovered portion of the short put creates large downside risk. Always have a stop-loss in place if Nifty falls sharply. + +**Run:** +```bash +javac code/PutRatioSpread.java && java -cp . PutRatioSpread +``` + +--- + +## Long Straddle — [code/LongStraddle.java](code/LongStraddle.java) + +**When to use:** You expect a big move in Nifty but are unsure of the direction — for example, before a major event like RBI policy, budget, or election results. + +**What it does:** Buys both an ATM call and an ATM put at the same strike. You profit if Nifty moves significantly in either direction — up or down. The total premium paid is your maximum loss, which occurs only if Nifty closes exactly at the strike at expiry. This is the opposite of the short straddle. + +Think of it as paying for volatility. You don't care which way the market goes — you just need it to move enough to cover both premiums. + +**Example:** +- Nifty is at 23,100. Buy 23100 CE for ₹150, Buy 23100 PE for ₹130. Total cost = ₹280. +- Nifty rises to 23,500 at expiry → call gains ₹400, put expires worthless, profit = 400 − 280 = **₹120 per unit** +- Nifty falls to 22,700 at expiry → put gains ₹400, call expires worthless, profit = 400 − 280 = **₹120 per unit** +- Nifty closes at 23,100 at expiry → both expire worthless, loss = **₹280 per unit** + +``` +Upper breakeven : 23,100 + 280 = 23,380 +Lower breakeven : 23,100 − 280 = 22,820 +Max profit : Unlimited — grows as Nifty moves far in either direction +Max loss : ₹280 per unit — if Nifty closes exactly at ATM strike +``` + +> **Long vs Short Straddle:** Long straddle profits from big moves; short straddle profits from calm markets. Choose based on your volatility expectation, not just direction. + +**Run:** +```bash +javac code/LongStraddle.java && java -cp . LongStraddle +``` + +--- + +## Long Strangle — [code/LongStrangle.java](code/LongStrangle.java) + +**When to use:** Same as the long straddle — you expect a big move but don't know the direction. You want a cheaper entry than the straddle and are okay with a wider breakeven range. + +**What it does:** Buys an OTM call (ATM+1) and an OTM put (ATM-1). Since both options are already out of the money, they cost less than ATM options. Total premium paid is lower, but Nifty needs to move further before the trade starts making money. + +**Example:** +- Nifty is at 23,100. Buy 23150 CE for ₹110, Buy 23050 PE for ₹95. Total cost = ₹205. +- Nifty rises to 23,500 at expiry → call gains ₹350, put expires worthless, profit = 350 − 205 = **₹145 per unit** +- Nifty falls to 22,700 at expiry → put gains ₹350, call expires worthless, profit = 350 − 205 = **₹145 per unit** +- Nifty closes anywhere between 23,050 and 23,150 at expiry → both expire worthless, loss = **₹205 per unit** + +``` +Upper breakeven : 23,150 + 205 = 23,355 +Lower breakeven : 23,050 − 205 = 22,845 +Max profit : Grows as Nifty moves far in either direction +Max loss : ₹205 per unit — if Nifty closes between the two bought strikes +``` + +> **Straddle vs Strangle:** Strangle is cheaper but needs a larger move to profit. Straddle costs more but starts profiting sooner. Pick strangle when you expect a large move; straddle when you expect a moderate but decisive move. + +**Run:** +```bash +javac code/LongStrangle.java && java -cp . LongStrangle +``` + +--- + +## Long Iron Butterfly — [code/LongIronButterfly.java](code/LongIronButterfly.java) + +**When to use:** You expect Nifty to move significantly away from its current level but want your risk and cost to be fully capped. The opposite of the iron butterfly. + +**What it does:** Buys both an ATM call and ATM put (like a straddle), and sells an OTM call (ATM+2) and OTM put (ATM-2) as wings to reduce the net cost. The sold wings cap your maximum profit but significantly lower what you pay to enter. You profit if Nifty moves beyond the ATM strike by more than the net debit paid. + +**Example:** +- Nifty is at 23,100. +- Buy 23100 CE (₹150) + Buy 23100 PE (₹130) = ₹280 paid +- Sell 23200 CE (₹60) + Sell 23000 PE (₹55) = ₹115 collected from wings +- Net cost = ₹165. Wing width = 100 points. +- Nifty rises to 23,300 at expiry → call spread gains 100, put expires worthless, profit = 100 − 165 = **−₹65** (still a loss within wing) +- Nifty rises beyond 23,200 → max profit = wing width − net debit = 100 − 165 → capped, profit zone kicks in above breakeven +- Nifty closes at 23,100 → both ATM options expire worthless, loss = **₹165 per unit** (max loss) + +``` +Upper breakeven : ATM strike + Net debit = 23,100 + 165 = 23,265 +Lower breakeven : ATM strike − Net debit = 23,100 − 165 = 22,935 +Max profit : Wing width − Net debit — capped at the outer wings +Max loss : ₹165 per unit — if Nifty closes exactly at ATM strike +``` + +> **Long Iron Butterfly vs Long Straddle:** The iron butterfly is cheaper to enter because the sold wings offset cost, but your profit is capped. Use it when you want defined risk on both cost and reward. + +**Run:** +```bash +javac code/LongIronButterfly.java && java -cp . LongIronButterfly +``` + +--- + +## Long Iron Condor — [code/LongIronCondor.java](code/LongIronCondor.java) + +**When to use:** You expect Nifty to break out of its current range but want fully capped risk. The opposite of the short iron condor. + +**What it does:** Buys an OTM call (ATM+1) and an OTM put (ATM-1), and sells further OTM call (ATM+2) and put (ATM-2) wings to cap the cost. You pay a net debit. The trade profits if Nifty moves beyond either of the inner bought strikes by enough to cover the net debit paid. Both max profit and max loss are fully defined. + +**Example:** +- Nifty is at 23,100. +- Buy 23150 CE (₹110) + Buy 23050 PE (₹95) = ₹205 paid +- Sell 23200 CE (₹60) + Sell 23000 PE (₹55) = ₹115 collected from wings +- Net cost = ₹90. Wing width = 50 points on each side. +- Nifty rises to 23,300 at expiry → call spread fully in profit = ₹50, profit = 50 − 90 = **−₹40** (within wing, still a loss) +- Nifty rises beyond 23,200 → max profit = wing width − net debit = 50 − 90 → capped +- Nifty closes between 22,960 and 23,240 → both spreads expire worthless or partially, loss up to **₹90** + +``` +Upper breakeven : Short call strike + Net debit = 23,150 + 90 = 23,240 +Lower breakeven : Short put strike − Net debit = 23,050 − 90 = 22,960 +Max profit : Wing width − Net debit — earned when Nifty moves beyond outer wings +Max loss : ₹90 per unit — the net premium paid +``` + +> **Long Iron Condor vs Long Strangle:** Both profit from big moves, but the condor is cheaper because the wings cap your cost. The trade-off is capped profit. Use the condor when you want a cheaper, defined-risk volatility play. + +**Run:** +```bash +javac code/LongIronCondor.java && java -cp . LongIronCondor +``` + +--- + +## Strip — [code/Strip.java](code/Strip.java) + +**When to use:** You expect a big move in Nifty but lean towards a fall being more likely. You want to profit in either direction but earn more if the market drops. + +**What it does:** Buys one ATM call and two ATM puts at the same strike. It's like a long straddle but with an extra put — so the downside pays out twice as much as the upside. You pay more premium than a straddle, but the additional put doubles your profit for every point Nifty falls below the breakeven. If Nifty rallies instead, you still profit, just at a slower rate. + +Think of it as a straddle with a bearish tilt — you're not sure which way it goes, but you're guessing down is more likely. + +**Example:** +- Nifty is at 23,100. Buy 23100 CE (₹150) + Buy 2× 23100 PE (₹130 each). Total cost = ₹410. +- Nifty falls to 22,700 at expiry → call worthless, 2 puts gain 2×400 = ₹800, profit = 800 − 410 = **₹390 per unit** +- Nifty rises to 23,600 at expiry → puts worthless, call gains ₹500, profit = 500 − 410 = **₹90 per unit** +- Nifty closes at 23,100 at expiry → all options expire worthless, loss = **₹410 per unit** + +``` +Upper breakeven : Strike + Total premium = 23,100 + 410 = 23,510 +Lower breakeven : Strike − (Total premium ÷ 2) = 23,100 − 205 = 22,895 +Max profit : Unlimited on both sides — larger on the downside (2× puts) +Max loss : ₹410 per unit — if Nifty closes exactly at the ATM strike +``` + +> **Strip vs Long Straddle:** Strip profits more on a big fall, less on a big rise. Use it when you expect volatility but have a slight bearish bias. + +**Run:** +```bash +javac code/Strip.java && java -cp . Strip +``` + +--- + +## Strap — [code/Strap.java](code/Strap.java) + +**When to use:** You expect a big move in Nifty but lean towards a rise being more likely. You want to profit in either direction but earn more if the market rallies. + +**What it does:** Buys two ATM calls and one ATM put at the same strike. It's like a long straddle but with an extra call — so the upside pays out twice as much as the downside. You pay more premium than a straddle, but the additional call doubles your profit for every point Nifty rises above the breakeven. If Nifty falls instead, you still profit, just at a slower rate. + +Think of it as a straddle with a bullish tilt — you're not sure which way it goes, but you're guessing up is more likely. + +**Example:** +- Nifty is at 23,100. Buy 2× 23100 CE (₹150 each) + Buy 23100 PE (₹130). Total cost = ₹430. +- Nifty rises to 23,600 at expiry → put worthless, 2 calls gain 2×500 = ₹1000, profit = 1000 − 430 = **₹570 per unit** +- Nifty falls to 22,700 at expiry → calls worthless, put gains ₹400, profit = 400 − 430 = **−₹30** (small loss — needs a bigger fall) +- Nifty falls to 22,670 at expiry → put gains ₹430, profit = **₹0** (lower breakeven) +- Nifty closes at 23,100 at expiry → all options expire worthless, loss = **₹430 per unit** + +``` +Upper breakeven : Strike + (Total premium ÷ 2) = 23,100 + 215 = 23,315 +Lower breakeven : Strike − Total premium = 23,100 − 430 = 22,670 +Max profit : Unlimited on both sides — larger on the upside (2× calls) +Max loss : ₹430 per unit — if Nifty closes exactly at the ATM strike +``` + +> **Strap vs Long Straddle:** Strap profits more on a big rally, less on a big fall. Use it when you expect volatility but have a slight bullish bias. + +**Run:** +```bash +javac code/Strap.java && java -cp . Strap +``` diff --git a/examples/strategies/others/code/CallRatioSpread.java b/examples/strategies/others/code/CallRatioSpread.java new file mode 100644 index 0000000..0cb36ed --- /dev/null +++ b/examples/strategies/others/code/CallRatioSpread.java @@ -0,0 +1,73 @@ +import com.upstox.ApiClient; +import com.upstox.ApiException; +import com.upstox.Configuration; +import com.upstox.api.PlaceOrderV3Request; +import com.upstox.api.PlaceOrderV3Response; +import io.swagger.client.api.InstrumentsApi; +import io.swagger.client.api.OrderApiV3; +import java.util.List; +import java.util.Map; + +public class CallRatioSpread { + public static void main(String[] args) { + // Replace with your access token + ApiClient defaultClient = Configuration.getDefaultApiClient(); + defaultClient.setAccessToken("your_access_token_here"); + + try { + InstrumentsApi instrumentsApi = new InstrumentsApi(); + OrderApiV3 orderApi = new OrderApiV3(); + + // Step 1: Find the ATM call option (long call) + List> longInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "next_week", 0, null, null).getData(); + Map longCall = longInstruments.get(0); + System.out.println("Long call (ATM) - Trading symbol : " + longCall.get("trading_symbol")); + System.out.println("Long call (ATM) - Instrument key : " + longCall.get("instrument_key")); + + // Step 2: Find the ATM+1 call option (short call) + List> shortInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "next_week", 1, null, null).getData(); + Map shortCall = shortInstruments.get(0); + System.out.println("Short call (ATM+1) - Trading symbol : " + shortCall.get("trading_symbol")); + System.out.println("Short call (ATM+1) - Instrument key : " + shortCall.get("instrument_key")); + + // Step 3: Buy 1x the ATM call + PlaceOrderV3Request buyOrder = new PlaceOrderV3Request(); + buyOrder.setInstrumentToken((String) longCall.get("instrument_key")); + buyOrder.setQuantity(((Double) longCall.get("lot_size")).intValue()); + buyOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyOrder.setPrice(0F); + buyOrder.setDisclosedQuantity(0); + buyOrder.setTriggerPrice(0F); + buyOrder.setMarketProtection(-1F); + buyOrder.setIsAmo(false); + + PlaceOrderV3Response buyResult = orderApi.placeOrder(buyOrder); + System.out.println("Buy 1x ATM CE order placed. Order ID: " + buyResult); + + // Step 4: Sell 2x the ATM+1 call + PlaceOrderV3Request sellOrder = new PlaceOrderV3Request(); + sellOrder.setInstrumentToken((String) shortCall.get("instrument_key")); + sellOrder.setQuantity(((Double) shortCall.get("lot_size")).intValue() * 2); + sellOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.SELL); + sellOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + sellOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + sellOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + sellOrder.setPrice(0F); + sellOrder.setDisclosedQuantity(0); + sellOrder.setTriggerPrice(0F); + sellOrder.setMarketProtection(-1F); + sellOrder.setIsAmo(false); + + PlaceOrderV3Response sellResult = orderApi.placeOrder(sellOrder); + System.out.println("Sell 2x ATM+1 CE order placed. Order ID: " + sellResult); + + } catch (ApiException e) { + System.err.println("API error: " + e.getMessage()); + } + } +} diff --git a/examples/strategies/others/code/LongIronButterfly.java b/examples/strategies/others/code/LongIronButterfly.java new file mode 100644 index 0000000..0768ebc --- /dev/null +++ b/examples/strategies/others/code/LongIronButterfly.java @@ -0,0 +1,121 @@ +import com.upstox.ApiClient; +import com.upstox.ApiException; +import com.upstox.Configuration; +import com.upstox.api.PlaceOrderV3Request; +import com.upstox.api.PlaceOrderV3Response; +import io.swagger.client.api.InstrumentsApi; +import io.swagger.client.api.OrderApiV3; +import java.util.List; +import java.util.Map; + +public class LongIronButterfly { + public static void main(String[] args) { + // Replace with your access token + ApiClient defaultClient = Configuration.getDefaultApiClient(); + defaultClient.setAccessToken("your_access_token_here"); + + try { + InstrumentsApi instrumentsApi = new InstrumentsApi(); + OrderApiV3 orderApi = new OrderApiV3(); + + // Step 1: Find the ATM call option (long call) + List> longCallInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "next_week", 0, null, null).getData(); + Map longCall = longCallInstruments.get(0); + System.out.println("Long call (ATM) - Trading symbol : " + longCall.get("trading_symbol")); + System.out.println("Long call (ATM) - Instrument key : " + longCall.get("instrument_key")); + + // Step 2: Find the ATM put option (long put) + List> longPutInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "next_week", 0, null, null).getData(); + Map longPut = longPutInstruments.get(0); + System.out.println("Long put (ATM) - Trading symbol : " + longPut.get("trading_symbol")); + System.out.println("Long put (ATM) - Instrument key : " + longPut.get("instrument_key")); + + // Step 3: Find the ATM+2 call option (short call wing) + List> shortCallInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "next_week", 2, null, null).getData(); + Map shortCall = shortCallInstruments.get(0); + System.out.println("Short call (ATM+2) - Trading symbol : " + shortCall.get("trading_symbol")); + System.out.println("Short call (ATM+2) - Instrument key : " + shortCall.get("instrument_key")); + + // Step 4: Find the ATM-2 put option (short put wing) + List> shortPutInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "next_week", -2, null, null).getData(); + Map shortPut = shortPutInstruments.get(0); + System.out.println("Short put (ATM-2) - Trading symbol : " + shortPut.get("trading_symbol")); + System.out.println("Short put (ATM-2) - Instrument key : " + shortPut.get("instrument_key")); + + // Step 5: Buy the ATM call + PlaceOrderV3Request buyCallOrder = new PlaceOrderV3Request(); + buyCallOrder.setInstrumentToken((String) longCall.get("instrument_key")); + buyCallOrder.setQuantity(((Double) longCall.get("lot_size")).intValue()); + buyCallOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyCallOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyCallOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyCallOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyCallOrder.setPrice(0F); + buyCallOrder.setDisclosedQuantity(0); + buyCallOrder.setTriggerPrice(0F); + buyCallOrder.setMarketProtection(-1F); + buyCallOrder.setIsAmo(false); + + PlaceOrderV3Response buyCallResult = orderApi.placeOrder(buyCallOrder); + System.out.println("Buy ATM CE order placed. Order ID: " + buyCallResult); + + // Step 6: Buy the ATM put + PlaceOrderV3Request buyPutOrder = new PlaceOrderV3Request(); + buyPutOrder.setInstrumentToken((String) longPut.get("instrument_key")); + buyPutOrder.setQuantity(((Double) longPut.get("lot_size")).intValue()); + buyPutOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyPutOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyPutOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyPutOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyPutOrder.setPrice(0F); + buyPutOrder.setDisclosedQuantity(0); + buyPutOrder.setTriggerPrice(0F); + buyPutOrder.setMarketProtection(-1F); + buyPutOrder.setIsAmo(false); + + PlaceOrderV3Response buyPutResult = orderApi.placeOrder(buyPutOrder); + System.out.println("Buy ATM PE order placed. Order ID: " + buyPutResult); + + // Step 7: Sell the ATM+2 call (wing) + PlaceOrderV3Request sellCallOrder = new PlaceOrderV3Request(); + sellCallOrder.setInstrumentToken((String) shortCall.get("instrument_key")); + sellCallOrder.setQuantity(((Double) shortCall.get("lot_size")).intValue()); + sellCallOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.SELL); + sellCallOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + sellCallOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + sellCallOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + sellCallOrder.setPrice(0F); + sellCallOrder.setDisclosedQuantity(0); + sellCallOrder.setTriggerPrice(0F); + sellCallOrder.setMarketProtection(-1F); + sellCallOrder.setIsAmo(false); + + PlaceOrderV3Response sellCallResult = orderApi.placeOrder(sellCallOrder); + System.out.println("Sell ATM+2 CE order placed. Order ID: " + sellCallResult); + + // Step 8: Sell the ATM-2 put (wing) + PlaceOrderV3Request sellPutOrder = new PlaceOrderV3Request(); + sellPutOrder.setInstrumentToken((String) shortPut.get("instrument_key")); + sellPutOrder.setQuantity(((Double) shortPut.get("lot_size")).intValue()); + sellPutOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.SELL); + sellPutOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + sellPutOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + sellPutOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + sellPutOrder.setPrice(0F); + sellPutOrder.setDisclosedQuantity(0); + sellPutOrder.setTriggerPrice(0F); + sellPutOrder.setMarketProtection(-1F); + sellPutOrder.setIsAmo(false); + + PlaceOrderV3Response sellPutResult = orderApi.placeOrder(sellPutOrder); + System.out.println("Sell ATM-2 PE order placed. Order ID: " + sellPutResult); + + } catch (ApiException e) { + System.err.println("API error: " + e.getMessage()); + } + } +} diff --git a/examples/strategies/others/code/LongIronCondor.java b/examples/strategies/others/code/LongIronCondor.java new file mode 100644 index 0000000..870ff19 --- /dev/null +++ b/examples/strategies/others/code/LongIronCondor.java @@ -0,0 +1,121 @@ +import com.upstox.ApiClient; +import com.upstox.ApiException; +import com.upstox.Configuration; +import com.upstox.api.PlaceOrderV3Request; +import com.upstox.api.PlaceOrderV3Response; +import io.swagger.client.api.InstrumentsApi; +import io.swagger.client.api.OrderApiV3; +import java.util.List; +import java.util.Map; + +public class LongIronCondor { + public static void main(String[] args) { + // Replace with your access token + ApiClient defaultClient = Configuration.getDefaultApiClient(); + defaultClient.setAccessToken("your_access_token_here"); + + try { + InstrumentsApi instrumentsApi = new InstrumentsApi(); + OrderApiV3 orderApi = new OrderApiV3(); + + // Step 1: Find the ATM+1 call option (long call) + List> longCallInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "next_week", 1, null, null).getData(); + Map longCall = longCallInstruments.get(0); + System.out.println("Long call (ATM+1) - Trading symbol : " + longCall.get("trading_symbol")); + System.out.println("Long call (ATM+1) - Instrument key : " + longCall.get("instrument_key")); + + // Step 2: Find the ATM-1 put option (long put) + List> longPutInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "next_week", -1, null, null).getData(); + Map longPut = longPutInstruments.get(0); + System.out.println("Long put (ATM-1) - Trading symbol : " + longPut.get("trading_symbol")); + System.out.println("Long put (ATM-1) - Instrument key : " + longPut.get("instrument_key")); + + // Step 3: Find the ATM+2 call option (short call wing) + List> shortCallInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "next_week", 2, null, null).getData(); + Map shortCall = shortCallInstruments.get(0); + System.out.println("Short call (ATM+2) - Trading symbol : " + shortCall.get("trading_symbol")); + System.out.println("Short call (ATM+2) - Instrument key : " + shortCall.get("instrument_key")); + + // Step 4: Find the ATM-2 put option (short put wing) + List> shortPutInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "next_week", -2, null, null).getData(); + Map shortPut = shortPutInstruments.get(0); + System.out.println("Short put (ATM-2) - Trading symbol : " + shortPut.get("trading_symbol")); + System.out.println("Short put (ATM-2) - Instrument key : " + shortPut.get("instrument_key")); + + // Step 5: Buy the ATM+1 call + PlaceOrderV3Request buyCallOrder = new PlaceOrderV3Request(); + buyCallOrder.setInstrumentToken((String) longCall.get("instrument_key")); + buyCallOrder.setQuantity(((Double) longCall.get("lot_size")).intValue()); + buyCallOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyCallOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyCallOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyCallOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyCallOrder.setPrice(0F); + buyCallOrder.setDisclosedQuantity(0); + buyCallOrder.setTriggerPrice(0F); + buyCallOrder.setMarketProtection(-1F); + buyCallOrder.setIsAmo(false); + + PlaceOrderV3Response buyCallResult = orderApi.placeOrder(buyCallOrder); + System.out.println("Buy ATM+1 CE order placed. Order ID: " + buyCallResult); + + // Step 6: Buy the ATM-1 put + PlaceOrderV3Request buyPutOrder = new PlaceOrderV3Request(); + buyPutOrder.setInstrumentToken((String) longPut.get("instrument_key")); + buyPutOrder.setQuantity(((Double) longPut.get("lot_size")).intValue()); + buyPutOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyPutOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyPutOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyPutOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyPutOrder.setPrice(0F); + buyPutOrder.setDisclosedQuantity(0); + buyPutOrder.setTriggerPrice(0F); + buyPutOrder.setMarketProtection(-1F); + buyPutOrder.setIsAmo(false); + + PlaceOrderV3Response buyPutResult = orderApi.placeOrder(buyPutOrder); + System.out.println("Buy ATM-1 PE order placed. Order ID: " + buyPutResult); + + // Step 7: Sell the ATM+2 call (wing) + PlaceOrderV3Request sellCallOrder = new PlaceOrderV3Request(); + sellCallOrder.setInstrumentToken((String) shortCall.get("instrument_key")); + sellCallOrder.setQuantity(((Double) shortCall.get("lot_size")).intValue()); + sellCallOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.SELL); + sellCallOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + sellCallOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + sellCallOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + sellCallOrder.setPrice(0F); + sellCallOrder.setDisclosedQuantity(0); + sellCallOrder.setTriggerPrice(0F); + sellCallOrder.setMarketProtection(-1F); + sellCallOrder.setIsAmo(false); + + PlaceOrderV3Response sellCallResult = orderApi.placeOrder(sellCallOrder); + System.out.println("Sell ATM+2 CE order placed. Order ID: " + sellCallResult); + + // Step 8: Sell the ATM-2 put (wing) + PlaceOrderV3Request sellPutOrder = new PlaceOrderV3Request(); + sellPutOrder.setInstrumentToken((String) shortPut.get("instrument_key")); + sellPutOrder.setQuantity(((Double) shortPut.get("lot_size")).intValue()); + sellPutOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.SELL); + sellPutOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + sellPutOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + sellPutOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + sellPutOrder.setPrice(0F); + sellPutOrder.setDisclosedQuantity(0); + sellPutOrder.setTriggerPrice(0F); + sellPutOrder.setMarketProtection(-1F); + sellPutOrder.setIsAmo(false); + + PlaceOrderV3Response sellPutResult = orderApi.placeOrder(sellPutOrder); + System.out.println("Sell ATM-2 PE order placed. Order ID: " + sellPutResult); + + } catch (ApiException e) { + System.err.println("API error: " + e.getMessage()); + } + } +} diff --git a/examples/strategies/others/code/LongStraddle.java b/examples/strategies/others/code/LongStraddle.java new file mode 100644 index 0000000..c37e6d2 --- /dev/null +++ b/examples/strategies/others/code/LongStraddle.java @@ -0,0 +1,73 @@ +import com.upstox.ApiClient; +import com.upstox.ApiException; +import com.upstox.Configuration; +import com.upstox.api.PlaceOrderV3Request; +import com.upstox.api.PlaceOrderV3Response; +import io.swagger.client.api.InstrumentsApi; +import io.swagger.client.api.OrderApiV3; +import java.util.List; +import java.util.Map; + +public class LongStraddle { + public static void main(String[] args) { + // Replace with your access token + ApiClient defaultClient = Configuration.getDefaultApiClient(); + defaultClient.setAccessToken("your_access_token_here"); + + try { + InstrumentsApi instrumentsApi = new InstrumentsApi(); + OrderApiV3 orderApi = new OrderApiV3(); + + // Step 1: Find the ATM call option (long call) + List> callInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "next_week", 0, null, null).getData(); + Map longCall = callInstruments.get(0); + System.out.println("Long call - Trading symbol : " + longCall.get("trading_symbol")); + System.out.println("Long call - Instrument key : " + longCall.get("instrument_key")); + + // Step 2: Find the ATM put option (long put) + List> putInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "next_week", 0, null, null).getData(); + Map longPut = putInstruments.get(0); + System.out.println("Long put - Trading symbol : " + longPut.get("trading_symbol")); + System.out.println("Long put - Instrument key : " + longPut.get("instrument_key")); + + // Step 3: Buy the ATM call + PlaceOrderV3Request buyCallOrder = new PlaceOrderV3Request(); + buyCallOrder.setInstrumentToken((String) longCall.get("instrument_key")); + buyCallOrder.setQuantity(((Double) longCall.get("lot_size")).intValue()); + buyCallOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyCallOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyCallOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyCallOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyCallOrder.setPrice(0F); + buyCallOrder.setDisclosedQuantity(0); + buyCallOrder.setTriggerPrice(0F); + buyCallOrder.setMarketProtection(-1F); + buyCallOrder.setIsAmo(false); + + PlaceOrderV3Response buyCallResult = orderApi.placeOrder(buyCallOrder); + System.out.println("Buy ATM CE order placed. Order ID: " + buyCallResult); + + // Step 4: Buy the ATM put + PlaceOrderV3Request buyPutOrder = new PlaceOrderV3Request(); + buyPutOrder.setInstrumentToken((String) longPut.get("instrument_key")); + buyPutOrder.setQuantity(((Double) longPut.get("lot_size")).intValue()); + buyPutOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyPutOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyPutOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyPutOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyPutOrder.setPrice(0F); + buyPutOrder.setDisclosedQuantity(0); + buyPutOrder.setTriggerPrice(0F); + buyPutOrder.setMarketProtection(-1F); + buyPutOrder.setIsAmo(false); + + PlaceOrderV3Response buyPutResult = orderApi.placeOrder(buyPutOrder); + System.out.println("Buy ATM PE order placed. Order ID: " + buyPutResult); + + } catch (ApiException e) { + System.err.println("API error: " + e.getMessage()); + } + } +} diff --git a/examples/strategies/others/code/LongStrangle.java b/examples/strategies/others/code/LongStrangle.java new file mode 100644 index 0000000..ddbd797 --- /dev/null +++ b/examples/strategies/others/code/LongStrangle.java @@ -0,0 +1,73 @@ +import com.upstox.ApiClient; +import com.upstox.ApiException; +import com.upstox.Configuration; +import com.upstox.api.PlaceOrderV3Request; +import com.upstox.api.PlaceOrderV3Response; +import io.swagger.client.api.InstrumentsApi; +import io.swagger.client.api.OrderApiV3; +import java.util.List; +import java.util.Map; + +public class LongStrangle { + public static void main(String[] args) { + // Replace with your access token + ApiClient defaultClient = Configuration.getDefaultApiClient(); + defaultClient.setAccessToken("your_access_token_here"); + + try { + InstrumentsApi instrumentsApi = new InstrumentsApi(); + OrderApiV3 orderApi = new OrderApiV3(); + + // Step 1: Find the ATM+1 call option (long call) + List> callInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "next_week", 1, null, null).getData(); + Map longCall = callInstruments.get(0); + System.out.println("Long call (ATM+1) - Trading symbol : " + longCall.get("trading_symbol")); + System.out.println("Long call (ATM+1) - Instrument key : " + longCall.get("instrument_key")); + + // Step 2: Find the ATM-1 put option (long put) + List> putInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "next_week", -1, null, null).getData(); + Map longPut = putInstruments.get(0); + System.out.println("Long put (ATM-1) - Trading symbol : " + longPut.get("trading_symbol")); + System.out.println("Long put (ATM-1) - Instrument key : " + longPut.get("instrument_key")); + + // Step 3: Buy the ATM+1 call + PlaceOrderV3Request buyCallOrder = new PlaceOrderV3Request(); + buyCallOrder.setInstrumentToken((String) longCall.get("instrument_key")); + buyCallOrder.setQuantity(((Double) longCall.get("lot_size")).intValue()); + buyCallOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyCallOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyCallOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyCallOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyCallOrder.setPrice(0F); + buyCallOrder.setDisclosedQuantity(0); + buyCallOrder.setTriggerPrice(0F); + buyCallOrder.setMarketProtection(-1F); + buyCallOrder.setIsAmo(false); + + PlaceOrderV3Response buyCallResult = orderApi.placeOrder(buyCallOrder); + System.out.println("Buy ATM+1 CE order placed. Order ID: " + buyCallResult); + + // Step 4: Buy the ATM-1 put + PlaceOrderV3Request buyPutOrder = new PlaceOrderV3Request(); + buyPutOrder.setInstrumentToken((String) longPut.get("instrument_key")); + buyPutOrder.setQuantity(((Double) longPut.get("lot_size")).intValue()); + buyPutOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyPutOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyPutOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyPutOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyPutOrder.setPrice(0F); + buyPutOrder.setDisclosedQuantity(0); + buyPutOrder.setTriggerPrice(0F); + buyPutOrder.setMarketProtection(-1F); + buyPutOrder.setIsAmo(false); + + PlaceOrderV3Response buyPutResult = orderApi.placeOrder(buyPutOrder); + System.out.println("Buy ATM-1 PE order placed. Order ID: " + buyPutResult); + + } catch (ApiException e) { + System.err.println("API error: " + e.getMessage()); + } + } +} diff --git a/examples/strategies/others/code/PutRatioSpread.java b/examples/strategies/others/code/PutRatioSpread.java new file mode 100644 index 0000000..d6a7aad --- /dev/null +++ b/examples/strategies/others/code/PutRatioSpread.java @@ -0,0 +1,73 @@ +import com.upstox.ApiClient; +import com.upstox.ApiException; +import com.upstox.Configuration; +import com.upstox.api.PlaceOrderV3Request; +import com.upstox.api.PlaceOrderV3Response; +import io.swagger.client.api.InstrumentsApi; +import io.swagger.client.api.OrderApiV3; +import java.util.List; +import java.util.Map; + +public class PutRatioSpread { + public static void main(String[] args) { + // Replace with your access token + ApiClient defaultClient = Configuration.getDefaultApiClient(); + defaultClient.setAccessToken("your_access_token_here"); + + try { + InstrumentsApi instrumentsApi = new InstrumentsApi(); + OrderApiV3 orderApi = new OrderApiV3(); + + // Step 1: Find the ATM put option (long put) + List> longInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "next_week", 0, null, null).getData(); + Map longPut = longInstruments.get(0); + System.out.println("Long put (ATM) - Trading symbol : " + longPut.get("trading_symbol")); + System.out.println("Long put (ATM) - Instrument key : " + longPut.get("instrument_key")); + + // Step 2: Find the ATM-1 put option (short put) + List> shortInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "next_week", -1, null, null).getData(); + Map shortPut = shortInstruments.get(0); + System.out.println("Short put (ATM-1) - Trading symbol : " + shortPut.get("trading_symbol")); + System.out.println("Short put (ATM-1) - Instrument key : " + shortPut.get("instrument_key")); + + // Step 3: Buy 1x the ATM put + PlaceOrderV3Request buyOrder = new PlaceOrderV3Request(); + buyOrder.setInstrumentToken((String) longPut.get("instrument_key")); + buyOrder.setQuantity(((Double) longPut.get("lot_size")).intValue()); + buyOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyOrder.setPrice(0F); + buyOrder.setDisclosedQuantity(0); + buyOrder.setTriggerPrice(0F); + buyOrder.setMarketProtection(-1F); + buyOrder.setIsAmo(false); + + PlaceOrderV3Response buyResult = orderApi.placeOrder(buyOrder); + System.out.println("Buy 1x ATM PE order placed. Order ID: " + buyResult); + + // Step 4: Sell 2x the ATM-1 put + PlaceOrderV3Request sellOrder = new PlaceOrderV3Request(); + sellOrder.setInstrumentToken((String) shortPut.get("instrument_key")); + sellOrder.setQuantity(((Double) shortPut.get("lot_size")).intValue() * 2); + sellOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.SELL); + sellOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + sellOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + sellOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + sellOrder.setPrice(0F); + sellOrder.setDisclosedQuantity(0); + sellOrder.setTriggerPrice(0F); + sellOrder.setMarketProtection(-1F); + sellOrder.setIsAmo(false); + + PlaceOrderV3Response sellResult = orderApi.placeOrder(sellOrder); + System.out.println("Sell 2x ATM-1 PE order placed. Order ID: " + sellResult); + + } catch (ApiException e) { + System.err.println("API error: " + e.getMessage()); + } + } +} diff --git a/examples/strategies/others/code/Strap.java b/examples/strategies/others/code/Strap.java new file mode 100644 index 0000000..115813e --- /dev/null +++ b/examples/strategies/others/code/Strap.java @@ -0,0 +1,73 @@ +import com.upstox.ApiClient; +import com.upstox.ApiException; +import com.upstox.Configuration; +import com.upstox.api.PlaceOrderV3Request; +import com.upstox.api.PlaceOrderV3Response; +import io.swagger.client.api.InstrumentsApi; +import io.swagger.client.api.OrderApiV3; +import java.util.List; +import java.util.Map; + +public class Strap { + public static void main(String[] args) { + // Replace with your access token + ApiClient defaultClient = Configuration.getDefaultApiClient(); + defaultClient.setAccessToken("your_access_token_here"); + + try { + InstrumentsApi instrumentsApi = new InstrumentsApi(); + OrderApiV3 orderApi = new OrderApiV3(); + + // Step 1: Find the ATM call option (long call x2) + List> callInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "next_week", 0, null, null).getData(); + Map longCall = callInstruments.get(0); + System.out.println("Long call - Trading symbol : " + longCall.get("trading_symbol")); + System.out.println("Long call - Instrument key : " + longCall.get("instrument_key")); + + // Step 2: Find the ATM put option (long put) + List> putInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "next_week", 0, null, null).getData(); + Map longPut = putInstruments.get(0); + System.out.println("Long put - Trading symbol : " + longPut.get("trading_symbol")); + System.out.println("Long put - Instrument key : " + longPut.get("instrument_key")); + + // Step 3: Buy 2x the ATM call + PlaceOrderV3Request buyCallOrder = new PlaceOrderV3Request(); + buyCallOrder.setInstrumentToken((String) longCall.get("instrument_key")); + buyCallOrder.setQuantity(((Double) longCall.get("lot_size")).intValue() * 2); + buyCallOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyCallOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyCallOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyCallOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyCallOrder.setPrice(0F); + buyCallOrder.setDisclosedQuantity(0); + buyCallOrder.setTriggerPrice(0F); + buyCallOrder.setMarketProtection(-1F); + buyCallOrder.setIsAmo(false); + + PlaceOrderV3Response buyCallResult = orderApi.placeOrder(buyCallOrder); + System.out.println("Buy 2x ATM CE order placed. Order ID: " + buyCallResult); + + // Step 4: Buy 1x the ATM put + PlaceOrderV3Request buyPutOrder = new PlaceOrderV3Request(); + buyPutOrder.setInstrumentToken((String) longPut.get("instrument_key")); + buyPutOrder.setQuantity(((Double) longPut.get("lot_size")).intValue()); + buyPutOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyPutOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyPutOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyPutOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyPutOrder.setPrice(0F); + buyPutOrder.setDisclosedQuantity(0); + buyPutOrder.setTriggerPrice(0F); + buyPutOrder.setMarketProtection(-1F); + buyPutOrder.setIsAmo(false); + + PlaceOrderV3Response buyPutResult = orderApi.placeOrder(buyPutOrder); + System.out.println("Buy 1x ATM PE order placed. Order ID: " + buyPutResult); + + } catch (ApiException e) { + System.err.println("API error: " + e.getMessage()); + } + } +} diff --git a/examples/strategies/others/code/Strip.java b/examples/strategies/others/code/Strip.java new file mode 100644 index 0000000..d607831 --- /dev/null +++ b/examples/strategies/others/code/Strip.java @@ -0,0 +1,73 @@ +import com.upstox.ApiClient; +import com.upstox.ApiException; +import com.upstox.Configuration; +import com.upstox.api.PlaceOrderV3Request; +import com.upstox.api.PlaceOrderV3Response; +import io.swagger.client.api.InstrumentsApi; +import io.swagger.client.api.OrderApiV3; +import java.util.List; +import java.util.Map; + +public class Strip { + public static void main(String[] args) { + // Replace with your access token + ApiClient defaultClient = Configuration.getDefaultApiClient(); + defaultClient.setAccessToken("your_access_token_here"); + + try { + InstrumentsApi instrumentsApi = new InstrumentsApi(); + OrderApiV3 orderApi = new OrderApiV3(); + + // Step 1: Find the ATM call option (long call) + List> callInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "next_week", 0, null, null).getData(); + Map longCall = callInstruments.get(0); + System.out.println("Long call - Trading symbol : " + longCall.get("trading_symbol")); + System.out.println("Long call - Instrument key : " + longCall.get("instrument_key")); + + // Step 2: Find the ATM put option (long put x2) + List> putInstruments = (List>) + instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "next_week", 0, null, null).getData(); + Map longPut = putInstruments.get(0); + System.out.println("Long put - Trading symbol : " + longPut.get("trading_symbol")); + System.out.println("Long put - Instrument key : " + longPut.get("instrument_key")); + + // Step 3: Buy 1x the ATM call + PlaceOrderV3Request buyCallOrder = new PlaceOrderV3Request(); + buyCallOrder.setInstrumentToken((String) longCall.get("instrument_key")); + buyCallOrder.setQuantity(((Double) longCall.get("lot_size")).intValue()); + buyCallOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyCallOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyCallOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyCallOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyCallOrder.setPrice(0F); + buyCallOrder.setDisclosedQuantity(0); + buyCallOrder.setTriggerPrice(0F); + buyCallOrder.setMarketProtection(-1F); + buyCallOrder.setIsAmo(false); + + PlaceOrderV3Response buyCallResult = orderApi.placeOrder(buyCallOrder); + System.out.println("Buy 1x ATM CE order placed. Order ID: " + buyCallResult); + + // Step 4: Buy 2x the ATM put + PlaceOrderV3Request buyPutOrder = new PlaceOrderV3Request(); + buyPutOrder.setInstrumentToken((String) longPut.get("instrument_key")); + buyPutOrder.setQuantity(((Double) longPut.get("lot_size")).intValue() * 2); + buyPutOrder.setTransactionType(PlaceOrderV3Request.TransactionTypeEnum.BUY); + buyPutOrder.setOrderType(PlaceOrderV3Request.OrderTypeEnum.MARKET); + buyPutOrder.setProduct(PlaceOrderV3Request.ProductEnum.D); + buyPutOrder.setValidity(PlaceOrderV3Request.ValidityEnum.DAY); + buyPutOrder.setPrice(0F); + buyPutOrder.setDisclosedQuantity(0); + buyPutOrder.setTriggerPrice(0F); + buyPutOrder.setMarketProtection(-1F); + buyPutOrder.setIsAmo(false); + + PlaceOrderV3Response buyPutResult = orderApi.placeOrder(buyPutOrder); + System.out.println("Buy 2x ATM PE order placed. Order ID: " + buyPutResult); + + } catch (ApiException e) { + System.err.println("API error: " + e.getMessage()); + } + } +}