策略說明:
基于凱特納通道的交易系統(tǒng)
系統(tǒng)要素:
1. 計算關(guān)鍵價格的凱特納通道
2. 價格突破凱特納通道后唬复,設(shè)定入場觸發(fā)單
入場條件:
1惑申、價格突破凱特納通道后涛舍,在當(dāng)根K線高點之上N倍通道幅度王凑,設(shè)定多頭觸發(fā)單获雕,此開倉點將掛單X根k線
2雕欺、價格突破凱特納通道后岛马,在當(dāng)根K線低點之下N倍通道幅度棉姐,設(shè)定空頭觸發(fā)單,此開倉點將掛單X根k線
出場條件:
1. 價格下穿軌道中軌時平倉
2. 價格小于N周期低點平倉
做多代碼及解讀如下:
Params
Numeric length(10);???? ?//聲明數(shù)值參數(shù)length啦逆,初值10伞矩,即均線參數(shù)。//
Numeric Constt(1.2);??? ?// 聲明數(shù)值參數(shù)Constt夏志,初值1.2乃坤,即通道倍數(shù)。//
Numeric ChanPcnt(0.5) ;?? // 聲明數(shù)值參數(shù)ChanPcnt沟蔑,初值0.5湿诊,即入場參數(shù)。//
Numeric buyN(5); ??? ?// 聲明數(shù)值參數(shù)buyN瘦材,初值5厅须,入場觸發(fā)條件有效K線周期。//
Numeric stopN(4);???? ?//聲明數(shù)值參數(shù)stopN食棕,初值4朗和, 低點止損參數(shù)。//
Vars
NumericSeries Price(0);?? ?// 聲明數(shù)值序列變量Price簿晓,初值0眶拉,即價格。//
NumericSeries KCU(0);??? ?// 聲明數(shù)值序列變量KCU憔儿,初值0忆植,通道上軌。//
NumericSeries KCL(0);??? ?// 聲明數(shù)值序列變量KCL皿曲,初值0唱逢,通道下軌吴侦。//
NumericSeries ChanRng(0);? ?//聲明數(shù)值序列變量ChanRng屋休,初值0, 通道寬度备韧。//
NumericSeries AvgVal(0);? ?//聲明數(shù)值序列變量AvgVal劫樟,初值0,通道中軌织堂。//
NumericSeries AvgRange(0); ?// 聲明數(shù)值序列變量AvgRange叠艳,初值0,真實波動均值易阳。//
NumericSeries Setbar(0); //聲明數(shù)值序列變量Setbar附较,初值0.//
NumericSeries CountL(0);? ?// 聲明數(shù)值序列變量CountL,初值0潦俺,觸發(fā)單周期變量拒课。//
NumericSeries hh;????? ?// 聲明數(shù)值序列變量hh徐勃,多頭觸發(fā)單價位。//
NumericSeries Lstopline;? ?// 聲明數(shù)值序列變量Lstopline早像,即止損線僻肖。//
bool con;?????????? ?// 聲明布爾型變量con。//
BoolSeries con2;?????? ?// 聲明布爾型序列變量con2卢鹦。//
Begin
If(!CallAuctionFilter()) Return; // 集合競價和小節(jié)休息過濾.//
// 指標計算臀脏。//
Price = Close;??????????????// 關(guān)鍵價格,賦值收盤價冀自,也可以換成中位價等揉稚。//
AvgVal = Average(Price,Length);??????// 計算均線 默認10周期。//
AvgRange = Average(TrueRange,Length);???// 計算真實波動均值(atr) 默認10周期熬粗。//
KCU = AvgVal + AvgRange*Constt;??????// 計算通道上軌=均線+1.2倍的10周期真實波動值窃植。//
KCL = AvgVal - AvgRange*Constt;??????// 計算通道下軌=均線-1.2倍的10周期真實波動值。//
ChanRng = (KCU - KCL)/2;?????????// 通道寬度 / 2????荐糜。//
CountL = CountL + 1;???????????// 每經(jīng)過1根K線CountL+1巷怜,用于判斷信號取消的變量,上穿上軌后暴氏,默認參數(shù):開倉點僅掛單5根k線延塑。//
con = CrossOver( Price,KCU);???????// bool變量con,當(dāng)價格上穿上軌時為真 答渔。//
If(con)???????//假如變量con為真关带。//??????????
{?
SetBar = High;//變量SetBar 賦值為當(dāng)前最高價。//
CountL = 0; //變量CountL = 0.//
hh = SetBar + (ChanRng*ChanPcnt);//直接代入上面求得的數(shù)值了沼撕。//
}?????????????????
// 系統(tǒng)入場宋雏。//
If(MarketPosition == 0)//當(dāng)前沒有持倉。//
{
If(Price[1] > KCU[1] and CountL <= buyN and High >= hh)?// 當(dāng)價格上穿上軌务豺,并且在buyN根K線內(nèi)>=變量CountL磨总,且當(dāng)前最高價大于等于變量hh時,買入開倉笼沥。//
{
Buy(0,max(Open,hh));//開倉買入蚪燕。//
}???????????????????
}
// 系統(tǒng)出場。//
con2 = CrossUnder(Close,AvgVal);?????// 布爾型變量con2奔浅,當(dāng)價格下穿軌道中軌時為真馆纳。//
Lstopline = Lowest(Low[1],stopN); //止損線為求4周期內(nèi)的最低價。//
If(MarketPosition == 1 and BarsSinceEntry > 0)//當(dāng)前持有多單汹桦,且建倉數(shù)位大于0.//
{
If(con2[1])// 價格下穿軌道中軌時鲁驶。//
{
Sell(0,Open);???//平倉。//????????????
}
If(Low <= Lstopline)// 當(dāng)前價格小于4周期低點平倉 .//
{
Sell(0,Min(Open,Lstopline));??//平倉舞骆。//???
}
}
End
做空代碼及結(jié)果如下:
Params
Numeric length(10);????
Numeric Constt(1.2);???? ?
Numeric ChanPcnt(0.5) ;?? ?
Numeric sellN(5); ????
Numeric stopN(4);????? ?
Vars
NumericSeries Price(0);??? ?
NumericSeries KCU(0);????
NumericSeries KCL(0);?? ??
NumericSeries ChanRng(0); ?
NumericSeries AvgVal(0);??
NumericSeries AvgRange(0); ?
NumericSeries Setbar(0);?
NumericSeries CountS(0);? ??????
NumericSeries ll;???????
NumericSeries Sstopline;?? ?
bool con;????????? ?
BoolSeries con2;??????
Begin
If(!CallAuctionFilter()) Return;
Price = Close;??????????????
AvgVal = Average(Price,Length);??????
AvgRange = Average(TrueRange,Length);???
KCU = AvgVal + AvgRange*Constt;?????
KCL = AvgVal - AvgRange*Constt;??????
ChanRng = (KCU - KCL)/2;??????????
CountS = CountS + 1;???????????
con = CrossUnder(price,KCL);???????
If(con)??
{
SetBar = Low;
countS = 0;
ll = SetBar - (ChanRng*Chanpcnt);
}??????????????????
If(MarketPosition == 0)??
{
If(Price[1] < KCL[1] and CountS <= sellN and Low <= ll)
{
SellShort(0,Min(Open,ll));
}
}?????????????????????
con2 = CrossOver(Close,AvgVal);???????
Sstopline = Highest(High[1],stopN);
If(MarketPosition == -1 and BarsSinceEntry > 0)
{
If(con2[1])
{
BuyToCover(0,Open);??????????????
}
If(High >= Sstopline)?
{
BuyToCover(0,max(Sstopline,Open));??????
}
}
End