Skip to content

Commit

Permalink
add security_list
Browse files Browse the repository at this point in the history
  • Loading branch information
sunli829 committed May 17, 2024
1 parent 110f69e commit 70f7005
Show file tree
Hide file tree
Showing 32 changed files with 619 additions and 26 deletions.
3 changes: 3 additions & 0 deletions c/cbindgen.toml
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,13 @@ cpp_compat = true
"CFilterWarrantInOutBoundsType" = "lb_filter_warrant_in_out_bounds_type_t"
"CWarrantStatus" = "lb_warrant_status_t"
"CWarrantInfo" = "lb_warrant_info_t"
"CSecurity" = "lb_security_t"
"CSecurityListCategory" = "lb_security_list_category_t"

[export]
include = [
"CSubscription",
"CSecurity",
"CSecurityStaticInfo",
"CSecurityQuote",
"COptionQuote",
Expand Down
42 changes: 42 additions & 0 deletions c/csrc/include/longport.h
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,16 @@ typedef enum lb_security_board_t {
SecurityBoardSGSector,
} lb_security_board_t;

/**
* Security list category
*/
typedef enum lb_security_list_category_t {
/**
* Overnight
*/
SecurityListCategoryOvernight,
} lb_security_list_category_t;

/**
* Sort order type
*/
Expand Down Expand Up @@ -1928,6 +1938,28 @@ typedef struct lb_subscription_t {
uintptr_t num_candlesticks;
} lb_subscription_t;

/**
* Security
*/
typedef struct lb_security_t {
/**
* Security code
*/
const char *symbol;
/**
* Security name (zh-CN)
*/
const char *name_cn;
/**
* Security name (en)
*/
const char *name_en;
/**
* Security name (zh-HK)
*/
const char *name_hk;
} lb_security_t;

/**
* The basic information of securities
*/
Expand Down Expand Up @@ -4054,6 +4086,16 @@ void lb_quote_context_realtime_candlesticks(const struct lb_quote_context_t *ctx
lb_async_callback_t callback,
void *userdata);

/**
* Get security list
* data in the local storage.
*/
void lb_quote_context_security_list(const struct lb_quote_context_t *ctx,
enum lb_market_t market,
enum lb_security_list_category_t category,
lb_async_callback_t callback,
void *userdata);

void lb_trade_context_new(const struct lb_config_t *config,
lb_async_callback_t callback,
void *userdata);
Expand Down
25 changes: 23 additions & 2 deletions c/src/quote_context/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ use crate::{
quote_context::{
enum_types::{
CAdjustType, CCalcIndex, CFilterWarrantExpiryDate, CFilterWarrantInOutBoundsType,
CPeriod, CSortOrderType, CWarrantSortBy, CWarrantStatus, CWarrantType,
CPeriod, CSecurityListCategory, CSortOrderType, CWarrantSortBy, CWarrantStatus,
CWarrantType,
},
types::{
CCandlestickOwned, CCapitalDistributionResponseOwned, CCapitalFlowLineOwned,
Expand All @@ -30,7 +31,7 @@ use crate::{
CPushBrokersOwned, CPushCandlestick, CPushCandlestickOwned, CPushDepth,
CPushDepthOwned, CPushQuote, CPushQuoteOwned, CPushTrades, CPushTradesOwned,
CRealtimeQuoteOwned, CSecurityBrokersOwned, CSecurityCalcIndexOwned,
CSecurityDepthOwned, CSecurityQuoteOwned, CSecurityStaticInfoOwned,
CSecurityDepthOwned, CSecurityOwned, CSecurityQuoteOwned, CSecurityStaticInfoOwned,
CStrikePriceInfoOwned, CSubscriptionOwned, CTradeOwned, CUpdateWatchlistGroup,
CWarrantInfoOwned, CWarrantQuoteOwned, CWatchlistGroupOwned, LB_WATCHLIST_GROUP_NAME,
LB_WATCHLIST_GROUP_SECURITIES,
Expand Down Expand Up @@ -1075,3 +1076,23 @@ pub unsafe extern "C" fn lb_quote_context_realtime_candlesticks(
Ok(rows)
});
}

/// Get security list
/// data in the local storage.
#[no_mangle]
pub unsafe extern "C" fn lb_quote_context_security_list(
ctx: *const CQuoteContext,
market: CMarket,
category: CSecurityListCategory,
callback: CAsyncCallback,
userdata: *mut c_void,
) {
let ctx_inner = (*ctx).ctx.clone();
execute_async(callback, ctx, userdata, async move {
let rows: CVec<CSecurityOwned> = ctx_inner
.security_list(market.into(), category.into())
.await?
.into();
Ok(rows)
});
}
11 changes: 11 additions & 0 deletions c/src/quote_context/enum_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,3 +555,14 @@ pub enum CWarrantStatus {
#[c(remote = "Normal")]
WarrantStatusNormal,
}

/// Security list category
#[derive(Debug, Copy, Clone, Eq, PartialEq, CEnum)]
#[c(remote = "longport::quote::SecurityListCategory")]
#[allow(clippy::enum_variant_names)]
#[repr(C)]
pub enum CSecurityListCategory {
/// Overnight
#[c(remote = "Overnight")]
SecurityListCategoryOvernight,
}
59 changes: 58 additions & 1 deletion c/src/quote_context/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use longport::quote::{
Brokers, Candlestick, CapitalDistribution, CapitalDistributionResponse, CapitalFlowLine, Depth,
IntradayLine, IssuerInfo, MarketTradingDays, MarketTradingSession, OptionDirection,
OptionQuote, OptionType, ParticipantInfo, Period, PrePostQuote, PushBrokers, PushCandlestick,
PushDepth, PushQuote, PushTrades, RealtimeQuote, SecurityBoard, SecurityBrokers,
PushDepth, PushQuote, PushTrades, RealtimeQuote, Security, SecurityBoard, SecurityBrokers,
SecurityCalcIndex, SecurityDepth, SecurityQuote, SecurityStaticInfo, StrikePriceInfo,
Subscription, Trade, TradeDirection, TradeSession, TradeStatus, TradingSessionInfo,
WarrantInfo, WarrantQuote, WarrantType, WatchlistGroup, WatchlistSecurity,
Expand Down Expand Up @@ -2761,3 +2761,60 @@ impl From<WarrantInfo> for CWarrantInfoOwned {
}
}
}

/// Security
#[repr(C)]
pub struct CSecurity {
/// Security code
pub symbol: *const c_char,
/// Security name (zh-CN)
pub name_cn: *const c_char,
/// Security name (en)
pub name_en: *const c_char,
/// Security name (zh-HK)
pub name_hk: *const c_char,
}

#[derive(Debug)]
pub(crate) struct CSecurityOwned {
pub symbol: CString,
pub name_cn: CString,
pub name_en: CString,
pub name_hk: CString,
}

impl From<Security> for CSecurityOwned {
fn from(info: Security) -> Self {
let Security {
symbol,
name_cn,
name_en,
name_hk,
} = info;
CSecurityOwned {
symbol: symbol.into(),
name_cn: name_cn.into(),
name_en: name_en.into(),
name_hk: name_hk.into(),
}
}
}

impl ToFFI for CSecurityOwned {
type FFIType = CSecurity;

fn to_ffi_type(&self) -> Self::FFIType {
let CSecurityOwned {
symbol,
name_cn,
name_en,
name_hk,
} = self;
CSecurity {
symbol: symbol.to_ffi_type(),
name_cn: name_cn.to_ffi_type(),
name_en: name_en.to_ffi_type(),
name_hk: name_hk.to_ffi_type(),
}
}
}
6 changes: 6 additions & 0 deletions cpp/include/quote_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,12 @@ class QuoteContext
void update_watchlist_group(const UpdateWatchlistGroup& req,
AsyncCallback<QuoteContext, void> callback) const;

/// Get security list
void security_list(
Market market,
SecurityListCategory category,
AsyncCallback<QuoteContext, std::vector<Security>> callback) const;

/// Get real-time quotes
///
/// Get real-time quotes of the subscribed symbols, it always returns the
Expand Down
20 changes: 20 additions & 0 deletions cpp/include/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,19 @@ enum class SecurityBoard
SGSector,
};

/// Security
struct Security
{
/// Security code
std::string symbol;
/// Security name (zh-CN)
std::string name_cn;
/// Security name (en)
std::string name_en;
/// Security name (zh-HK)
std::string name_hk;
};

/// The basic information of securities
struct SecurityStaticInfo
{
Expand Down Expand Up @@ -1124,6 +1137,13 @@ struct WarrantInfo
WarrantStatus status;
};

/// Security list category
enum class SecurityListCategory
{
/// Overnight
Overnight,
};

} // namespace quote

