diff --git a/README.md b/README.md index 114d42e6c..f4b226a48 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,8 @@ https://longbridge.github.io/openapi - Debugging - Enable logs via `LONGBRIDGE_LOG_PATH`. - If quotes connect but look empty, keep `LONGBRIDGE_PRINT_QUOTE_PACKAGES=true` to confirm opened quote packages. +- Paper trading + - If your account is a paper trading (simulation) account and you want to place orders, set `LONGBRIDGE_PAPERTRADING=true`. When enabled, all API calls target the paper trading environment and the server validates the token — if the token belongs to a real-money account the server returns an error. When `false` (the default) the server imposes no restrictions. ## Minimal Verification diff --git a/c/README.md b/c/README.md index c1df4e0b2..3f0e74471 100644 --- a/c/README.md +++ b/c/README.md @@ -162,6 +162,35 @@ setx LONGBRIDGE_ACCESS_TOKEN "Access Token get from user center" | LONGBRIDGE_PUSH_CANDLESTICK_MODE | `realtime` or `confirmed` (Default: `realtime`) | | LONGBRIDGE_PRINT_QUOTE_PACKAGES | Print quote packages when connected, `true` or `false` (Default: `true`) | | LONGBRIDGE_LOG_PATH | Set the path of the log files (Default: `no logs`) | +| LONGBRIDGE_PAPERTRADING | Enable paper trading mode, `true` or `false` (Default: `false`). See [Paper Trading](#paper-trading). | + +## Paper Trading + +If your account is a **paper trading** (simulation) account and you want to +place orders, you must explicitly enable paper trading mode. When enabled, all +API calls target the paper trading environment and the server validates the +token — if the token belongs to a real-money account the server returns an +error. + +By default this option is **off**: the server imposes no restrictions and +accepts requests from both paper trading and real-money accounts. + +**Via environment variable** (applies automatically to all constructor methods): + +```bash +export LONGBRIDGE_PAPERTRADING=true # macOS / Linux +``` + +```powershell +$env:LONGBRIDGE_PAPERTRADING = "true" # Windows PowerShell +``` + +**Programmatically:** + +```c +lb_config_t* config = lb_config_from_apikey("app-key", "app-secret", "access-token"); +lb_config_enable_papertrading(config); +``` ## Quote API _(Get basic information of securities)_ diff --git a/c/csrc/include/longbridge.h b/c/csrc/include/longbridge.h index 1b2fdc031..faa3dc880 100644 --- a/c/csrc/include/longbridge.h +++ b/c/csrc/include/longbridge.h @@ -9028,6 +9028,22 @@ void lb_config_set_push_candlestick_mode(struct lb_config_t *config, */ void lb_config_disable_print_quote_packages(struct lb_config_t *config); +/** + * Enable paper trading mode. + * + * When enabled, all API calls target the paper trading (simulation) + * environment. The server validates the token: if it belongs to a real-money + * account the server returns an error. + * + * When disabled (the default) the server imposes no restrictions — both + * paper trading and real-money accounts are accepted. + * + * Paper trading users should call this function as a safety guard. + * + * @param config Config object + */ +void lb_config_enable_papertrading(struct lb_config_t *config); + /** * Set the log file path * diff --git a/c/src/config.rs b/c/src/config.rs index 18ecc7b7f..0b7f20c2c 100644 --- a/c/src/config.rs +++ b/c/src/config.rs @@ -171,6 +171,23 @@ pub unsafe extern "C" fn lb_config_disable_print_quote_packages(config: *mut CCo (*config).0.set_dont_print_quote_packages(); } +/// Enable paper trading mode. +/// +/// When enabled, all API calls target the paper trading (simulation) +/// environment. The server validates the token: if it belongs to a real-money +/// account the server returns an error. +/// +/// When disabled (the default) the server imposes no restrictions — both +/// paper trading and real-money accounts are accepted. +/// +/// Paper trading users should call this function as a safety guard. +/// +/// @param config Config object +#[unsafe(no_mangle)] +pub unsafe extern "C" fn lb_config_enable_papertrading(config: *mut CConfig) { + (*config).0.set_enable_papertrading(); +} + /// Set the log file path /// /// @param config Config object diff --git a/cpp/README.md b/cpp/README.md index c3a37d913..be58e35af 100644 --- a/cpp/README.md +++ b/cpp/README.md @@ -157,6 +157,35 @@ setx LONGBRIDGE_ACCESS_TOKEN "Access Token get from user center" | LONGBRIDGE_PUSH_CANDLESTICK_MODE | `realtime` or `confirmed` (Default: `realtime`) | | LONGBRIDGE_PRINT_QUOTE_PACKAGES | Print quote packages when connected, `true` or `false` (Default: `true`) | | LONGBRIDGE_LOG_PATH | Set the path of the log files (Default: `no logs`) | +| LONGBRIDGE_PAPERTRADING | Enable paper trading mode, `true` or `false` (Default: `false`). See [Paper Trading](#paper-trading). | + +## Paper Trading + +If your account is a **paper trading** (simulation) account and you want to +place orders, you must explicitly enable paper trading mode. When enabled, all +API calls target the paper trading environment and the server validates the +token — if the token belongs to a real-money account the server returns an +error. + +By default this option is **off**: the server imposes no restrictions and +accepts requests from both paper trading and real-money accounts. + +**Via environment variable** (applies automatically to all constructor methods): + +```bash +export LONGBRIDGE_PAPERTRADING=true # macOS / Linux +``` + +```powershell +$env:LONGBRIDGE_PAPERTRADING = "true" # Windows PowerShell +``` + +**Programmatically:** + +```c++ +Config config = Config::from_apikey("app-key", "app-secret", "access-token") + .enable_papertrading(); +``` ## Quote API _(Get basic information of securities)_ diff --git a/cpp/include/config.hpp b/cpp/include/config.hpp index 75bd26cab..928c5b80e 100644 --- a/cpp/include/config.hpp +++ b/cpp/include/config.hpp @@ -81,6 +81,18 @@ class Config /// Enable overnight quote Config& enable_overnight(); + /// Enable paper trading mode. + /// + /// When enabled, all API calls target the paper trading (simulation) + /// environment. The server validates the token: if it belongs to a + /// real-money account the server returns an error. + /// + /// By default this option is disabled and the server imposes no restrictions + /// — both paper trading and real-money accounts are accepted. + /// + /// Paper trading users should call this method as a safety guard. + Config& enable_papertrading(); + /// Set the push candlestick mode Config& set_push_candlestick_mode(PushCandlestickMode mode); diff --git a/cpp/src/config.cpp b/cpp/src/config.cpp index 5b6972c64..f2c40c9cd 100644 --- a/cpp/src/config.cpp +++ b/cpp/src/config.cpp @@ -98,6 +98,13 @@ Config::enable_overnight() return *this; } +Config& +Config::enable_papertrading() +{ + lb_config_enable_papertrading(config_); + return *this; +} + Config& Config::set_push_candlestick_mode(PushCandlestickMode mode) { diff --git a/java/README.md b/java/README.md index b803e34a3..9ab211e10 100644 --- a/java/README.md +++ b/java/README.md @@ -152,6 +152,37 @@ setx LONGBRIDGE_ACCESS_TOKEN "Access Token get from user center" | LONGBRIDGE_PUSH_CANDLESTICK_MODE | `realtime` or `confirmed` (Default: `realtime`) | | LONGBRIDGE_PRINT_QUOTE_PACKAGES | Print quote packages when connected, `true` or `false` (Default: `true`) | | LONGBRIDGE_LOG_PATH | Set the path of the log files (Default: `no logs`) | +| LONGBRIDGE_PAPERTRADING | Enable paper trading mode, `true` or `false` (Default: `false`). See [Paper Trading](#paper-trading). | + +## Paper Trading + +If your account is a **paper trading** (simulation) account and you want to +place orders, you must explicitly enable paper trading mode. When enabled, all +API calls target the paper trading environment and the server validates the +token — if the token belongs to a real-money account the server returns an +error. + +By default this option is **off**: the server imposes no restrictions and +accepts requests from both paper trading and real-money accounts. + +**Via environment variable** (applies automatically to all constructor methods): + +```bash +export LONGBRIDGE_PAPERTRADING=true # macOS / Linux +``` + +```powershell +$env:LONGBRIDGE_PAPERTRADING = "true" # Windows PowerShell +``` + +**Programmatically:** + +```java +import com.longbridge.*; + +Config config = Config.fromApikey("app-key", "app-secret", "access-token") + .enablePapertrading(); +``` ## Quote API _(Get basic information of securities)_ diff --git a/java/javasrc/src/main/java/com/longbridge/Config.java b/java/javasrc/src/main/java/com/longbridge/Config.java index 6b6044577..ca28e9b1b 100644 --- a/java/javasrc/src/main/java/com/longbridge/Config.java +++ b/java/javasrc/src/main/java/com/longbridge/Config.java @@ -187,6 +187,25 @@ public Config logPath(String path) { return this; } + /** + * Enable paper trading mode. + * + *
When enabled, all API calls target the paper trading (simulation) + * environment. The server validates the token: if it belongs to a real-money + * account the server returns an error. + * + *
By default this option is disabled and the server imposes no restrictions + * — requests from both paper trading and real-money accounts are accepted. + * + *
Paper trading users should call this method as a safety guard.
+ *
+ * @return this object
+ */
+ public Config enablePapertrading() {
+ this.raw = SdkNative.configSetEnablePapertrading(this.raw);
+ return this;
+ }
+
/**
* Gets a new {@code access_token}.
*
diff --git a/java/javasrc/src/main/java/com/longbridge/SdkNative.java b/java/javasrc/src/main/java/com/longbridge/SdkNative.java
index 7beea04c5..bb228c353 100644
--- a/java/javasrc/src/main/java/com/longbridge/SdkNative.java
+++ b/java/javasrc/src/main/java/com/longbridge/SdkNative.java
@@ -52,6 +52,8 @@ public static native long newHttpClientFromApikey(String appKey, String appSecre
public static native long configSetLogPath(long config, String logPath);
+ public static native long configSetEnablePapertrading(long config);
+
public static native void configRefreshAccessToken(long config, OffsetDateTime expiredAt,
AsyncCallback callback);
diff --git a/java/src/config.rs b/java/src/config.rs
index bae1c9016..d3b35533c 100644
--- a/java/src/config.rs
+++ b/java/src/config.rs
@@ -166,6 +166,18 @@ pub unsafe extern "system" fn Java_com_longbridge_SdkNative_configSetLogPath(
})
}
+#[unsafe(no_mangle)]
+pub unsafe extern "system" fn Java_com_longbridge_SdkNative_configSetEnablePapertrading(
+ mut env: JNIEnv,
+ _class: JClass,
+ config: jlong,
+) -> jlong {
+ jni_result(&mut env, config, |_env| {
+ (*(config as *mut Config)).set_enable_papertrading();
+ Ok(config)
+ })
+}
+
// ── Async operations
// ──────────────────────────────────────────────────────────
diff --git a/nodejs/README.md b/nodejs/README.md
index b2eafde9f..198e59387 100644
--- a/nodejs/README.md
+++ b/nodejs/README.md
@@ -145,6 +145,7 @@ setx LONGBRIDGE_ACCESS_TOKEN "Access Token get from user center"
| LONGBRIDGE_PUSH_CANDLESTICK_MODE | `realtime` or `confirmed` (Default: `realtime`) |
| LONGBRIDGE_PRINT_QUOTE_PACKAGES | Print quote packages when connected, `true` or `false` (Default: `true`) |
| LONGBRIDGE_LOG_PATH | Set the path of the log files (Default: `no logs`) |
+| LONGBRIDGE_PAPERTRADING | Enable paper trading mode, `true` or `false` (Default: `false`). See [Paper Trading](#paper-trading). |
Then create a config from the environment:
@@ -230,6 +231,40 @@ async function main() {
main();
```
+## Paper Trading
+
+If your account is a **paper trading** (simulation) account and you want to
+place orders, you must explicitly enable paper trading mode. When enabled, all
+API calls target the paper trading environment and the server validates the
+token — if the token belongs to a real-money account the server returns an
+error.
+
+By default this option is **off**: the server imposes no restrictions and
+accepts requests from both paper trading and real-money accounts.
+
+**Via environment variable** (applies automatically to all constructor methods):
+
+```bash
+export LONGBRIDGE_PAPERTRADING=true # macOS / Linux
+```
+
+```powershell
+$env:LONGBRIDGE_PAPERTRADING = "true" # Windows PowerShell
+```
+
+**Programmatically:**
+
+```javascript
+const { Config } = require('longbridge');
+
+const config = Config.fromApikey(
+ process.env.LONGBRIDGE_APP_KEY,
+ process.env.LONGBRIDGE_APP_SECRET,
+ process.env.LONGBRIDGE_ACCESS_TOKEN,
+ { enablePapertrading: true },
+);
+```
+
## Troubleshooting
- Windows `setx` requires a new terminal; use `set` for the current `cmd.exe` session.
diff --git a/nodejs/src/config.rs b/nodejs/src/config.rs
index 8d8fbb95b..ff732cad5 100644
--- a/nodejs/src/config.rs
+++ b/nodejs/src/config.rs
@@ -31,6 +31,17 @@ pub struct ExtraConfigParams {
pub enable_print_quote_packages: Option