关于逐周期模式下的Variable型全局变量的理解:
1.Variable型全局变量的作用范围是单个品种上的每个计算周期。
2.Variable型全局变量在历史测试时只在一个品种上计算一次,所以可以实现全局变量的作用。
3.Variable型全局变量在图表交易时在每个品种的每个Tick上均计算一次,在一个新的计算周期开始时会重新初始化,但因为与holding配合使用,实际每个过去发生的K线上的持仓状态是稳定的,所以也可以实现全局变量的作用。
4.Variable型全局变量在后台交易时在每个品种的每个Tick上均计算一次,在一个新的计算周期开始时会重新初始化,但因为与tholding配合使用,实际每个过去发生的K线上的持仓状态是非稳定的,所以无法实现全局变量的作用。如下面的这段代码:
variable:s=0;
if tholding=0 then begin
s:=1;
end;
if tholding>0 then begin
s:=2;
end;
在逐周期模式下,在每根K线上,代码都会重新执行一次.
但,Variable型的全局变量,只会在一开始的时候初始化一次,随后的每个周期,会根据具体条件对此全局变量赋予不同的值,
从而可以让编程者根据其返回值再行判断和利用.
下面是一个根据楼主提供信息改编的一个简单的例子,希望可以帮助楼主更好的理解和使用Variable型的全局变量
//IF合约,1分钟周期,日内
ma5:ma(close,5);
ma15:ma(close,15);
//开多平空
if CROSS(ma5,ma15) and time>090100 and time<145000 then
begin
sellshort(holding<0,0,thisclose);
buy(holding=0,1,thisclose);
end
//开空平多
if CROSS(ma15,ma5) and time>090100 and time<145000 then
begin
sell(holding>0,0,thisclose);
buyshort(holding=0,1,thisclose);
end
//收盘前5分钟平仓
if time > 145500 then
begin
sell(holding > 0, 0, thisclose);
sellshort(holding < 0, 0, thisclose);
end
variable:s=0; //初始化s=0
if holding>0 then begin
s:=1; // 可以得到 holding>0-----s = 1
end;
if holding<0 then begin
s:=2; //可以得到 holding<0-----s = 2
end;
if holding=0 then begin
s:=-1; //可以得到 holding<0-----s= -1
end
ss:s,linethick0;
楼主可以在K线图上看到,S只有在一开始的时候,才被初始化过一次,为0;随后,S一直在1,2,-1之间变化