-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanomaly_detection_example.rb
More file actions
412 lines (340 loc) · 11.8 KB
/
anomaly_detection_example.rb
File metadata and controls
412 lines (340 loc) · 11.8 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
#!/usr/bin/env ruby
# frozen_string_literal: true
# Example: Market Anomaly Detection System
# This example demonstrates AI-powered anomaly detection for cryptocurrency markets
require 'bundler/setup'
require_relative '../../lib/buda_api'
# Configuration
API_KEY = ENV['BUDA_API_KEY'] || 'your_api_key_here'
API_SECRET = ENV['BUDA_API_SECRET'] || 'your_api_secret_here'
class AnomalyMonitor
def initialize(client, llm_provider = :openai)
@client = client
@detector = BudaApi::AI::AnomalyDetector.new(client, llm_provider: llm_provider)
@monitoring_active = false
@alert_count = 0
end
def start_monitoring(markets = nil, options = {})
markets ||= BudaApi::Constants::Market::MAJOR
@monitoring_active = true
@alert_count = 0
puts "🔍 Starting Anomaly Detection System"
puts "Markets: #{markets.join(', ')}"
puts "=" * 50
# Initial scan
puts "\n📊 Initial Market Scan"
puts "-" * 30
initial_results = @detector.detect_market_anomalies(
markets: markets,
include_ai_analysis: true
)
display_detection_results(initial_results)
# Set up continuous monitoring
if options[:continuous]
start_continuous_monitoring(markets, options[:interval] || 300)
end
# Set up alerts
if options[:alerts]
setup_alert_system(options[:alert_config] || {})
end
end
def start_continuous_monitoring(markets, interval_seconds)
puts "\n🔄 Starting Continuous Monitoring"
puts "Scan interval: #{interval_seconds} seconds"
puts "Press Ctrl+C to stop monitoring"
puts "-" * 30
begin
while @monitoring_active
sleep(interval_seconds)
puts "\n⏰ #{Time.now.strftime('%H:%M:%S')} - Scanning markets..."
results = @detector.detect_market_anomalies(
markets: markets,
include_ai_analysis: false # Skip AI for frequent scans
)
if results[:anomalies_detected] > 0
@alert_count += 1
puts "🚨 ANOMALIES DETECTED (##{@alert_count})"
display_detection_results(results, compact: true)
else
puts "✅ No anomalies detected"
end
end
rescue Interrupt
puts "\n⏹️ Monitoring stopped by user"
@monitoring_active = false
end
end
def setup_alert_system(config)
puts "\n🚨 Setting up Alert System"
puts "-" * 30
alert_system = @detector.setup_anomaly_alerts(config)
puts "Alert system configured: #{alert_system[:status]}"
puts "Configuration: #{alert_system[:config]}"
alert_system
end
def analyze_single_market(market_id)
puts "\n🔍 Deep Market Analysis: #{market_id}"
puts "-" * 30
# Real-time analysis
current_anomalies = @detector.detect_market_anomalies(
markets: [market_id],
include_ai_analysis: true
)
display_detection_results(current_anomalies)
# Historical analysis
puts "\n📈 Historical Anomaly Analysis (24h)"
puts "-" * 30
historical_results = @detector.analyze_historical_anomalies(market_id, 24)
if historical_results[:type] == :historical_analysis
puts "Data Points Analyzed: #{historical_results[:data_points]}"
puts "Historical Anomalies: #{historical_results[:anomalies_found]}"
if historical_results[:anomalies_found] > 0
puts "\nTop Historical Anomalies:"
historical_results[:anomalies].first(5).each_with_index do |anomaly, i|
puts "#{i + 1}. #{anomaly[:type]}: #{anomaly[:description]} (#{anomaly[:severity]})"
end
end
else
puts "Historical analysis: #{historical_results[:message]}"
end
end
def display_detection_results(results, compact: false)
if results[:type] == :anomaly_detection_error
puts "❌ Detection Error: #{results[:error]}"
return
end
puts "Markets Analyzed: #{results[:markets_analyzed]}"
puts "Anomalies Detected: #{results[:anomalies_detected]}"
if results[:anomalies_detected] == 0
puts "✅ All markets operating normally"
return
end
# Display severity summary
severity = results[:severity_summary]
if severity[:critical] > 0 || severity[:high] > 0
puts "🚨 Alert Levels:"
puts " Critical: #{severity[:critical]}" if severity[:critical] > 0
puts " High: #{severity[:high]}" if severity[:high] > 0
puts " Medium: #{severity[:medium]}" if severity[:medium] > 0
puts " Low: #{severity[:low]}" if severity[:low] > 0
end
# Display top anomalies
display_count = compact ? 3 : 10
puts "\n📋 Detected Anomalies:"
results[:anomalies].first(display_count).each_with_index do |anomaly, i|
severity_emoji = get_severity_emoji(anomaly[:severity])
puts "#{i + 1}. #{severity_emoji} #{anomaly[:market_id]} - #{anomaly[:type].to_s.upcase}"
puts " #{anomaly[:description]}"
puts " Severity: #{anomaly[:severity]} (#{anomaly[:severity_score].round(1)})"
unless compact
if anomaly[:recommendation]
puts " 💡 #{anomaly[:recommendation]}"
end
if anomaly[:details]
display_anomaly_details(anomaly[:details])
end
end
puts
end
# Display recommendations
if results[:recommendations].any? && !compact
puts "💡 General Recommendations:"
results[:recommendations].each do |rec|
puts " • #{rec}"
end
puts
end
# Display AI analysis if available
if results[:ai_analysis] && !compact
puts "🧠 AI Analysis:"
puts results[:ai_analysis][:analysis]
puts
end
end
def get_severity_emoji(severity)
case severity
when :critical then "🚫"
when :high then "🔴"
when :medium then "🟡"
when :low then "🟢"
else "❓"
end
end
def display_anomaly_details(details)
case
when details[:current_price]
puts " Price: #{details[:current_price]} CLP"
puts " Change: #{details[:change_24h]}%" if details[:change_24h]
when details[:current_volume]
puts " Volume: #{details[:current_volume]}"
puts " Ratio: #{details[:volume_ratio]}x normal" if details[:volume_ratio]
when details[:current_spread]
puts " Spread: #{details[:current_spread]}%"
puts " Normal: #{details[:normal_spread]}%" if details[:normal_spread]
when details[:large_orders]
puts " Large Orders: #{details[:large_orders].length}"
puts " Total Value: #{details[:total_value].round(2)} CLP" if details[:total_value]
end
end
def generate_anomaly_report
puts "\n📊 Anomaly Detection Report"
puts "=" * 50
# Generate comprehensive report
markets = BudaApi::Constants::Market::MAJOR
full_results = @detector.detect_market_anomalies(
markets: markets,
include_ai_analysis: true
)
display_detection_results(full_results)
# Save report to file
timestamp = Time.now.strftime("%Y%m%d_%H%M%S")
filename = "anomaly_report_#{timestamp}.json"
File.write(filename, JSON.pretty_generate(full_results))
puts "📄 Full report saved to: #{filename}"
full_results
end
end
def main
puts "🔍 Market Anomaly Detection System"
puts "=" * 50
unless BudaApi.ai_available?
puts "❌ AI features not available. Install ruby_llm gem first."
return
end
# Initialize client and monitor
client = BudaApi::AuthenticatedClient.new(
api_key: API_KEY,
api_secret: API_SECRET,
sandbox: true
)
monitor = AnomalyMonitor.new(client, :openai)
puts "Select monitoring mode:"
puts "1. One-time scan (default)"
puts "2. Continuous monitoring"
puts "3. Single market analysis"
puts "4. Generate full report"
print "Choice (1-4): "
choice = gets.chomp
case choice
when '2'
# Continuous monitoring
puts "\nContinuous monitoring setup:"
print "Scan interval (seconds, default 300): "
interval = gets.chomp.to_i
interval = 300 if interval <= 0
monitor.start_monitoring(
nil, # Use default markets
continuous: true,
interval: interval,
alerts: true
)
when '3'
# Single market analysis
puts "\nAvailable markets: #{BudaApi::Constants::Market::MAJOR.join(', ')}"
print "Enter market ID (e.g., BTC-CLP): "
market_id = gets.chomp.upcase
if BudaApi::Constants::Market::MAJOR.include?(market_id)
monitor.analyze_single_market(market_id)
else
puts "❌ Invalid market ID"
end
when '4'
# Generate full report
monitor.generate_anomaly_report
else
# One-time scan (default)
monitor.start_monitoring(
BudaApi::Constants::Market::MAJOR,
continuous: false
)
end
rescue BudaApi::ApiError => e
puts "❌ API Error: #{e.message}"
rescue Interrupt
puts "\n⏹️ Monitoring stopped"
rescue => e
puts "❌ Error: #{e.message}"
puts e.backtrace.first(3).join("\n") if ENV['DEBUG']
end
def demo_mode
puts "\n🎭 Demo Mode - Simulated Anomaly Detection"
puts "=" * 50
# Simulate some anomalies for demonstration
simulated_anomalies = [
{
type: :price_spike,
market_id: "BTC-CLP",
severity: :high,
severity_score: 8.2,
description: "Price spike detected: +12.5% change",
recommendation: "Monitor for potential reversal or continuation",
timestamp: Time.now
},
{
type: :volume_anomaly,
market_id: "ETH-CLP",
severity: :medium,
severity_score: 6.1,
description: "Volume anomaly: 4.2x normal volume",
recommendation: "Watch for breaking news or large transactions",
timestamp: Time.now
},
{
type: :whale_activity,
market_id: "BTC-CLP",
severity: :high,
severity_score: 7.8,
description: "Large order activity detected: 3 whale orders",
recommendation: "Expect potential price impact from large orders",
timestamp: Time.now
}
]
simulated_results = {
type: :anomaly_detection,
timestamp: Time.now,
markets_analyzed: 3,
anomalies_detected: 3,
anomalies: simulated_anomalies,
severity_summary: { critical: 0, high: 2, medium: 1, low: 0 },
recommendations: [
"🚨 Multiple high-severity anomalies detected",
"🐋 Large order activity - monitor for price impact",
"📊 Increased market volatility - use caution with orders"
]
}
puts "📊 Simulated Detection Results:"
monitor = Object.new
monitor.define_singleton_method(:display_detection_results) do |results, compact: false|
# Use the same display logic as the real monitor
puts "Markets Analyzed: #{results[:markets_analyzed]}"
puts "Anomalies Detected: #{results[:anomalies_detected]}"
puts "\n🚨 Alert Levels:"
severity = results[:severity_summary]
puts " High: #{severity[:high]}" if severity[:high] > 0
puts " Medium: #{severity[:medium]}" if severity[:medium] > 0
puts "\n📋 Detected Anomalies:"
results[:anomalies].each_with_index do |anomaly, i|
severity_emoji = case anomaly[:severity]
when :high then "🔴"
when :medium then "🟡"
else "🟢"
end
puts "#{i + 1}. #{severity_emoji} #{anomaly[:market_id]} - #{anomaly[:type].to_s.upcase}"
puts " #{anomaly[:description]}"
puts " 💡 #{anomaly[:recommendation]}"
puts
end
puts "💡 Recommendations:"
results[:recommendations].each { |rec| puts " • #{rec}" }
end
monitor.display_detection_results(simulated_results)
puts "\n🎯 This is a demonstration of the anomaly detection system."
puts "In real mode, it would analyze actual market data from the Buda API."
end
if __FILE__ == $0
if ARGV.include?('--demo')
demo_mode
else
main
end
end