等级: 免费版
- 注册:
- 2022-9-18
- 曾用名:
|
Params
Numeric Length(30);//移动平均周期参数
Numeric NumATRs(1);//ATR乘数
Numeric Offset(1);//标准差乘数
Numeric ATRLength(40);
Numeric TrailingStart(3);//跟踪止损系数
Numeric StopLossSet(5);//普通止损系数
Numeric TrailingStop(1);
Vars
NumericSeries TPrice;//T价格
Numeric AvgValue;//
NumericSeries ShiftValue;
Numeric UpperBand;
Numeric LowerBand;
Numeric MyPrice;
Numeric UpLine; //上轨
Numeric DownLine; //下轨
NumericSeries MidLine; //中间线
Numeric Band;//布林带
NumericSeries HigherAfterEntry;//入场以来最高
NumericSeries LowerAfterEntry;//入场以来最低
Numeric DayOpen;
Numeric StopLine;
BoolSeries bLongTrailingStoped;
BoolSeries bShortTrailingStoped;
Numeric MinPoint;
Begin
MinPoint = MinMove*PriceScale;//每点价值:最小变动价位*每点价值量乘数
DayOpen=AvgEntryPrice;//当日开盘:持仓平均成本
TPrice=(High[1]+Low[1]+Close[1])/3;//TPrice为加权平均当日价格(取前一日的)
AvgValue=AverageFC(TPrice,Length);//TPrice的移动平均
ShiftValue=NumATRs*AvgTrueRange(ATRLength);//上档价值:ATR乘以乘数
UpperBand=AvgValue+ShiftValue[1];//上轨1:移动平均价格加 上档价值
LowerBand=AvgValue-ShiftValue[1];//下轨1:移动平均价格减 下档价值
MidLine = AverageFC(Close,Length);//中轨,收盘价的移动平均
Band = StandardDev(Close,Length,2);// 标准差
UpLine = MidLine + Offset * Band;//中轨+1倍标准差,上轨2
DownLine = MidLine - Offset * Band; //中轨-1倍标准差,下轨2
If(BarStatus > 0)//如果不是第一根K线
{
bLongTrailingStoped = bLongTrailingStoped[1];//跟踪止损更新
bShortTrailingStoped = bShortTrailingStoped[1];//跟踪止损更新
}
Commentary("bLongTrailingStoped="+IIFString(bLongTrailingStoped,"True","False"));//调试输出
Commentary("bShortTrailingStoped="+IIFString(bShortTrailingStoped,"True","False"));//调试输出
If(BarsSinceEntry==1)//入场第一根K线
{
HigherAfterEntry=AvgEntryPrice;//初始化入场以来最高
LowerAfterEntry=HigherAfterEntry;//初始化入场以来最低
}Else// If(BarsSinceEntry>1)
{
HigherAfterEntry=max(HigherafterEntry[1],High[1]);//更新入场以来最高
LowerAfterEntry=min(LowerAfterEntry[1],Low[1]);//更新入场以来最低
}
If(bLongTrailingStoped==False && MarketPosition!=1&&High>=UpperBand)//如果没有达到跟踪止损,且, 持仓为0或为空,最高价大于 上轨1
{
MyPrice=UpperBand;//更新价格为上轨1
If(Open>MyPrice)MyPrice=Open;//如果开盘价即大于上轨1,则更新为开盘价
Buy(1,MyPrice);//多头开仓1手
bLongTrailingStoped=True;//开启多头跟踪止损
bShortTrailingStoped=False;//关闭空头跟踪止损
Return;//返回
}
If(bShortTrailingStoped==False && MarketPosition!=-1&&Low<=LowerBand)//如果没有达到跟踪止损,且,持仓为0或为多,最高价大于 下轨1
{
MyPrice=LowerBand;//更新价格为下轨1
If(Open<MyPrice)MyPrice=Open;//如果开盘价即小于下轨
SellShort(1,MyPrice); //空头开仓1手
bShortTrailingStoped=True;//开启空头跟踪止损
bLongTrailingStoped=False;//关闭多头跟踪止损
Return;//返回
}
If(HigherAfterEntry>=AvgEntryPrice+DayOpen*TrailingStart*0.01&&MarketPosition==1)
//如果 进场后的最高价大于 平均持仓价格+DayOpen也是平均持仓从成本*3*0.01 且 持仓为多头
{
StopLine=HigherAfterEntry-DayOpen*TrailingStop*0.01;//更新止损线
}Else//止损
{
StopLine=UpperBand-DayOpen*StopLossSet*0.01;//止损线更新为另一种,百分比止损
}
If(Low<=StopLine)//最低价小于止损线,这里没有要求一定持有多仓
{
MyPrice=StopLine;//更新价格
If(Open<MyPrice)MyPrice=Open;//更新为开盘价
Sell(1,MyPrice);//平多仓
bLongTrailingStoped=True;//开启多头跟踪止损
bShortTrailingStoped=False;//关闭空头跟踪止损
Return;//返回
}
If(LowerAfterEntry<=AvgEntryPrice-DayOpen*TrailingStart*0.01&&MarketPosition==-1)//更新空头止损价格
{
StopLine=LowerAfterEntry+DayOpen*TrailingStop*0.01;
}Else//止损
{
StopLine=LowerBand+DayOpen*StopLossSet*0.01;
}
If(High>=StopLine)//空头离场
{
MyPrice=StopLine;
If(Open>MyPrice)MyPrice=Open;
Buytocover(1,MyPrice);
bShortTrailingStoped=True;
bLongTrailingStoped=False;
Return;
}
//再次入场的代码
If(bLongTrailingStoped && MarketPosition==0 && High > HigherAfterEntry)//最高价创新高再次入场
{
MyPrice = HigherAfterEntry + MinPoint;
If(Open > MyPrice) MyPrice = Open;
Buy(1,MyPrice);
bLongTrailingStoped = False;
bShortTrailingStoped= True;
Return;
}
If(bShortTrailingStoped && MarketPosition==0 && Low < LowerAfterEntry)//最低价创新低再次入场
{
MyPrice = LowerAfterEntry - MinPoint;
If(Open < MyPrice) MyPrice = Open;
SellShort(1,MyPrice);
bLongTrailingStoped = True;
bShortTrailingStoped= False;
Return;
}
End
|
|