本脚本主要是每天自动按照交易时间启动交易软件,定时关闭软件。除了需要脚本启动外,还需要在开拓者中做账户密码设置等操作。从TB5以后,只要设置好密码及自动启动程序化交易,很多事情就变得容易起来。本脚本略去邮件提醒功能。每天收盘在c盘根目录的1.xls文件中记录每天收盘时的账户权益后,关闭TB。文件编译成exe文件后,放入启动项中随系统启动。
#include<file.au3>
#Include <date.au3>
#include <Array.au3>
#include <Excel.au3>
#include <Misc.au3>
Local $version="htstart"
if _Singleton($version,1)=0 Then;禁止同个脚本重复运行,对于程序化交易来说非常重要
Exit
EndIf
Dim $result,$tmpl,$path,$TBloginname,$TBloginpassword,$cal,$strNowDate,$Holiday,$bIsHoliday;定义变量
dim $sFilePath1,$oExcel,$val1,$val2,$H1
$TBloginname="*********" ;用你的TB登陆账号替换***
$TBloginpassword="******" ;用你的TB登陆密码替换***
$strNowDate =_NowDate();取得系统当前日期,务必设置系统日期为短格式日期,使之与ini文件中的日期格式保持一致
$Holiday=IniRead("C:\price.ini", "holiday","年","");读取ini中的所有非周末节假日日期
$bIsHoliday = StringInStr($Holiday,$strNowDate);检测当天是否是非周末节假日,如果是则返回0,不启动TB
While 1;程序常驻
while (WinExists("帐户登录")) ;启动前先检测是否存在账户登录界面,如果存在则点击登录按钮
WinActivate("帐户登录")
Sleep(500)
ControlFocus("帐户登录","","Button1")
Sleep(500)
ControlClick("帐户登录","","Button1")
Sleep(10000)
WEnd
If @WDAY>1 And @WDAY<7 and $bIsHoliday =0 And ((@HOUR=8 and @MIN>=40 And @MIN<=59) Or (@HOUR>=9 And @HOUR<15) Or (@HOUR=20 and @MIN>=40 And @MIN<=59) Or (@HOUR>=21 And @HOUR<=23)) Then;交易时段定时启动TB,节假日不启动
if(WinExists("交易开拓者错误报告发送")) Then
WinClose("交易开拓者错误报告发送")
EndIf
if(WinExists("TradeBlazer")) Then
WinClose("TradeBlazer")
EndIf
if(WinExists("提示")) Then
WinClose("提示")
EndIf
Sleep(5000)
If ProcessExists("TradeBlazer.exe") And WinExists("交易开拓者平台(旗舰版)") = 0 Then
ProcessClose("TradeBlazer.exe")
ProcessClose("TBDataCenter.exe")
Sleep(10000)
EndIf
If WinExists("交易开拓者平台(旗舰版)") = 0 Then
Run("C:\tbv5612_portable\TradeBlazer.exe","C:\tbv5612_portable\");根据TB的安装目录修改
Sleep(5000)
WinWaitActive("[CLASS:#32770]","",30)
Sleep(1000)
WinActivate("[CLASS:#32770]")
ControlFocus("[CLASS:#32770]","","Edit1")
Sleep(500)
ControlSetText("[CLASS:#32770]","","Edit1",$TBloginname)
Sleep(15)
ControlFocus("[CLASS:#32770]","","Edit2")
Sleep(500)
ControlSetText("[CLASS:#32770]","","Edit2",$TBloginpassword)
Sleep(15)
ControlFocus("[CLASS:#32770]","","Edit2")
Sleep(500)
ControlSetText("[CLASS:#32770]","","Edit2",$TBloginpassword)
Sleep(15)
ControlFocus("[CLASS:#32770]","","Button2")
Sleep(15)
ControlClick("[CLASS:#32770]","","Button2")
Sleep(5000)
WinActivate("交易开拓者平台(旗舰版)")
MouseClick("left",400,300,1)
Sleep(5000)
WinActivate("交易开拓者平台(旗舰版)")
MouseClick("left",400,300,1)
Sleep(5000)
WinActivate("交易开拓者平台(旗舰版)")
MouseClick("left",400,300,1)
Sleep(5000)
WinActivate("交易开拓者平台(旗舰版)")
MouseClick("left",400,300,1)
Sleep(5000)
WinActivate("交易开拓者平台(旗舰版)")
MouseClick("left",400,300,1)
Sleep(5000)
WinActivate("交易开拓者平台(旗舰版)")
MouseClick("left",400,300,1)
Sleep(5000)
WinActivate("交易开拓者平台(旗舰版)")
MouseClick("left",400,300,1)
Sleep(5000)
WinActivate("交易开拓者平台(旗舰版)")
MouseClick("left",400,300,1)
Sleep(40000)
MouseClick("left",400,300,1)
Sleep(20000)
if(WinExists("交易开拓者错误报告发送")) Then
WinClose("交易开拓者错误报告发送")
EndIf
if(WinExists("TradeBlazer")) Then
WinClose("TradeBlazer")
EndIf
if(WinExists("提示")) Then
WinClose("提示")
EndIf
WinWaitActive("交易开拓者平台(旗舰版)","",30)
Sleep(1000)
WinActivate("交易开拓者平台(旗舰版)")
Sleep(2000)
WinMenuSelectItem("交易开拓者平台(旗舰版)","","文件(&F)", "启动所有自动交易")
EndIf
EndIf
If WinExists("交易开拓者") And @WDAY>1 And @WDAY<=7 and $bIsHoliday =0 And Not((@HOUR=8 and @MIN>=40 And @MIN<=59) Or (@HOUR>=9 And @HOUR<15) Or (@HOUR=20 and @MIN>=40 And @MIN<=59) Or (@HOUR>=21 And @HOUR<=23) Or (@HOUR>=0 And @HOUR<1)) Then;在c盘根目录的1.xls文件中记录每天收盘时的账户权益后,关闭TB
if ControlListView ("交易开拓者平台(旗舰版)", "", "SysListView321", "GetText",0,0)<>"" and $val1="" and @WDAY>1 And @WDAY<=6 And @HOUR*100=1500 Then
$sFilePath1 = "c:\1.xls"
$oExcel = _ExcelBookOpen($sFilePath1,0)
_ExcelSheetActivate($oExcel, "Sheet1")
$H1=1
Do
$val2=_ExcelReadCell($oExcel,$H1,1)
$H1=$H1+1
Until $val2=""
if _ExcelReadCell($oExcel, $H1-2,1)<>_NowDate() then
_ExcelWriteCell($oExcel, _NowDate(),$H1-1, 1)
_ExcelWriteCell($oExcel,ControlListView ("交易开拓者平台(旗舰版)", "", "SysListView321", "GetText",0,0),$H1-1, 2)
_ExcelWriteCell($oExcel,ControlListView ("交易开拓者平台(旗舰版)", "", "SysListView321", "GetText",0,1),$H1-1, 3)
_ExcelWriteCell($oExcel,ControlListView ("交易开拓者平台(旗舰版)", "", "SysListView321", "GetText",0,2),$H1-1, 4)
_ExcelWriteCell($oExcel,ControlListView ("交易开拓者平台(旗舰版)", "", "SysListView321", "GetText",0,3),$H1-1, 5)
_ExcelWriteCell($oExcel,ControlListView ("交易开拓者平台(旗舰版)", "", "SysListView321", "GetText",0,4),$H1-1, 6)
_ExcelWriteCell($oExcel,ControlListView ("交易开拓者平台(旗舰版)", "", "SysListView321", "GetText",0,5),$H1-1, 7)
_ExcelWriteCell($oExcel,ControlListView ("交易开拓者平台(旗舰版)", "", "SysListView321", "GetText",0,6),$H1-1, 8)
_ExcelWriteCell($oExcel,ControlListView ("交易开拓者平台(旗舰版)", "", "SysListView321", "GetText",0,7),$H1-1, 9)
_ExcelWriteCell($oExcel,ControlListView ("交易开拓者平台(旗舰版)", "", "SysListView321", "GetText",0,8),$H1-1, 10)
_ExcelWriteCell($oExcel,ControlListView ("交易开拓者平台(旗舰版)", "", "SysListView321", "GetText",0,9),$H1-1, 11)
EndIf
$val1=_ExcelReadCell($oExcel,$H1-1,1)
_ExcelBookSave($oExcel)
_ExcelBookClose($oExcel)
Sleep(5000)
If ProcessExists("Excel.exe") Then
ProcessClose("Excel.exe")
EndIf
EndIf
Sleep(240000)
WinActivate("交易开拓者平台(旗舰版)")
Sleep(500)
WinMenuSelectItem("交易开拓者平台(旗舰版)","","文件(&F)", "退出")
Sleep(1000)
ControlFocus("确认","","Button2")
ControlClick("确认","","Button1")
Sleep(5000)
If WinExists("保存工作区") Then
Sleep(10)
ControlFocus("保存工作区","","Button2")
ControlClick("保存工作区","","Button2")
Sleep(15000)
EndIf
EndIf
WEnd
转载自原文链接, 如需删除请联系管理员。
原文链接:autoit与程序化交易软件开拓者TB无人值守之一——启动开拓者TB并将每天收盘时的账户权益写入EXCEL,转载请注明来源!