onposition域,如果当日没有成交,natural变量的数据都传不进去,在onbar域里头运行A_GetPosition都没用。这就造成在onposition域如果当日没有成交,pos就一直为空,pos结构体的数据都是空的。如果当日没有成交,在positiond域用global pos1=pos向其域传递pos都是空的,会一直显示昨仓为0。成交一次,驱动onposition工作一次后即正常。只要的单元重启,onpositon里,重启前没平仓的持仓就又不准确了。A_GetPosition在其它域能正常获得pos结构体数据。
实验代码如下:
Params //参数
Vars
Natural Order ordnat;
Natural Position POSnat;
Natural Array<Integer> orderid;
Natural Integer longCansell;
Natural Integer shortCansell;
Natural Integer BSCount;
Defs
Events //关键字,策略启始标志。以下实现事件函数
OnInit() //初始化事件函数,策略运行期间,首先运行且只有一次,应用在订阅数据等操作
{
A_SubscribeTradeByCreateId(Enum_Trade_Source_all);
}
OnReady() //在策略执行前的初始化事件(数据源数据准确完成)之后,只运行一次
{
Range[0:DataCount-1]
{
print("");
print("---onreday---");
}
}
onBar(ArrayRef<Integer> indexs)//Bar更新事件函数,参数indexs表示变化的数据源图层ID数组
{
Range[0:DataCount-1]
{
Commentary("多仓量: "+text(POSnat.longCurrentVolume));
Commentary("空仓量: "+text(POSnat.SHORTCurrentVolume));
Commentary("多头可平仓数量 : "+text(longCansell));
Commentary("空头可平仓数量: "+text(shortCansell));
}
}
OnPosition(PositionRef pos) //持仓更新事件函数,参数pos表示更新的持仓结构体
{
Range[0:DataCount-1]
{
print(" ");
print("---ontposition--- ");
bool ret=A_GetPosition(Symbol,POS, "",0);
if(ret==true)
{
print(text(pos));
longCansell= POS.longCansellVolume;//多头可平仓数量
shortCansell= POS.shortCancoverVolume;//空头可平仓数量
}
else print("同步不成功");
}
}
补充说明一下,建议您通过fileappend来输出。然后通过数据中心来查看文件
亲测,OnBar事件域是可以通过A_GetPosition读取到持仓数据的。如果你觉得读不到,麻烦提供读取不到的示例代码。