# 十一、 C# 函數庫

## 11.1 域名 Namespace

MdcCSApi

## 11.2 MdcSession 連線物件

### 11.2.1 連線資訊

#### 11.2.1.1 Start 方法

要求物件開始連線登入程序\
細節請參 [mdca\_connection\_start](/mdcapi/jiuc-han-shu-ku.md#9-1-32-mdcaconnectionstart-fang-fa)，除 Handle 外。

#### 11.2.1.2 Close 方法

要求關閉回收連線\
細節請參 [mdca\_connection\_close](/mdcapi/jiuc-han-shu-ku.md#9-1-33-mdcaconnectionclose-fang-fa)，除 Handle 外。

### 11.2.2 行情快照訂閱

#### 11.2.2.1 QuotationSubscribe 方法

訂閱即時行情

**函數原型:**

```
void QuotationSubscribe(string Exchange, string[] Commoditys)
```

**參數:** \
Exchange: 查詢交易所 \
Commoditys: 訂閱商品陣列

**例外 (RuntimeErrorException):** \
依 [mdca\_quotation\_subscribe](/mdcapi/jiuc-han-shu-ku.md#9-1-61-mdcaquotationsubscribe-fang-fa) 的Result 產生例外。

#### 11.2.2.2 QuotationUnsubscribe 方法

解訂閱即時行情

**函數原型:**

```
void QuotationUnsubscribe(string Exchange, string[] Commoditys)
```

**參數:** \
Exchange: 查詢交易所 \
Commoditys: 解訂閱商品陣列

**例外 (RuntimeErrorException):** \
依 [mdca\_quotation\_unsubscribe](/mdcapi/jiuc-han-shu-ku.md#9-1-62-mdcaquotationunsubscribe-fang-fa) 的Result 產生例外。

#### 11.2.2.3 QuotationRequest 方法

行情快照查詢

**函數原型:**

```
void QuotationRequest (UInt32 RequestId, string Exchange, string[] Commoditys)
```

**參數:** \
RequestId: 查詢識別ID \
Exchange: 查詢交易所 \
Commoditys: 查詢商品陣列

**例外 (RuntimeErrorException):** \
依 [mdca\_quotation\_request](/mdcapi/jiuc-han-shu-ku.md#9-1-63-mdcaquotationrequest-fang-fa) 的Result 產生例外。

### 11.2.3 即時與歷史查詢

#### 11.2.3.1 HistoryRequest 方法

歷史資料查詢訂閱

**說明:** \
此呼叫向伺服端要求資料，取得伺服回應時，會從 [OnHistoryResponse](/mdcapi/shi-yi-c-han-shu-ku.md#11-2-43-onhistoryresponse-shi-jian-fang-fa) 中得到一次性完整的資料結構內容。

**函數原型:**

```
void HistoryRequest(RequestArgument argument)
```

**參數:**

argument: 查詢參數 (參 [Mdcs\_RequestArgument](/mdcapi/bac-c++-c-gong-yong-zi-liao-jie-gou.md#814-requestargument-cha-xun-can-shu))

**例外 (RuntimeErrorException):** 依 [mdca\_history\_request](/mdcapi/jiuc-han-shu-ku.md#9-1-71-mdcahistoryrequest-fang-fa) 的Result 產生例外。

#### 11.2.3.2 HistoryUnsubscribe 方法

取消歷史資料訂閱

**說明:** \
此呼叫會解除即時行情的訂閱活動，並回收工作。

**函數原型:**

```
void HistoryUnsubscribe(uint RequestId)
```

**參數:** \
RequestId: 查詢唯一識別碼\
**例外 (RuntimeErrorException):** \
依 [mdca\_history\_unsubscribe](/mdcapi/jiuc-han-shu-ku.md#9-1-72-mdcahistoryunsubscribe-fang-fa) 的Result 產生例外。

### 11.2.4 事件虛函式

#### 11.2.4.1 OnConnectionState 事件方法

連線狀態事件\
細節請參 [mdca\_listener::connection\_state](/mdcapi/jiuc-han-shu-ku.md#9-2-11-connectionstate-shi-jian-fang-fa)，除 Handle 外。

#### 11.2.4.2 OnQuotationChange 事件方法

初始化快照資料 及 即時行情變動 事件\
細節請參 [mdca\_listener::quotation\_change](/mdcapi/jiuc-han-shu-ku.md#9-2-12-quotationchange-shi-jian-fang-fa)，除 Handle 外。

#### 11.2.4.3 OnHistoryResponse 事件方法

歷史資料查詢訂閱回應\
細節請參 [mdca\_listener::history\_response](/mdcapi/jiuc-han-shu-ku.md#9-2-13-historyresponse-shi-jian-fang-fa)，除 Handle 外。

#### 11.2.4.4 OnQuotationResponse 事件方法

快照行情查詢回傳事件\
細節請參 [mdca\_listener::quotation\_response](/mdcapi/jiuc-han-shu-ku.md#9-2-14-quotationresponse-shi-jian-fang-fa)，除 Handle 外。

#### 11.2.4.5 OnCommodityResponse 事件方法

商品資訊查詢回傳事件\
細節請參 [mdca\_listener::commodity\_response](/mdcapi/jiuc-han-shu-ku.md#9-2-15-commodityresponse-shi-jian-fang-fa)，除 Handle 外。

#### 11.2.4.6 OnCommodityChange 事件方法

商品資訊變動回傳事件\
細節請參 [mdca\_listener::commodity\_change](/mdcapi/jiuc-han-shu-ku.md#9-2-16-commoditychange-shi-jian-fang-fa)，除 Handle 外。

#### 11.2.4.7 OnContinuousChange 事件方法

連續月資訊變動回傳事件\
細節請參 [mdca\_listener::continuous\_change](/mdcapi/jiuc-han-shu-ku.md#9-2-17-continuouschange-shi-jian-fang-fa)，除 Handle 外。

## 11.3 MdcToolkit 工具函式物件

### 11.3.1 價格處理函式

#### 11.3.1.1 PriceString 方法

價格轉換字串

**函數原型:**

```
string PriceString(CommodityRoot commodity_root, Commodity commodity, double price)
```

**參數:** \
commodity\_root: 商品根物件 \
commodity: 商品物件 \
price: 待轉換價格

**返回值:** \
轉換出來的價格字串

**例外 (RuntimeErrorException):** \
依 [mdca\_price\_string](/mdcapi/jiuc-han-shu-ku.md#9-1-81-mdcapricestring-fang-fa) 的Result 產生例外。

#### 11.3.1.2 StringPrice 方法

字串轉換為價格

**函數原型:**

```
double StringPrice(CommodityRoot commodity_root, Commodity commodity, string price)
```

**參數:** \
commodity\_root: 商品根物件 \
commodity: 商品物件 \
price: 待轉換價格字串

**返回值:** \
轉換出來的價格\
**例外 (RuntimeErrorException):** \
依 [mdca\_string\_price](/mdcapi/jiuc-han-shu-ku.md#9-1-82-mdcastringprice-fang-fa) 的Result 產生例外。

#### 11.3.1.3 PricescaleOffset 方法

價格檔位偏移工具

**函數原型:**

```
double PricescaleOffset(CommodityRoot commodity_root, Commodity commodity, double price, int tick_offset)
```

**參數:** \
commodity\_root: 商品根物件 \
commodity: 商品物件 \
price: 待轉換價格字串 \
tick\_offset: 檔位偏移值

**返回值:** \
偏移的價格

**例外 (RuntimeErrorException):** \
依 [mdca\_pricescale\_offset](/mdcapi/jiuc-han-shu-ku.md#9-1-83-mdcapricescaleoffset-fang-fa) 的Result 產生例外。

#### 11.3.1.4 PricescaleValues 方法

展開價格檔位至價格陣列

**函數原型:**

```
double[] PricescaleValues(double lower_limit_price, double upper_limit_price, CommodityRoot commodity_root, Commodity
commodity)
```

**參數:** \
lower\_limit\_price: 跌停價 (起始展價的價格) \
upper\_limit\_price: 跌停價 (起始展價的價格) \
commodity\_root: 商品根物件 commodity: 商品物件

**返回值:** \
價格陣列

**例外 (RuntimeErrorException):** \
依 [mdca\_pricescale\_values](/mdcapi/jiuc-han-shu-ku.md#9-1-84-mdcapricescalevalues-fang-fa) 的Result 產生例外。

#### 11.3.1.5 PricescaleStrings 方法

展開價格檔位至字串串列

**函數原型:**

```
string[] PricescaleStrings(double lower_limit_price, double upper_limit_price, CommodityRoot commodity_root, Commodity
commodity)
```

**參數:** \
lower\_limit\_price: 跌停價 (起始展價的價格) \
upper\_limit\_price: 跌停價 (起始展價的價格) \
commodity\_root: 商品根物件 \
commodity: 商品物件

**返回值:** \
價格字串陣列 \
**例外 (RuntimeErrorException):** \
依 [mdca\_pricescale\_strings](/mdcapi/jiuc-han-shu-ku.md#9-1-85-mdcapricescalestrings-fang-fa) 的Result 產生例外。

#### 11.3.1.6 PricescaleDetail 方法

取出價格資訊

**函數原型:**

```
void PricescaleDetail(CommodityRoot commodity_root, Commodity commodity, double price, out double integer, out float
numerator, out float denominator, out int numerator_decimal_place, out int denominator_digit_place)
```

**參數:** \
commodity\_root: 商品根物件 \
commodity: 商品物件 \
price: 價格 \
integer: 整數 \
numerator: 分子 \
denominator: 分母\
numerator\_decimal\_place: 分子小數位數 \
denominator\_digit\_place: 分母整數位數 \
**例外 (RuntimeErrorException):** \
依 [mdca\_pricescale\_detail](/mdcapi/jiuc-han-shu-ku.md#9-1-86-mdcapricescaledetail-fang-fa) 的Result 產生例外。

## 11.4 MdcCommon 通用資訊物件

### 11.4.1 Version 方法

函數庫相容版本號 細節請參 [mdca\_version](/mdcapi/jiuc-han-shu-ku.md#9-1-11-mdcaversion-fang-fa)。

### 11.4.2 FileVersion 方法

函數庫相容版本號 細節請參 [mdca\_file\_version](/mdcapi/jiuc-han-shu-ku.md#9-1-12-mdcafileversion-fang-fa)。

### 11.4.3 LogFile 方法

設定 Log 檔案路徑及檔名 細節請參 [mdca\_log\_file](/mdcapi/jiuc-han-shu-ku.md#9-1-92-mdcalogfile-fang-fa)。

### 11.4.4 FileVersion 方法

函數庫相容版本號 細節請參 [mdca\_log\_flag](/mdcapi/jiuc-han-shu-ku.md#9-1-93-mdcalogflag-fang-fa)。

## 11.5 Mdcs\_Quotation 商品行情快照物件

**物件原型:**

```
class Mdcs_Quotation {
public string Exchange; /* 交易所 */
public string Commodity; /* 商品代碼 */
public UInt32 UniqueID; /*唯一識別ID */
public Mdcs_Quot_Basic Basic; /* 基本資料 */
public Mdcs_Quot_Deal Deal; /* 成交資料 */
public Mdcs_Quot_Depth Depth; /* 委託簿資料 */
public Mdcs_Depth_Detail Depth_Detail; /* 檔位變動資訊 */
}
```

**說明:** \
當 Depth 沒有變動時，Depth\_Detial 將為 NULL，當 Depth 有變動時，Depth\_Detail 將提供委買(Bids\[])、委賣(Asks\[]) 委託簿可視(N 檔) 範圍的價量異動資訊清單。

※ UniqueID 特性請參 [附錄二、UniqueID 應用邏輯與特性](/mdcapi/untitled.md#132-fu-lu-er-quotationptruniqueid-ying-yong-luo-ji-yu-te-xing)

## 11.6 Mdcs\_Depth\_Detail 檔位變動資訊物件

**物件原型:**

```
class Mdcs_Depth_Detail {
public KeyValuePair<double, uint>[] Asks { get; } /* 委賣變動清單 */
public KeyValuePair<double, uint>[] Bids { get; } /* 委買變動清單 */
}
```

**說明:** \
我們使用了 KeyValuePair 來存下價格(Key)、量(Value)的變動資訊，Bids / Asks 為唯讀陣列，當陣列 Length = 0 時，表示該邊委託不存在異動，反之，存在異動的項目，您可依價格Key)、量(Value) 來維護UI，當量為0 時，該價已移出委託視野或因成交消失。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jihsunfutures.gitbook.io/mdcapi/shi-yi-c-han-shu-ku.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
