Created
December 29, 2015 03:06
-
-
Save syctseng/01b502db761ee8be899c to your computer and use it in GitHub Desktop.
帳戶監視器_凱基期貨
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Intelligence; | |
using Package; | |
using Smart; | |
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Data; | |
using System.Drawing; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Timers; | |
using System.Windows.Forms; | |
namespace Watcher | |
{ | |
internal delegate void SetAddInfoCallBack(string text); | |
public partial class Form1 : Form | |
{ | |
private TaiFexCom tfcom; | |
private UTF8Encoding encoding = new System.Text.UTF8Encoding(); | |
private string broker = ""; | |
private string accountNO = ""; | |
private string id = ""; | |
private string password = ""; | |
private string AccountIni = @"account.ini"; | |
private string showAccountNO = ""; | |
private string Equity = ""; | |
private string outputPath = ""; | |
private int refrashInterval = 90000; | |
private static System.Timers.Timer aTimer; | |
public Form1() | |
{ | |
InitializeComponent(); | |
this.Text = "帳戶監視器"; | |
tfcom = new TaiFexCom("itrade.kgi.com.tw", 443, "API", false); | |
tfcom.ConnectTimeout = 5000; | |
tfcom.OnRcvMessage += OnRcvMessage; //資料接收事件 | |
tfcom.OnGetStatus += OnGetStatus; //狀態通知事件 | |
tfcom.OnRcvServerTime += OnRcvServerTime; //接收主機時間 | |
} | |
private string WatcherInitialized() | |
{ | |
string[] strAccountSplit; | |
if (!File.Exists(AccountIni)) | |
{ | |
AddInfo("The account.ini file doesn't exist." | |
+ Environment.NewLine | |
+ "Please create this file with account and password info."); | |
System.IO.File.WriteAllText(AccountIni, "brokerID,accountNumber,personalID,password"); | |
return ""; | |
} | |
else | |
{ | |
string AccountInfo = ""; | |
AccountInfo = System.IO.File.ReadAllText(AccountIni); | |
if (string.IsNullOrEmpty(AccountInfo)) | |
{ | |
AddInfo("KGI account ID & password are not stored at account.ini file." | |
+ Environment.NewLine | |
+ "Please add those info at that file and restart the program."); | |
return ""; | |
} | |
else | |
{ | |
AccountInfo = AccountInfo.Trim(); | |
strAccountSplit = AccountInfo.Split(','); | |
broker = strAccountSplit[0]; | |
accountNO = strAccountSplit[1]; | |
id = strAccountSplit[2]; | |
password = strAccountSplit[3]; | |
showAccountNO = "xxxx" + accountNO.Remove(0, 4); | |
return accountNO; | |
} | |
} | |
} | |
#region TaiFexCom API 事件 | |
private void OnGetStatus(object sender, COM_STATUS staus, byte[] msg) | |
{ | |
TaiFexCom com = (TaiFexCom)sender; | |
if (this.InvokeRequired) | |
{ | |
Smart.OnGetStatus_EventHandler d = new Smart.OnGetStatus_EventHandler(OnGetStatus); | |
this.Invoke(d, new object[] { sender, staus, msg }); | |
return; | |
} | |
OnGetStatusUpdateUI(sender, staus, msg); | |
} | |
private void OnRcvServerTime(Object sender, DateTime serverTime, int ConnQuality) | |
{ | |
if (this.InvokeRequired) | |
{ | |
Smart.OnRcvServerTime_EventHandler d = new Smart.OnRcvServerTime_EventHandler(OnRcvServerTime); | |
this.Invoke(d, new object[] { sender, serverTime, ConnQuality }); | |
return; | |
} | |
} | |
private void OnRcvMessage(object sender, PackageBase package) | |
{ | |
if (this.InvokeRequired) | |
{ | |
OnRcvMessage_EventHandler d = new OnRcvMessage_EventHandler(OnRcvMessage); | |
this.Invoke(d, new object[] { sender, package }); | |
return; | |
} | |
switch ((DT)package.DT) | |
{ | |
case DT.LOGIN: | |
P001503 p1503 = (P001503)package; | |
if (p1503.Code != 0) | |
AddInfo("登入失敗 CODE = " + p1503.Code + " " + tfcom.GetMessageMap(p1503.Code)); | |
else | |
{ | |
AddInfo("登入成功 "); | |
} | |
break; | |
#region 帳務查詢 | |
case DT.FINANCIAL_COVER_TRADER: //1614 | |
P001614 p1614 = (P001614)package; | |
AddInfo("RCV 1614 [" + p1614.Rows + "]"); | |
break; | |
case DT.INVENTORY_TRADER: //1616 | |
P001616 p1616 = (P001616)package; | |
AddInfo("RCV 1616 [" + p1616.Rows + "]"); | |
break; | |
case DT.INVENTORY_DETAIL_TRADER: //1618 | |
P001618 p1618 = (P001618)package; | |
AddInfo("RCV 1618 [" + p1618.Rows + "]"); | |
break; | |
case DT.FINANCIAL_COVER_TRADER_Detail: //1624 | |
P001624 p1624 = (P001624)package; | |
AddInfo("RCV 1624 [" + p1624.Rows + "]"); | |
break; | |
case DT.FINANCIAL_TRADERN: //1626 | |
P001626 p1626 = (P001626)package; | |
if (p1626.Count > 0) | |
{ | |
foreach (P001626_2 p1626b in p1626.p001626_2) | |
{ | |
Equity = p1626b.ActMarketValue; | |
} | |
AddInfo("權益回報:NT$ " + Equity); | |
outputPath = @textBox_輸出路徑.Text + showAccountNO + ".txt"; | |
System.IO.File.WriteAllText(outputPath, Equity); | |
} | |
//AddInfo("RCV 1626 [" + p1626.Count + "]" ); | |
break; | |
case DT.FINANCIAL_CURRENCY: //1628 | |
P001628 p1628 = (P001628)package; | |
AddInfo("RCV 1628 CODE= " + p1628.Code + " MSG=" + p1628.ErrorMsg); | |
break; | |
#endregion 帳務查詢 | |
} | |
} | |
private void OnGetStatusUpdateUI(object sender, COM_STATUS staus, byte[] msg) | |
{ | |
TaiFexCom com = (TaiFexCom)sender; | |
string smsg = null; | |
switch (staus) | |
{ | |
case COM_STATUS.LOGIN_READY: //登入成功 | |
// DoShowLogin(); | |
AddInfo("凱基下單登入成功:" + Environment.NewLine + com.Accounts); | |
break; | |
case COM_STATUS.LOGIN_FAIL: //登入失敗 | |
AddInfo(String.Format("凱基下單登入失敗:[{0}]", encoding.GetString(msg))); | |
break; | |
case COM_STATUS.LOGIN_UNKNOW: //登入狀態不明 | |
AddInfo(String.Format("凱基下單登入狀態不明:[{0}]", encoding.GetString(msg))); | |
break; | |
case COM_STATUS.CONNECT_READY: //連線成功 | |
smsg = "凱基下單伺服器" + tfcom.ServerHost + ":" + tfcom.ServerPort + Environment.NewLine + | |
"伺服器回應: [" + encoding.GetString(msg) + "]" + Environment.NewLine; | |
AddInfo(smsg); | |
break; | |
case COM_STATUS.CONNECT_FAIL: //連線失敗 | |
smsg = encoding.GetString(msg); | |
AddInfo("凱基下單連線失敗:" + smsg + " " + tfcom.ServerHost + ":" + tfcom.ServerPort); | |
break; | |
case COM_STATUS.DISCONNECTED: //斷線 | |
// DoShowLogout(); | |
smsg = encoding.GetString(msg); | |
AddInfo("凱基下單斷線:" + smsg); | |
break; | |
case COM_STATUS.SUBSCRIBE: | |
smsg = encoding.GetString(msg); | |
break; | |
case COM_STATUS.UNSUBSCRIBE: | |
smsg = encoding.GetString(msg); | |
break; | |
case COM_STATUS.ACK_REQUESTID: //下單或改單第一次回覆 | |
long RequestId = BitConverter.ToInt64(msg, 0); | |
byte status = msg[8]; | |
break; | |
} | |
} | |
#endregion TaiFexCom API 事件 | |
private void AddInfo(string msg) | |
{ | |
if (this.msgBox.InvokeRequired) | |
{ | |
SetAddInfoCallBack d = new SetAddInfoCallBack(AddInfo); | |
this.Invoke(d, new object[] { msg }); | |
} | |
else | |
{ | |
string fMsg = String.Format("[{0}] {1} {2}", DateTime.Now.ToString("HH:mm:ss:ffff"), msg, Environment.NewLine); | |
try | |
{ | |
if (msgBox.TextLength > 50000) msgBox.ResetText(); | |
msgBox.AppendText(fMsg); | |
} | |
catch { }; | |
} | |
} | |
private void button_登入登出_Click(object sender, EventArgs e) | |
{ | |
WatcherInitialized(); | |
if (button_登入登出.Text == "登出") | |
{ | |
tfcom.Logout(); | |
button_登入登出.Text = "登入"; | |
this.Text = "帳戶監視器:未登入"; | |
} | |
else | |
{ | |
tfcom.LoginDirect(tfcom.ServerHost, tfcom.ServerPort, id, password, ' '); | |
button_登入登出.Text = "登出"; | |
this.Text = "帳戶監視器:已登入"; | |
} | |
} | |
private void SetTimer() | |
{ | |
aTimer = new System.Timers.Timer(refrashInterval); | |
aTimer.Elapsed += OnTimedEvent; | |
aTimer.AutoReset = true; | |
aTimer.Enabled = true; | |
} | |
private void OnTimedEvent(object sender, ElapsedEventArgs e) | |
{ | |
if (this.Text == "帳戶監視器:監視中") | |
GetEquity(); | |
else | |
aTimer.Stop(); | |
} | |
private void GetEquity() | |
{ | |
long rtn = tfcom.RetriveFMargin("I", broker, accountNO, "", ""); | |
if (rtn == -1) | |
MessageBox.Show("無查詢權限:請確認 分公司代號 與 帳戶號碼"); | |
else | |
this.Text = "帳戶監視器:監視中"; | |
} | |
private void button_監視_Click(object sender, EventArgs e) | |
{ | |
if (button_監視.Text == "監視中") | |
{ | |
button_監視.Text = "開始監視"; | |
this.Text = "帳戶監視器:已登入"; | |
} | |
else if (this.Text == "帳戶監視器:已登入") | |
{ | |
button_監視.Text = "監視中"; | |
GetEquity(); | |
SetTimer(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment