Params
Numeric Length(3); //中轨周期、标准差周期、平均通道宽度/平均成交量周期参数
Numeric Offset(2); //标准差倍数
Numeric TrailStartPct(1);//跟踪止盈启动点,百分点--0.8%
Numeric Param(1); //通道宽度百分比敞口参数,也是成交量放量参数
Numeric Pcnt(3);//初始固定比例止损,1%
Numeric Direction(1);//方向控制。0:多空都做,1:只做多;-1:只做空。
Numeric Lots(2);
Vars
NumericSeries UpLine;
NumericSeries DownLine;
NumericSeries MidLine;
NumericSeries Range;
NumericSeries AvgRange;
NumericSeries AvgVol;
Numeric Band;
Numeric MyPrice;
BoolSeries Strailon(False);
BoolSeries Ltrailon(False);
NumericSeries count;
NumericSeries HighAfterEntry;//开仓后出现的最高价
NumericSeries LowAfterEntry;//开仓后出现的最低价
NumericSeries ProfitPcnt1;
NumericSeries ProfitPcnt2;
NumericSeries PP;
NumericSeries TT;
Begin
//布林轨
MidLine=AverageFC(Close,Length);
Band=StandardDev(Close,Length,2);
UpLine=MidLine+Offset*Band;
DownLine=MidLine-Offset*Band;
PlotNumeric("中轨",MidLine);
PlotNumeric("上轨",UpLine);
PlotNumeric("下轨",DownLine);
//平均通道宽度百分比
Range=2*Offset*Band/MidLine;
AvgRange=AverageFC(Range,Length);
//平均成交量
AvgVol=AverageFC(V,Length);
//
PP=Min((Entryprice-Lowafterentry)/EntryPrice,0.1);
TT=Min((Highafterentry-Entryprice)/EntryPrice,0.1);//多头跟踪止盈,当盈利小于10%时,如8%时,回落至获利的80%止盈。当盈利大于10%时,立即止盈。
ProfitPcnt1=PP*10; //空头止盈幅度
ProfitPcnt2=TT*10;//多头止盈幅度
//-----------------------------进场--------------------------------------//
//布林开口,成交放量,连续两天突破上轨且创昨天新高
If (currentbar>Length and MarketPosition==0 and Range[1]>param*AvgRange[1] and V[1]>Param*AvgVol[1] and H[1]>Upline[1] and H[2]>Upline[2] and H>=High[1])
{
MyPrice=Max(O,High[1]);
If(Direction>=0)
Buy(Lots,MyPrice);
}
If (currentbar>Length and MarketPosition==0 and Range[1]>param*AvgRange[1] and V[1]>Param*AvgVol[1] and L[1]<DownLine[1] and L[2]<DownLine[2] and L<=Low[1])
{
MyPrice=Min(O,Low[1]);
If(Direction<=0)
SellShort(Lots,MyPrice);
}
//-------------------------------固定比例初始止损-----------------------------//
Myprice=entryprice*(1-Pcnt/100);
If(MarketPosition==1 and low<=Myprice and BarsSinceEntry>0)
{
MyPrice=Min(MyPrice,Open);
if(Myprice-EntryPrice<0)
{
Sell(Lots, Myprice);
Commentary("做多止损");
}
}
Myprice=EntryPrice*(1+Pcnt/100);
If(MarketPosition==-1 and High>=Myprice and BarsSinceEntry>0)
{
Myprice=Max(Myprice,Open);
if(Myprice-EntryPrice>0)
{
BuyToCover(Lots, Myprice);
Commentary("做空止损");
}
}
//---------------------------止盈--------------------------------//
If(MarketPosition==-1)
{
If(BarsSinceEntry==0)
{
STrailOn=false;
}else
if (BarsSinceEntry>0)
{
If(Lowafterentry <= EntryPrice*(1-TrailStartPct/100)) //启动止盈点
{
STrailon = True;
}
MyPrice = EntryPrice-(Entryprice-Lowafterentry)*ProfitPcnt1; //空头止盈
If(STrailon and High>=MyPrice)
{
MyPrice = Max(Open,MyPrice);
BuyToCover(Lots,MyPrice);
Commentary("空头止盈");
}
}
}
If(MarketPosition==1)
{
If(BarsSinceEntry==0)
{
LTrailOn=false;
}else
if (BarsSinceEntry>0)
{
If(Highafterentry >= EntryPrice*(1+TrailStartPct/100)) //启动止盈点
{
LTrailon = True;
}
MyPrice = EntryPrice+(Highafterentry-EntryPrice)*ProfitPcnt2; //多头止盈
If(LTrailon and Low<=MyPrice)
{
MyPrice = Min(Open,MyPrice);
sell(Lots,MyPrice);
Commentary("多头止盈");
}
}
}
//记录入场后的最高价、最低价
If(MarketPosition == 1 and BarsSinceentry == 0)
{
HighAfterEntry = Max(EntryPrice, high);
LowAfterEntry = LowAfterEntry[1];
}else
If(MarketPosition == -1 and BarsSinceentry == 0)
{
LowAfterEntry = Min(EntryPrice, low);
HighAfterEntry = HighAfterEntry[1];
}else
if(MarketPosition != 0 and BarsSinceentry >= 1)
{
HighAfterEntry = Max(HighAfterEntry[1], high);
LowAfterEntry = Min(LowAfterEntry[1], low);
}
End