sqa 0.0.24 → 0.0.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (203) hide show
  1. checksums.yaml +4 -4
  2. data/.goose/memory/development.txt +3 -0
  3. data/.semver +6 -0
  4. data/ARCHITECTURE.md +648 -0
  5. data/CHANGELOG.md +95 -0
  6. data/CLAUDE.md +674 -0
  7. data/COMMITS.md +196 -0
  8. data/DATAFRAME_ARCHITECTURE_REVIEW.md +421 -0
  9. data/NEXT-STEPS.md +154 -0
  10. data/README.md +839 -265
  11. data/TASKS.md +358 -0
  12. data/TEST_RESULTS.md +140 -0
  13. data/TODO.md +42 -0
  14. data/_notes.txt +25 -0
  15. data/bin/sqa-console +11 -0
  16. data/data/talk_talk.json +103284 -0
  17. data/develop_summary.md +313 -0
  18. data/docs/advanced/backtesting.md +206 -0
  19. data/docs/advanced/ensemble.md +68 -0
  20. data/docs/advanced/fpop.md +153 -0
  21. data/docs/advanced/index.md +112 -0
  22. data/docs/advanced/multi-timeframe.md +67 -0
  23. data/docs/advanced/pattern-matcher.md +75 -0
  24. data/docs/advanced/portfolio-optimizer.md +79 -0
  25. data/docs/advanced/portfolio.md +166 -0
  26. data/docs/advanced/risk-management.md +210 -0
  27. data/docs/advanced/strategy-generator.md +158 -0
  28. data/docs/advanced/streaming.md +209 -0
  29. data/docs/ai_and_ml.md +80 -0
  30. data/docs/api/dataframe.md +1114 -0
  31. data/docs/api/index.md +126 -0
  32. data/docs/assets/css/custom.css +88 -0
  33. data/docs/assets/images/sqa.jpg +0 -0
  34. data/docs/assets/js/mathjax.js +18 -0
  35. data/docs/concepts/index.md +60 -0
  36. data/docs/contributing/index.md +60 -0
  37. data/docs/data-sources/index.md +66 -0
  38. data/docs/data_frame.md +316 -97
  39. data/docs/factors_that_impact_price.md +26 -0
  40. data/docs/finviz.md +11 -0
  41. data/docs/fx_pro_bit.md +25 -0
  42. data/docs/genetic_programming.md +104 -0
  43. data/docs/getting-started/index.md +107 -0
  44. data/docs/getting-started/installation.md +229 -0
  45. data/docs/getting-started/quick-start.md +244 -0
  46. data/docs/i_gotta_an_idea.md +22 -0
  47. data/docs/index.md +161 -0
  48. data/docs/indicators/index.md +97 -0
  49. data/docs/indicators.md +110 -24
  50. data/docs/options.md +8 -0
  51. data/docs/strategies/bollinger-bands.md +146 -0
  52. data/docs/strategies/consensus.md +64 -0
  53. data/docs/strategies/custom.md +310 -0
  54. data/docs/strategies/ema.md +53 -0
  55. data/docs/strategies/index.md +92 -0
  56. data/docs/strategies/kbs.md +164 -0
  57. data/docs/strategies/macd.md +96 -0
  58. data/docs/strategies/market-profile.md +54 -0
  59. data/docs/strategies/mean-reversion.md +58 -0
  60. data/docs/strategies/rsi.md +95 -0
  61. data/docs/strategies/sma.md +55 -0
  62. data/docs/strategies/stochastic.md +63 -0
  63. data/docs/strategies/volume-breakout.md +54 -0
  64. data/docs/tags.md +7 -0
  65. data/examples/README.md +354 -0
  66. data/examples/advanced_features_example.rb +350 -0
  67. data/examples/fpop_analysis_example.rb +191 -0
  68. data/examples/genetic_programming_example.rb +148 -0
  69. data/examples/kbs_strategy_example.rb +208 -0
  70. data/examples/pattern_context_example.rb +300 -0
  71. data/examples/rails_app/Gemfile +34 -0
  72. data/examples/rails_app/README.md +416 -0
  73. data/examples/rails_app/app/assets/javascripts/application.js +107 -0
  74. data/examples/rails_app/app/assets/stylesheets/application.css +659 -0
  75. data/examples/rails_app/app/controllers/analysis_controller.rb +11 -0
  76. data/examples/rails_app/app/controllers/api/v1/stocks_controller.rb +227 -0
  77. data/examples/rails_app/app/controllers/application_controller.rb +22 -0
  78. data/examples/rails_app/app/controllers/backtest_controller.rb +11 -0
  79. data/examples/rails_app/app/controllers/dashboard_controller.rb +21 -0
  80. data/examples/rails_app/app/controllers/portfolio_controller.rb +7 -0
  81. data/examples/rails_app/app/views/analysis/show.html.erb +209 -0
  82. data/examples/rails_app/app/views/backtest/show.html.erb +171 -0
  83. data/examples/rails_app/app/views/dashboard/index.html.erb +118 -0
  84. data/examples/rails_app/app/views/dashboard/show.html.erb +408 -0
  85. data/examples/rails_app/app/views/errors/show.html.erb +17 -0
  86. data/examples/rails_app/app/views/layouts/application.html.erb +60 -0
  87. data/examples/rails_app/app/views/portfolio/index.html.erb +33 -0
  88. data/examples/rails_app/bin/rails +6 -0
  89. data/examples/rails_app/config/application.rb +45 -0
  90. data/examples/rails_app/config/boot.rb +5 -0
  91. data/examples/rails_app/config/database.yml +18 -0
  92. data/examples/rails_app/config/environment.rb +11 -0
  93. data/examples/rails_app/config/routes.rb +26 -0
  94. data/examples/rails_app/config.ru +8 -0
  95. data/examples/realtime_stream_example.rb +274 -0
  96. data/examples/sinatra_app/Gemfile +42 -0
  97. data/examples/sinatra_app/Gemfile.lock +268 -0
  98. data/examples/sinatra_app/QUICKSTART.md +169 -0
  99. data/examples/sinatra_app/README.md +471 -0
  100. data/examples/sinatra_app/RUNNING_WITHOUT_TALIB.md +90 -0
  101. data/examples/sinatra_app/TROUBLESHOOTING.md +95 -0
  102. data/examples/sinatra_app/app.rb +404 -0
  103. data/examples/sinatra_app/config.ru +5 -0
  104. data/examples/sinatra_app/public/css/style.css +723 -0
  105. data/examples/sinatra_app/public/debug_macd.html +82 -0
  106. data/examples/sinatra_app/public/js/app.js +107 -0
  107. data/examples/sinatra_app/start.sh +53 -0
  108. data/examples/sinatra_app/views/analyze.erb +306 -0
  109. data/examples/sinatra_app/views/backtest.erb +325 -0
  110. data/examples/sinatra_app/views/dashboard.erb +831 -0
  111. data/examples/sinatra_app/views/error.erb +58 -0
  112. data/examples/sinatra_app/views/index.erb +118 -0
  113. data/examples/sinatra_app/views/layout.erb +61 -0
  114. data/examples/sinatra_app/views/portfolio.erb +43 -0
  115. data/examples/strategy_generator_example.rb +346 -0
  116. data/hsa_portfolio.csv +11 -0
  117. data/justfile +0 -0
  118. data/lib/api/alpha_vantage_api.rb +462 -0
  119. data/lib/sqa/backtest.rb +329 -0
  120. data/lib/sqa/data_frame/alpha_vantage.rb +51 -63
  121. data/lib/sqa/data_frame/data.rb +92 -0
  122. data/lib/sqa/data_frame/yahoo_finance.rb +35 -43
  123. data/lib/sqa/data_frame.rb +154 -243
  124. data/lib/sqa/ensemble.rb +359 -0
  125. data/lib/sqa/fpop.rb +199 -0
  126. data/lib/sqa/gp.rb +259 -0
  127. data/lib/sqa/indicator.rb +16 -6
  128. data/lib/sqa/init.rb +15 -8
  129. data/lib/sqa/market_regime.rb +240 -0
  130. data/lib/sqa/multi_timeframe.rb +379 -0
  131. data/lib/sqa/pattern_matcher.rb +497 -0
  132. data/lib/sqa/portfolio.rb +260 -6
  133. data/lib/sqa/portfolio_optimizer.rb +377 -0
  134. data/lib/sqa/risk_manager.rb +442 -0
  135. data/lib/sqa/seasonal_analyzer.rb +209 -0
  136. data/lib/sqa/sector_analyzer.rb +300 -0
  137. data/lib/sqa/stock.rb +131 -127
  138. data/lib/sqa/strategy/bollinger_bands.rb +42 -0
  139. data/lib/sqa/strategy/consensus.rb +5 -2
  140. data/lib/sqa/strategy/kbs_strategy.rb +470 -0
  141. data/lib/sqa/strategy/macd.rb +46 -0
  142. data/lib/sqa/strategy/mp.rb +1 -1
  143. data/lib/sqa/strategy/stochastic.rb +60 -0
  144. data/lib/sqa/strategy/volume_breakout.rb +57 -0
  145. data/lib/sqa/strategy.rb +5 -0
  146. data/lib/sqa/strategy_generator.rb +947 -0
  147. data/lib/sqa/stream.rb +361 -0
  148. data/lib/sqa/ticker.rb +9 -2
  149. data/lib/sqa/version.rb +1 -7
  150. data/lib/sqa.rb +35 -20
  151. data/main.just +81 -0
  152. data/mkdocs.yml +252 -0
  153. data/trace.log +0 -0
  154. metadata +265 -69
  155. data/bin/sqa +0 -6
  156. data/docs/alpha_vantage_technical_indicators.md +0 -62
  157. data/docs/average_true_range.md +0 -9
  158. data/docs/bollinger_bands.md +0 -15
  159. data/docs/candlestick_pattern_recognizer.md +0 -4
  160. data/docs/donchian_channel.md +0 -5
  161. data/docs/double_top_bottom_pattern.md +0 -3
  162. data/docs/exponential_moving_average.md +0 -19
  163. data/docs/fibonacci_retracement.md +0 -30
  164. data/docs/head_and_shoulders_pattern.md +0 -3
  165. data/docs/market_profile.md +0 -4
  166. data/docs/momentum.md +0 -19
  167. data/docs/moving_average_convergence_divergence.md +0 -23
  168. data/docs/peaks_and_valleys.md +0 -11
  169. data/docs/relative_strength_index.md +0 -6
  170. data/docs/simple_moving_average.md +0 -8
  171. data/docs/stochastic_oscillator.md +0 -4
  172. data/docs/ta_lib.md +0 -160
  173. data/docs/true_range.md +0 -12
  174. data/lib/patches/dry-cli.rb +0 -228
  175. data/lib/sqa/activity.rb +0 -10
  176. data/lib/sqa/cli.rb +0 -62
  177. data/lib/sqa/commands/analysis.rb +0 -309
  178. data/lib/sqa/commands/base.rb +0 -139
  179. data/lib/sqa/commands/web.rb +0 -199
  180. data/lib/sqa/commands.rb +0 -22
  181. data/lib/sqa/constants.rb +0 -23
  182. data/lib/sqa/indicator/average_true_range.rb +0 -33
  183. data/lib/sqa/indicator/bollinger_bands.rb +0 -28
  184. data/lib/sqa/indicator/candlestick_pattern_recognizer.rb +0 -60
  185. data/lib/sqa/indicator/donchian_channel.rb +0 -29
  186. data/lib/sqa/indicator/double_top_bottom_pattern.rb +0 -34
  187. data/lib/sqa/indicator/elliott_wave_theory.rb +0 -57
  188. data/lib/sqa/indicator/exponential_moving_average.rb +0 -25
  189. data/lib/sqa/indicator/exponential_moving_average_trend.rb +0 -36
  190. data/lib/sqa/indicator/fibonacci_retracement.rb +0 -23
  191. data/lib/sqa/indicator/head_and_shoulders_pattern.rb +0 -26
  192. data/lib/sqa/indicator/market_profile.rb +0 -32
  193. data/lib/sqa/indicator/mean_reversion.rb +0 -37
  194. data/lib/sqa/indicator/momentum.rb +0 -28
  195. data/lib/sqa/indicator/moving_average_convergence_divergence.rb +0 -29
  196. data/lib/sqa/indicator/peaks_and_valleys.rb +0 -29
  197. data/lib/sqa/indicator/predict_next_value.rb +0 -202
  198. data/lib/sqa/indicator/relative_strength_index.rb +0 -47
  199. data/lib/sqa/indicator/simple_moving_average.rb +0 -24
  200. data/lib/sqa/indicator/simple_moving_average_trend.rb +0 -32
  201. data/lib/sqa/indicator/stochastic_oscillator.rb +0 -68
  202. data/lib/sqa/indicator/true_range.rb +0 -39
  203. data/lib/sqa/trade.rb +0 -26
