設定與路徑¶
設定的來源與優先順序、可用的設定,以及 CJTrade 寫入檔案的目錄。
設定從哪裡來¶
設定為 KEY = value 形式。每個鍵依下列順序查找,採用第一個找到的值:
- 環境變數(
export API_KEY=...,或API_KEY=... command), - 工作目錄中的
.env檔, - 工作目錄或其子目錄中的任何
*.cjconf檔(層級較淺的目錄優先,同層依字母 順序), - 使用者設定目錄中的
*.cjconf檔(見路徑), - CJTrade 內附的預設值(
*.cjsys檔)。
因此 export 的變數優先於任何檔案,專案中的檔案優先於使用者設定目錄中的檔案。
*.cjconf 檔存放個人設定和機密資料,不應納入 git(repository 的 .gitignore
已排除)。以 bash scripts/gen_config.sh arenax 或 ... sinopac 產生初始設定檔。
某個值是從哪個檔案來的?
cjtrade.pkgs.config.config_loader.describe_sources() 會印出每項設定及其
來源檔案或變數。
券商設定(*.cjconf)¶
| 鍵 | 適用對象 | 說明 |
|---|---|---|
API_KEY |
ArenaX、Sinopac | ArenaX:testkey123。Sinopac:你的 API key |
SECRET_KEY |
Sinopac | 你的 API secret |
CA_CERT_PATH |
Sinopac | Sinopac.pfx 憑證的路徑 |
CA_PASSWORD |
Sinopac | 憑證的密碼 |
SIMULATION |
Sinopac | y 為永豐的沙盒(模擬環境),n 為你的正式帳戶(產生出來的檔案是 n) |
LLM_MODEL、LLM_API_KEY、GEMINI_API_KEY |
LLM 用戶端、CJShell llm |
使用的模型及其金鑰 |
FINMIND_TOKEN |
FinMind 價格來源 | 選填;可提高請求次數上限 |
CJTrade 全域變數¶
| 變數 | 說明 |
|---|---|
CJTRADE_HOME |
下方「路徑」表中所有目錄的根目錄 |
CJTRADE_OFFLINE=1 |
網路價格來源直接失敗,不嘗試連線 |
CJTRADE_LOG_LEVEL |
DEBUG、INFO(預設)、WARNING、... |
ARENAX_PORT |
AccountClient 連到 ArenaX 的埠(預設 8801) |
I_UNDERSTAND_REAL_TRADING_RISKS=y |
啟動 arenaxd --backend=real 時必須設定 |
ArenaX 伺服器設定(AX_*)¶
各 backend 的預設值位於 src/cjtrade/apps/ArenaX/configs/<backend>.cjsys;
設定對應的變數即可覆寫。
| 變數 | 預設值(demo) | 說明 |
|---|---|---|
AX_HOST_PORT |
8801 | 埠(backtest 8802、paper 8803、real 8888) |
AX_DATA_SOURCES |
yfinance |
價格來源,同 --data |
AX_PRICE_DB_PATH |
共用快取 | 價格檔,同 --price-db |
AX_INITIAL_BALANCE |
100000 | 新帳戶的起始現金 |
AX_STATE_FILE |
從此帳戶快照接續,而非建立全新帳戶 | |
AX_BACKTEST_DURATION_DAYS |
11(backtest 300) | 從開始日期起要載入幾天的價格 |
AX_BACKTEST_PLAYBACK_SPEED |
120 | 每實際一秒前進的市場秒數 |
AX_SKIP_NON_TRADING_HOURS |
y |
跳過夜間和週末 |
AX_COMMISSION_DISCOUNT |
1.0 | 手續費乘數;0.6 代表手續費打六折 |
AX_SLIPPAGE_MODEL、AX_SLIPPAGE_PCT |
none、0.0005 |
滑價模型和幅度(尚未套用到 ArenaX 的成交) |
FinHub 設定(FINHUB_*)¶
路徑¶
| 資料夾 | 存放內容 | macOS | Linux |
|---|---|---|---|
| config | *.cjconf、finhub.env |
~/Library/Application Support/cjtrade |
~/.config/cjtrade |
| data | 報告、帳戶快照、FinHub 的資料庫 | ~/Library/Application Support/cjtrade |
~/.local/share/cjtrade |
| cache | 價格快取 arenax_price.db |
~/Library/Caches/cjtrade |
~/.cache/cjtrade |
| state、logs | 執行期狀態、日誌檔 | ~/Library/Application Support/cjtrade、~/Library/Logs/cjtrade |
~/.local/state/cjtrade、~/.local/state/cjtrade/log |
設定 CJTRADE_HOME=/some/path 後,這些資料夾會變成 /some/path/config、
/some/path/data、/some/path/cache、/some/path/state 和
/some/path/logs。arenaxd 啟動時會印出使用的目錄。
API¶
Warning
load_supported_config_files() 會寫入整個 process 的 os.environ(先找到的
來源優先),paths 的輔助函式第一次被呼叫時會建立目錄。
cjtrade.pkgs.config.config_loader
¶
Loading of .cjconf configuration files.
ONE RULE
The first source to set a key wins. Later sources cannot overwrite it.
That is load_dotenv(override=False), and it applies uniformly to every
source below.
So the search order is the precedence order, most specific first:
0. environment variables already set (set before we run, so nothing
in a file can displace them)
1. ./.env
2. ./**/*.cjconf shallower before deeper,
alphabetical within a depth
3. <user config dir>/*.cjconf see pkgs.paths.config_dir()
Bundled *.cjsys defaults are not loaded here. Apps load them separately
(brokerside_server.load_cjsys) after calling this, which under the
first-wins rule makes package defaults the weakest source of all -- correct for
defaults, but worth knowing when a .cjsys value appears to be ignored.
Differences from git, deliberately kept for now
git config applies the least specific scope first and lets later ones
overwrite, so the most specific wins by being applied last. Here the most
specific wins by being applied first. The resolved value usually agrees, but
the mechanism is inverted, and flipping it would silently change what every
existing deployment resolves to. Tracked in ref/TODO.md under "Config provenance",
together with recording which file each key came from.
Within a directory the order used to be whatever rglob returned, i.e. the
filesystem's readdir order -- so two .cjconf files setting the same key
resolved differently on different machines. _discover now sorts by depth
then path, which is deterministic and matches git's local-beats-parent
intuition.
CJTradeConfiguration
¶
Placeholder for a hierarchical configuration object.
Warnings
Not implemented; the class is empty.
default_config_paths()
¶
Search roots, most specific first. Resolved lazily.
Not a module-level constant: paths.config_dir() creates the directory,
and evaluating this at import time would create it just by importing.
Warnings
Calling this creates the user config directory if it does not exist.
Source code in src/cjtrade/pkgs/config/config_loader.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | |
config_sources(paths_=_UNSET, recursive_pattern=SUPPORTED_FILE_PATTERN)
¶
The files that would be loaded, in precedence order. Loads nothing.
Useful on its own for answering "which files is this process reading?", and it is the ordering the provenance work in ref/TODO.md will attach sources to.
Pass None to mean "no sources at all"; omit the argument for the defaults.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
paths_
|
iterable of str or Path
|
Search roots: files, directories or glob patterns. |
_UNSET
|
recursive_pattern
|
str
|
Glob applied when a root is a directory. |
SUPPORTED_FILE_PATTERN
|
Returns:
| Type | Description |
|---|---|
list of Path
|
Each resolved file appears once, at its first position. |
Warnings
With the default roots, creates the user config directory if missing.
Source code in src/cjtrade/pkgs/config/config_loader.py
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 | |
load_supported_config_files(paths=_UNSET, recursive_pattern=SUPPORTED_FILE_PATTERN, override=False)
¶
Load every discovered config file into os.environ.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
paths
|
iterable of str or Path
|
Search roots, most specific first. Omit for
|
_UNSET
|
recursive_pattern
|
str
|
Glob applied when a root is a directory. |
SUPPORTED_FILE_PATTERN
|
override
|
bool
|
Leave at False to keep the first-wins rule. Passing True inverts the precedence of every source at once, so do it knowingly. |
False
|
Returns:
| Type | Description |
|---|---|
list of str
|
The loaded file paths as strings, in the order they were applied. |
Warnings
Mutates os.environ for the whole process. With the default roots,
also creates the user config directory if missing.
Source code in src/cjtrade/pkgs/config/config_loader.py
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 | |
describe_sources(paths=_UNSET, recursive_pattern=SUPPORTED_FILE_PATTERN)
¶
Human-readable listing of the search order, for diagnostics.
Warnings
With the default roots, creates the user config directory if missing.
Source code in src/cjtrade/pkgs/config/config_loader.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | |
cjtrade.pkgs.paths
¶
Where CJTrade keeps user data, config, cache, state and logs.
Single source of truth for every path that is not source code. Importing this instead of writing a literal keeps three things true:
-
It is cross-platform. Hard-coding "./data/..." puts user data inside whatever directory the process happened to start in -- usually the repo, so a trading account's mirror DB ends up next to the code that reads it, and a
git cleancan delete it. Hard-coding "~/.config/..." is wrong on macOS and Windows, which do not use XDG. -
It is relocatable. CJTRADE_HOME overrides everything, which is how you run two independent instances (say a paper and a backtest one) without them sharing a price cache or an account state.
-
The categories mean something. platformdirs separates data (keep), cache (safe to delete, will be refetched), state (machine-local, survives reboot) and logs. Putting a 160MB refetchable price cache under "data" is why backups and
duget confusing.
Typical locations
macOS ~/Library/Application Support/cjtrade
~/Library/Caches/cjtrade
Linux ~/.local/share/cjtrade (XDG_DATA_HOME)
~/.cache/cjtrade (XDG_CACHE_HOME)
Windows %LOCALAPPDATA%\cjtrade
Override with CJTRADE_HOME=/some/path, which puts every category under it as data/, config/, cache/, state/ and logs/ -- handy for tests and for keeping a run entirely self-contained.
data_dir(*, ensure=True)
¶
Durable user data: account states, mirror DBs. Losing this loses history.
Warnings
Creates the directory unless ensure=False.
Source code in src/cjtrade/pkgs/paths.py
63 64 65 66 67 68 69 70 | |
config_dir(*, ensure=True)
¶
User config: .cjconf files and anything else the user edits by hand.
Warnings
Creates the directory unless ensure=False.
Source code in src/cjtrade/pkgs/paths.py
73 74 75 76 77 78 79 80 | |
cache_dir(*, ensure=True)
¶
Refetchable data: the ArenaX price cache. Safe to delete at any time.
Warnings
Creates the directory unless ensure=False.
Source code in src/cjtrade/pkgs/paths.py
83 84 85 86 87 88 89 90 | |
state_dir(*, ensure=True)
¶
Machine-local state that is not user-authored: saved form answers.
Warnings
Creates the directory unless ensure=False.
Source code in src/cjtrade/pkgs/paths.py
93 94 95 96 97 98 99 100 | |
log_dir(*, ensure=True)
¶
Log files.
Warnings
Creates the directory unless ensure=False.
Source code in src/cjtrade/pkgs/paths.py
103 104 105 106 107 108 109 110 | |
price_db_path()
¶
ArenaX historical price cache. Cache, not data: it is refetchable.
Warnings
Creates the cache directory if missing (not the file).
Source code in src/cjtrade/pkgs/paths.py
116 117 118 119 120 121 122 123 | |
mirror_db_path(broker, username='')
¶
Local mirror of a broker's orders and fills.
Data, not cache: CJ_OrderMap here is the only link between a CJTrade order id and the broker's own id, so losing it orphans live orders.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
broker
|
str
|
Broker name. |
required |
username
|
str
|
If given, the file is |
''
|
Warnings
Creates the data directory if missing, but not mirrors/ or the file.
Source code in src/cjtrade/pkgs/paths.py
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | |
account_states_dir()
¶
Per-session account snapshots, one immutable uuid-named file each.
Warnings
Creates the directory if missing.
Source code in src/cjtrade/pkgs/paths.py
147 148 149 150 151 152 153 154 155 156 | |
reports_dir()
¶
Backtest reports: HTML tearsheets and pickled BacktestResults.
Data, not cache: a report describes one run of a strategy against one account state, and neither can be reconstructed once the account has moved on -- so unlike the price cache, deleting this loses something.
Warnings
Creates the directory if missing.
Source code in src/cjtrade/pkgs/paths.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 | |
form_state_dir()
¶
Remembered answers for pkgs.ui forms, keyed by schema stem.
Warnings
Creates the directory if missing.
Source code in src/cjtrade/pkgs/paths.py
175 176 177 178 179 180 181 182 183 184 | |
log_path(name)
¶
A named log file, e.g. log_path("shioaji.log").
Warnings
Creates the log directory if missing (not the file).
Source code in src/cjtrade/pkgs/paths.py
187 188 189 190 191 192 193 194 | |
describe()
¶
Human-readable summary of every location, for diagnostics.
cjtrade subcommands print this so a user reporting a problem can say
where their data actually lives instead of guessing.
Source code in src/cjtrade/pkgs/paths.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | |
legacy_data_warning()
¶
Warn when the pre-platformdirs ./data directory still holds files.
Deliberately does not move anything: the price cache runs into the hundreds of megabytes and has live -shm/-wal sidecars, so relocating it underneath a running process is a good way to corrupt it. Tell the user the command instead and let them pick the moment.
Source code in src/cjtrade/pkgs/paths.py
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | |