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是可以正常获取我的持仓量的。麻烦老师帮我看看,谢谢
老师,我也不知道什么原因,都还没成交呢,print控制台就已经显示有仓位了
是的老师,已经成交了