namespace trade {
Expand Down
24 changes: 24 additions & 0 deletions cpp/src/convert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ using longport::quote::PushQuote;
using longport::quote::PushTrades;
using longport::quote::RealtimeQuote;
using longport::quote::SecuritiesUpdateMode;
using longport::quote::Security;
using longport::quote::SecurityBoard;
using longport::quote::SecurityBrokers;
using longport::quote::SecurityCalcIndex;
using longport::quote::SecurityDepth;
using longport::quote::SecurityListCategory;
using longport::quote::SecurityQuote;
using longport::quote::SecurityStaticInfo;
using longport::quote::SortOrderType;
Expand Down Expand Up @@ -2002,5 +2004,27 @@ convert(lb_warrant_info_t info)
};
}

inline lb_security_list_category_t
convert(SecurityListCategory category)
{
switch (category) {
case SecurityListCategory::Overnight:
return SecurityListCategoryOvernight;
default:
throw std::invalid_argument("unreachable");
}
}

inline Security
convert(const lb_security_t* info)
{
return Security{
info->symbol,
info->name_cn,
info->name_en,
info->name_hk,
};
}

} // namespace convert
} // namespace longport
35 changes: 35 additions & 0 deletions cpp/src/quote_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,41 @@ QuoteContext::update_watchlist_group(
new AsyncCallback<QuoteContext, void>(callback));
}

