//------------------------------------------------------------------------
// 简称:Keltner_Channel
// 名称:肯特纳通道(KC)
// 类别: 交易指令
// 类型: 用户应用
//------------------------------------------------------------------------
Params
Numeric Length(20);
Numeric NumATRs(1);
Vars
NumericSeries TPrice;
Numeric AvgValue;
NumericSeries ShiftValue;
Numeric UpperBand;
Numeric LowerBand;
Numeric MyPrice;
Begin
TPrice = (High[1]+Low[1]+Close[1])/3;
AvgValue = AverageFC(TPrice,Length);
ShiftValue = NumATRs*AvgTrueRange(Length);
UpperBand = AvgValue + ShiftValue[1];
LowerBand = AvgValue - ShiftValue[1];
If(MarketPosition!=1 && High >= UpperBand)
{
MyPrice = UpperBand;
If(Open > MyPrice) MyPrice = Open;
Buy(1,MyPrice);
Return;
}
If(MarketPosition!=-1 && Low <= LowerBand)
{
MyPrice = LowerBand;
If(Open < MyPrice) MyPrice = Open;
SellShort(1,MyPrice);
Return;
}
If(BarsSinceEntry == 1)
{
HigherAfterEntry = AvgEntryPrice;
LowerAfterEntry = HigherAfterEntry;
}Else If(BarsSinceEntry > 1)
{
HigherAfterEntry = Max(HigherAfterEntry[1],High[1]);
LowerAfterEntry = Min(LowerAfterEntry[1],Low[1]);
}
Commentary("HigherAfterEntry="+Text(HigherAfterEntry));
Commentary("LowerAfterEntry="+Text(LowerAfterEntry));
If(MarketPosition==1)
{
If(Low <= HigherAfterEntry - TrailingStop*MinPoint)
{
MyPrice = HigherAfterEntry - TrailingStop*MinPoint;
If(Open < MyPrice) MyPrice = Open;
Sell(1,MyPrice);
}
}Else If(MarketPosition == -1)
{
If(High >= LowerAfterEntry + TrailingStop*MinPoint)
{
MyPrice = LowerAfterEntry + TrailingStop*MinPoint;
If(Open > MyPrice) MyPrice = Open;
BuyToCover(1,MyPrice);
}
}
PlotNumeric("UpperBand",UpperBand);
PlotNumeric("LowerBand",LowerBand);
PlotNumeric("MidLine",AvgValue);
End
|