我现在是用一个小程序TWSStart来完成这件事。 不过我想自己写一个小程序来做这件事。而且我想要换成IB_Gateway来做交易,这样的话TWSStart就用不上了,只能自己写自动login程序了。 用VBS或者批处理应该可以写吧,不过我不知道怎么做,请高人指点一下。
以前用AutoHotkey写过,但是我认为基本差别不大。先运行tws.bat得到tws的handle,或者你可以通过FindWindow api或者AppActivate得到tws的handle。有了这个handle,你可以使用SendKeys api来控制它。
下面是一个很早以前的AutoHotkey的代码,估计对现在版本的tws不好使了。你也可以利用Java写的IBController。 Code: #WinActivateForce ;#include sc_security.ahk DetectHiddenWindows, Off FileReadLine, ib_path, ib_controller.cfg, 1 FileReadLine, restart_time, ib_controller.cfg, 2 Main: ;DetectHiddenWindows, On ib_id = myin1 ib_password = auto_IB = y minimize_ib = n allow_auto_logoff = y first_time = y ;last_check_time:= getHMTime() GoSub, IBLoginInfo Loop { IfWinNotExist, IB Login Info break Sleep, 200 } ;SetTimer, PosTweak, 250 SetTimer, CheckIB, 2000 ;menu k_MenuItemIB = Start AutoIB k_MenuItemNoIB = Stop AutoIB k_MenuItemCloseIB = Close IB k_MenuItemMinimizeIB = Minimize IB k_MenuItemRestoreIB = Restore IB Menu, Tray, Add, %k_MenuItemMinimizeIB%, Minimize_Restore_IB Menu, Tray, Add, %k_MenuItemNoIB%, Autorun_IB Menu, Tray, Add, %k_MenuItemCloseIB%, Close_IB Menu, Tray, Add, &Exit, k_MenuExit Menu, Tray, NoStandard return GuiClose: k_MenuExit: IfWinExist, New Login WinKill, New Login IfWinExist, Login WinKill, Login IfWinExist, Trader Workstation WinClose, Trader Workstation IfWinNotExist, Universal Account WinClose, Universal Account ExitApp IBLoginInfo: Gui, 5:+AlwaysOnTop -Resize -MinimizeBox Gui, 5:Add, Text,, Usernamee: Gui, 5:Add, Text,, Password: Gui, 5:Add, Edit, vuser_input_id w130 +Password ym, %ib_id% Gui, 5:Add, Edit, vuser_input_password w130 +Password GuiControl, 5:Focus, user_input_password Gui, 5:Add, Button, gIBLoginOK x33 w70, OK ; Gui, 5:Add, Button, gIBLoginCANCEL w70 x+15, CANCEL ; Gui, 5:Show,, IB Login Info Return IBLoginOK: Gui, 5:Submit ib_id = %user_input_id% ib_password=%user_input_password% Gui, 5:Destroy return 5GuiClose: IBLoginCANCEL: Gui, 5:Destroy ExitApp Return getHMTime() { FormatTime, TimeString, T12, Time StringSplit, hms, TimeString, :, . hm_time=%hms1% hm_time*=100 hm_time+=%hms2% return %hm_time% } Close_IB: IfWinNotExist, Universal Account return WinClose, Universal Account If auto_IB = y { auto_IB = n Menu, Tray, Rename, %k_MenuItemNoIB%, %k_MenuItemIB% } Return Autorun_IB: If auto_IB=n { auto_IB = y Menu, Tray, Rename, %k_MenuItemIB%, %k_MenuItemNoIB% } Else { auto_IB = n Menu, Tray, Rename, %k_MenuItemNoIB%, %k_MenuItemIB% } Return Minimize_Restore_IB: If minimize_IB=n { minimize_IB=y ;WinHide, Universal Account Menu, Tray, Rename, %k_MenuItemMinimizeIB%, %k_MenuItemRestoreIB% } Else { minimize_IB=n ;WinShow, Universal Account WinMove, Universal Account,, 700, 110 ;WinActivate, Universal Account Menu, Tray, Rename, %k_MenuItemRestoreIB%, %k_MenuItemMinimizeIB% } Return CheckIB: ;auto-minimize IfEqual, minimize_ib, y WinMinimize, Universal Account ;auto logoff If allow_auto_logoff = n { IfWinExist, Exit Session Setting { BlockInput, On WinActivate, Exit Session Setting Send, {Tab 5} Sleep, 100 Send, {Tab 2} ;send 2 more tab to make sure it is in the end Sleep, 50 ;very careefull when go back, give computer enough time to respond Send,+{TAB} ;Close Sleep, 50 Send,+{TAB} ;Update Sleep, 50 Send,+{TAB} ;PM Sleep, 50 ;Now in PM area FormatTime, TimeString,, Time StringSplit, time_array, TimeString, %A_Space% WinActivate, Exit Session Setting If time_array2=PM { Sleep, 50 Send,+{TAB} ;AM Sleep, 50 Send, {SPACE} Sleep, 50 Send,{TAB} ;back to PM } else Send, {SPACE} Sleep, 50 Send,{TAB} ;to Update Sleep, 50 Send,{TAB} ;to Close Sleep, 50 Send, {SPACE } Sleep, 50 Send, {SPACE } ;send it again to make sure it is closed BlockInput, Off } } /* IfWinExist, Exit Session Setting { FormatTime, TimeString,, Time StringSplit, time_array, TimeString, %A_Space% WinActivate, Exit Session Setting If time_array2=AM MouseClick, Left, 260, 85 Else MouseClick, Left, 145, 85 Sleep, 100 MouseClick, Left, 140, 125 Sleep, 100 MouseClick, Left, 215, 125 } */ ;auto accept connection IfWinExist, Trader Workstation { WinActivate, Trader Workstation Sleep, 100 Send, {TAB} Send, {ENTER} Send, {SPACE} ;not working this one ;MouseClick, Left, 110, 85 } IfWinExist, IB Trader Workstation { WinActivate, IB Trader Workstation Sleep, 100 Send, {TAB} Send, {ENTER} Send, {SPACE} ;not working this one ;MouseClick, Left, 110, 85 } If auto_IB=n Return IfWinExist, Universal Account Return ;clean up IfWinExist, New Login WinKill, New Login IfWinExist, Login WinKill, Login ;no need to wait when start-up If first_time=y { first_time = n Gosub, RunIB Return } ;wait for one minute when tws closes sleep_minutes = 1000*120 Sleep, %sleep_minute% Gosub, RunIB Return RunIB: IfExist, %ib_path% RunWait, %ib_path% Else Return WinWait, New Login BlockInput, On WinActivate, New Login Sleep,500 MouseClick, Left, 60, 60 IfWinActive, New Login,,Send, %ib_id% Send, {Tab} StringLen, ib_pwd_length, ib_password IfNotEqual, ib_pwd_length, 0 { IfWinActive, New Login,,Send, %ib_password% Sleep,500 Send,{Enter} } BlockInput, Off WinWait, Universal Account,,120 Return
多谢各位解答,autoit还真没用过,有时间看看去。 有个朋友帮我写的bat和vbs代码解决了这个下问题,我试了一下,很好用: 1. autologin.bat start ..\JDK\1.5\bin\javaw -cp jts.jar;hsqldb.jar;jcommon-1.0.12.jar;jhall.jar;other.jar;rss.jar -Dsun.java2d.noddraw=true -Xmx512M ibgateway.GWClient . cscript autologin.vbs 2. autologin.vbs set WshShell = WScript.CreateObject("WScript.Shell") Do While WshShell.AppActivate("IB Gateway") = False WScript.Sleep(1000) WScript.Echo(".") Loop WScript.Sleep(1000) WshShell.SendKeys ("用户名{TAB}") WScript.Sleep(1000) WshShell.SendKeys ("密码{ENTER}")