optionchain_simulator 0.0.3

OptionChain-Simulator is a lightweight REST API service that simulates an evolving option chain with every request. It is designed for developers building or testing trading systems, backtesters, and visual tools that depend on option data streams but want to avoid relying on live data feeds.
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
# OptionChain-Simulator API Specification

This document outlines the API endpoints, request/response formats, and expected behaviors for the OptionChain-Simulator service.

## API Overview

The OptionChain-Simulator provides a RESTful API for interacting with option chain simulations and historical data. The API adheres to REST principles and uses standard HTTP methods to perform operations on resources.

## Base URL

All API endpoints are relative to the base URL:

```
https://api.optionchainsimulator.example/v1
```

## Authentication

Authentication is handled via API keys provided in the `X-API-Key` header. For future implementations, OAuth2 authentication will be supported.

```http
GET /chain/simulated/123e4567-e89b-12d3-a456-426614174000 HTTP/1.1
Host: api.optionchainsimulator.example
X-API-Key: your_api_key_here
```

## API Versioning

The API is versioned in the URL path. The current version is `v1`.

## Resource Hierarchy

```mermaid
graph TD
    A[API Root] --> B[Chain]
    A --> C[Config]
    B --> D[Simulated]
    B --> E[Historical]
    D --> F[Session ID]
    E --> G[Asset]
    G --> H[Date]
    C --> I[Simulator]
```

## Endpoints

### Simulation Endpoints

#### Create Simulation Session

Creates a new simulation session with specified parameters.

```
POST /chain/simulated
```

**Request Body**:

```json
{
  "initialPrice": 100.0,
  "volatility": 0.2,
  "riskFreeRate": 0.03,
  "strikes": [90, 95, 100, 105, 110],
  "expirations": ["2023-06-30", "2023-09-30"],
  "method": "GeometricBrownian"
}
```

**Response**:

```json
{
  "id": "123e4567-e89b-12d3-a456-426614174000",
  "createdAt": "2023-04-15T14:30:00Z",
  "parameters": {
    "initialPrice": 100.0,
    "volatility": 0.2,
    "riskFreeRate": 0.03,
    "strikes": [90, 95, 100, 105, 110],
    "expirations": ["2023-06-30", "2023-09-30"],
    "method": "GeometricBrownian"
  },
  "currentStep": 0,
  "totalSteps": 20,
  "state": "Initialized"
}
```

**Status Codes**:
- `201 Created`: Session successfully created
- `400 Bad Request`: Invalid parameters
- `429 Too Many Requests`: Rate limit exceeded

#### Get Next Simulation Step

Advances the simulation by one step and returns the updated option chain.

```
GET /chain/simulated/{session_id}
```

**Response**:

```json
{
  "underlying": "SIMULATION",
  "timestamp": "2023-04-15T14:35:00Z",
  "price": 101.23,
  "contracts": [
    {
      "type": "Call",
      "strike": 100.0,
      "expiration": "2023-06-30",
      "price": 5.67,
      "delta": 0.58,
      "gamma": 0.04,
      "theta": -0.03,
      "vega": 0.25
    },
    {
      "type": "Put",
      "strike": 100.0,
      "expiration": "2023-06-30",
      "price": 4.32,
      "delta": -0.42,
      "gamma": 0.04,
      "theta": -0.02,
      "vega": 0.25
    }
    // Additional contracts...
  ],
  "sessionInfo": {
    "id": "123e4567-e89b-12d3-a456-426614174000",
    "currentStep": 1,
    "totalSteps": 20
  }
}
```

**Status Codes**:
- `200 OK`: Step successfully retrieved
- `404 Not Found`: Session not found
- `410 Gone`: Session expired or terminated

#### Update Simulation Parameters

Completely replaces the simulation parameters, potentially resetting the simulation.

```
PUT /chain/simulated/{session_id}
```

**Request Body**:

```json
{
  "initialPrice": 105.0,
  "volatility": 0.25,
  "riskFreeRate": 0.035,
  "strikes": [95, 100, 105, 110, 115],
  "expirations": ["2023-06-30", "2023-09-30"],
  "method": "GeometricBrownian"
}
```

**Response**:

```json
{
  "id": "123e4567-e89b-12d3-a456-426614174000",
  "updatedAt": "2023-04-15T14:40:00Z",
  "parameters": {
    "initialPrice": 105.0,
    "volatility": 0.25,
    "riskFreeRate": 0.035,
    "strikes": [95, 100, 105, 110, 115],
    "expirations": ["2023-06-30", "2023-09-30"],
    "method": "GeometricBrownian"
  },
  "currentStep": 0,
  "totalSteps": 20,
  "state": "Reinitialized"
}
```

**Status Codes**:
- `200 OK`: Parameters successfully updated
- `400 Bad Request`: Invalid parameters
- `404 Not Found`: Session not found

#### Modify Simulation Parameters

Updates specific simulation parameters without replacing the entire configuration.

```
PATCH /chain/simulated/{session_id}
```

**Request Body**:

```json
{
  "volatility": 0.3
}
```

**Response**:

```json
{
  "id": "123e4567-e89b-12d3-a456-426614174000",
  "updatedAt": "2023-04-15T14:45:00Z",
  "parameters": {
    "initialPrice": 100.0,
    "volatility": 0.3,
    "riskFreeRate": 0.03,
    "strikes": [90, 95, 100, 105, 110],
    "expirations": ["2023-06-30", "2023-09-30"],
    "method": "GeometricBrownian"
  },
  "currentStep": 5,
  "totalSteps": 20,
  "state": "Modified"
}
```

**Status Codes**:
- `200 OK`: Parameters successfully modified
- `400 Bad Request`: Invalid parameters
- `404 Not Found`: Session not found

#### Delete Simulation Session

Terminates and removes a simulation session.

```
DELETE /chain/simulated/{session_id}
```

**Response**:

```json
{
  "message": "Session successfully terminated",
  "id": "123e4567-e89b-12d3-a456-426614174000"
}
```

**Status Codes**:
- `200 OK`: Session successfully terminated
- `404 Not Found`: Session not found

### Historical Data Endpoints

#### Get Historical Option Chain

Retrieves a reconstructed option chain for a specified asset on a particular date.

```
GET /chain/historical/{asset}/{date}
```

**Parameters**:
- `asset`: Asset identifier (e.g., "GOLD", "OIL")
- `date`: Date in ISO 8601 format (YYYY-MM-DD)

**Response**:

```json
{
  "underlying": "GOLD",
  "timestamp": "2022-06-15T00:00:00Z",
  "price": 1825.40,
  "contracts": [
    {
      "type": "Call",
      "strike": 1800.0,
      "expiration": "2022-07-30",
      "price": 65.23,
      "impliedVolatility": 0.18
    },
    {
      "type": "Put",
      "strike": 1800.0,
      "expiration": "2022-07-30",
      "price": 39.85,
      "impliedVolatility": 0.20
    }
    // Additional contracts...
  ]
}
```

**Status Codes**:
- `200 OK`: Data successfully retrieved
- `404 Not Found`: Asset or date not found in database
- `400 Bad Request`: Invalid asset or date format

### Configuration Endpoints

#### Get Simulator Configuration

Retrieves the current global configuration for the simulator.

```
GET /config/simulator
```

**Response**:

```json
{
  "defaultParameters": {
    "initialPrice": 100.0,
    "volatility": 0.2,
    "riskFreeRate": 0.03,
    "strikesCount": 5,
    "strikesSpread": 0.1,
    "expirations": ["30d", "90d"],
    "method": "GeometricBrownian"
  },
  "limits": {
    "maxSessions": 10,
    "sessionTTL": 1800,
    "maxSteps": 100,
    "maxStrikes": 20,
    "maxExpirations": 5
  }
}
```

**Status Codes**:
- `200 OK`: Configuration successfully retrieved

#### Update Simulator Configuration (Admin Only)

Updates the global configuration for the simulator.

```
PUT /config/simulator
```

**Request Body**:

```json
{
  "defaultParameters": {
    "initialPrice": 100.0,
    "volatility": 0.25,
    "riskFreeRate": 0.035,
    "strikesCount": 7,
    "strikesSpread": 0.15,
    "expirations": ["30d", "60d", "90d"],
    "method": "GeometricBrownian"
  },
  "limits": {
    "maxSessions": 20,
    "sessionTTL": 3600,
    "maxSteps": 200,
    "maxStrikes": 30,
    "maxExpirations": 10
  }
}
```

**Response**:

```json
{
  "message": "Configuration successfully updated",
  "updatedAt": "2023-04-15T15:00:00Z"
}
```

**Status Codes**:
- `200 OK`: Configuration successfully updated
- `400 Bad Request`: Invalid configuration
- `403 Forbidden`: Insufficient permissions

## HTTP Status Codes

The API uses standard HTTP status codes to indicate the success or failure of requests:

| Code | Description |
|------|-------------|
| 200 | OK - The request was successful |
| 201 | Created - A new resource was successfully created |
| 400 | Bad Request - The request was invalid or cannot be served |
| 401 | Unauthorized - Authentication is required and has failed |
| 403 | Forbidden - The request is not allowed |
| 404 | Not Found - The requested resource does not exist |
| 409 | Conflict - The request could not be completed due to a conflict |
| 410 | Gone - The resource requested is no longer available |
| 429 | Too Many Requests - Rate limit has been exceeded |
| 500 | Internal Server Error - An error occurred on the server |

