以下为TB四周法则原代码第一部分:
Params
Numeric B_Length1(22);// 长周期突破
Numeric B_Length2(4);
Numeric B_ATRLength(32);// 多头ATR周期
Numeric B_TrailStopNumATR(1.80);//多头 追踪止损,回撤Average ATR的倍数
Numeric S_Length1(8);
Numeric S_Length2(30);
Numeric S_ATRLength(12);// 空头ATR周期
Numeric S_TrailStopNumATR(4);// 空头追踪止损
Numeric lots(1);// 头寸大小
Vars
Numeric HiBand1;
Numeric HiBand2;
Numeric LoBand1;
Numeric LoBand2;
NumericSeries ATRValue;
Numeric MyPrice;
Numeric StopLine;
NumericSeries HigherAfterEntry;
NumericSeries LowerAfterEntry;
//以下多头
Begin
// 记录开仓后盈利峰值价
If(BarsSinceEntry == 1)
{
HigherAfterEntry = AvgEntryPrice;
}Else If(BarsSinceEntry > 1)
{
HigherAfterEntry = Max(HigherAfterEntry[1],High[1]);
}Else
{
HigherAfterEntry = HigherAfterEntry[1];
}
Commentary("HigherAfterEntry="+Text(HigherAfterEntry));
ATRValue = AvgTrueRange(B_ATRLength);
Commentary("ATRValue="+text(ATRValue));
HiBand1 = highest(high[1],B_Length1);
LoBand1 = lowest(low[1],B_Length1);
HiBand2 = highest(high[1],B_Length2);
LoBand2 = lowest(low[1],B_Length2);
PlotNumeric("HiBand1",HiBand1);
PlotNumeric("LoBand1",LoBand1);
plotnumeric("HiBand2",HiBand2);
plotnumeric("LoBand2",LoBand2);
// 过滤集合竞价
If((BarType==1 or BarType==2) && BarStatus == 2 && date!=date[1] && high==low) return;
If(BarType==0 && BarStatus == 2 && CurrentTime<=0.09 && high==low) return;
// 进场部分
if(MarketPosition==0 && high>=HiBand1)
{
MyPrice = HiBand1;
if (Open>MyPrice) MyPrice = Open;
buy(lots,MyPrice);
}
以下为TB四周法则原代码第二部分:
// 止损部分
if(barssinceentry>=1)
{
If(MarketPosition==1)
{
StopLine = LoBand2;
if (StopLine < HigherAfterEntry - ATRValue[1] * B_TrailStopNumATR)
StopLine = HigherAfterEntry - ATRValue[1] * B_TrailStopNumATR;
If(Low <= StopLine)
{
MyPrice = StopLine;
If(Open < MyPrice) MyPrice = Open;
Sell(Lots,MyPrice);
}
}
}
//以下空头
// 记录开仓后盈利峰值价
If(BarsSinceEntry == 1)
{
LowerAfterEntry = AvgEntryPrice;
}Else If(BarsSinceEntry > 1)
{
LowerAfterEntry = Min(LowerAfterEntry[1],Low[1]);
}Else
{
LowerAfterEntry = LowerAfterEntry[1];
}
Commentary("LowerAfterEntry="+Text(LowerAfterEntry));
ATRValue = AvgTrueRange(S_ATRLength);
Commentary("ATRValue="+text(ATRValue));
HiBand1 = highest(high[1],S_Length1);
LoBand1 = lowest(low[1],S_Length1);
HiBand2 = highest(high[1],S_Length2);
LoBand2 = lowest(low[1],S_Length2);
PlotNumeric("HiBand1",HiBand1);
PlotNumeric("LoBand1",LoBand1);
plotnumeric("HiBand2",HiBand2);
plotnumeric("LoBand2",LoBand2);
// 过滤集合竞价
If((BarType==1 or BarType==2) && BarStatus == 2 && date!=date[1] && high==low) return;
If(BarType==0 && BarStatus == 2 && CurrentTime<=0.09 && high==low) return;
// 进场部分
if(MarketPosition==0 && Low<=LoBand1)
{
MyPrice = LoBand1;
if (Open<MyPrice) MyPrice = Open;
SellShort(lots,MyPrice);
}
以下为TB四周法则原代码第三部分:
// 止损部分
if(barssinceentry>=1)
{
If(MarketPosition==-1)
{
StopLine = HiBand2;
if (StopLine > LowerAfterEntry + ATRValue[1] * S_TrailStopNumATR)
StopLine = LowerAfterEntry + ATRValue[1] * S_TrailStopNumATR;
If(high >= StopLine)
{
MyPrice = StopLine;
If(Open > MyPrice) MyPrice = Open;
BuyToCover(Lots,MyPrice);
}
}
}
End
好,明天帮你改改
看着真累。好像是说从买入后的最高点回落1.8倍的atr离场,突破4周低点也离场的 意思?
variable:hl=0,zs=0;
input:B_Length1(22),B_Length2(4),B_ATRLength(32),B_TrailStopNumATR(1.80);
input:S_Length1(8),S_Length2(30),S_ATRLength(12),S_TrailStopNumATR(4),lots(1);
HiBand1:=ref(hhv(h,B_Length1),1);
LoBand2:=llv(l,B_Length2);
LoBand1:=ref(llv(l,S_Length1),1);
HiBand2:=hhv(h,S_Length2);
B_atr:=B_TrailStopNumATR*ma(tr,B_ATRLength);
S_atr:=S_TrailStopNumATR*ma(tr,S_ATRLength);
if islastbar and dynainfo(207)<opentime(1) then exit;//过滤集合竞价
if holding>0 and l<zs then begin
sell(1,1,limitr,min(o,zs));
goto skip@;//平仓当根不开空,看你TB模型的意思
end
if holding<0 and h>zs then begin
sellshort(1,1,limitr,max(o,zs));
goto skip@;//平仓当根不开多,看你TB模型的意思
end
if holding=0 and h>HiBand1 then begin
buy(1,1,limitr,max(o,HiBand1));
hl:=h;
end
if holding=0 and l<LoBand1 then begin
buyshort(1,1,limitr,min(o,LoBand1));
hl:=l;
end
skip@;
if holding>0 and h>=hl then begin
hl:=h;
zs:=max(hl-B_TrailStopNumATR*B_atr,LoBand2);
end
if holding<0 and l<=hl then begin
hl:=l;
zs:=min(hl+S_TrailStopNumATR*S_atr,HiBand2);
end
TB模型里,突破开多以开盘瞬间或者颈线为开仓价,沿用此写法。但提出一下,实盘时这个价格基本难以成交
至少要加一个滑点测试吧。比如:突破3000,条件成立瞬间价格至少是3000.2(股指),以3000进行测试,有点“欺人”味道吧
阿火辛苦啦
逻辑写得好、效率高,一调就通!
1、就是在编译时有以下提示:系统发现...使用<DYNAINFO>返回常数变量的函数,如果使用不当则会导致信号消失等漏单情况...
看到这是过滤集合竞价用到的函数,请问没什么关系吧?
2、在每个自动交易策略中,一定要有过滤集合竞价吗?
谢谢