MQ上程序回测套利合约时出现问题 (文华财经wh9)

投资者咨询:MQ上程序回测套利合约时出现问题 (文华财经wh9)
来源:文华财经  日期:2018-7-17 19:05
 1. 套利合约的成交怎么不是以spead的价格为单位的,而是显示第一条腿,第二腿的价格? 而且两条腿的差价是close价,并不是我的buy 价格

2.  套利合约是不是会出现buy or sell失灵的情况,明显测出那个bar该Sellshort(1,open)的,但图中没有显示出信号( 我测试在那个bar肯定运行到Sellshort语句了 )


技术人员回复
日期:2018-7-17 19:20
 您提供下完整的源码和回测报告上半部分截图,说明下哪根k线有疑问,我们帮您具体分析下
投资者咨询:MQ上程序回测套利合约时出现问题 (文华财经wh9)
来源:文华财经  日期:2018-7-17 19:05
 Params

Numeric EntryLot(1);
Numeric N(45);  //前面多少根线有无平仓
Numeric Length(20); // 布林带参数
Numeric Offset(2); // 布林带参数
Vars
NumericSeries preEntryPrice(0);        // 前一次开仓的价格
NumericSeries UpLine; //上轨
NumericSeries DownLine; //下轨 
NumericSeries MidLine; //中间线
NumericSeries Band;
NumericSeries Out;  // 0 没有平仓,1买平,-1卖平
Numeric FrontOut(0);
Numeric i;
Numeric TradeTime; // 交易时间
Begin
    If(BarStatus == 0)
    {
preEntryPrice = InvalidNumeric;

}
MidLine = AverageFC(Close,Length);
Band = StandardDev(Close,Length,2); 
UpLine = MidLine + Offset * Band;
DownLine = MidLine - Offset * Band; 
PlotNumeric("UpLine",UpLine);
PlotNumeric("DownLine",DownLine);
PlotNumeric("MidLine",MidLine);

    
Out = 0; 

TradeTime = Time();
if ( TradeTime > 0 &&  CurrentBar > 20 ) 
{
preEntryPrice = preEntryPrice[1];
If( CurrentBar > N )
for  i=1 to N
{
if( out[i] >0 )
{
FrontOut = 1;  //有买平止损,上涨趋势
break;
}
else if( out[i] <0 )
{
Commentary("Fr " + Text(FrontOut));
FrontOut = -1;    //有卖平止损,下降趋势
break;
}
}
}
// PlotNumeric("FrontOut",FrontOut+10000);
Commentary("Fr " + Text(FrontOut) + "Out[1]="+" " + Text(Out[1])+"Bar="+" " + Text(CurrentBar));
If( close[1] > UpLine[1] )
{
if( MarketPosition > 0 &&  FrontOut <= 0)
{
Sell(DefaultVol, open);
// Sell(DefaultVol,Tracing_Order);
preEntryPrice = 0;
}
if( MarketPosition == 0 )
{
If( FrontOut <= 0 )
{
SellShort(EntryLot, Open); // 非上涨趋势开卖仓
// SellShort(Entrylot,Tracing_Order);
preEntryPrice = open;
}
}
}
else If( close[1] < DownLine[1] )
{
if( MarketPosition < 0 && FrontOut >= 0 )
{
BuyToCover(DefaultVol, open);
// BuyToCover(DefaultVol,Tracing_Order );
preEntryPrice = 0;
}
if( MarketPosition == 0 )
{
If( FrontOut >= 0 )
{
Buy(EntryLot, Open); // 非下降趋势开买仓
// Buy(Entrylot,Tracing_Order);
preEntryPrice = open;
}
}
}
 
If( (high > preEntryPrice + 50 ) && preEntryPrice != 0 && MarketPosition < 0 ) // 买平止损
{
Commentary("MACDDff=买平止损"+" " + Text(preEntryPrice+50) );
i = preEntryPrice + 50;
if ( i < open ) 
i=open;
Buy(EntryLot, i);
// Buy(Entrylot,Tracing_Order);
preEntryPrice = i;
Out= 1;
}
else If( (low < preEntryPrice - 50 ) && preEntryPrice != 0 && MarketPosition > 0 ) // 卖平止损
{
Commentary("MACDDff=卖平止损"+" " + Text(preEntryPrice-50) );
i = preEntryPrice - 50;
if ( i > open ) 
i=open;
        PlotNumeric("Transaction Pos",-1000);
SellShort(EntryLot, i);
//   SellShort(Entrylot,Tracing_Order);

preEntryPrice = i;
Out= -1;
}
Commentary("MACDDff="+" " + Text(preEntryPrice) );
    PlotNumeric("preEntryPrice",preEntryPrice);
 
}
End


