//------------------------------------------------------------------------
// 简称: test01
// 名称:
// 类别: 策略应用
// 类型: 用户应用
// 输出: Void
//------------------------------------------------------------------------
Params
//此处添加参数
Numeric ma_length(26);
Numeric initcapital(10); //单位:万
Numeric ATRLength(26);
Numeric n_atr(0.5);//倍数
Vars
//此处添加变量
Series<Numeric> avg;//计算中轨
Numeric lots(1); //下单手数
Series<Numeric> atr; //平均波动率
Series<Numeric> tr; //波动率
Series<Numeric> ma1;
Series<Numeric> ma2;
Bool dailyState;//日线多头是1 日线空头是0
Bool min_30_State;//30分钟多头是1,空头是0
Defs
//此处添加策略函数
Events
OnInit()
{
//=========交易相关设置==============
SetInitCapital(initcapital * 10000); //设置初始资金
SetCommissionRate(BitOr(Enum_Rate_FreeOfExitToday,Enum_Rate_ByFillAmount),5); //设置手续费率为成交金额的5%%,不收平今。BitOr是一个系统函数,当需要同时设置两项内容时,需要用bitor把两项内容括起来。
SetSlippage(Enum_Rate_PointPerHand,2); //设置滑点为2跳/手
SetMarginRate(0.12);
}
//Bar更新事件函数,参数indexs表示变化的数据源图层ID数组
OnBar(ArrayRef<Integer> indexs)
{
Range[0:DataSourceSize-1]
{
//计算中轨
avg = SMA(Close, ma_length);
//计算ATR波动率
tr = Max(High - Low, Max(high - close[1], close[1] - Low));
atr = SMA(tr, ATRLength);
ma1 = avg + n_atr * atr;
ma2 = avg - n_atr * atr;
PlotNumeric("上轨", ma1);
PlotNumeric("下轨", ma2);
}
if (data0.close > data0.ma1)
{ dailyState = True;}
if (data0.close < data0.ma2)
{dailyState = False;}
if (data1.close > data1.ma1)
{min_30_State = True;}
if (data1.close < data1.ma2)
{min_30_State = False;}
if (MarketPosition == 1 and data1.close < ma2-n_atr*atr)
{sell(1,close);}
if (MarketPosition ==-1 and data1.close > ma1+n_atr*atr)
{buytocover(1,close);}
if (dailyState)
{
if (min_30_State)
{
if (data1.close <= data1.ma1 and data1.close >= data1.ma2 and MarketPosition == 0)
{
buy(1,close);
}
}
}
else
{
if (not min_30_State)
{
if (data1.close >= data1.ma2 and data1.close <= data1.ma1 and MarketPosition == 0)
{
sellshort(1,close);
}
}
}
/* if (marketposition == 1 and close < ma2)
{
sell(1,close);
}
if (marketposition == -1 and close > ma1)
{
buytocover(1,close);
}
if (close > ma1 and marketposition == 0)
{
buy(1,close);
}
if (close < ma2 and marketposition == 0)
{
sellshort(1,close);
}*/
}我的本意是双周期共振然后小周期回调进场,但是回测出了问题。以下是回测k线:

如图所示向下箭头意味着空头开仓,向上是平仓。只有空头开平仓,没有多头。最大的问题是和双周期共振逻辑不符合,成了单周期交易了。