如何在程序中增加一个定时器轮询?
EasyLanguage中提供Timer(定时器)可以每隔特定的时间执行一次特定的代码,对于某些操作来说,这是一个很有用的工具,如:每30秒检查一次委托中尚未成交的委托并撤单等。
示例1:使用Timer定时检查委托状态
using guosen;
using elsystem;
using tsdata.common;
using tsdata.marketdata;
using elsystem.collections;
vars:
Timer timer1(null),
guosen.OrderTicket otk1(null),
guosen.Order ord(null),
string accID("410038122059");//根据情况修改Account ID
method void AnalysisTechnique_Initialized( elsystem.Object sender, elsystem.InitializedEventArgs args )
begin
timer1 = new Timer;
timer1.Interval = 3000;//Timer的间隔为3000毫秒,也即3秒
timer1.Elapsed += timer1_Elapsed;
timer1.Enable = true;
otk1 = new guosen.OrderTicket;
otk1.Symbol = "000002.SZ";
otk1.Quantity = 100;
otk1.BypassClientValidation = True;//跳过本地验证
otk1.Account = accID;
otk1.Action = OrderAction.Buy;
otk1.SymbolType = tsdata.common.SecurityType.Stock;
otk1.Type = tsdata.trading.OrderType.Market;//市价单
otk1.Duration="AUT";//自动选择期限
otk1.ExtendedProperties.SetItem("OrderName", "customOrderIDxxxxxxxx");//自定义的OrderName
ord = otk1.Send();//发送委托
end;
Method void timer1_Elapsed(elsystem.Object sender,elsystem.TimerElapsedEventArgs args)
Begin
print(DateTime.Now.ToString()+" Timer Elapsed. " + "OrderState:"+ord.State.ToString());
End;
//打印语句输出栏结果示例:
//2016/12/6 15:52:49 Timer Elapsed. OrderState:filled
//2016/12/6 15:52:52 Timer Elapsed. OrderState:filled
//2016/12/6 15:52:55 Timer Elapsed. OrderState:filled
//2016/12/6 15:52:58 Timer Elapsed. OrderState:filled