上面是源码,下面是回测报告截图,15分钟图。您回测的时候看下 05210900以及后面的几根线,这里应该有平常信号的,结果没反应,但运行语句肯定运行到这了,因为图上都画出 -1000的点了。



图片点击可在新窗口打开查看
技术人员回复
日期:2018-7-17 19:38
 您的源码比较复杂,我们需要时间分析一下,预计明天17:00之前给您回复
投资者咨询:MQ上程序回测套利合约时出现问题 (文华财经wh9)
来源:文华财经  日期:2018-7-17 19:05
 Params

Numeric EntryLot(1);
Numeric Length(20); // 布林带参数
Numeric Offset(2); // 布林带参数
Vars
NumericSeries preEntryPrice(0);        // 前一次开仓的价格
NumericSeries UpLine; //上轨
NumericSeries DownLine; //下轨 
NumericSeries MidLine; //中间线
NumericSeries Band;

Numeric i;
Numeric TradeTime; // 交易时间
Begin
    If(BarStatus == 0)
    {
preEntryPrice = InvalidNumeric;

}
MidLine = AverageFC(Close,Length);
Band = StandardDev(Close,Length,2); 
UpLine = MidLine + Offset * Band;
DownLine = MidLine - Offset * Band; 
PlotNumeric("UpLine",UpLine);
PlotNumeric("DownLine",DownLine);
PlotNumeric("MidLine",MidLine);

    

TradeTime = Time();
if ( TradeTime > 0 &&  CurrentBar > 20 ) 
{
preEntryPrice = preEntryPrice[1];

If( close[1] > UpLine[1] )
{
if( MarketPosition > 0 )
{
Sell(DefaultVol, open);
preEntryPrice = 0;
}
if( MarketPosition == 0 )
{
SellShort(EntryLot, Open); // 非上涨趋势开卖仓
preEntryPrice = open;
}
}
else If( close[1] < DownLine[1] )
{
if( MarketPosition < 0  )
{
BuyToCover(DefaultVol, open);
preEntryPrice = 0;
}
if( MarketPosition == 0 )
{
Buy(EntryLot, Open); // 非下降趋势开买仓
preEntryPrice = open;
}
}
 
If( (high > preEntryPrice + 50 ) && preEntryPrice != 0 && MarketPosition < 0 ) // 买平止损
{
i = preEntryPrice + 50;
if ( i < open ) 
i=open;
   Buy(EntryLot, i);
preEntryPrice = i;
}
else If( (low < preEntryPrice - 50 ) && preEntryPrice != 0 && MarketPosition > 0 ) // 卖平止损
{
i = preEntryPrice - 50;
if ( i > open ) 
i=open;
        PlotNumeric("Transaction Pos",-1000);
SellShort(EntryLot, i);


preEntryPrice = i;
}

 
}
End



发了一个简化版的源码,问题是一样的,可以按这个源码测试
技术人员回复
日期:2018-7-18 15:45

回复问题2:

 

 5楼源码中卖平止损应该使用sell修改参考:

 

 if ( i > open ) 
i=open;
        PlotNumeric("Transaction Pos",-1000);
Sell(EntryLot, i);
 
此外取最近一次开仓信号开盘价不需要重复赋值可以参考这种方式直接取值:
 
preEntryPrice = Ref(Open,BarsLast(cross(SKVol+BKVol,0.5)));
   
技术人员回复
日期:2018-7-18 16:23

 回复问题1:

 

套利合约支持不了指定开盘价回测看效果的,因为套利实际是两腿合约分别委托,并不是真正的一个整体


所以回测以两腿合约各自的成交价来回测,并且一个信号控制两个委托,实现不了再指定价格这么复杂的思路了,都是以收盘价来计算的


您的需求可以考虑控制价格的方式,实际运行看效果