|
| 1 | +# Fork Guide: Strategy Switch Console |
| 2 | + |
| 3 | +This guide explains how to fork this repository and deploy the same public-readonly, login-to-switch console for your own platform repositories. |
| 4 | + |
| 5 | +## What You Need |
| 6 | + |
| 7 | +- A GitHub account or organization that owns your forked runtime settings repository. |
| 8 | +- Optional platform repositories for LongBridge, IBKR, Schwab, and Firstrade automation. |
| 9 | +- A Cloudflare account with Workers enabled. |
| 10 | +- A GitHub OAuth App for login. |
| 11 | +- A fine-grained GitHub token that can dispatch this repository's workflow. |
| 12 | +- A separate GitHub Actions secret that can write variables in your platform repositories. |
| 13 | + |
| 14 | +Do not commit broker credentials, cloud credentials, API keys, account passwords, or personal access tokens. |
| 15 | + |
| 16 | +## Repository Mapping |
| 17 | + |
| 18 | +The default repository mapping points to QuantStrategyLab: |
| 19 | + |
| 20 | +```text |
| 21 | +longbridge -> QuantStrategyLab/LongBridgePlatform |
| 22 | +ibkr -> QuantStrategyLab/InteractiveBrokersPlatform |
| 23 | +schwab -> QuantStrategyLab/CharlesSchwabPlatform |
| 24 | +firstrade -> QuantStrategyLab/FirstradePlatform |
| 25 | +``` |
| 26 | + |
| 27 | +Fork users can override these without editing source code. |
| 28 | + |
| 29 | +For GitHub Actions, set platform repository variables in your fork: |
| 30 | + |
| 31 | +```text |
| 32 | +RUNTIME_SETTINGS_LONGBRIDGE_REPO=your-org/LongBridgePlatform |
| 33 | +RUNTIME_SETTINGS_IBKR_REPO=your-org/InteractiveBrokersPlatform |
| 34 | +RUNTIME_SETTINGS_SCHWAB_REPO=your-org/CharlesSchwabPlatform |
| 35 | +RUNTIME_SETTINGS_FIRSTRADE_REPO=your-org/FirstradePlatform |
| 36 | +``` |
| 37 | + |
| 38 | +You can also use one JSON variable: |
| 39 | + |
| 40 | +```json |
| 41 | +{ |
| 42 | + "longbridge": "your-org/LongBridgePlatform", |
| 43 | + "ibkr": "your-org/InteractiveBrokersPlatform", |
| 44 | + "schwab": "your-org/CharlesSchwabPlatform", |
| 45 | + "firstrade": "your-org/FirstradePlatform" |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +Store that JSON as `RUNTIME_SETTINGS_PLATFORM_REPOSITORIES_JSON`. |
| 50 | + |
| 51 | +For the Cloudflare Worker, use the same JSON as `STRATEGY_SWITCH_PLATFORM_REPOSITORIES_JSON`, or set individual Worker variables: |
| 52 | + |
| 53 | +```text |
| 54 | +RUNTIME_SETTINGS_REPO=your-org/QuantRuntimeSettings |
| 55 | +STRATEGY_SWITCH_LONGBRIDGE_REPO=your-org/LongBridgePlatform |
| 56 | +STRATEGY_SWITCH_IBKR_REPO=your-org/InteractiveBrokersPlatform |
| 57 | +STRATEGY_SWITCH_SCHWAB_REPO=your-org/CharlesSchwabPlatform |
| 58 | +STRATEGY_SWITCH_FIRSTRADE_REPO=your-org/FirstradePlatform |
| 59 | +``` |
| 60 | + |
| 61 | +## GitHub Actions Setup |
| 62 | + |
| 63 | +Create a GitHub Environment named `runtime-strategy-switch`. |
| 64 | + |
| 65 | +Add this secret to that environment: |
| 66 | + |
| 67 | +```text |
| 68 | +RUNTIME_SETTINGS_GH_TOKEN |
| 69 | +``` |
| 70 | + |
| 71 | +This token is used by `.github/workflows/manual-strategy-switch.yml` to write GitHub Actions variables in your platform repositories and optionally dispatch each platform's sync workflow. Prefer a fine-grained PAT scoped only to the repositories you actually use. |
| 72 | + |
| 73 | +The token should not need `contents: write`. |
| 74 | + |
| 75 | +## Worker Setup |
| 76 | + |
| 77 | +Create a GitHub OAuth App: |
| 78 | + |
| 79 | +```text |
| 80 | +Homepage URL: https://your-worker-domain |
| 81 | +Authorization callback URL: https://your-worker-domain/callback |
| 82 | +``` |
| 83 | + |
| 84 | +Copy the Worker config: |
| 85 | + |
| 86 | +```bash |
| 87 | +cp web/strategy-switch-console/wrangler.toml.example web/strategy-switch-console/wrangler.toml |
| 88 | +``` |
| 89 | + |
| 90 | +Edit `wrangler.toml`: |
| 91 | + |
| 92 | +```toml |
| 93 | +name = "your-strategy-switch-console" |
| 94 | + |
| 95 | +[vars] |
| 96 | +RUNTIME_SETTINGS_REPO = "your-org/QuantRuntimeSettings" |
| 97 | +STRATEGY_SWITCH_PLATFORM_REPOSITORIES_JSON = '{"longbridge":"your-org/LongBridgePlatform","ibkr":"your-org/InteractiveBrokersPlatform","schwab":"your-org/CharlesSchwabPlatform","firstrade":"your-org/FirstradePlatform"}' |
| 98 | +``` |
| 99 | + |
| 100 | +Set Worker secrets: |
| 101 | + |
| 102 | +```bash |
| 103 | +cd web/strategy-switch-console |
| 104 | +wrangler secret put GITHUB_CLIENT_ID |
| 105 | +wrangler secret put GITHUB_CLIENT_SECRET |
| 106 | +wrangler secret put SESSION_SECRET |
| 107 | +wrangler secret put RUNTIME_SETTINGS_DISPATCH_TOKEN |
| 108 | +wrangler secret put STRATEGY_SWITCH_SYNC_TOKEN |
| 109 | +wrangler secret put ALLOWED_GITHUB_LOGINS |
| 110 | +wrangler secret put ALLOWED_GITHUB_ORGS |
| 111 | +wrangler secret put STRATEGY_SWITCH_ADMIN_LOGINS |
| 112 | +wrangler secret put STRATEGY_SWITCH_ADMIN_ORGS |
| 113 | +``` |
| 114 | + |
| 115 | +`RUNTIME_SETTINGS_DISPATCH_TOKEN` only needs permission to dispatch the runtime settings workflow in your fork. It is not the token that writes platform variables. |
| 116 | + |
| 117 | +## Account Options |
| 118 | + |
| 119 | +Copy the generic example: |
| 120 | + |
| 121 | +```bash |
| 122 | +cp web/strategy-switch-console/account-options.example.json /tmp/strategy-switch-accounts.json |
| 123 | +``` |
| 124 | + |
| 125 | +Edit it with your own route names, service names, account selectors, and supported strategy domains. Keep it to routing metadata only. |
| 126 | + |
| 127 | +Store it as a Worker secret: |
| 128 | + |
| 129 | +```bash |
| 130 | +wrangler secret put STRATEGY_SWITCH_ACCOUNT_OPTIONS_JSON < /tmp/strategy-switch-accounts.json |
| 131 | +``` |
| 132 | + |
| 133 | +For editable settings, create a KV namespace: |
| 134 | + |
| 135 | +```bash |
| 136 | +wrangler kv namespace create STRATEGY_SWITCH_CONFIG |
| 137 | +``` |
| 138 | + |
| 139 | +Add the returned id to `wrangler.toml`, deploy, then use `/admin` to edit: |
| 140 | + |
| 141 | +```text |
| 142 | +auth_config |
| 143 | +account_options |
| 144 | +strategy_profiles |
| 145 | +audit_log |
| 146 | +``` |
| 147 | + |
| 148 | +## Strategy Catalog |
| 149 | + |
| 150 | +Runtime-enabled strategies live in: |
| 151 | + |
| 152 | +```text |
| 153 | +web/strategy-switch-console/strategy-profiles.example.json |
| 154 | +``` |
| 155 | + |
| 156 | +Each item needs: |
| 157 | + |
| 158 | +```json |
| 159 | +{ |
| 160 | + "profile": "my_strategy_profile", |
| 161 | + "label": "My Strategy Profile", |
| 162 | + "domain": "us_equity", |
| 163 | + "runtime_enabled": true |
| 164 | +} |
| 165 | +``` |
| 166 | + |
| 167 | +Supported domains are currently: |
| 168 | + |
| 169 | +```text |
| 170 | +us_equity |
| 171 | +hk_equity |
| 172 | +``` |
| 173 | + |
| 174 | +After editing the strategy catalog or page: |
| 175 | + |
| 176 | +```bash |
| 177 | +python3 scripts/sync_strategy_switch_page_asset.py |
| 178 | +``` |
| 179 | + |
| 180 | +## Deploy and Verify |
| 181 | + |
| 182 | +Deploy the Worker: |
| 183 | + |
| 184 | +```bash |
| 185 | +cd web/strategy-switch-console |
| 186 | +wrangler deploy |
| 187 | +``` |
| 188 | + |
| 189 | +Verify public mode: |
| 190 | + |
| 191 | +```bash |
| 192 | +curl -s https://your-worker-domain/api/config |
| 193 | +``` |
| 194 | + |
| 195 | +Expected unauthenticated response: |
| 196 | + |
| 197 | +```json |
| 198 | +{ |
| 199 | + "accountOptions": null |
| 200 | +} |
| 201 | +``` |
| 202 | + |
| 203 | +Then open the Worker URL: |
| 204 | + |
| 205 | +- Signed-out users should only see the public read-only page. |
| 206 | +- Signed-in allowlisted users should see account, strategy, mode, current status, and the switch button. |
| 207 | +- Admin users should be able to open `/admin`. |
| 208 | + |
| 209 | +## Local Checks |
| 210 | + |
| 211 | +Run these before opening a PR: |
| 212 | + |
| 213 | +```bash |
| 214 | +jq empty web/strategy-switch-console/account-options.example.json web/strategy-switch-console/strategy-profiles.example.json |
| 215 | +node --experimental-default-type=module tests/strategy_switch_worker_validation.mjs |
| 216 | +python3 scripts/runtime_settings.py validate |
| 217 | +python3 -m unittest discover -s tests -v |
| 218 | +git diff --check |
| 219 | +``` |
0 commit comments