## Request/Response Flow

```mermaid
sequenceDiagram
    participant Client
    participant API as REST API
    participant SessionMgr as Session Manager
    participant Simulator as Simulator Service
    
    Client->>API: POST /chain/simulated
    API->>SessionMgr: Create session
    SessionMgr->>Simulator: Initialize simulation
    Simulator-->>SessionMgr: Initial state
    SessionMgr-->>API: Session created
    API-->>Client: 201 Created (session details)
    
    Client->>API: GET /chain/simulated/{id}
    API->>SessionMgr: Get next step
    SessionMgr->>Simulator: Advance simulation
    Simulator-->>SessionMgr: Updated chain
    SessionMgr-->>API: Chain data
    API-->>Client: 200 OK (chain data)
    
    Client->>API: PATCH /chain/simulated/{id}
    API->>SessionMgr: Update parameter
    SessionMgr->>Simulator: Recalculate
    Simulator-->>SessionMgr: Updated state
    SessionMgr-->>API: Session updated
    API-->>Client: 200 OK (updated session)
    
    Client->>API: DELETE /chain/simulated/{id}
    API->>SessionMgr: Terminate session
    SessionMgr-->>API: Session terminated
    API-->>Client: 200 OK (confirmation)
```

## Data Models

### Option Chain

```json
{
  "underlying": "string",       // Underlying asset identifier
  "timestamp": "ISO-8601 date", // Time of the chain data
  "price": "number",            // Current price of the underlying
  "contracts": [                // Array of option contracts
    {
      "type": "Call|Put",       // Option type
      "strike": "number",       // Strike price
      "expiration": "ISO-8601 date", // Expiration date
      "price": "number",        // Option price
      "delta": "number",        // Optional: Delta greek
      "gamma": "number",        // Optional: Gamma greek
      "theta": "number",        // Optional: Theta greek
      "vega": "number",         // Optional: Vega greek
      "impliedVolatility": "number" // Optional: IV
    }
  ],
  "sessionInfo": {              // Optional: Included for simulation responses
    "id": "UUID",               // Session identifier
    "currentStep": "number",    // Current simulation step
    "totalSteps": "number"      // Total steps in simulation
  }
}
```

### Simulation Parameters

```json
{
  "initialPrice": "number",     // Starting price of the underlying
  "volatility": "number",       // Volatility parameter (0.0-1.0)
  "riskFreeRate": "number",     // Risk-free interest rate
  "strikes": ["number"],        // Array of strike prices
  "expirations": ["ISO-8601 date"], // Array of expiration dates
  "method": "string"            // Pricing model to use
}
```

### Session

```json
{
  "id": "UUID",                 // Unique session identifier
  "createdAt": "ISO-8601 date", // Creation timestamp
  "updatedAt": "ISO-8601 date", // Last update timestamp
  "parameters": {               // Simulation parameters
    // See Simulation Parameters model
  },
  "currentStep": "number",      // Current step in the simulation
  "totalSteps": "number",       // Total steps planned
  "state": "string"             // Session state
}
```

## Rate Limiting

The API implements rate limiting to prevent abuse. Limits are applied per API key:

- 100 requests per minute for standard tier
- 1000 requests per minute for premium tier

Rate limit information is included in response headers:

```
X-RateLimit-Limit: 100
X-RateLimit-Remaining: 95
X-RateLimit-Reset: 1618505400
```

When the rate limit is exceeded, a 429 Too Many Requests response is returned.

## Pagination

For endpoints that return collections, pagination is supported using the following query parameters:

- `page`: Page number (1-based)
- `limit`: Number of items per page (default: 20, max: 100)

Response includes pagination metadata:

```json
{
  "data": [
    // Collection items
  ],
  "pagination": {
    "total": 42,
    "page": 1,
    "limit": 20,
    "pages": 3
  }
}
```

## Error Handling

Error responses follow a consistent format:

```json
{
  "error": {
    "code": "string",    
    "message": "string", 
    "details": {}        
  }
}
```

Example error response:

```json
{
  "error": {
    "code": "invalid_parameters",
    "message": "One or more parameters are invalid",
    "details": {
      "volatility": "Must be between 0.0 and 1.0"
    }
  }
}
```

## Future API Extensions

In future versions, the API will support:

1. WebSocket connections for real-time updates
2. GraphQL endpoint for more flexible queries
3. Batch operations for bulk processing
4. Export functionality to various formats (CSV, Excel)

## API Client Libraries

Official client libraries will be provided for:

- Python
- Rust
- Go
- C++


## Developer Tools

A Swagger/OpenAPI specification is available at:

```
https://api.optionchainsimulator.example/docs/openapi.json
```

Interactive API documentation is available at:

```
https://api.optionchainsimulator.example/docs
```