Module: SqaDemo::Sinatra::Routes::Api

Defined in:
lib/sqa_demo/sinatra/routes/api.rb

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



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
# File 'lib/sqa_demo/sinatra/routes/api.rb', line 9

def self.registered(app)
  # Get stock data
  app.get '/api/stock/:ticker' do
    content_type :json

    ticker = params[:ticker].upcase
    period = params[:period] || 'all'

    begin
      stock = SQA::Stock.new(ticker: ticker)
      ohlcv = extract_ohlcv(stock)

      # Filter by period
      filtered_dates, filtered_opens, filtered_highs, filtered_lows, filtered_closes, filtered_volumes =
        filter_by_period(ohlcv[:dates], ohlcv[:opens], ohlcv[:highs], ohlcv[:lows], ohlcv[:closes], ohlcv[:volumes], period: period)

      # Calculate basic stats
      current_price = filtered_closes.last
      prev_price = filtered_closes[-2]
      change = current_price - prev_price
      change_pct = (change / prev_price) * 100

      # 52-week high/low uses full data for reference
      high_52w = ohlcv[:closes].last(252).max rescue ohlcv[:closes].max
      low_52w = ohlcv[:closes].last(252).min rescue ohlcv[:closes].min

      {
        ticker: ticker,
        period: period,
        current_price: current_price,
        change: change,
        change_percent: change_pct,
        high_52w: high_52w,
        low_52w: low_52w,
        dates: filtered_dates,
        open: filtered_opens,
        high: filtered_highs,
        low: filtered_lows,
        close: filtered_closes,
        volume: filtered_volumes
      }.to_json
    rescue => e
      status 500
      { error: e.message }.to_json
    end
  end

  # Get technical indicators
  app.get '/api/indicators/:ticker' do
    content_type :json

    ticker = params[:ticker].upcase
    period = params[:period] || 'all'

    begin
      stock = SQA::Stock.new(ticker: ticker)
      ohlcv = extract_ohlcv(stock)

      prices = ohlcv[:closes]
      opens = ohlcv[:opens]
      highs = ohlcv[:highs]
      lows = ohlcv[:lows]
      volumes = ohlcv[:volumes]
      dates = ohlcv[:dates]
      n = prices.length

      # Calculate indicators on full dataset (they need historical context)
      indicators = calculate_all_indicators(opens, highs, lows, prices, volumes, n)

      # Detect candlestick patterns
      detected_patterns = detect_candlestick_patterns(opens, highs, lows, prices, dates, n)

      # Filter results by period
      filtered_data = filter_indicators_by_period(dates, indicators, period)

      filtered_data.merge(
        period: period,
        patterns: detected_patterns
      ).to_json
    rescue => e
      status 500
      { error: e.message }.to_json
    end
  end

  # Run backtest
  app.post '/api/backtest/:ticker' do
    content_type :json

    ticker = params[:ticker].upcase
    strategy_name = params[:strategy] || 'RSI'

    begin
      stock = SQA::Stock.new(ticker: ticker)

      # Resolve strategy
      strategy = resolve_strategy(strategy_name)

      # Run backtest
      backtest = SQA::Backtest.new(
        stock: stock,
        strategy: strategy,
        initial_capital: 10_000.0,
        commission: 1.0
      )

      results = backtest.run

      {
        total_return: results.total_return,
        annualized_return: results.annualized_return,
        sharpe_ratio: results.sharpe_ratio,
        max_drawdown: results.max_drawdown,
        win_rate: results.win_rate,
        total_trades: results.total_trades,
        profit_factor: results.profit_factor,
        avg_win: results.avg_win,
        avg_loss: results.avg_loss
      }.to_json
    rescue => e
      status 500
      { error: e.message }.to_json
    end
  end

  # Run market analysis
  app.get '/api/analyze/:ticker' do
    content_type :json

    ticker = params[:ticker].upcase

    begin
      stock = SQA::Stock.new(ticker: ticker)
      prices = stock.df["adj_close_price"].to_a

      # Market regime
      regime = SQA::MarketRegime.detect(stock)

      # Seasonal analysis
      seasonal = SQA::SeasonalAnalyzer.analyze(stock)

      # FPOP analysis
      fpop_results = analyze_fpop(stock, prices)

      # Risk metrics
      returns = prices.each_cons(2).map { |a, b| (b - a) / a }
      var_95 = SQA::RiskManager.var(returns, confidence: 0.95)
      sharpe = SQA::RiskManager.sharpe_ratio(returns)
      max_dd = SQA::RiskManager.max_drawdown(prices)

      {
        regime: {
          type: regime[:type],
          volatility: regime[:volatility],
          strength: regime[:strength_score],
          trend: regime[:trend_score]
        },
        seasonal: {
          best_months: seasonal[:best_months],
          worst_months: seasonal[:worst_months],
          best_quarters: seasonal[:best_quarters],
          has_pattern: seasonal[:has_seasonal_pattern]
        },
        fpop: fpop_results,
        risk: {
          var_95: var_95,
          sharpe_ratio: sharpe,
          max_drawdown: max_dd[:max_drawdown]
        }
      }.to_json
    rescue => e
      status 500
      { error: e.message }.to_json
    end
  end

  # Compare strategies
  app.post '/api/compare/:ticker' do
    content_type :json

    ticker = params[:ticker].upcase

    begin
      stock = SQA::Stock.new(ticker: ticker)

      strategies = {
        'RSI' => SQA::Strategy::RSI,
        'SMA' => SQA::Strategy::SMA,
        'EMA' => SQA::Strategy::EMA,
        'MACD' => SQA::Strategy::MACD,
        'BollingerBands' => SQA::Strategy::BollingerBands
      }

      results = strategies.map do |name, strategy_class|
        backtest = SQA::Backtest.new(
          stock: stock,
          strategy: strategy_class,
          initial_capital: 10_000.0,
          commission: 1.0
        )

        result = backtest.run

        {
          strategy: name,
          return: result.total_return,
          sharpe: result.sharpe_ratio,
          drawdown: result.max_drawdown,
          win_rate: result.win_rate,
          trades: result.total_trades
        }
      rescue => e
        nil
      end.compact

      results.sort_by! { |r| -r[:return] }
      results.to_json
    rescue => e
      status 500
      { error: e.message }.to_json
    end
  end
end