投资者咨询:MQ上程序回测套利合约时出现问题 (文华财经wh9)
来源:文华财经 日期:2018-7-17 19:05
技术人员回复
日期:2018-7-17 19:20
投资者咨询: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);
}
技术人员回复
日期:2018-7-17 19:38
投资者咨询: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;
}
}
发了一个简化版的源码,问题是一样的,可以按这个源码测试
技术人员回复
日期: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
