全部 智大领峰 TBQuant功能 TBL语言 TB开户 问答专区 高手圈 其他
实盘模拟中开仓条件未达成,但开仓了
2024-09-03 10:56

主要存在两个问题:1.按如下代码,在实盘中尽管没有达成策略开仓条件,但是一点击启动自动交易就会报单。

Params

Numeric Length(14); //周期

Numeric Length2(20);

Numeric Constt(0.8); // 通道倍数

Numeric qushi(0.2);

Vars


plot pen1;

plot pen2;

Series<Numeric> UpLine; //上轨

Series<Numeric> DownLine; //下轨

Series<Numeric> MidLine; //中间线

Series<Numeric> AvgRange;

Numeric thigh;

Numeric tlow;

Series<Numeric> TRange;

Series<Numeric> angle;

Series<Numeric> AvgValue1;

Series<Numeric> AvgValue2;

Series<Numeric> xielv;

Series<Numeric> avgxielv;

Series<Numeric> fcstup;

Series<Numeric> fcstdown;

Series<Numeric> fcstmid;

Numeric money;//开仓资金

Numeric myprice;//委托价格

Numeric lmrate;//多头保证金率

Numeric smrate;//空头保证金率

   Numeric lots;//委托数量

   Numeric lotsl;//委托数量

   Numeric lotss;//委托数量

   Global Numeric risk;//账户当前风险程度

   Series<Numeric> lposition(0);

   Series<Numeric> sposition(0);

   Series<Numeric> mylentry;

   Series<Numeric> mysentry;

  Series<String> entr;

 

 

Events

OnInit()

   {

    pen1.figure(0);

    pen2.figure(0);

   }

   

   OnReady()

{

SetBackBarMaxCount(1+Max(Length,Length2));

}

OnBarOpen(ArrayRef<Integer> indexs)

{

Numeric slope;

       Numeric angle;

       Numeric intercept;

       Numeric val;

Bool ret = LinearReg(MidLine[1], Length, 0, slope, angle, intercept, val);//求线性回归

       xielv = slope;

       avgxielv = AverageFC(xielv,Length);

       fcstmid = val;  

       

}

OnBar(ArrayRef<Integer> indexs)

{

risk = A_totalMargin(0) / A_CurrentEquity(0);//定义risk为保证金占动态权益比例

Numeric a;

Numeric result = 1;

for a = 0 to DataSourceSize-1

{

result = result*data[a].BarExistStatus;

}

If (result <> 1) Return;//检查跨周期数据源是否闪烁


MarginRate mRate;//获取账户对应合约的保证金率

       A_GetMarginRate(Symbol, mRate);

       lmrate = mRate.longMarginRatio;

       smrate = mRate.shortMarginRatio;

       

       Position pos;//获取指定合约当前仓位

       A_GetPosition(pos, );

       lposition = pos.longCurrentVolume;

       sposition = pos.shortCurrentVolume;

       

Numeric slope;

       Numeric angle;

       Numeric intercept;

       Numeric val;

Bool ret = LinearReg(MidLine[1], 2, 0, slope, angle, intercept, val);//求线性回归

       pen2.setOption(中线斜率,color,blue);

       pen2.setOption(斜率均值,color,red);

       pen2.setOption(趋势下限,color,white);

       pen2.setOption(趋势上限,color,white);

       pen2.setOption(0,color,white);

pen2.line(中线斜率,xielv);

pen2.line(斜率均值, avgxielv);

pen2.line(0, 0);

pen2.line(趋势下限,- qushi);

pen2.line(趋势上限, qushi);

       

Range[0:0]

{

MidLine = AverageFC((high+low)/2,Length);

AvgRange = Average(TrueRange,Length); // 计算真实波动均值(atr)

UpLine = MidLine + AvgRange*Constt; // 计算通道上轨=均线+1.2倍的10周期真实波动值

DownLine = MidLine - AvgRange*Constt;// 计算通道下轨=均线-1.2倍的10周期真实波动值

Numeric slope;

Numeric angle;

Numeric intercept;

Numeric val;

Bool ret = LinearReg(upLine[1], Length, 0, slope, angle, intercept, val);//求线性回归

fcstup = val;

LinearReg(downLine[1], Length, 0, slope, angle, intercept, val);

fcstdown = val;

PlotNumeric(UpLine,fcstup);

PlotNumeric(DownLine,fcstdown);

PlotNumeric(midLine,fcstmid);

if(CurrentBar >= Max(Length,Length2))

           {

Commentary(始动态权益: + Text(A_CurrentEquity));

Commentary(始保证金占用: + Text(A_totalMargin));

Commentary(斜率: + Text(avgxielv));

Commentary(始风险度:+Text(risk));


If(CurrentTime < 0.09 ) Return;

If(CurrentTime < 0.1030 && CurrentTime > 0.1015) Return;

If(CurrentTime < 0.1330 && CurrentTime > 0.1130) Return;

If(CurrentTime < 0.21 && CurrentTime > 0.15) Return;

money =  A_CurrentEquity * 0.3;//默认为30%

myprice = Open;//这里使用open,更为精确的是使用委托价格

lotsl = IntPart(money/(myprice*contractunit*BigPointValue* lmrate)); //计算多头开仓手数

mylentry = Max(1,lotsl);

lotss = IntPart(money/(myprice*contractunit*BigPointValue* smRate)); //计算空头开仓手数

mysentry = Max(1,lotss);

If(risk <= 0.4 && lPosition == 0 && abs(avgxielv) <=  qushi && Low <= fcstmid )//震荡多头开仓1

{

Array<Integer> orderids;

A_SendOrderEx(enum_Buy, Enum_Entry, mylentry,Q_AskPrice/rollover, orderids,,);//低线做多

entr = dl;

}

If( risk <= 0.4 && sposition == 0 && abs(avgxielv) <=  qushi && high >= fcstmid ) //震荡空头开仓3

{

Array<Integer> orderids;

A_SendOrderEx(Enum_Sell,Enum_Entry, mysentry, Q_BidPrice/rollover, orderids,,);//高线做空

entr = ul;

}

           }

}

}

OnBarClose(ArrayRef<Integer> indexs)

{

risk = A_totalMargin(0) / A_CurrentEquity(0);//定义risk为保证金占动态权益比例

Commentary(终风险度:+Text(risk));

Commentary(终仓位: + Text(lposition) + ,+Text(-sposition));

Commentary(趋势逆转: + Text(trendy));

}


//------------------------------------------------------------------------

// 编译版本 2024/06/20 220532

// 版权所有 xingn1991

// 更改声明 TradeBlazer Software保留对TradeBlazer平台

// 每一版本的TradeBlazer公式修改和重写的权利

//------------------------------------------------------------------------

2.报单价格问题。实盘中我的策略单元设置指定了具体商品,不复权,如下图所示,策略代码中的A函数报单价格无论后面加没加rollover,最后报出去的都比盘口价格要高很多,不知问题出在哪了。恳请刘老师、王老师帮忙分析分析。

data-href=

data-href=

xingn1991

问题解决了,在开仓条件中要加入barstatus == 2,不然会把历史bar读一遍。

2024-09-03 14:41
您未登录,请先 登录注册 后发表评论
顶部