Params
Numeric Length1(5);
Numeric Length2(10);
Numeric Length3(20);
Vars
Series<Numeric> MA05;
Series<Numeric> MA10;
Series<Numeric> MA20;
Series<Numeric> my_exitprice_1(0);
Series<Numeric> my_exitprice_2(0);
Series<Numeric> my_entryprice_1(0);
Series<Numeric> my_entryprice_2(0);
Global Numeric s1(0);
Global Numeric s2(0);
Global Numeric s3(0);
Global Numeric s4(0);
Global Numeric CD(0);
Global Integer my_order;
Global Array<Integer> oid;
Global Numeric lots1(0);
Global Numeric lots2(0);
Events
OnInit()
{
//设置最大连续建仓次数
SetConsecEntries(2);
}
OnPosition(PositionRef pos)
{
lots1=pos.longCurrentVolume;
lots2=pos.shortCurrentVolume;
}
OnBar(ArrayRef<Integer> indexs)
{
//PlotAuto(\"空头数\",text(lots2),High,Yellow);
//PlotAuto(\"多头数\",text(lots1),Low,Red);
{
MA05=Average(Close[1],Length1);
MA10=Average(Close[1],Length2);
MA10=Average(Close[1],Length3);
//````````````````````````````````````````````````
If( MarketPosition==0 And Close[1] > MA20 )
{
Buy(1,Open);
s1=1;
}
//--------------------------------------开多------------------------------------------------------------------
If ( MarketPosition>0 And Close[1]<MA10 )
{
Sell(lots1,Open);
s1=0;
}
//--------------------------------------平多-------------------------------------------------------------------
If( MarketPosition==0 And Close[1] < MA20 )
{
SellShort(1,Open);
s1=3;
}
//---------------------------------------开空-------------------------------------------------------------------
If( MarketPosition<0 And Close[1]>MA10 )
{
BuyToCover(lots2,Open);
s1=0;
}
//---------------------------------------------平空-------------------------------------------------------------
//-------------------------------------------挂单开仓-----------------------------------------------------------
If(BarStatus==2)
{
If(s1==1 And s3==0)
{
BOOL RET =A_SendOrderEx(Enum_Buy,Enum_Entry,1,close[1]-5,oid);
If(RET ==True)
{
s1=0;
s3=1;
}
}
Else If(s3==1 And Close[1]< Min(Close[2],Open[2]) And Close[1]< min(Close[3],Open[3]))
{
A_DeleteOrderEx(oid[0]);
s1=0;
s3=0;
}
//------- ------------ ---------- ---------- --------- ---------
If(s1==3 And s4==0)
{
BOOL RET =A_SendOrderEx(Enum_sell,Enum_Entry,1,close[1]-5,oid);
If(RET ==True)
{
s1=0;
s4=1;
}
}
Else If(s4==1 And Close[1]>Max(Close[2],Open[2]) And Close[1]>Max(Close[3],Open[3]))
{
A_DeleteOrderEx(oid[0]);
s1=0;
s4=0;
}
//------------------------------------------新高新低止盈止损·····················································
If(MarketPosition>0 And Low<my_exitprice_1)
{
Sell(lots1,my_exitprice_1);//这里的lots1平仓时无法获取持仓量
Commentary(\"新低止损=\"+Text(my_exitprice_1));
}
If(MarketPosition<0 And High>my_exitprice_1)
{
BuyToCover(lots2,my_exitprice_1);//这里的lots2平仓时无法获取持仓量
Commentary(\"新高止损=\"+Text(my_exitprice_1));
}
//------------------------------------------------------------------------------------------------------------
}
}
}
老师,我这个onposition无法获取持仓量请问怎么解决。我是先用BUYSELL追单,然后用A函数挂单,因为挂单不一定能成交,所以我就用onposition来获取持仓量,在到达平仓条件后把所有仓位平掉。但是失败了
如果我纯粹用BUYSELL下单,挂单也是用BUYSELL,就不会出现这个问题,ONPOSITION是可以正常获取我的持仓量的。麻烦老师帮我看看,谢谢
随便搞了个程序,挺正常的触发啊
//------------------------------------------------------------------------
// 简称: test_2023_0301
// 名称:
// 类别: 公式应用
// 类型: 用户应用
// 输出: Void
//------------------------------------------------------------------------
Params
//此处添加参数
Vars
//此处添加变量
Global Numeric a;
Global Array<Integer> ordid;
Global Numeric t1;
Defs
//此处添加公式函数
Events
//此处实现事件函数
//初始化事件函数,策略运行期间,首先运行且只有一次
OnInit()
{
a=0;
}
OnPosition(PositionRef pos)
{
print(CurrentTime=+text(CurrentTime));
print(pos.long=+text(pos.longCurrentVolume));
t1 = pos.longCurrentVolume;
print(---------------------);
}
//Bar更新事件函数,参数indexs表示变化的数据源图层ID数组
OnBar(ArrayRef<Integer> indexs)
{
if(a%10==1 and BarStatus==2)
{
A_SendOrderEx(Enum_Buy,Enum_Entry,1,close+20,ordid);
}
a = a+1;
print(a=+text(a));
Commentary(t1=+text(t1));
}
//------------------------------------------------------------------------
// 编译版本 2023/03/01 142024
// 版权所有 wangkaiming
// 更改声明 TradeBlazer Software保留对TradeBlazer平台
// 每一版本的TradeBlazer公式修改和重写的权利
//------------------------------------------------------------------------
老师,我也不知道什么原因,都还没成交呢,print控制台就已经显示有仓位了
持仓改变时才能触发,你确定挂单当时持仓改变了吗
是的老师,已经成交了