元大證券 API 操作說明 - yuanta
Transcript of 元大證券 API 操作說明 - yuanta
2
目錄
一、PolarisB2BAPI.dll 元件函數說明 ........................................................................ 4
1. inMsgAddByte 函數 .......................................................................................... 4
2. inMsgAddDate 函數 .......................................................................................... 4
3. inMsgAddTime 函數 .......................................................................................... 5
4. inMsgAddDateTime 函數 .................................................................................. 5
5. inMsgAddLong 函數 .......................................................................................... 6
5. inMsgAddLongLong 函數 ................................................................................. 6
6. inMsgAddsmi 函數 ............................................................................................ 7
7. inMsgAddStr 函數 ............................................................................................. 7
8. inMsgClear 函數 ................................................................................................ 7
9. inMsgSend 函數 ................................................................................................. 8
10. Login 函數 ........................................................................................................ 8
11. Logout 函數 ...................................................................................................... 9
12. Open 函數 ......................................................................................................... 9
13. outMsgClear 函數 ............................................................................................ 9
14. outMsgGetByte 函數 ........................................................................................ 9
15. outMsgGetDate 函數 ...................................................................................... 10
16. outMsgGetTime 函數 ..................................................................................... 10
3
17. outMsgGetDateTime 函數 ............................................................................. 10
18. outMsgGetLong 函數 ..................................................................................... 11
19. outMsgGetLongLong 函數 ............................................................................ 11
20. outMsgGetsmi 函數 ....................................................................................... 11
21. outMsgGetStr 函數......................................................................................... 12
22. outMsgLoad 函數 ........................................................................................... 12
23. Request 函數 .................................................................................................. 12
23. OnResponse 事件 .......................................................................................... 13
四、PolarisB2BAPI.dll 元件使用步驟說明 .............................................................. 15
五、測試環境設定說明 ............................................................................................. 19
4
一、PolarisB2BAPI.dll 元件函數說明
1. inMsgAddByte 函數
將規格文件中型別為 byte的資料傳入結構中。
public void inMsgAddByte
(
byte inVal
);
Parameters
inVal[in]
輸入結構欄位型別為 byte 的值
Return Values
無
2. inMsgAddDate 函數
將規格文件中型別為 TPolaDate 的資料傳入結構中。
public void inMsgAddDate
(
ushort wYear,
byte byMonth,
byte byDay
);
Parameters
wYear[in]
輸入結構欄位型別為 TPolaDate 中的西元年的值
byMonth[in]
輸入結構欄位型別為 TPolaDate 中的月份的值
byDay[in]
輸入結構欄位型別為 TPolaDate 中的日期的值
Return Values
無
5
3. inMsgAddTime 函數
將規格文件中型別為 TPolaTime 的資料傳入結構中。
public void inMsgAddTime
(
byte byHour,
byte byMinute,
byte bySecond,
ushort wMSecond
);
Parameters
byHour[in]
輸入結構欄位型別為 TPolaTime中的小時的值
byMinute[in]
輸入結構欄位型別為 TPolaTim中的分鐘的值
bySecond[in]
輸入結構欄位型別為 TPolaTime中的秒數的值
wMSecond[in]
輸入結構欄位型別為 TPolaTime 中的毫秒數的值
Return Values
無
4. inMsgAddDateTime 函數
將規格文件中型別為 TPolaDateTime 的資料傳入結構中。
public void inMsgAddDateTime
(
ushort wYear,
byte byMonth,
byte byDay
byte byHour,
byte byMinute,
byte bySecond,
ushort wMSecond
);
Parameters
wYear[in]
輸入結構欄位型別為 TPolaDateTime 中的西元年的值
6
byMonth[in]
輸入結構欄位型別為 TPolaDateTime 中的月份的值
byDay[in]
輸入結構欄位型別為 TPolaDateTime 中的日期的值
byHour[in]
輸入結構欄位型別為 TPolaDateTime 中的小時的值
byMinute[in]
輸入結構欄位型別為 TPolaDateTime 中的分鐘的值
bySecond[in]
輸入結構欄位型別為 TPolaDateTime 中的秒數的值
wMSecond[in]
輸入結構欄位型別為 TPolaDateTime 中的毫秒數的值
Return Values
無
5. inMsgAddLong 函數
將規格文件中型別為 uint或為 int的資料傳入結構中。
public void inMsgAddLong
(
int intVal
);
Parameters
inVal[in]
輸入結構欄位型別為 uint 或為 int中的值
Return Values
無
5. inMsgAddLongLong 函數
將規格文件中型別為 ulong 或為 long的資料傳入結構中。
public void inMsgAddLongLong
(
long inVal
);
Parameters
inVal[in]
輸入結構欄位型別為 ulong 或為 long中的值
Return Values
無
7
6. inMsgAddsmi 函數
將規格文件中型別為 ushort 或為 short的資料傳入結構中。
public void inMsgAddsmi
(
short inVal
);
Parameters
inVal[in]
輸入結構欄位型別為 ushort 或為 short中的值
Return Values
無
7. inMsgAddStr 函數
將規格文件中型別為 TByte 開頭的資料傳入結構中。
public void inMsgAddStr
(
string strValue,
int intStrLen
);
Parameters
strValue[in]
輸入結構欄位型別為 TByte 開頭中的字串值
intStrLen[in]
輸入結構欄位中所佔的 Size大小
Return Values
無
8. inMsgClear 函數
將已填入輸入結構的記憶體清除
public void inMsgClear( );
Parameters
無
Return Values
無
8
9. inMsgSend 函數
將已填入輸入結構的記憶體送至 Server
public int inMsgSend
(
string strFunID,
uint dwHandle,
string UserData,
bool bolSendClear
);
Parameters
strFunID[in]
元件規格文件中所定義的 FunctoinID (ex 30.100.10.10)
dwHandle[in]
呼叫該功能之畫面 Handle 值
UserData[in]
欲執行功能之登入帳號
bolSendClear[in]
傳送資料後是否清除記憶體資料
Return Values
0:失敗
1:成功
10. Login 函數
執行登入
public int Login
(
int FunID,
string UserData,
string UserPwd
);
Parameters
FunID[in]
0:總帳登入 1:分戶登入
UserData[in]
欲登入的帳號
UserPwd[in]
欲登入帳號的密碼
9
Return Values
0:失敗
1:成功
11. Logout 函數
執行登出
public int Logout( );
Parameters
無
Return Values
無
OnResponse事件回傳結果
12. Open 函數
開啟與 Server的連線
public void Open( );
Parameters
無
Return Values
無
13. outMsgClear 函數
將已載入輸出結構的記憶體清除
public void OutMsgClear( );
Parameters
無
Return Values
無
14. outMsgGetByte 函數
將規格文件中型別為 byte的資料取出。
public void outMsgGetByte
(
ref byte outVal
);
10
Parameters
outVal[ref]
欲取出輸出結構欄位型別為 byte的變數
Return Values
無
15. outMsgGetDate 函數
將規格文件中型別為 TPolaDate 的資料取出。
public void outMsgGetDate
(
ref string strDate
);
Parameters
strDate[ref]
欲取出輸出結構欄位型別為 TPolaDate的變數,其格式為 1911/01/01的字串
Return Values
無
16. outMsgGetTime 函數
將規格文件中型別為 TPolaTime 的資料取出。
public void outMsgGetTime
(
ref string strTime
);
Parameters
strTime[ref]
欲取出輸出結構欄位型別為 TPolaTime的變數,其格式為 24:59:59:999的字串
Return Values
無
17. outMsgGetDateTime 函數
將規格文件中型別為 TPolaDateTime 的資料取出。
public void outMsgGetDateTime
(
ref string strDateTime
);
11
Parameters
strTime[ref]
欲取出輸出結構欄位型別為 TPolaTime 的變數,其格式為 1911/01/01 24:59:59:999 的
字串
Return Values
無
18. outMsgGetLong 函數
將規格文件中型別為 uint或為 int的資料取出。
public void outMsgGetLong
(
ref int outVal
);
Parameters
outVal[in]
欲取出輸出結構欄位型別為 uint或為 int的變數
Return Values
無
19. outMsgGetLongLong 函數
將規格文件中型別為 ulong 或為 long的資料取出。
public void outMsgGetLongLong
(
ref long outVal
);
Parameters
outVal[ref]
欲取出輸出結構欄位型別為 ulong或為 long的變數
Return Values
無
20. outMsgGetsmi 函數
將規格文件中型別為 ushort 或為 short的資料取出。
public void outMsgGetsmi
(
ref short outVal
);
12
Parameters
outVal[out]
欲取出輸出結構欄位型別為 ushort或為 short的變數
Return Values
無
21. outMsgGetStr 函數
將規格文件中型別為 TByte 開頭的資料取出。
public void outMsgGetStr
(
ref string strValue,
int outStrLen
);
Parameters
strValue[in]
欲取出輸出結構欄位型別為 TByte開頭的變數
outStrLen[in]
輸出結構欄位中所佔的 Size大小
Return Values
無
22. outMsgLoad 函數
將輸出得到的 byte[]存入元件,以依序取出其內容值。
public void outMsgLoad
(
object intVal
);
Parameters
inVal [in]
欲存入元件以取出內容的 byte[]值
Return Values
0:失敗
1:成功
23. Request 函數
將整個依照規格文件排列之結構 byte[]送至 Server
public int Request
13
(
string strIndex,
uint dwHandle,
string UserData,
object arydata
);
Parameters
strIndex[in]
元件規格文件中所定義的 FunctoinID (ex 30.100.10.10)
dwHandle[in]
呼叫該功能之畫面 Handle 值
UserData[in]
欲執行功能之登入帳號
aryData[in]
欲傳送的 byte[]資料
Return Values
0:失敗
1:成功
23. OnResponse 事件
當 Server資料回應至 Client 端時所觸發的事件
public void OnResponse
(
int intmark,
uint dwIndex,
string strIndex,
object Handle,
object aryValue
)
Parameters
intmark[out]
回應資料的種類
0:系統資訊回應
1:RQ/RP資訊回應
2:訂閱資訊回應
dwIndex[out]
intmark:0時
1: Connect
2: Disconnect
14
3: SocketError
4: need to update DLL!
5: Not Connected, Can Not Login Yet!
6: 建立路徑失敗
intmark:1時
0: 總帳登入
1: 分戶登入
3: 尚未登入
4: 輸入的登入帳號錯誤
5: 功能代碼錯誤,或權限不足
6: 登入訂閱失敗
7: SocketRPRead失敗
8: 取消訂閱失敗
9: Sign on 失敗
10: Logout!
其他:為 16進制的 FunctionID (Ex:14640A0A)
strIndex[out]
字串型別的 FunctionID (Ex:20.100.10.10)
Handle[out]
回應訂閱事件時所傳入的 Handle值
aryValue
回傳的 byte[]的資料,以用來帶入 outMsgLoad方法
15
四、PolarisB2BAPI.dll 元件使用步驟說明
Step 1. 安裝 PolarisB2BAPI.dll 並將元件加入專案參考
先於 Dos 模式下註冊 DLL,並將其元件加入欲使用專案的參考當中
regsvr32 PolarisB2BAPI.dll
16
Step 2. 宣告並建立元件實體,並加入回應事件
PolarisB2BAPI.PolaisB2BTrader objB2BApi = new PolarisB2BAPI.PolaisB2BTrader();
objB2BApi.OnResponse += new
PolarisB2BAPI.IPolaisB2BTraderEvents_OnResponseEventHandler(objB2BApi_OnResponse);
Step 3. 使用 Open方法開啟與 Server的連線
objB2BApi.Open();
Step 4. 定義各 FunctionID 的結構擋
依照各功能元件 SPEC文件來定義結構檔以供程式處理
17
Step 5. 用各 inMsg 等方法來執行 RQ/RP 功能
Step 6. 等待 Server 回應訊息以觸發事件
void objB2BApi_OnResponse(int intmark, uint dwIndex, string strIndex, object Handle, object aryValue)
先判斷 intmark數值來確認為何種來源的訊息,接下來判斷 dwIndex看為何種功能碼的回應
Step 7 解讀回傳的資料
必須先將資料載入元件,再依序利用 outMsgGet…等方法解出內容
int intCheck = objB2BApi.outMsgLoad(abyData); //將資料載入元件
objB2BApi.outMsgGetLong(ref intCount);
objB2BApi.outMsgGetStr(ref strTemp, Marshal.SizeOf(struChildOut.struStkAccountInfo));
18
Step 8. 適時關閉與 Server 的連線,及釋放元件所占用的資源
objB2BApi.Close(); //關閉連線
Marshal.ReleaseComObject(objB2BApi); //釋放 objB2BApi 所佔用的資源
19
五、測試環境設定說明
1. 改 C:\WINDOWS\system32\drivers\etc\hosts
#B2BAPI 外部測試機(internet)
220.130.122.55 tradeapi.polaris.com.tw
2. Dos 模式下註冊 DLL(必須在資料夾所在路徑執行,參考第 15 頁)
regsvr32 PolarisB2BAPI.dll
( Win7 的系統 [開命令提示字元] 時要 ”以管理員身份執行”)
3. 點兩下 B110000005_TWCA.pfx,將此憑證匯入到 IE 瀏覽器內的憑證,
匯入密碼: yuanta
4. 開啟測試程式: PolarisB2BAPI_TestAP.exe
5. Click [Open]
6. 輸入帳號種類: S
7. 輸入帳號: 98875005091
8. 輸入密碼: 1234
9. Click [一般登入]
9.檢視訊息 測試線路是否接通!!
PS1.下單需要憑證,才可送單成功!
PS2.測試程式 AP 為標準公板, 並非所有功能都可使用~ 亦非實作所有功能
實際權限請查看 FunctionList.xls
PS3.測試程式 原始檔 僅提供參考,不保證程式效能最佳化!