@@ -0,0 +1,169 @@
1
+ # SQA Web App - Quick Start Guide
2
+
3
+ Get the SQA web application running in 5 minutes.
4
+
5
+ ## 1. Prerequisites
6
+
7
+ ```bash
8
+ # Install TA-Lib (macOS)
9
+ brew install ta-lib
10
+
11
+ # Start Redis
12
+ redis-server
13
+
14
+ # Set API key (optional, uses Yahoo Finance as fallback)
15
+ export AV_API_KEY="your_key_here"
16
+ ```
17
+
18
+ ## 2. Install Dependencies
19
+
20
+ ```bash
21
+ cd examples/sinatra_app
22
+ bundle install
23
+ ```
24
+
25
+ ## 3. Start the Application
26
+
27
+ **IMPORTANT:** Always use `bundle exec` to ensure all gems are loaded correctly:
28
+
29
+ ```bash
30
+ bundle exec ruby app.rb
31
+ ```
32
+
33
+ Or use the provided startup script:
34
+
35
+ ```bash
36
+ ./start.sh
37
+ ```
38
+
39
+ The app will start on http://localhost:4567
40
+
41
+ **Common mistake:** Running `ruby app.rb` without `bundle exec` will cause the app to fail or behave incorrectly.
42
+
43
+ ## 4. Quick Tour
44
+
45
+ ### Home Page
46
+ - Click on any quick link ticker (AAPL, MSFT, etc.)
47
+ - Or enter a custom ticker in the search box
48
+
49
+ ### Dashboard
50
+ - View interactive price charts (candlestick or line)
51
+ - See volume chart
52
+ - Check RSI and MACD indicators
53
+ - View key metrics (52-week high/low, current RSI, market regime)
54
+ - Compare all strategies at once
55
+
56
+ ### Analysis Page
57
+ - Market regime detection (bull/bear/sideways)
58
+ - Seasonal patterns (best months and quarters)
59
+ - FPOP (Future Period) analysis
60
+ - Risk metrics (VaR, Sharpe ratio, max drawdown)
61
+
62
+ ### Backtest Page
63
+ - Select a strategy (RSI, MACD, SMA, EMA, Bollinger Bands, KBS)
64
+ - View detailed backtest results
65
+ - Compare all strategies side-by-side
66
+
67
+ ## 5. Keyboard Shortcuts
68
+
69
+ - `Ctrl/Cmd + K` - Open ticker search
70
+ - `Escape` - Close modal
71
+
72
+ ## 6. API Examples
73
+
74
+ ### Get Stock Data
75
+ ```bash
76
+ curl http://localhost:4567/api/stock/AAPL
77
+ ```
78
+
79
+ ### Get Technical Indicators
80
+ ```bash
81
+ curl http://localhost:4567/api/indicators/AAPL
82
+ ```
83
+
84
+ ### Run Backtest
85
+ ```bash
86
+ curl -X POST http://localhost:4567/api/backtest/AAPL \
87
+ -d "strategy=RSI"
88
+ ```
89
+
90
+ ### Get Market Analysis
91
+ ```bash
92
+ curl http://localhost:4567/api/analyze/AAPL
93
+ ```
94
+
95
+ ### Compare Strategies
96
+ ```bash
97
+ curl -X POST http://localhost:4567/api/compare/AAPL
98
+ ```
99
+
100
+ ## 7. Development Mode
101
+
102
+ Auto-reload on file changes:
103
+
104
+ ```bash
105
+ bundle install # rerun is in the development group
106
+ bundle exec rerun 'ruby app.rb'
107
+ ```
108
+
109
+ ## 8. Troubleshooting
110
+
111
+ **Port already in use:**
112
+ ```bash
113
+ # Kill process on port 4567
114
+ lsof -ti:4567 | xargs kill -9
115
+ ```
116
+
117
+ **TA-Lib not found:**
118
+ ```bash
119
+ # Reinstall TA-Lib
120
+ brew reinstall ta-lib
121
+ ```
122
+
123
+ **Redis connection error:**
124
+ ```bash
125
+ # Start Redis
126
+ redis-server &
127
+ ```
128
+
129
+ **Stock data not loading:**
130
+ - Check internet connection
131
+ - Verify ticker symbol is valid
132
+ - Try a different ticker (e.g., AAPL, MSFT, GOOGL)
133
+
134
+ ## 9. Popular Tickers
135
+
136
+ Try these popular stocks:
137
+
138
+ **Tech:**
139
+ - AAPL (Apple)
140
+ - MSFT (Microsoft)
141
+ - GOOGL (Google)
142
+ - AMZN (Amazon)
143
+ - NVDA (NVIDIA)
144
+ - TSLA (Tesla)
145
+
146
+ **Finance:**
147
+ - JPM (JP Morgan)
148
+ - BAC (Bank of America)
149
+ - GS (Goldman Sachs)
150
+
151
+ **ETFs:**
152
+ - SPY (S&P 500)
153
+ - QQQ (NASDAQ 100)
154
+ - DIA (Dow Jones)
155
+
156
+ ## 10. Next Steps
157
+
158
+ - Explore different strategies on the Backtest page
159
+ - Compare seasonal patterns across multiple stocks
160
+ - Check market regime before making trade decisions
161
+ - Use FPOP analysis to identify high-probability setups
162
+
163
+ ## Support
164
+
165
+ See README.md for full documentation and API reference.
166
+
167
+ ---
168
+
169
+ Happy analyzing! 📊
@@ -0,0 +1,471 @@
1
+ # SQA Web Application
2
+
3
+ A modern web interface for the SQA (Simple Qualitative Analysis) stock analysis library. Built with Sinatra, featuring interactive charts powered by Plotly.js, and a responsive UI for comprehensive stock market analysis.
4
+
5
+ ## Features
6
+
7
+ ### 📊 Interactive Dashboard
8
+ - **Candlestick & Line Charts** - Visualize price movements with professional financial charts
9
+ - **Volume Analysis** - Track trading volume with color-coded bars
10
+ - **Technical Indicators** - RSI, MACD, SMA, EMA, Bollinger Bands
11
+ - **Key Metrics** - 52-week high/low, current RSI, market regime
12
+ - **Real-time Data** - Fetch latest stock data from Alpha Vantage or Yahoo Finance
13
+
14
+ ### 🤖 Strategy Backtesting
15
+ - **6 Built-in Strategies** - RSI, MACD, SMA, EMA, Bollinger Bands, KBS
16
+ - **Detailed Metrics** - Total return, Sharpe ratio, max drawdown, win rate
17
+ - **Strategy Comparison** - Compare all strategies side-by-side
18
+ - **Performance Analytics** - Profit factor, average win/loss, total trades
19
+
20
+ ### 📈 Market Analysis
21
+ - **Market Regime Detection** - Identify bull/bear/sideways markets
22
+ - **Seasonal Patterns** - Discover best months and quarters for trading
23
+ - **FPOP Analysis** - Future Period Loss/Profit projections
24
+ - **Risk Metrics** - VaR, Sharpe ratio, maximum drawdown
25
+
26
+ ### 🎨 Modern UI/UX
27
+ - **Responsive Design** - Works on desktop, tablet, and mobile
28
+ - **Dark Navigation** - Professional gradient navbar
29
+ - **Interactive Cards** - Hover effects and smooth transitions
30
+ - **Keyboard Shortcuts** - Ctrl/Cmd+K for quick search
31
+ - **Loading States** - Spinners and progress indicators
32
+
33
+ ## Installation
34
+
35
+ ### Prerequisites
36
+
37
+ - Ruby >= 3.2
38
+ - Bundler
39
+ - TA-Lib library (for technical indicators)
40
+ - Redis (for KBS strategy)
41
+
42
+ ### Install TA-Lib
43
+
44
+ **macOS:**
45
+ ```bash
46
+ brew install ta-lib
47
+ ```
48
+
49
+ **Ubuntu/Debian:**
50
+ ```bash
51
+ sudo apt-get install ta-lib-dev
52
+ ```
53
+
54
+ **Windows:**
55
+ Download from [TA-Lib website](http://ta-lib.org/)
56
+
57
+ ### Install Dependencies
58
+
59
+ ```bash
60
+ cd examples/sinatra_app
61
+ bundle install
62
+ ```
63
+
64
+ ### Set Up API Keys
65
+
66
+ SQA supports two data sources:
67
+
68
+ **Option 1: Alpha Vantage (Recommended)**
69
+ ```bash
70
+ export AV_API_KEY="your_api_key_here"
71
+ ```
72
+ Get a free API key from [Alpha Vantage](https://www.alphavantage.co/support/#api-key)
73
+
74
+ **Option 2: Yahoo Finance**
75
+ No API key required, but less reliable and may have rate limits.
76
+
77
+ ### Start Redis (for KBS)
78
+
79
+ ```bash
80
+ redis-server
81
+ ```
82
+
83
+ ## Usage
84
+
85
+ ### Start the Application
86
+
87
+ **⚠️ IMPORTANT:** Always use `bundle exec` to run the app:
88
+
89
+ ```bash
90
+ bundle exec ruby app.rb
91
+ ```
92
+
93
+ **Or use the startup script:**
94
+
95
+ ```bash
96
+ ./start.sh
97
+ ```
98
+
99
+ **Why `bundle exec`?** Running `ruby app.rb` without `bundle exec` may fail to load required gems, causing the dashboard to not work properly (buttons won't respond, charts won't load, API calls will fail).
100
+
101
+ Or using Rack:
102
+ ```bash
103
+ bundle exec rackup
104
+ ```
105
+
106
+ The application will start on `http://localhost:4567`
107
+
108
+ ### Navigate the App
109
+
110
+ 1. **Home Page** - Quick access to popular stocks
111
+ 2. **Search** - Enter any ticker symbol (e.g., AAPL, MSFT, GOOGL)
112
+ 3. **Dashboard** - View charts and indicators
113
+ 4. **Analysis** - Market regime, seasonal patterns, FPOP
114
+ 5. **Backtest** - Test trading strategies
115
+
116
+ ### Keyboard Shortcuts
117
+
118
+ - `Ctrl/Cmd + K` - Open ticker search modal
119
+ - `Escape` - Close modal
120
+
121
+ ## API Endpoints
122
+
123
+ ### Stock Data
124
+
125
+ ```http
126
+ GET /api/stock/:ticker
127
+ ```
128
+
129
+ Returns stock price data with OHLCV (Open, High, Low, Close, Volume).
130
+
131
+ **Response:**
132
+ ```json
133
+ {
134
+ "ticker": "AAPL",
135
+ "current_price": 175.50,
136
+ "change": 2.30,
137
+ "change_percent": 1.33,
138
+ "high_52w": 198.23,
139
+ "low_52w": 124.17,
140
+ "dates": ["2023-01-01", "2023-01-02", ...],
141
+ "open": [170.0, 171.5, ...],
142
+ "high": [172.0, 173.0, ...],
143
+ "low": [169.5, 170.8, ...],
144
+ "close": [171.0, 172.3, ...],
145
+ "volume": [50000000, 48000000, ...]
146
+ }
147
+ ```
148
+
149
+ ### Technical Indicators
150
+
151
+ ```http
152
+ GET /api/indicators/:ticker
153
+ ```
154
+
155
+ Returns calculated technical indicators.
156
+
157
+ **Response:**
158
+ ```json
159
+ {
160
+ "dates": ["2023-01-01", ...],
161
+ "rsi": [45.2, 48.1, ...],
162
+ "macd": [0.5, 0.7, ...],
163
+ "macd_signal": [0.4, 0.6, ...],
164
+ "macd_hist": [0.1, 0.1, ...],
165
+ "bb_upper": [180.0, ...],
166
+ "bb_middle": [175.0, ...],
167
+ "bb_lower": [170.0, ...],
168
+ "sma_20": [173.5, ...],
169
+ "sma_50": [170.2, ...],
170
+ "ema_20": [174.0, ...]
171
+ }
172
+ ```
173
+
174
+ ### Backtest Strategy
175
+
176
+ ```http
177
+ POST /api/backtest/:ticker
178
+ Content-Type: application/x-www-form-urlencoded
179
+
180
+ strategy=RSI
181
+ ```
182
+
183
+ **Response:**
184
+ ```json
185
+ {
186
+ "total_return": 15.45,
187
+ "annualized_return": 12.30,
188
+ "sharpe_ratio": 1.25,
189
+ "max_drawdown": 8.50,
190
+ "win_rate": 58.33,
191
+ "total_trades": 24,
192
+ "profit_factor": 1.85,
193
+ "avg_win": 3.20,
194
+ "avg_loss": -2.10
195
+ }
196
+ ```
197
+
198
+ ### Market Analysis
199
+
200
+ ```http
201
+ GET /api/analyze/:ticker
202
+ ```
203
+
204
+ **Response:**
205
+ ```json
206
+ {
207
+ "regime": {
208
+ "type": "bull",
209
+ "volatility": "medium",
210
+ "strength": 0.75,
211
+ "trend": 15.2
212
+ },
213
+ "seasonal": {
214
+ "best_months": [10, 11, 12],
215
+ "worst_months": [6, 7, 8],
216
+ "best_quarters": [4, 1],
217
+ "has_pattern": true
218
+ },
219
+ "fpop": [...],
220
+ "risk": {
221
+ "var_95": -0.025,
222
+ "sharpe_ratio": 1.15,
223
+ "max_drawdown": 0.12
224
+ }
225
+ }
226
+ ```
227
+
228
+ ### Compare Strategies
229
+
230
+ ```http
231
+ POST /api/compare/:ticker
232
+ ```
233
+
234
+ **Response:**
235
+ ```json
236
+ [
237
+ {
238
+ "strategy": "RSI",
239
+ "return": 15.45,
240
+ "sharpe": 1.25,
241
+ "drawdown": 8.50,
242
+ "win_rate": 58.33,
243
+ "trades": 24
244
+ },
245
+ ...
246
+ ]
247
+ ```
248
+
249
+ ## Technology Stack
250
+
251
+ ### Backend
252
+ - **Sinatra** - Lightweight Ruby web framework
253
+ - **SQA Library** - Stock analysis and backtesting
254
+ - **TA-Lib** - Technical analysis indicators (via sqa-tai gem)
255
+ - **Polars** - High-performance DataFrame operations
256
+ - **Redis** - KBS blackboard persistence
257
+
258
+ ### Frontend
259
+ - **Plotly.js** - Interactive financial charts
260
+ - **Font Awesome** - Icons
261
+ - **Vanilla JavaScript** - No framework dependencies
262
+ - **CSS3** - Modern styling with gradients and animations
263
+
264
+ ## File Structure
265
+
266
+ ```
267
+ sinatra_app/
268
+ ├── app.rb # Main Sinatra application
269
+ ├── config.ru # Rack configuration
270
+ ├── Gemfile # Ruby dependencies
271
+ ├── public/
272
+ │ ├── css/
273
+ │ │ └── style.css # Application styles
274
+ │ └── js/
275
+ │ └── app.js # Client-side JavaScript
276
+ ├── views/
277
+ │ ├── layout.erb # Base template
278
+ │ ├── index.erb # Home page
279
+ │ ├── dashboard.erb # Main dashboard with charts
280
+ │ ├── analyze.erb # Analysis page
281
+ │ ├── backtest.erb # Backtesting interface
282
+ │ ├── portfolio.erb # Portfolio optimizer (coming soon)
283
+ │ └── error.erb # Error page
284
+ └── README.md # This file
285
+ ```
286
+
287
+ ## Routes
288
+
289
+ | Route | Method | Description |
290
+ |-------|--------|-------------|
291
+ | `/` | GET | Home page with quick links |
292
+ | `/dashboard/:ticker` | GET | Main dashboard for ticker |
293
+ | `/analyze/:ticker` | GET | Market analysis page |
294
+ | `/backtest/:ticker` | GET | Strategy backtesting page |
295
+ | `/portfolio` | GET | Portfolio optimization (coming soon) |
296
+ | `/api/stock/:ticker` | GET | Get stock data |
297
+ | `/api/indicators/:ticker` | GET | Get technical indicators |
298
+ | `/api/backtest/:ticker` | POST | Run strategy backtest |
299
+ | `/api/analyze/:ticker` | GET | Get market analysis |
300
+ | `/api/compare/:ticker` | POST | Compare all strategies |
301
+
302
+ ## Configuration
303
+
304
+ ### Port and Binding
305
+
306
+ Edit `app.rb`:
307
+
308
+ ```ruby
309
+ set :port, 4567
310
+ set :bind, '0.0.0.0' # Allow external connections
311
+ ```
312
+
313
+ ### Data Source
314
+
315
+ The SQA library automatically selects the data source:
316
+ 1. Alpha Vantage (if `AV_API_KEY` is set)
317
+ 2. Yahoo Finance (fallback)
318
+
319
+ ## Deployment
320
+
321
+ ### Using Rack
322
+
323
+ ```bash
324
+ rackup -p 4567
325
+ ```
326
+
327
+ ### Using Passenger (Production)
328
+
329
+ ```bash
330
+ passenger start
331
+ ```
332
+
333
+ ### Using Docker
334
+
335
+ ```dockerfile
336
+ FROM ruby:3.3
337
+ WORKDIR /app
338
+ COPY Gemfile* ./
339
+ RUN bundle install
340
+ COPY . .
341
+ EXPOSE 4567
342
+ CMD ["ruby", "app.rb"]
343
+ ```
344
+
345
+ ## Development
346
+
347
+ ### Adding New Routes
348
+
349
+ Edit `app.rb`:
350
+
351
+ ```ruby
352
+ get '/my-route/:ticker' do
353
+ ticker = params[:ticker].upcase
354
+ # ... logic
355
+ erb :my_template
356
+ end
357
+ ```
358
+
359
+ ### Adding New Views
360
+
361
+ Create `views/my_template.erb`:
362
+
363
+ ```erb
364
+ <div class="dashboard">
365
+ <h1><%= @ticker %></h1>
366
+ <!-- Your content -->
367
+ </div>
368
+ ```
369
+
370
+ ### Customizing Styles
371
+
372
+ Edit `public/css/style.css`:
373
+
374
+ ```css
375
+ :root {
376
+ --primary-color: #2196F3; /* Change primary color */
377
+ }
378
+ ```
379
+
380
+ ## Troubleshooting
381
+
382
+ ### TA-Lib Errors
383
+
384
+ ```
385
+ Error: libta-lib.so: cannot open shared object file
386
+ ```
387
+
388
+ **Solution:** Install TA-Lib library (see Installation section)
389
+
390
+ ### Redis Connection Errors
391
+
392
+ ```
393
+ Error: Could not connect to Redis
394
+ ```
395
+
396
+ **Solution:** Start Redis server: `redis-server`
397
+
398
+ ### Stock Data Not Loading
399
+
400
+ ```
401
+ Error: Failed to load data for TICKER
402
+ ```
403
+
404
+ **Solutions:**
405
+ 1. Check API key is set: `echo $AV_API_KEY`
406
+ 2. Verify ticker symbol is valid
407
+ 3. Check internet connection
408
+ 4. Try a different ticker
409
+
410
+ ### Port Already in Use
411
+
412
+ ```
413
+ Error: Address already in use - bind(2)
414
+ ```
415
+
416
+ **Solution:** Kill the process using port 4567 or change the port in `app.rb`
417
+
418
+ ## Performance Tips
419
+
420
+ 1. **Enable Caching** - Cache API responses to reduce load times
421
+ 2. **Use Alpha Vantage** - More reliable than Yahoo Finance
422
+ 3. **Limit Historical Data** - Use shorter time periods for faster loading
423
+ 4. **Optimize Charts** - Reduce data points for better rendering performance
424
+
425
+ ## Security Notes
426
+
427
+ ⚠️ **Educational Use Only** - This application is for educational purposes.
428
+
429
+ **Security Considerations:**
430
+ - API keys in environment variables (never commit to git)
431
+ - Input validation on ticker symbols
432
+ - Rate limiting for API endpoints (recommended for production)
433
+ - HTTPS for production deployments
434
+ - CSRF protection for form submissions
435
+
436
+ ## Contributing
437
+
438
+ Improvements welcome! Areas for contribution:
439
+
440
+ - [ ] Portfolio optimization interface
441
+ - [ ] Pattern discovery visualization
442
+ - [ ] Genetic programming UI
443
+ - [ ] Real-time streaming dashboard
444
+ - [ ] Multi-stock comparison
445
+ - [ ] Export data to CSV/PDF
446
+ - [ ] Dark mode toggle
447
+ - [ ] User authentication
448
+ - [ ] Saved watchlists
449
+
450
+ ## License
451
+
452
+ MIT License - Same as SQA library
453
+
454
+ ## Credits
455
+
456
+ - **SQA Library** - Stock analysis framework
457
+ - **Plotly.js** - Interactive charts
458
+ - **Sinatra** - Web framework
459
+ - **TA-Lib** - Technical analysis
460
+ - **Font Awesome** - Icons
461
+
462
+ ## Links
463
+
464
+ - [SQA Repository](https://github.com/MadBomber/sqa)
465
+ - [Plotly.js Documentation](https://plotly.com/javascript/)
466
+ - [Sinatra Documentation](http://sinatrarb.com/)
467
+ - [TA-Lib](http://ta-lib.org/)
468
+
469
+ ---
470
+
471
+ **Disclaimer:** This software is for educational and research purposes only. Do not use for actual trading without proper due diligence. The authors are not responsible for financial losses.
@@ -0,0 +1,90 @@
1
+ # Running the Sinatra App Without TA-Lib
2
+
3
+ The SQA Sinatra web app can run with **limited functionality** when the TA-Lib system library is not installed. This is useful for development, testing, or deployments where installing TA-Lib is difficult.
4
+
5
+ ## What Works Without TA-Lib
6
+
7
+ ✅ **Basic Stock Data Display**
8
+ - Loading stock price history from cached CSV files
9
+ - Displaying price charts (candlestick and line)
10
+ - Volume charts
11
+ - Price statistics (current price, 52-week high/low)
12
+
13
+ ✅ **Advanced Analysis Features**
14
+ - Market regime detection (bull/bear/sideways)
15
+ - Seasonal pattern analysis
16
+ - Risk metrics (VaR, Sharpe ratio, max drawdown)
17
+ - FPOP (Future Period Profit/Loss) analysis
18
+
19
+ ✅ **Strategy Comparison**
20
+ - Comparing different trading strategies via backtesting
21
+
22
+ ## What Doesn't Work Without TA-Lib
23
+
24
+ ❌ **Technical Indicators**
25
+ - RSI (Relative Strength Index)
26
+ - MACD (Moving Average Convergence Divergence)
27
+ - Bollinger Bands
28
+ - Moving Averages (SMA, EMA)
29
+ - All other TA-Lib indicators (150+ total)
30
+
31
+ These endpoints will return errors:
32
+ - `GET /api/indicators/:ticker` - Returns error when indicators are called
33
+
34
+ ## Running the App
35
+
36
+ ### With Cached Data (No API Key Required)
37
+
38
+ 1. Ensure you have cached stock data in `~/sqa_data/`:
39
+ ```
40
+ ~/sqa_data/
41
+ ├── aapl.csv # Price history
42
+ ├── aapl.json # Stock metadata
43
+ ```
44
+
45
+ 2. Install dependencies:
46
+ ```bash
47
+ cd examples/sinatra_app
48
+ bundle install
49
+ ```
50
+
51
+ 3. Start the server:
52
+ ```bash
53
+ bundle exec ruby app.rb
54
+ ```
55
+
56
+ 4. Visit http://localhost:4567
57
+
58
+ ### Installing TA-Lib (Optional)
59
+
60
+ To enable technical indicators, install the TA-Lib system library:
61
+
62
+ **macOS:**
63
+ ```bash
64
+ brew install ta-lib
65
+ ```
66
+
67
+ **Ubuntu/Debian:**
68
+ ```bash
69
+ wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz
70
+ tar -xzf ta-lib-0.4.0-src.tar.gz
71
+ cd ta-lib/
72
+ ./configure --prefix=/usr
73
+ make
74
+ sudo make install
75
+ sudo ldconfig
76
+ ```
77
+
78
+ **Note:** After installing TA-Lib, you may need to reinstall the gems:
79
+ ```bash
80
+ bundle install
81
+ ```
82
+
83
+ ## Error Handling
84
+
85
+ The app gracefully handles missing indicators by:
86
+ - Starting successfully without TA-Lib
87
+ - Showing helpful error messages when indicator endpoints are accessed
88
+ - Allowing all non-indicator features to work normally
89
+
90
+ If you see errors about indicators, it's expected behavior when TA-Lib is not available.