策略說(shuō)明:
本策略基于當(dāng)前收盤(pán)價(jià)與之前k線的高低進(jìn)行打分, 并通過(guò)打分的均值與對(duì)應(yīng)的收盤(pán)價(jià)均值進(jìn)行交易
系統(tǒng)要素:
1. 當(dāng)當(dāng)前收盤(pán)價(jià)格大于之前LookBack根K線內(nèi)某一根k線的收盤(pán)價(jià)時(shí)記+1分, 否則記-1分, 加總這些分?jǐn)?shù)以獲得當(dāng)前K線的得分
2. 對(duì)k線的打分計(jì)算一條均線
3. 對(duì)k線的收盤(pán)計(jì)算一條均線
入場(chǎng)條件:
1. 當(dāng)價(jià)格高于收盤(pán)價(jià)均線, 且打分也高于打分均線時(shí)的入場(chǎng)做多
2. 當(dāng)價(jià)格低于收盤(pán)價(jià)均線, 且打分也低于打分均線時(shí)的入場(chǎng)做空
出場(chǎng)條件:?
1. 基于ATR的保護(hù)性止損
2. 基于ATR的跟蹤止損
3. 基于ATR的盈虧平衡止損
做多的源代碼及解讀如下:
Params
??Numeric LookBack(10); //聲明數(shù)值參數(shù)LookBack,初值10白翻,用于給當(dāng)前K線打分的回溯根數(shù)乍炉。//
??Numeric MALength(18); //聲明數(shù)值參數(shù)MALength绢片,初值18,均線值岛琼。//
??Numeric ATRLength(10); //聲明數(shù)值參數(shù)ATRLength底循,初值10,ATR的值槐瑞。//
Numeric ProtectStopATRMulti(0.5); //聲明數(shù)值參數(shù)ProtectStopATRMulti熙涤,初值0.5,保護(hù)性止損的ATR乘數(shù)困檩。//
Numeric TrailStopATRMulti(3); //聲明數(shù)值參數(shù)TrailStopATRMulti祠挫,初值3,跟蹤止損的ATR乘數(shù)悼沿。//
Numeric BreakEvenStopATRMulti(5); //聲明數(shù)值參數(shù)BreakEvenStopATRMulti等舔,初值5,盈虧平衡止損的ATR乘數(shù)糟趾。//
Vars
NumericSeries MA(0); //聲明數(shù)值序列變量MA慌植,初值0,收盤(pán)價(jià)均線义郑。//
NumericSeries TrendScore(0); //聲明數(shù)值序列變量TrendScore蝶柿,初值0,當(dāng)前K線的打分魔慷。//
NumericSeries TrendScoreMA(0); //聲明數(shù)值序列變量TrendScoreMA,初值0著恩,k線打分的均線院尔。//
NumericSeries ATR(0); //聲明數(shù)值序列變量ATR,初值0.//
Numeric i; //聲明數(shù)值變量i喉誊。//
Numeric Temp;//聲明數(shù)值變量Temp邀摆。//
NumericSeries HighAfterEntry; //聲明數(shù)值序列變量HighAfterEntry,持倉(cāng)后的高點(diǎn)記錄伍茄。//
NumericSeries ProtectStopL; //聲明數(shù)值序列變量ProtectStopL栋盹,基于ATR的保護(hù)性止損。//
Numeric TrailStopL; //聲明數(shù)值變量TrailStopL敷矫,基于ATR的跟蹤止損例获。//
Numeric BreakEvenStopL; //聲明數(shù)值變量BreakEvenStopL,基于ATR的盈虧平衡止損曹仗。//
Numeric ExitLineL; //聲明數(shù)值變量ExitLineL榨汤,平倉(cāng)線。//
NumericSeries MP; //聲明數(shù)值序列變量MP怎茫,即MarketPosition狀態(tài)記錄收壕。//
Begin
If(!CallAuctionFilter()) Return;// 集合競(jìng)價(jià)和小節(jié)休息過(guò)濾妓灌。//
//系統(tǒng)設(shè)置,K線打分 - 當(dāng)當(dāng)前收盤(pán)價(jià)格大于之前LookBack根K線內(nèi)某一根k線的收盤(pán)價(jià)時(shí)記+1分, 否則記-1分, 加總這些分?jǐn)?shù)以獲得當(dāng)前K線的得分蜜宪。//
TrendScore = 0;//初始賦值0.//
SetGlobalVar(1,0);//設(shè)置第一個(gè)位置全局變量虫埂,初值0.//
for?i = LookBack DownTo 1 //從10循環(huán)到1//
??{
?? If(i == LookBack)//假如變量 i 等于參數(shù)LookBack值。//
{
Temp = 0;//變量Temp賦值0.//
}
Else//變量i不等于參數(shù)LookBack值的圃验。//
{
Temp = GetGlobalVar(1); //變量Temp賦值為位置1的全局變量值0掉伏。//
}
If(C>=C[i]) Temp = Temp +1;//假如當(dāng)前收盤(pán)價(jià)c >=c[i],則變量Temp = Temp(初值0) + 1.//
Else Temp = Temp - 1;//假如當(dāng)前收盤(pán)價(jià)C <C[i]损谦,則變量Temp = Temp(初值0)-1.//
SetGlobalVar(1,Temp); //設(shè)置位置為1的全局變量岖免,值為T(mén)emp值。//
??}
TrendScore = GetGlobalVar(1);//獲取位置1的全局變量值Temp照捡,賦值給變量TrendScore颅湘。//
//均線和ATR計(jì)算。//
MA = AverageFC(C,MALength); //收盤(pán)價(jià)均線栗精。//
TrendScoreMA = AverageFC(TrendScore,MALength);//代入相應(yīng)數(shù)值闯参,求得變量TrendScoreMA均線。//
ATR = AvgTrueRange(ATRLength);//計(jì)算ATR公式了悲立,前面解讀過(guò)了鹿寨。//
//系統(tǒng)入場(chǎng),當(dāng)價(jià)格高于收盤(pán)價(jià)均線, 且打分也高于打分均線時(shí)的入場(chǎng)做多薪夕。//
If(MarketPosition <> 1 and MA[1] <> 0) //假如當(dāng)前沒(méi)有持多單脚草,且前一均線值不等于0.//
{
If(Close[1] >= MA[1] and TrendScore[1] >= TrendScoreMA[1] And Vol > 0)//假如前一收盤(pán)價(jià)大于等于前一均線值,且前一變量TrendScore[1]大于等于前一變量TrendScoreMA[1]原献,且成交量大于0的馏慨。//
{
Buy(0,Open); //以開(kāi)盤(pán)價(jià)開(kāi)倉(cāng)買(mǎi)入。//
ProtectStopL = Low[1] - ProtectStopATRMulti * ATR[1];//基于ATR的保護(hù)性止損計(jì)算公式姑隅,代入相應(yīng)數(shù)值就行了写隶。//
}
}
//系統(tǒng)出場(chǎng)。//
If(BarsSinceEntry == 0)//假如建倉(cāng)數(shù)位等于0.//
HighAfterEntry = High;//記錄持倉(cāng)高價(jià)為當(dāng)前最高價(jià)讲仰。//
Else //建倉(cāng)數(shù)位不等于0的慕趴。//
HighAfterEntry = Max(HighAfterEntry[1],High);//把前一個(gè)持倉(cāng)高價(jià)與當(dāng)前最高價(jià)對(duì)比,取大值鄙陡,再賦值給變量HighAfterEntry值冕房。//
TrailStopL = HighAfterEntry[1] - TrailStopATRMulti * ATR[1];//基于ATR的跟蹤止損計(jì)算公式,也是代入相應(yīng)數(shù)值即可趁矾。//
BreakEvenStopL = LastEntryPrice;//基于ATR的盈虧平衡止損毒费。//
If(MarketPosition == 1 and mp[1] == 1)//假如當(dāng)前持有多單,且前一個(gè)mp[1]等于1.//
{
If(HighAfterEntry[1] >= BreakEvenStopL + BreakEvenStopATRMulti * ATR[1])//代入相應(yīng)數(shù)值計(jì)算愈魏,與前一個(gè)持倉(cāng)記錄高價(jià)對(duì)比觅玻。//
{
if(TrailStopL >= BreakEvenStopL) ExitLineL = TrailStopL;//假如變量TrailStopL 大于等于變量BreakEvenStopL值想际。則出場(chǎng)價(jià)ExitLineL = 變量TrailStopL值。//
Else ExitLineL = BreakEvenStopL;//假如變量TrailStopL值小于變量BreakEvenStopL值的溪厘,則出場(chǎng)價(jià)ExitLineL = 變量BreakEvenStopL值胡本。//
}
Else//持倉(cāng)記錄高價(jià)小的情況。//
{
if(TrailStopL >= ProtectStopL[1]) ExitLineL = TrailStopL;//假如變量TrailStopL大于等于前一變量ProtectStopL[1]值畸悬,則出場(chǎng)價(jià)ExitLineL = 變量TrailStopL值侧甫。//
Else ExitLineL = ProtectStopL[1];//假如變量TrailStopL小于前一變量ProtectStopL[1]值,則出場(chǎng)價(jià)ExitLineL = ProtectStopL[1]蹋宦。//
}
//出場(chǎng)披粟。//
if(L <= ExitLineL And Vol > 0)//假如當(dāng)前低價(jià)小于等于ExitLineL值,且成交量大于0.//
{
Sell(0,Min(Open, ExitLineL));//平倉(cāng)冷冗。//
}
}
MP = MarketPosition; //記錄持倉(cāng)狀態(tài)守屉。//
End
做空代碼及結(jié)果如下:
Params
??Numeric LookBack(10);
??Numeric MALength(18);
??Numeric ATRLength(10);
Numeric ProtectStopATRMulti(0.5);
Numeric TrailStopATRMulti(3);
Numeric BreakEvenStopATRMulti(5);
Vars
NumericSeries MA(0);
NumericSeries TrendScore(0);
NumericSeries TrendScoreMA(0);
NumericSeries ATR(0);
Numeric i;
Numeric Temp;
NumericSeries LowAfterEntry;
NumericSeries ProtectStopS;
Numeric TrailStopS;
Numeric BreakEvenStopS;
Numeric ExitLineS;
NumericSeries MP;
Begin
If(!CallAuctionFilter()) Return;
TrendScore = 0;
SetGlobalVar(1,0);
for?i = LookBack DownTo 1?
??{
?? If(i == LookBack)
{
Temp = 0;
}
Else
{
Temp = GetGlobalVar(1);
}
If(C>=C[i]) Temp = Temp +1;
Else Temp = Temp - 1;
SetGlobalVar(1,Temp);
??}
TrendScore = GetGlobalVar(1);
MA = AverageFC(C,MALength);
TrendScoreMA = AverageFC(TrendScore,MALength);
ATR = AvgTrueRange(ATRLength);
If(MarketPosition <> -1 and MA[1] <> 0)
{
If(Close[1] <= MA[1] and TrendScore[1] <= TrendScoreMA[1] And Vol > 0)
{
SellShort(0,Open);
ProtectStopS = High[1] + ProtectStopATRMulti * ATR[1];
}
}
If(BarsSinceEntry == 0)
LowAfterEntry = Low;
Else
LowAfterEntry = Min(LowAfterEntry[1],Low);
TrailStopS = LowAfterEntry[1] + TrailStopATRMulti * ATR[1];
BreakEvenStopS = LastEntryPrice;
If(MarketPosition == -1 and mp[1] == -1)
{
If(LowAfterEntry[1] <= BreakEvenStopS - BreakEvenStopATRMulti * ATR[1])
{
if(TrailStopS <= BreakEvenStopS) ExitLineS = TrailStopS;
Else ExitLineS = BreakEvenStopS;
}
Else
{
if(TrailStopS <= ProtectStopS[1]) ExitLineS = TrailStopS;
Else ExitLineS = ProtectStopS[1];
}
if(H >= ExitLineS And Vol > 0)
{
BuyToCover(0,Max(Open, ExitLineS));
}
}
MP = MarketPosition;
End
整體看著也很一般,但能從中學(xué)到的就是全局變量可以靈活使用蒿辙,當(dāng)然拇泛,這只是個(gè)人觀點(diǎn)。