longport
provides an easy-to-use interface for invokes LongPort OpenAPI
.
Install LongPort OpenAPI SDK
Setting environment variables(MacOS/Linux)
export LONGPORT_APP_KEY="App Key get from user center"
export LONGPORT_APP_SECRET="App Secret get from user center"
export LONGPORT_ACCESS_TOKEN="Access Token get from user center"
Setting environment variables(Windows)
setx LONGPORT_APP_KEY "App Key get from user center"
setx LONGPORT_APP_SECRET "App Secret get from user center"
setx LONGPORT_ACCESS_TOKEN "Access Token get from user center"
#include <longport.h>
#include <stdio.h>
#ifdef WIN32
#include <windows.h>
#else
#include <curses.h>
#endif
void
on_quote(const struct lb_async_result_t* res)
{
if (res->error) {
printf("failed to get quote: %s\n", lb_error_message(res->error));
return;
}
lb_security_quote_t* data = (lb_security_quote_t*)res->data;
fop(int i = 0; i < res->length; i++)
{
const lb_security_quote_t* quote = &data[i];
printf("%s timestamp=%ld last_done=%f open=%f high=%f low=%f volume=%ld "
"turnover=%f\n",
quote->symbol,
quote->timestamp,
lb_decimal_to_double(quote->last_done),
lb_decimal_to_double(quote->open),
lb_decimal_to_double(quote->high),
lb_decimal_to_double(quote->low),
quote->volume,
lb_decimal_to_double(quote->turnover));
}
}
void
on_quote_context_created(const struct lb_async_result_t* res)
{
if (res->error) {
printf("failed to create quote context: %s\n",
lb_error_message(res->error));
return;
}
*((const lb_quote_context_t**)res->userdata) = res->ctx;
const char* symbols[] = { "700.HK", "AAPL.US", "TSLA.US", "NFLX.US" };
lb_quote_context_quote(res->ctx, symbols, on_quote, NULL);
}
int
main(int argc, char const* argv[])
{
#ifdef WIN32
SetConsoleOutputCP(CP_UTF8);
#endif
lb_error_t* err = NULL;
lb_config_t* config = lb_config_from_env(&err);
if (err) {
printf("failed to load configuration from environment: %s\n",
lb_error_message(err));
lb_error_free(err);
return -1;
}
const lb_quote_context_t* ctx = NULL;
lb_quote_context_new(config, on_quote_context_created, &ctx);
getchar();
lb_quote_context_release(ctx);
return 0;
}
#include <longport.h>
#include <stdio.h>
#ifdef WIN32
#include <windows.h>
#else
#include <curses.h>
#endif
void
on_quote(const struct lb_quote_context_t* ctx,
const struct lb_push_quote_t* quote,
void* userdata)
{
printf("%s timestamp=%ld last_done=%f open=%f high=%f low=%f volume=%ld "
"turnover=%f\n",
quote->symbol,
quote->timestamp,
lb_decimal_to_double(quote->last_done),
lb_decimal_to_double(quote->open),
lb_decimal_to_double(quote->high),
lb_decimal_to_double(quote->low),
quote->volume,
lb_decimal_to_double(quote->turnover));
}
void
on_subscrbe(const struct lb_async_result_t* res)
{
if (res->error) {
printf("failed to subscribe quote: %s\n", lb_error_message(res->error));
return;
}
}
void
on_quote_context_created(const struct lb_async_result_t* res)
{
if (res->error) {
printf("failed to create quote context: %s\n",
lb_error_message(res->error));
return;
}
*((const lb_quote_context_t**)res->userdata) = res->ctx;
lb_quote_context_set_on_quote(res->ctx, on_quote, NULL, NULL);
const char* symbols[] = { "700.HK", "AAPL.US", "TSLA.US", "NFLX.US" };
lb_quote_context_subscribe(
res->ctx, symbols, 4, LB_SUBFLAGS_QUOTE, true, on_subscrbe, NULL);
}
int
main(int argc, char const* argv[])
{
#ifdef WIN32
SetConsoleOutputCP(CP_UTF8);
#endif
lb_error_t* err = NULL;
lb_config_t* config = lb_config_from_env(&err);
if (err) {
printf("failed to load configuration from environment: %s\n",
lb_error_message(err));
lb_error_free(err);
return -1;
}
const lb_quote_context_t* ctx = NULL;
lb_quote_context_new(config, on_quote_context_created, &ctx);
getchar();
lb_quote_context_release(ctx);
return 0;
}
#include <longport.h>
#include <stdio.h>
#ifdef WIN32
#include <windows.h>
#else
#include <curses.h>
#endif
void
on_submit_order(const struct lb_async_result_t* res)
{
if (res->error) {
printf("failed to submit order: %s\n", lb_error_message(res->error));
return;
}
const lb_submit_order_response_t* resp = res->data;
printf("order id: %s\n", resp->order_id);
}
void
on_trade_context_created(const struct lb_async_result_t* res)
{
if (res->error) {
printf("failed to create trade context: %s\n",
lb_error_message(res->error));
return;
}
*((const lb_quote_context_t**)res->userdata) = res->ctx;
lb_decimal_t* submitted_price = lb_decimal_from_double(50.0);
lb_submit_order_options_t opts = {
"700.HK", OrderTypeLO, OrderSideBuy, 200, TimeInForceDay, submitted_price,
NULL, NULL, NULL, NULL, NULL, NULL,
NULL,
};
lb_decimal_free(submitted_price);
lb_trade_context_submit_order(res->ctx, &opts, on_submit_order, NULL);
}
int
main(int argc, char const* argv[])
{
#ifdef WIN32
SetConsoleOutputCP(CP_UTF8);
#endif
lb_error_t* err = NULL;
lb_config_t* config = lb_config_from_env(&err);
if (err) {
printf("failed to load configuration from environment: %s\n",
lb_error_message(err));
lb_error_free(err);
return -1;
}
const lb_trade_context_t* ctx = NULL;
lb_trade_context_new(config, on_trade_context_created, (void*)&ctx);
getchar();
lb_trade_context_release(ctx);
return 0;
}
Licensed under either of
- Apache License, Version 2.0,(LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT) at your option.