void
QuoteContext::security_list(
Market market,
SecurityListCategory category,
AsyncCallback<QuoteContext, std::vector<Security>> callback) const
{
lb_quote_context_security_list(
ctx_,
convert(market),
convert(category),
[](auto res) {
auto callback_ptr =
callback::get_async_callback<QuoteContext, std::vector<Security>>(
res->userdata);
QuoteContext ctx((const lb_quote_context_t*)res->ctx);
Status status(res->error);

if (status) {
auto rows = (const lb_security_t*)res->data;
std::vector<Security> rows2;
std::transform(rows,
rows + res->length,
std::back_inserter(rows2),
[](auto row) { return convert(&row); });

(*callback_ptr)(AsyncResult<QuoteContext, std::vector<Security>>(
ctx, std::move(status), &rows2));
} else {
(*callback_ptr)(AsyncResult<QuoteContext, std::vector<Security>>(
ctx, std::move(status), nullptr));
}
},
new AsyncCallback<QuoteContext, std::vector<Security>>(callback));
}

void
QuoteContext::realtime_quote(
const std::vector<std::string>& symbols,
Expand Down
2 changes: 2 additions & 0 deletions java/Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ args = [
"javasrc/src/main/java/com/longport/quote/FilterWarrantExpiryDate.java",
"javasrc/src/main/java/com/longport/quote/WarrantInfo.java",
"javasrc/src/main/java/com/longport/quote/QueryWarrantOptions.java",
"javasrc/src/main/java/com/longport/quote/SecurityListCategory.java",
"javasrc/src/main/java/com/longport/quote/Security.java",

"javasrc/src/main/java/com/longport/trade/AccountBalance.java",
"javasrc/src/main/java/com/longport/trade/BalanceType.java",
Expand Down
8 changes: 8 additions & 0 deletions java/c/com_longport_SdkNative.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions java/javasrc/src/main/java/com/longport/SdkNative.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ public static native void quoteContextDeleteWatchlistGroup(long context, DeleteW
public static native void quoteContextUpdateWatchlistGroup(long context, UpdateWatchlistGroup req,
AsyncCallback callback);

public static native void quoteContextSecurityList(long context, Market market,
SecurityListCategory category,
AsyncCallback callback);

public static native void quoteContextRealtimeQuote(long context, String[] symbols, AsyncCallback callback);

public static native void quoteContextRealtimeDepth(long context, String symbol, AsyncCallback callback);
Expand Down
Loading

0 comments on commit 70f7005

Please sign in to comment.