Repository navigation
Expand file tree
/
Copy pathRangeForward.java
More file actions
73 lines (64 loc) · 3.73 KB
/
Copy pathRangeForward.java
File metadata and controls
73 lines (64 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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<Map<String, Object>> putInstruments = (List<Map<String, Object>>)
instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "PE", "next_week", -1, null, null).getData();
Map<String, Object> 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<Map<String, Object>> callInstruments = (List<Map<String, Object>>)
instrumentsApi.searchInstrument("Nifty 50", "NSE", "FO", "CE", "next_week", 1, null, null).getData();
Map<String, Object> 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());
}
}
}