forked from piraterobot0/Tracking-Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyzer.exs
More file actions
470 lines (401 loc) · 16 KB
/
analyzer.exs
File metadata and controls
470 lines (401 loc) · 16 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
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
#!/usr/bin/env elixir
# Derive Options Market Analyzer - Standalone Version
#
# Analyzes market data files created by the watcher script.
# Compares snapshots, identifies trends, and highlights opportunities.
#
# Usage:
# elixir analyzer.exs [command] [args]
#
# Commands:
# latest - Analyze the most recent market snapshot
# compare file1 file2 - Compare two market snapshots
# trends [directory] - Analyze trends across all snapshots in directory
# opportunities [file] - Find top opportunities in a snapshot
Mix.install([
{:jason, "~> 1.4"},
{:decimal, "~> 2.0"}
])
defmodule MarketAnalyzer do
@moduledoc """
Analyzes market data snapshots from the watcher.
"""
def run(args) do
case args do
["latest"] -> analyze_latest()
["latest", dir] -> analyze_latest(dir)
["compare", file1, file2] -> compare_snapshots(file1, file2)
["trends"] -> analyze_trends("market_data")
["trends", dir] -> analyze_trends(dir)
["opportunities"] -> find_opportunities_latest()
["opportunities", file] -> find_opportunities(file)
_ -> show_usage()
end
end
defp show_usage do
IO.puts("""
Derive Options Market Analyzer
Usage:
elixir analyzer.exs [command] [args]
Commands:
latest [dir] - Analyze the most recent market snapshot
compare file1 file2 - Compare two market snapshots
trends [directory] - Analyze trends across all snapshots
opportunities [file] - Find top opportunities in a snapshot
Examples:
elixir analyzer.exs latest
elixir analyzer.exs compare market_data/market_1234.json market_data/market_5678.json
elixir analyzer.exs trends market_data
elixir analyzer.exs opportunities
""")
end
defp analyze_latest(dir \\ "market_data") do
case get_latest_file(dir) do
{:ok, file} ->
IO.puts("\n=== Analyzing Latest Market Snapshot ===")
IO.puts("File: #{file}")
analyze_file(file)
{:error, reason} ->
IO.puts("Error: #{reason}")
end
end
defp find_opportunities_latest(dir \\ "market_data") do
case get_latest_file(dir) do
{:ok, file} -> find_opportunities(file)
{:error, reason} -> IO.puts("Error: #{reason}")
end
end
defp find_opportunities(file) do
case load_snapshot(file) do
{:ok, snapshot} ->
IO.puts("\n=== Top Trading Opportunities ===")
IO.puts("Snapshot: #{snapshot["timestamp"]}")
IO.puts("Markets analyzed: #{snapshot["markets_scanned"]}")
display_opportunities(snapshot["data"])
display_by_strategy(snapshot["data"])
{:error, reason} ->
IO.puts("Error loading file: #{reason}")
end
end
defp analyze_file(file) do
case load_snapshot(file) do
{:ok, snapshot} ->
data = snapshot["data"]
# Overall statistics
IO.puts("\nTimestamp: #{snapshot["timestamp"]}")
IO.puts("Total markets: #{length(data)}")
# Spread analysis
spreads = Enum.map(data, fn m -> m["spread_dollars"] end)
avg_spread = average(spreads)
min_spread = Enum.min(spreads, fn -> 0 end)
max_spread = Enum.max(spreads, fn -> 0 end)
IO.puts("\n=== Spread Analysis ===")
IO.puts("Average spread: $#{Float.round(avg_spread, 2)}")
IO.puts("Min spread: $#{Float.round(min_spread, 2)}")
IO.puts("Max spread: $#{Float.round(max_spread, 2)}")
# Volume analysis
volumes = Enum.map(data, fn m -> m["volume_24h"] || 0 end)
total_volume = Enum.sum(volumes)
avg_volume = average(volumes)
IO.puts("\n=== Volume Analysis ===")
IO.puts("Total 24h volume: #{Float.round(total_volume, 2)}")
IO.puts("Average volume: #{Float.round(avg_volume, 2)}")
# Expiration breakdown
IO.puts("\n=== Expiration Breakdown ===")
data
|> Enum.group_by(fn m -> m["expiry"] end)
|> Enum.sort_by(fn {exp, _} -> exp end)
|> Enum.each(fn {expiry, markets} ->
avg_spread = markets
|> Enum.map(fn m -> m["spread_dollars"] end)
|> average()
IO.puts("#{expiry}: #{length(markets)} markets, avg spread: $#{Float.round(avg_spread, 2)}")
end)
# Strike analysis
IO.puts("\n=== Strike Analysis ===")
data
|> Enum.group_by(fn m -> m["strike"] end)
|> Enum.sort_by(fn {strike, _} -> strike end)
|> Enum.each(fn {strike, markets} ->
calls = Enum.filter(markets, fn m -> m["type"] == "CALL" end)
puts = Enum.filter(markets, fn m -> m["type"] == "PUT" end)
call_spread = if length(calls) > 0 do
calls |> Enum.map(fn m -> m["spread_dollars"] end) |> average()
else
0
end
put_spread = if length(puts) > 0 do
puts |> Enum.map(fn m -> m["spread_dollars"] end) |> average()
else
0
end
IO.puts("Strike #{strike}: Calls avg $#{Float.round(call_spread, 2)}, Puts avg $#{Float.round(put_spread, 2)}")
end)
{:error, reason} ->
IO.puts("Error: #{reason}")
end
end
defp compare_snapshots(file1, file2) do
with {:ok, snap1} <- load_snapshot(file1),
{:ok, snap2} <- load_snapshot(file2) do
IO.puts("\n=== Market Comparison ===")
IO.puts("Snapshot 1: #{snap1["timestamp"]}")
IO.puts("Snapshot 2: #{snap2["timestamp"]}")
data1 = Map.new(snap1["data"], fn m -> {m["instrument_name"], m} end)
data2 = Map.new(snap2["data"], fn m -> {m["instrument_name"], m} end)
# Find common instruments
common = MapSet.intersection(
MapSet.new(Map.keys(data1)),
MapSet.new(Map.keys(data2))
)
IO.puts("\nCommon instruments: #{MapSet.size(common)}")
# Analyze spread changes
spread_changes = common
|> Enum.map(fn inst ->
m1 = data1[inst]
m2 = data2[inst]
change = m2["spread_dollars"] - m1["spread_dollars"]
{inst, change, m1["spread_dollars"], m2["spread_dollars"]}
end)
|> Enum.sort_by(fn {_, change, _, _} -> abs(change) end, :desc)
IO.puts("\n=== Largest Spread Changes ===")
spread_changes
|> Enum.take(10)
|> Enum.each(fn {inst, change, old, new} ->
direction = if change > 0, do: "↑", else: "↓"
IO.puts("#{inst}: $#{Float.round(old, 2)} → $#{Float.round(new, 2)} (#{direction}$#{Float.round(abs(change), 2)})")
end)
# Volume changes
volume_changes = common
|> Enum.map(fn inst ->
m1 = data1[inst]
m2 = data2[inst]
v1 = m1["volume_24h"] || 0
v2 = m2["volume_24h"] || 0
change = v2 - v1
{inst, change, v1, v2}
end)
|> Enum.sort_by(fn {_, change, _, _} -> abs(change) end, :desc)
IO.puts("\n=== Largest Volume Changes ===")
volume_changes
|> Enum.take(10)
|> Enum.each(fn {inst, change, old, new} ->
direction = if change > 0, do: "↑", else: "↓"
IO.puts("#{inst}: #{Float.round(old, 1)} → #{Float.round(new, 1)} (#{direction}#{Float.round(abs(change), 1)})")
end)
# New opportunities
if Map.has_key?(snap2, "data") and length(snap2["data"]) > 0 do
scores2 = snap2["data"]
|> Enum.filter(fn m -> Map.has_key?(m, "opportunity_scores") end)
|> Enum.sort_by(fn m -> -m["opportunity_scores"]["total_score"] end)
if length(scores2) > 0 do
IO.puts("\n=== New Top Opportunities ===")
scores2
|> Enum.take(5)
|> Enum.each(fn m ->
score = m["opportunity_scores"]["total_score"]
IO.puts("Score #{score}: #{m["instrument_name"]} - Spread: $#{m["spread_dollars"]}")
end)
end
end
else
{:error, reason} -> IO.puts("Error: #{reason}")
end
end
defp analyze_trends(dir) do
case File.ls(dir) do
{:ok, files} ->
json_files = files
|> Enum.filter(fn f -> String.ends_with?(f, ".json") end)
|> Enum.sort()
if length(json_files) == 0 do
IO.puts("No JSON files found in #{dir}")
return
end
IO.puts("\n=== Trend Analysis ===")
IO.puts("Analyzing #{length(json_files)} snapshots...")
# Load all snapshots
snapshots = json_files
|> Enum.map(fn f ->
path = Path.join(dir, f)
case load_snapshot(path) do
{:ok, snap} -> snap
_ -> nil
end
end)
|> Enum.filter(fn s -> s != nil end)
if length(snapshots) < 2 do
IO.puts("Need at least 2 snapshots for trend analysis")
return
end
# Track spread trends over time
IO.puts("\n=== Spread Trends ===")
spread_trends = snapshots
|> Enum.map(fn snap ->
spreads = snap["data"] |> Enum.map(fn m -> m["spread_dollars"] end)
{
snap["timestamp"],
average(spreads),
Enum.min(spreads, fn -> 0 end),
Enum.max(spreads, fn -> 0 end)
}
end)
spread_trends
|> Enum.each(fn {time, avg, min, max} ->
time_str = String.slice(time, 11, 8) # Extract time portion
IO.puts("#{time_str}: Avg $#{Float.round(avg, 2)}, Min $#{Float.round(min, 2)}, Max $#{Float.round(max, 2)}")
end)
# Volume trends
IO.puts("\n=== Volume Trends ===")
volume_trends = snapshots
|> Enum.map(fn snap ->
volumes = snap["data"] |> Enum.map(fn m -> m["volume_24h"] || 0 end)
{
snap["timestamp"],
Enum.sum(volumes),
average(volumes)
}
end)
volume_trends
|> Enum.each(fn {time, total, avg} ->
time_str = String.slice(time, 11, 8)
IO.puts("#{time_str}: Total #{Float.round(total, 0)}, Avg #{Float.round(avg, 2)}")
end)
# Market count trends
IO.puts("\n=== Market Count Trends ===")
snapshots
|> Enum.each(fn snap ->
time_str = String.slice(snap["timestamp"], 11, 8)
count = length(snap["data"])
IO.puts("#{time_str}: #{count} active markets")
end)
{:error, reason} ->
IO.puts("Error reading directory: #{reason}")
end
end
defp display_opportunities(data) do
# Filter and sort by opportunity score
opportunities = data
|> Enum.filter(fn m -> Map.has_key?(m, "opportunity_scores") end)
|> Enum.sort_by(fn m -> -m["opportunity_scores"]["total_score"] end)
if length(opportunities) == 0 do
IO.puts("\nNo opportunity scores found. Running basic analysis...")
# Basic opportunity detection without scores
good_opportunities = data
|> Enum.filter(fn m ->
spread = m["spread_dollars"]
volume = m["volume_24h"] || 0
spread >= 3 and spread <= 15 and volume >= 10
end)
|> Enum.sort_by(fn m -> -m["volume_24h"] end)
IO.puts("\n=== Markets with Good Spreads and Volume ===")
good_opportunities
|> Enum.take(10)
|> Enum.each(fn m ->
IO.puts("#{m["instrument_name"]}: Spread $#{m["spread_dollars"]}, Volume #{Float.round(m["volume_24h"] || 0, 1)}")
end)
else
IO.puts("\n=== Top 15 Opportunities by Score ===")
opportunities
|> Enum.take(15)
|> Enum.each(fn m ->
score = m["opportunity_scores"]["total_score"]
IO.puts("Score #{score}: #{m["instrument_name"]}")
IO.puts(" Spread: $#{m["spread_dollars"]} (#{m["spread_percent"]}%)")
IO.puts(" Volume: #{Float.round(m["volume_24h"] || 0, 1)}, OI: #{Float.round(m["open_interest"] || 0, 1)}")
IO.puts(" Days to expiry: #{Float.round(m["days_to_expiry"], 1)}")
end)
end
end
defp display_by_strategy(data) do
IO.puts("\n=== Opportunities by Strategy Type ===")
# Short-term high theta opportunities
short_term = data
|> Enum.filter(fn m ->
days = m["days_to_expiry"]
spread = m["spread_dollars"]
days <= 7 and spread >= 5
end)
|> Enum.sort_by(fn m -> -m["spread_dollars"] end)
if length(short_term) > 0 do
IO.puts("\n[Short-term High Theta] (≤7 days, spread ≥$5)")
short_term
|> Enum.take(5)
|> Enum.each(fn m ->
IO.puts(" #{m["instrument_name"]}: #{Float.round(m["days_to_expiry"], 1)} days, Spread $#{m["spread_dollars"]}")
end)
end
# Medium-term balanced
medium_term = data
|> Enum.filter(fn m ->
days = m["days_to_expiry"]
spread = m["spread_dollars"]
volume = m["volume_24h"] || 0
days > 7 and days <= 21 and spread >= 3 and volume >= 20
end)
|> Enum.sort_by(fn m -> -(m["volume_24h"] || 0) end)
if length(medium_term) > 0 do
IO.puts("\n[Medium-term Balanced] (7-21 days, good volume)")
medium_term
|> Enum.take(5)
|> Enum.each(fn m ->
IO.puts(" #{m["instrument_name"]}: Volume #{Float.round(m["volume_24h"] || 0, 1)}, Spread $#{m["spread_dollars"]}")
end)
end
# High volume opportunities
high_volume = data
|> Enum.filter(fn m -> (m["volume_24h"] || 0) >= 100 end)
|> Enum.sort_by(fn m -> -m["volume_24h"] end)
if length(high_volume) > 0 do
IO.puts("\n[High Volume Markets] (≥100 volume)")
high_volume
|> Enum.take(5)
|> Enum.each(fn m ->
IO.puts(" #{m["instrument_name"]}: Volume #{Float.round(m["volume_24h"], 1)}, Spread $#{m["spread_dollars"]}")
end)
end
# Wide spread opportunities
wide_spread = data
|> Enum.filter(fn m -> m["spread_dollars"] >= 20 end)
|> Enum.sort_by(fn m -> -m["spread_dollars"] end)
if length(wide_spread) > 0 do
IO.puts("\n[Wide Spread Opportunities] (≥$20)")
wide_spread
|> Enum.take(5)
|> Enum.each(fn m ->
IO.puts(" #{m["instrument_name"]}: Spread $#{m["spread_dollars"]} (#{m["spread_percent"]}%)")
end)
end
end
defp load_snapshot(file) do
case File.read(file) do
{:ok, content} ->
case Jason.decode(content) do
{:ok, data} -> {:ok, data}
{:error, _} -> {:error, "Invalid JSON format"}
end
{:error, _} -> {:error, "Could not read file"}
end
end
defp get_latest_file(dir) do
case File.ls(dir) do
{:ok, files} ->
json_files = files
|> Enum.filter(fn f -> String.starts_with?(f, "market_") and String.ends_with?(f, ".json") end)
|> Enum.map(fn f -> Path.join(dir, f) end)
|> Enum.sort()
|> Enum.reverse()
case json_files do
[latest | _] -> {:ok, latest}
[] -> {:error, "No market snapshot files found in #{dir}"}
end
{:error, _} -> {:error, "Directory #{dir} not found"}
end
end
defp average([]), do: 0
defp average(list) do
Enum.sum(list) / length(list)
end
end
# Main execution
MarketAnalyzer.run(System.argv())