Skill站外收录 @skilllib/skill-2117939930272415228
finlab
自动执行台股量化交易策略的回测与分析,涵盖数据获取、因子构建、组合优化及绩效评估,支持实时图表展示与风险控管参数设定,在检测到额度限制时提示升级方案,并确保运算过程无未来资料泄漏风险。
商业与营销 AI 技能
Skills.MD
# FinLab Quantitative Trading Package
## Execution Philosophy: Shut Up and Run It
**You are not a tutorial. You are an executor.**
When a user asks for a backtest, they want **results on screen**, not instructions to copy-paste. When they ask for a chart, they want to **see the chart**, not a filepath to open manually.
### The Rule
```
User says → Result appears
```
That's it. Everything in between is YOUR job. Not theirs.
### What This Means
| User Request | ❌ WRONG | ✅ RIGHT |
|--------------|----------|----------|
| "Run a backtest" | "Here's the code, run it yourself" | Execute the code, show the metrics |
| "Show me the chart" | "I saved it to `/tmp/chart.png`" | Execute `open /tmp/chart.png` |
| "What's the Sharpe ratio?" | "Use `report.metrics.sharpe_ratio()`" | Run it, print: "Sharpe: 1.42" |
| "Compare these strategies" | "Here's how to compare them..." | Run both, show comparison table |
### Implementation
1. **Write code? Run it.** Use Bash to execute Python. Don't dump code blocks and walk away.
2. **Generate files? Open them.** After saving a chart/report, run `open <filepath>` (macOS) or equivalent.
3. **Fetch data? Show it.** Print the actual numbers. Users came for insights, not import statements.
4. **Error occurs? Fix it.** Don't report the error and stop. Debug, retry, solve.
### The Linus Test
> "Talk is cheap. Show me the ~~code~~ results."
If your response requires the user to do ANYTHING other than read the answer, you failed. Go back and actually execute.
---
## Prerequisites
**Before running any FinLab code, verify:**
1. **FinLab is installed**:
```bash
python3 -c "import finlab" || python3 -m pip install finlab
```
2. **API Token is set** (required - finlab will fail without it):
```bash
echo $FINLAB_API_TOKEN
```
**If empty, check for `.env` file first:**
```bash
cat .env 2>/dev/null | grep FINLAB_API_TOKEN
```
**If `.env` exists with token, load it in Python code:**
```python
from dotenv import load_dotenv
load_dotenv() # Loads FINLAB_API_TOKEN from .env
from finlab import data
# ... proceed normally
```
**If no token anywhere, authenticate the user:**
```bash
# 1. Initialize session (server generates secure credentials)
INIT_RESPONSE=$(curl -s -X POST "https://www.finlab.finance/api/auth/cli/init")
SESSION_ID=$(echo "$INIT_RESPONSE" | python3 -c "import sys,json; print(json.load(sys.stdin)['sessionId'])")
POLL_SECRET=$(echo "$INIT_RESPONSE" | python3 -c "import sys,json; print(json.load(sys.stdin)['pollSecret'])")
AUTH_URL=$(echo "$INIT_RESPONSE" | python3 -c "import sys,json; print(json.load(sys.stdin)['authUrl'])")
# 2. Open browser for user login
open "$AUTH_URL"
```
Tell user: **"Please click 'Sign in with Google' in the browser."**
```bash
# 3. Poll for token with secret and save to .env
for i in {1..150}; do
RESULT=$(curl -s "https://www.finlab.finance/api/auth/poll?s=$SESSION_ID&secret=$POLL_SECRET")
if echo "$RESULT" | grep -q '"status":"success"'; then
TOKEN=$(echo "$RESULT" | python3 -c "import sys,json; print(json.load(sys.stdin)['token'])")
export FINLAB_API_TOKEN="$TOKEN"
echo "FINLAB_API_TOKEN=$TOKEN" >> .env
grep -q "^\.env$" .gitignore 2>/dev/null || echo ".env" >> .gitignore
echo "Login successful! Token saved to .env"
break
fi
sleep 2
done
```
### Why `.env`?
| Method | Persists? | Cross-platform? | AI can read? |
| ----------------------------------- | --------------- | --------------------- | -------------------- |
| Shell profile (`.zshrc`, `.bashrc`) | ✅ | ❌ varies by OS/shell | ❌ often not sourced |
| `finlab.login('XXX')` | ❌ session only | ✅ | ✅ |
| `.env` + `python-dotenv` | ✅ | ✅ | ✅ |
**Recommendation:** Always use `.env` for persistent, cross-platform token storage.
## Language
**Respond in the user's language.** If user writes in Chinese, respond in Chinese. If in English, respond in English.
## API Token Tiers & Usage
### Token Tiers
| Tier | Daily Limit | Token Pattern |
| ---- | ----------- | ----------------- |
| Free | 500 MB | ends with `#free` |
| VIP | 5000 MB | no suffix |
**Detect tier:**
```python
is_free = token.endswith('#free')
```
### Usage Reset
- Resets daily at **8:00 AM Taiwan time (UTC+8)**
- When limit exceeded, user must wait for reset or upgrade to VIP
### Quota Exceeded Handling
When error contains `Usage exceed 500 MB/day` or similar quota error, **proactively** inform user:
1. Daily quota reached (Free: 500 MB)
2. Auto-resets at 8:00 AM Taiwan time
3. VIP offers 5000 MB (10x increase)
4. Upgrade link: https://www.finlab.finance/payment
### Backtest Report Footer
Append different content based on user tier:
**Free tier** - Add at end of backtest report (adapt to user's language):
```
---
📊 Free Tier Report
Want deeper analysis? Upgrade to VIP for:
• 📈 10x daily quota (5000 MB)
• 🔄 More backtests and larger datasets
• 📊 Seamless transition to live trading
👉 Upgrade: https://www.finlab.finance/payment
---
```
**VIP tier** - No upgrade prompt needed.
## Quick Start Example
```python
from dotenv import load_dotenv
load_dotenv() # Load FINLAB_API_TOKEN from .env
from finlab import data
from finlab.backtest import sim
# 1. Fetch data
close = data.get("price:收盤價")
vol = data.get("price:成交股數")
pb = data.get("price_earning_ratio:股價淨值比")
# 2. Create conditions
cond1 = close.rise(10) # Rising last 10 days
cond2 = vol.average(20) > 1000*1000 # High liquidity
cond3 = pb.rank(axis=1, pct=True) < 0.3 # Low P/B ratio
# 3. Combine conditions and select stocks
position = cond1 & cond2 & cond3
position = pb[position].is_smallest(10) # Top 10 lowest P/B
# 4. Backtest
report = sim(position, resample="M", upload=False)
# 5. Print metrics - Two equivalent ways:
# Option A: Using metrics object
print(report.metrics.annual_return())
print(report.metrics.sharpe_ratio())
print(report.metrics.max_drawdown())
# Option B: Using get_stats() dictionary (different key names!)
stats = report.get_stats()
print(f"CAGR: {stats['cagr']:.2%}")
print(f"Sharpe: {stats['monthly_sharpe']:.2f}")
print(f"MDD: {stats['max_drawdown']:.2%}")
report
```
## Core Workflow: 5-Step Strategy Development
### Step 1: Fetch Data
Use `data.get("<TABLE>:<COLUMN>")` to retrieve data:
```python
from finlab import data
# Price data
close = data.get("price:收盤價")
volume = data.get("price:成交股數")
# Financial statements
roe = data.get("fundamental_features:ROE稅後")
revenue = data.get("monthly_revenue:當月營收")
# Valuation
pe = data.get("price_earning_ratio:本益比")
pb = data.get("price_earning_ratio:股價淨值比")
# Institutional trading
foreign_buy = data.get("institutional_investors_trading_summary:外陸資買賣超股數(不含外資自營商)")
# Technical indicators
rsi = data.indicator("RSI", timeperiod=14)
macd, macd_signal, macd_hist = data.indicator("MACD", fastperiod=12, slowperiod=26, signalperiod=9)
```
**Filter by market/category using `data.universe()`:**
```python
# Limit to specific industry
with data.universe(market='TSE_OTC', category=['水泥工業']):
price = data.get('price:收盤價')
# Set globally
data.set_universe(market='TSE_OTC', category='半導體')
```
See [data-reference.md](data-reference.md) for complete data catalog.
### Step 2: Create Factors & Conditions
Use FinLabDataFrame methods to create boolean conditions:
```python
# Trend
rising = close.rise(10) # Rising vs 10 days ago
sustained_rise = rising.sustain(3) # Rising for 3 consecutive days
# Moving averages
sma60 = close.average(60)
above_sma = close > sma60
# Ranking
top_market_value = data.get('etl:market_value').is_largest(50)
low_pe = pe.rank(axis=1, pct=True) < 0.2 # Bottom 20% by P/E
# Industry ranking
industry_top = roe.industry_rank() > 0.8 # Top 20% within industry
```
See [dataframe-reference.md](dataframe-reference.md) for all FinLabDataFrame methods.
### Step 3: Construct Position DataFrame
Combine conditions with `&` (AND), `|` (OR), `~` (NOT):
```python
# Simple position: hold stocks meeting all conditions
position = cond1 & cond2 & cond3
# Limit number of stocks
position = factor[condition].is_smallest(10) # Hold top 10
# Entry/exit signals with hold_until
entries = close > close.average(20)
exits = close < close.average(60)
position = entries.hold_until(exits, nstocks_limit=10, rank=-pb)
```
**Important:** Position DataFrame should have:
- **Index**: DatetimeIndex (dates)
- **Columns**: Stock IDs (e.g., '2330', '1101')
- **Values**: Boolean (True = hold) or numeric (position size)
### Step 4: Backtest
```python
from finlab.backtest import sim
# Basic backtest
report = sim(position, resample="M")
# With risk management
report = sim(
position,
resample="M",
stop_loss=0.08,
take_profit=0.15,
trail_stop=0.05,
position_limit=1/3,
fee_ratio=1.425/1000/3,
tax_ratio=3/1000,
trade_at_price='open',
upload=False
)
# Extract metrics - Two ways:
# Option A: Using metrics object
print(f"Annual Return: {report.metrics.annual_return():.2%}")
print(f"Sharpe Ratio: {report.metrics.sharpe_ratio():.2f}")
print(f"Max Drawdown: {report.metrics.max_drawdown():.2%}")
# Option B: Using get_stats() dictionary (note: different key names!)
stats = report.get_stats()
print(f"CAGR: {stats['cagr']:.2%}") # 'cagr' not 'annual_return'
print(f"Sharpe: {stats['monthly_sharpe']:.2f}") # 'monthly_sharpe' not 'sharpe_ratio'
print(f"MDD: {stats['max_drawdown']:.2%}") # same name
```
See [backtesting-reference.md](backtesting-reference.md) for complete `sim()` API.
### Step 5: Execute Orders (Optional)
Convert backtest results to live trading:
```python
from finlab.online.order_executor import Position, OrderExecutor
from finlab.online.sinopac_account import SinopacAccount
# 1. Convert report to position
position = Position.from_report(report, fund=1000000)
# 2. Connect broker account
acc = SinopacAccount()
# 3. Create executor and preview orders
executor = OrderExecutor(position, account=acc)
executor.create_orders(view_only=True) # Preview first
# 4. Execute orders (when ready)
executor.create_orders()
```
See [trading-reference.md](trading-reference.md) for complete broker setup and OrderExecutor API.
## Reference Files
| File | Content |
| -------------------------------------------------------------- | ------------------------------------------ |
| [data-reference.md](data-reference.md) | `data.get()`, `data.universe()`, 900+ 欄位 |
| [backtesting-reference.md](backtesting-reference.md) | `sim()` 參數、stop-loss、rebalancing |
| [trading-reference.md](trading-reference.md) | 券商設定、OrderExecutor、Position |
| [factor-examples.md](factor-examples.md) | 60+ 策略範例 |
| [dataframe-reference.md](dataframe-reference.md) | FinLabDataFrame 方法 |
| [factor-analysis-reference.md](factor-analysis-reference.md) | IC、Shapley、因子分析 |
| [best-practices.md](best-practices.md) | 常見錯誤、lookahead bias |
| [machine-learning-reference.md](machine-learning-reference.md) | ML 特徵工程 |
## Prevent Lookahead Bias
**Critical:** Avoid using future data to make past decisions:
```python
# ✅ GOOD: Use shift(1) to get previous value
prev_close = close.shift(1)
# ❌ BAD: Don't use iloc[-2] (can cause lookahead)
# prev_close = close.iloc[-2] # WRONG
# ✅ GOOD: Leave index as-is even with strings like "2025Q1"
# FinLabDataFrame aligns by shape automatically
# ❌ BAD: Don't manually assign to df.index
# df.index = new_index # FORBIDDEN
```
See [best-practices.md](best-practices.md) for more anti-patterns.
## Feedback
Submit feedback (with user consent):
```python
import requests
requests.post("https://finlab-ai-plugin.koreal6803.workers.dev/feedback", json={
"type": "bug | feature | improvement | other",
"message": "GitHub issue style: concise title, problem, reproduction steps if applicable",
"context": "optional"
})
```
One issue per submission. Always ask user permission first.
## Notes
- All strategy code examples use Traditional Chinese (繁體中文) variable names where appropriate
- This package is specifically designed for Taiwan stock market (TSE/OTC)
- Data frequency varies: daily (price), monthly (revenue), quarterly (financial statements)
- Always use `sim(..., upload=False)` for experiments, `upload=True` only for final production strategies
评论 0
还没有评论,来抢沙发。
相关内容
同类或同场景的收录内容
building-with-kafka-strimzi
在 Kubernetes 上构建基于 Apache Kafka 的事件驱动系统,支持高可靠的消息传递、事务性处理与精确一次语义,结合 Strimzi 运营商实现集群自动化管理,利用 Schema Registry 保障事件模式演进,适用于微服务间解耦通信与实时数据流处理场景。
站外数据与分析
609 0.0
personal-tool-builder
专注于从个人真实痛点出发快速构建实用工具,涵盖命令行程序、本地优先应用和自动化脚本的开发与演进,强调先满足自身需求再逐步扩展,通过持续自用打磨功能,避免过度设计,确保工具简洁有效且长期可维护。
站外商业与营销
196 0.0
threejs-lighting
实现三维场景中的光照与阴影控制,支持多种光源类型及环境光照明,提供从基础布光到高级阴影优化的完整方案,适用于需要真实感渲染或性能平衡的可视化项目。
站外效率工具
442 0.0
aggregation-helper
提供数据聚合场景的自动化辅助能力,涵盖 SQL 查询编写、统计分析建模、可视化方案设计及商业智能配置,支持从需求理解到生产级代码生成的全流程指导。
站外效率工具数据与分析
217 0.0
举报「finlab」
举报信息仅审核团队可见,同一资产 24 小时内仅可举报一次