搜档网
当前位置:搜档网 › TB-四周模型策略代码

TB-四周模型策略代码

交流探讨一个TB四周策略交易策略(共享源码)
Params
Numeric B_Length1(20);// 长周期突破
Numeric B_Length2(10);
Numeric B_ATRLength(14);//多头ATR周期
Numeric B_TrailStopNumATR(3);//多头 追踪止损,回撤Average ATR的倍数
Numeric S_Length1(20);
Numeric S_Length2(10);
Numeric S_ATRLength(14);//空头ATR周期
Numeric S_TrailStopNumATR(3);// 空头追踪止损
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);

// 过滤集合竞价
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);

// 过滤集合竞价
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 (OpenSellShort(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

相关主题