跨周期數(shù)據(jù)轉(zhuǎn)換函數(shù)以及跨周期技術(shù)指標(biāo)調(diào)用的實(shí)現(xiàn)
本帖最后由 追漲殺跌 于 2011-7-10 19:44 編輯
TB的跨周期調(diào)用,以前已經(jīng)有一些解決方案。但總體來說,使用還不夠方便。以前的帖子中Nopain曾提出過一個(gè)算法,非常精巧。最近在這個(gè)算法的基礎(chǔ)上, 把整個(gè)實(shí)現(xiàn)方法完善了一下。下面把總體思路和函數(shù)代碼貼出來,請大家試用,也歡迎大家提出修改意見。
總體思路: 1、還是小周期調(diào)用大周期的數(shù)據(jù),這樣才能保證盤中和盤后結(jié)果一樣。大周期調(diào)用小周期,我個(gè)人覺得很難實(shí)現(xiàn),也沒有必要; 2、具體方法是新編一個(gè)函數(shù),用來根據(jù)小周期下的數(shù)據(jù)推算出大周期的BAR數(shù)據(jù),為了省事,計(jì)算時(shí),會(huì)將大周期下的開高低收以及成交量和持倉量等數(shù)據(jù)都算出來,然后將這些數(shù)據(jù)通過引用參數(shù)返回。當(dāng)然有的朋友可能只需要一個(gè)收盤價(jià)就可以了,那樣的話,可以自行修改這個(gè)函數(shù),把多余的數(shù)據(jù)去掉; 3、返回的大周期數(shù)據(jù)將以序列變量的形式保留在對應(yīng)的小周期K線上,以便以后調(diào)用; 4、另外為了便于大周期的數(shù)據(jù)計(jì)算出來后的回溯使用,函數(shù)會(huì)將小周期下當(dāng)前BAR到大周期下的前一個(gè)BAR的最后一個(gè)小周期BAR需要回溯的BAR數(shù)目,作為函數(shù)的結(jié)果返回。這句話,不容易描述清楚,估計(jì)大家聽起來也費(fèi)力,但沒關(guān)系,后面用例子來解釋,知道這回事就行了。 5、另外還增加了一個(gè)返回值(也是通過引用參數(shù)返回),表示的是當(dāng)前小周期K線對應(yīng)的大周期K線在整個(gè)大周期中的索引值。舉例來說,5分鐘圖,樣本數(shù)300,那轉(zhuǎn)換成30分鐘周期圖,總共應(yīng)該是50根K線,那最新一根5分鐘圖對應(yīng)的30分鐘K線在整個(gè)30分鐘圖中的索引值就應(yīng)該是49(類似CurrentBar 0-49);
好,其他先不廢話,把代碼貼出來,再舉幾個(gè)例子。
新建用戶函數(shù)MtBar, 返回值為數(shù)值類型,代碼如下:
-
Params
-
Numeric TimeFrame(1440); // 目標(biāo)時(shí)間周期:月線=40320,周線=10080,日線=1440,4小時(shí)線=240 其他周期等于相應(yīng)的分鐘數(shù),如:1小時(shí)=60 30分鐘=30。。。
-
Numeric BarsBack(1); // 目標(biāo)時(shí)間周期BAR偏移:1--表示將目標(biāo)時(shí)間周期下的前1根K線數(shù)據(jù)作為與當(dāng)前Bar對應(yīng)的目標(biāo)時(shí)間周期下的K線數(shù)據(jù)
-
// 0--表示將目標(biāo)時(shí)間周期下的截止到目前為止的數(shù)據(jù)轉(zhuǎn)換為與當(dāng)前BAR對應(yīng)的目標(biāo)時(shí)間周期下K線數(shù)據(jù)
-
NumericRef oCurBar; // 目標(biāo)時(shí)間周期下的Bar索引
-
NumericRef oOPenHT; // 目標(biāo)時(shí)間周期下的開盤價(jià)
-
NumericRef oHighHT; // 目標(biāo)時(shí)間周期下的最高價(jià)
-
NumericRef oLowHT; // 目標(biāo)時(shí)間周期下的最低價(jià)
-
NumericRef oCloseHT; // 目標(biāo)時(shí)間周期下的收盤價(jià)
-
NumericRef oVolHT; // 目標(biāo)時(shí)間周期下的成交量
-
NumericRef oOpenIntHT; // 目標(biāo)時(shí)間周期下的持倉量
-
-
Vars
-
NumericSeries barCnt;
-
NumericSeries CurBar;
-
NumericSeries barCntSum;
-
NumericSeries OpenHT;
-
NumericSeries HighHT;
-
NumericSeries LowHT;
-
NumericSeries CloseHT;
-
NumericSeries VolHT;
-
NumericSeries OpenIntHT;
-
Numeric CurTime;
-
Numeric PreTime;
-
bool condition(false);
-
Numeric i;
-
Begin
-
If (TimeFrame == 40320) // 月線
-
{
-
CurTime = Month;
-
PreTime = Month[1];
-
}
-
Else If (TimeFrame == 10080)
-
{
-
CurTime = IntPart(DateDiff(19700105,Date)/7);
-
PreTime = IntPart(DateDiff(19700105,Date[1])/7);
-
}
-
Else
-
{
-
CurTime = IntPart((DateDiff(19700105,date)*1440 + Hour*60 + Minute)/TimeFrame);
-
PreTime = IntPart((DateDiff(19700105,date[1])*1440 + Hour[1]*60 + Minute[1])/TimeFrame);
-
}
-
condition = CurTime != PreTime;
-
-
If (CurrentBar==0) // 如果是第一根Bar, CurBar=0
-
{
-
barCnt = 0;
-
CurBar = 0;
-
OpenHT = Open;
-
HighHT = High;
-
LowHT = Low;
-
CloseHT = Close;
-
VolHT = Vol;
-
OpenIntHT = OpenInt;
-
}
-
Else
-
{
-
If(Condition) // 如果在目標(biāo)周期下,屬于另一根K線,則CurBar加1
-
{
-
barCnt = 1;
-
CurBar = CurBar[1] + 1;
-
OpenHT = Open;
-
HighHT = High;
-
LowHT = Low;
-
VolHT = Vol;
-
}Else // 如果在目標(biāo)周期下,屬于同一根K線,則CurBar不變,但最高價(jià)和最低價(jià)要記錄價(jià)格的變化,成交量要累加
-
{
-
barCnt = barCnt[1] + 1;
-
CurBar = CurBar[1];
-
OpenHT = OpenHT[1];
-
HighHT = Max(HighHT[1],High);
-
LowHT = Min(LowHT[1],Low);
-
VolHT = VolHT[1] + Vol;
-
//FileAppend("d:\\test.log","222 CurrentBar="+text(CurrentBar)+" BarCnt="+text(BarCnt)+" CurBar="+text(oCurBar)+" O="+Text(OpenHt)+" H="+Text(HighHT)+" L="+Text(LowHT)+" C="+Text(CloseHT));
-
}
-
// 收盤價(jià)和持倉量總是取最新值
-
CloseHT = Close;
-
OpenIntHT = OpenInt;
-
}
-
-
// 上面的程序,在每根小周期的K線上,記錄了它所屬的大時(shí)間周期下的開高低收等值的變化。
-
// 其中大周期的最終收盤數(shù)據(jù),記錄在這一組小周期K線的最后一根上。
-
barCntSum = barCnt ;
-
If(BarsBack == 0) // 如果Bar偏移參數(shù)為0,則取每根小周期K線上保留的大時(shí)間周期截止到這根小周期K線為止的BAR數(shù)據(jù)
-
{
-
barCntSum = 0 ;
-
}Else If(BarsBack == 1) // 如果Bar偏移參數(shù)為1,則取大時(shí)間周期的上一根K線的BAr數(shù)據(jù)
-
{
-
barCntSum = barCnt ;
-
}Else // 如果BAR偏移參數(shù)為其他,則取大時(shí)間周期的指定偏移后的那根K線的BAR數(shù)據(jù)
-
{
-
For i = 2 To BarsBack
-
{
-
barCntSum = barCntSum + barCnt[barCntSum];
-
}
-
}
-
-
// 最后將相應(yīng)的K線數(shù)據(jù)作為引用參數(shù)返回
-
oCurBar = CurBar;
-
oOpenHT = OpenHT[barCntSum];
-
oHighHT = HighHT[barCntSum];
-
oLowHT = LowHT[barCntSum];
-
oCloseHT = CloseHT[barCntSum];
-
oVolHT = VolHT[barCntSum];
-
oOpenIntHT = OpenIntHT[barCntSum];
-
Return barCnt;
-
End
|
|
|
|
|
|
|
|
呵呵, 代碼貼得有點(diǎn)亂,整理一下,重發(fā)過。
-
Params
-
Numeric TimeFrame(1440);
-
// 目標(biāo)時(shí)間周期:月線=40320,周線=10080,日線=1440,4小時(shí)線=240
-
// 其他1小時(shí)內(nèi)的周期等于相應(yīng)的分鐘數(shù),如:1小時(shí)=60, 30分鐘=30。。。
-
// 支持不規(guī)則分鐘數(shù),如3分鐘,8分鐘,之類都行
-
-
Numeric BarsBack(1);
-
// 目標(biāo)時(shí)間周期BAR偏移:
-
// 1--表示將目標(biāo)時(shí)間周期下的前1根K線數(shù)據(jù)作為與當(dāng)前Bar對應(yīng)的目標(biāo)時(shí)間周期下的K線數(shù)據(jù)
-
// 0--表示將目標(biāo)時(shí)間周期下的截止到目前為止的數(shù)據(jù)轉(zhuǎn)換為與當(dāng)前BAR對應(yīng)的目標(biāo)時(shí)間周期下K線數(shù)據(jù)
-
-
NumericRef oCurBar; // 目標(biāo)時(shí)間周期下的Bar索引
-
NumericRef oOPenHT; // 目標(biāo)時(shí)間周期下的開盤價(jià)
-
NumericRef oHighHT; // 目標(biāo)時(shí)間周期下的最高價(jià)
-
NumericRef oLowHT; // 目標(biāo)時(shí)間周期下的最低價(jià)
-
NumericRef oCloseHT; // 目標(biāo)時(shí)間周期下的收盤價(jià)
-
NumericRef oVolHT; // 目標(biāo)時(shí)間周期下的成交量
-
NumericRef oOpenIntHT; // 目標(biāo)時(shí)間周期下的持倉量
-
-
Vars
-
NumericSeries barCnt;
-
NumericSeries CurBar;
-
NumericSeries barCntSum;
-
NumericSeries OpenHT;
-
NumericSeries HighHT;
-
NumericSeries LowHT;
-
NumericSeries CloseHT;
-
NumericSeries VolHT;
-
NumericSeries OpenIntHT;
-
Numeric CurTime;
-
Numeric PreTime;
-
bool condition(false);
-
Numeric i;
-
Begin
-
If (TimeFrame == 40320) // 月線
-
{
-
CurTime = Month;
-
PreTime = Month[1];
-
}
-
Else If (TimeFrame == 10080) // 周線
-
{
-
CurTime = IntPart(DateDiff(19700105,Date)/7);
-
PreTime = IntPart(DateDiff(19700105,Date[1])/7);
-
}
-
Else // 其他時(shí)間周期
-
{
-
CurTime = IntPart((DateDiff(19700105,date)*1440 + Hour*60 + Minute)/TimeFrame);
-
PreTime = IntPart((DateDiff(19700105,date[1])*1440 + Hour[1]*60 + Minute[1])/TimeFrame);
-
}
-
condition = CurTime != PreTime;
-
-
If (CurrentBar==0) // 如果是第一根Bar, CurBar=0
-
{
-
barCnt = 0;
-
CurBar = 0;
-
OpenHT = Open;
-
HighHT = High;
-
LowHT = Low;
-
CloseHT = Close;
-
VolHT = Vol;
-
OpenIntHT = OpenInt;
-
}
-
Else
-
{
-
If(Condition)
-
// 如果在目標(biāo)周期下,屬于另一根K線,則CurBar加1
-
{
-
barCnt = 1;
-
CurBar = CurBar[1] + 1;
-
OpenHT = Open;
-
HighHT = High;
-
LowHT = Low;
-
VolHT = Vol;
-
}Else
-
// 如果在目標(biāo)周期下,屬于同一根K線,則CurBar不變,但最高價(jià)和最低價(jià)要記錄價(jià)格的變化,成交量要累加
-
{
-
barCnt = barCnt[1] + 1;
-
CurBar = CurBar[1];
-
OpenHT = OpenHT[1];
-
HighHT = Max(HighHT[1],High);
-
LowHT = Min(LowHT[1],Low);
-
VolHT = VolHT[1] + Vol;
-
}
-
// 收盤價(jià)和持倉量總是取最新值
-
CloseHT = Close;
-
OpenIntHT = OpenInt;
-
}
-
-
// 上面的程序,在每根小周期的K線上,記錄了它所屬的大時(shí)間周期下的開高低收等值的變化。
-
// 接下來,要把在大的時(shí)間周期級別上,屬于同一根K線的開高低收這些數(shù)據(jù),記錄在這一組小周期K線的最后一根上。
-
barCntSum = barCnt ;
-
If(BarsBack == 0)
-
// 如果Bar偏移參數(shù)為0,則取每根小周期K線上保留的大時(shí)間周期截止到這根小周期K線為止的BAR數(shù)據(jù)
-
{
-
barCntSum = 0 ;
-
}Else If(BarsBack == 1)
-
// 如果Bar偏移參數(shù)為1,則取大時(shí)間周期的上一根K線的BAr數(shù)據(jù)
-
{
-
barCntSum = barCnt ;
-
}Else
-
// 如果BAR偏移參數(shù)為其他,則取大時(shí)間周期的指定偏移后的那根K線的BAR數(shù)據(jù)
-
{
-
For i = 2 To BarsBack
-
{
-
barCntSum = barCntSum + barCnt[barCntSum];
-
}
-
}
-
-
// 最后將相應(yīng)的K線數(shù)據(jù)作為引用參數(shù)返回
-
oCurBar = CurBar;
-
oOpenHT = OpenHT[barCntSum];
-
oHighHT = HighHT[barCntSum];
-
oLowHT = LowHT[barCntSum];
-
oCloseHT = CloseHT[barCntSum];
-
oVolHT = VolHT[barCntSum];
-
oOpenIntHT = OpenIntHT[barCntSum];
-
Return barCnt;
-
End
|
|
|
|
|
|
|
|
忘了說一句,以上代碼是在TB V4中編寫與調(diào)試的。
接下來,我們來實(shí)現(xiàn)跨周期的求和函數(shù)MtSummation,上面的函數(shù)中特地返回了一個(gè)值oCurBar,就是我上面提到的大周期下的BAR索引值,就是想為實(shí)現(xiàn)MtSummationFC留下的伏筆,等以后有時(shí)間再解決?,F(xiàn)在先還是用最笨的循環(huán)累加的算法。
-
Params
-
NumericSeries Price(1);
-
NumericSeries BarCnt(0);
-
Numeric Length(10);
-
Vars
-
NumericSeries SumValue(0);
-
Numeric i;
-
Numeric j(0);
-
Begin
-
SumValue = 0;
-
For i = 1 to Length
-
{
-
If (Price[j] <> InvalidNumeric)
-
{
-
SumValue = SumValue + Price[j];
-
j = j + BarCnt[j];
-
}
-
else Break;
-
}
-
Return SumValue;
-
End
|
|
|
|
|
|
|
|
接下來,就可以實(shí)現(xiàn)計(jì)算跨周期簡單移動(dòng)平均的函數(shù)MtMa。
-
Params
-
Numeric TimeFrame(1440); // 目標(biāo)時(shí)間周期參數(shù),參數(shù)說明參見MtBar
-
Numeric BarsBack(1); // 目標(biāo)時(shí)間周期BAR偏移參數(shù),說明見MtBar函數(shù)
-
Numeric Length(10); // 均線周期
-
NumericRef oMA; // 以目標(biāo)時(shí)間周期下的K線數(shù)據(jù)計(jì)算出的移動(dòng)平均線
-
Vars
-
NumericSeries mtBarCnt;
-
NumericSeries mtClose;
-
Numeric refCurBar;
-
Numeric refOpen;
-
Numeric refHigh;
-
Numeric refLow;
-
Numeric refClose;
-
Numeric refVol;
-
Numeric refOpenInt;
-
-
Numeric SumValue(0);
-
Numeric i;
-
Numeric j(0);
-
Begin
-
mtBarCnt = MtBar(TimeFrame,BarsBack,refCurBar,refOpen,refHigh,refLow,refClose,refVol,refOpenInt);
-
mtClose = refClose;
-
-
SumValue = MtSummation(mtClose,mtBarCnt,Length);
-
oMA = SumValue/Length;
-
Return mtBarCnt;
-
End
|
|
|
|
|
|
|
|
現(xiàn)在可以舉個(gè)例子來說明,怎么用上面的幾個(gè)函數(shù)來做交易策略了。假如我們的策略如下: 1、我們以日線的均線交叉來判斷大趨勢,然后在5分鐘圖上來做交易; 2、日線的短期均線上穿長期均線,則只做多,不做空;反之只做空,不做多; 3、確定了大趨勢后,我們再根據(jù)5分鐘圖來判斷小趨勢,以決定進(jìn)場時(shí)機(jī)。我們?nèi)匀挥镁€來判斷,在多頭大趨勢下,如果5分鐘的短期均線上穿長期均線,則進(jìn)場做多,反穿出場,但不反手做空;在空頭大趨勢下,類似; 我們在5分鐘圖上調(diào)用剛才的函數(shù)的來實(shí)現(xiàn)。新建公式應(yīng)用TestMtMa,代碼如下:
-
Params
-
Numeric TimeFrame(1440); // 目標(biāo)時(shí)間周期參數(shù),參數(shù)說明參見MtBar
-
Numeric BarsBack(1); // 目標(biāo)時(shí)間周期BAR偏移參數(shù),說明見MtBar函數(shù)
-
-
Numeric Length1(10); // 大周期的短期均線周期
-
Numeric Length2(20); // 大周期的長期均線周期
-
Numeric Length3(10); // 小周期的短期均線周期
-
Numeric Length4(20); // 小周期的長期均線周期
-
Numeric Lots(1);
-
Vars
-
NumericSeries MA1;
-
NumericSeries MA2;
-
Numeric oMA1;
-
Numeric oMA2;
-
-
NumericSeries MA3;
-
NumericSeries MA4;
-
Begin
-
MtMa(TimeFrame,BarsBack,Length1,oMA1);
-
MA1 = oMA1;
-
PlotNumeric("MA1",MA1);
-
MtMa(TimeFrame,BarsBack,Length2,oMA2);
-
MA2 = oMA2;
-
PlotNumeric("MA2",MA2);
-
MA3 = AverageFC(Close,Length3);
-
MA4 = AverageFC(Close,Length4);
-
PlotNumeric("MA3",MA3);
-
PlotNumeric("MA4",MA4);
-
-
-
If (MA1>MA2) // 大周期均線金叉,多頭趨勢
-
{
-
if (MarketPosition!=1 and MA3[1]>MA4[1])
-
{
-
Buy(Lots,Open);
-
}
-
if (MarketPosition==1 and MA3[1]<MA4[1])
-
{
-
Sell(Lots,Open);
-
}
-
}
-
If (MA1<MA2) // 大周期均線死叉,空頭趨勢
-
{
-
if (MarketPosition!=-1 and MA3[1]<MA4[1])
-
{
-
SellShort(Lots,Open);
-
}
-
if (MarketPosition==-1 and MA3[1]>MA4[1])
-
{
-
BuyToCover(Lots,Open);
-
}
-
}
-
End
復(fù)制代碼
|
|
|
|
|
|
|
|
如果上面的策略,我們不想用日線均線來判斷大趨勢,而改用小時(shí)線來判斷,則只要將參數(shù)TimeFrame改為60, 就可以了。
|
|
|
|
|
|
|
|
很好很強(qiáng)大,慢慢學(xué)習(xí)。 使用起來非常方便啊,只要修改時(shí)間參數(shù)就行了,十分感謝?。?/div>
|
|
|
|
|
|
|
|
謝謝道勤的鼓勵(lì)! 繼續(xù)把其他幾個(gè)常用指標(biāo)的跨周期函數(shù)貼出來。 新建MtMacd函數(shù),代碼如下:
-
Params
-
Numeric TimeFrame(1440); // 目標(biāo)時(shí)間周期參數(shù),參數(shù)說明參見MtBar
-
Numeric BarsBack(1); // 目標(biāo)時(shí)間周期BAR偏移參數(shù),說明見MtBar函數(shù)
-
Numeric FastLength(12);
-
Numeric SlowLength(26);
-
Numeric MACDLength(9);
-
-
NumericRef oMACDValue; // 以目標(biāo)時(shí)間周期下的K線數(shù)據(jù)計(jì)算出的MACDValue
-
NumericRef oAvgMACD; // 以目標(biāo)時(shí)間周期下的K線數(shù)據(jù)計(jì)算出的AvgMACD
-
-
Vars
-
NumericSeries mtBarCnt;
-
NumericSeries mtCurBar;
-
NumericSeries mtClose;
-
Numeric refCurBar;
-
Numeric refOpen;
-
Numeric refHigh;
-
Numeric refLow;
-
Numeric refClose;
-
Numeric refVol;
-
Numeric refOpenInt;
-
-
NumericSeries MACDValue;
-
NumericSeries AvgMACD;
-
Numeric sFcactor;
-
NumericSeries XAvgValue1;
-
NumericSeries XAvgValue2;
-
-
Begin
-
-
mtBarCnt = MtBar(TimeFrame,BarsBack,refCurBar,refOpen,refHigh,refLow,refClose,refVol,refOpenInt);
-
mtCurBar = refCurBar;
-
mtClose = refClose;
-
-
sFcactor = 2 / ( FastLength + 1 );
-
if (MtCurBar == 0 || XAvgValue1[mtBarCnt] == InvalidNumeric) XAvgValue1 = mtClose;
-
else XAvgValue1 = XAvgValue1[mtBarCnt] + sFcactor * ( mtClose - XAvgValue1[mtBarCnt] ) ;
-
-
sFcactor = 2 / ( SlowLength + 1 );
-
if (MtCurBar == 0 || XAvgValue2[mtBarCnt] == InvalidNumeric) XAvgValue2 = mtClose;
-
else XAvgValue2 = XAvgValue2[mtBarCnt] + sFcactor * ( mtClose - XAvgValue2[mtBarCnt] ) ;
-
-
MACDValue = XAvgValue1 - XAvgValue2;
-
-
sFcactor = 2 / ( MACDLength + 1 );
-
if (MtCurBar == 0 || AvgMACD[mtBarCnt] == InvalidNumeric) AvgMACD = MACDValue;
-
else AvgMACD = AvgMACD[mtBarCnt] + sFcactor * ( MACDValue - AvgMACD[mtBarCnt] ) ;
-
-
oMACDValue = MACDValue;
-
oAvgMACD = AvgMACD;
-
return mtBarCnt;
-
End
|
|
|
|
|
編譯好上面的MtMacd函數(shù)后,以后就可以隨時(shí)通過調(diào)用這個(gè)函數(shù)來顯示跨 周期的MACD指標(biāo),以及在小 周期上根據(jù)大 周期的MACD指標(biāo)進(jìn)行交易,下面繼續(xù)舉個(gè)例子,也是在V4中實(shí)現(xiàn),把指標(biāo)和交易指令放在一個(gè)公式應(yīng)用中,注意要在“屬性設(shè)置”中,設(shè)置成“子圖顯示”。這次簡單點(diǎn),沒有做成多 周期共振,只是簡單地在5分鐘圖表上,根據(jù)30分鐘的MACD指標(biāo)交叉來進(jìn)行交易,代碼如下:
- Params
- Numeric TimeFrame(30); // 目標(biāo)時(shí)間周期參數(shù),參數(shù)說明參見MtBar
- Numeric BarsBack(1); // 目標(biāo)時(shí)間周期BAR偏移參數(shù),說明見MtBar函數(shù)
- Numeric FastLength(12);
- Numeric SlowLength(26);
- Numeric MACDLength(9);
- Numeric Lots(1);
- Vars
- NumericSeries MACDValue;
- NumericSeries AvgMACD;
- NumericSeries MACDDiff;
- Numeric ooMACDValue;
- Numeric ooAvgMACD;
- Numeric PreBar;
- Begin
- PreBar = MtMACD(TimeFrame,BarsBack,FastLength,SlowLength,MACDLength,ooMACDValue,ooAvgMACD);
- MACDVAlue = ooMACDValue;
- AvgMACD = ooAvgMACD;
- MACDDiff = MACDValue - AvgMACD;
- PlotNumeric("MACD",MACDValue);
- PlotNumeric("MACDAvg",AvgMACD);
- If (MACDDiff >= 0)
- PlotNumeric("MACDDiff",MACDDiff,0,Red);
- Else
- PlotNumeric("MACDDiff",MACDDiff,0,Green);
- PlotNumeric("零線",0);
- If (MacdDiff[PreBar]<0 and MacdDiff>0)
- {
- Buy(Lots,Open);
- }
- If (MacdDiff[PreBar]>0 and MacdDiff<0)
- {
- SellShort(Lots,Open);
- }
- End
先滿足haomai01朋友的需要, 把跨 周期DMI函數(shù)先貼出來。函數(shù)名MtDMI
- Params
- Numeric TimeFrame(1440); // 目標(biāo)時(shí)間周期參數(shù),參數(shù)說明參見MtBar
- Numeric BarsBack(1); // 目標(biāo)時(shí)間周期BAR偏移參數(shù),說明見MtBar函數(shù)
- Numeric Length(14);
- NumericRef oDMIPlus;
- NumericRef oDMIMinus;
- NumericRef oADX;
- Vars
- NumericSeries mtBarCnt;
- NumericSeries mtHigh;
- NumericSeries mtLow;
- NumericSeries mtClose;
- Numeric refCurBar;
- Numeric refOpen;
- Numeric refHigh;
- Numeric refLow;
- Numeric refClose;
- Numeric refVol;
- Numeric refOpenInt;
- NumericSeries DMIPlus;
- NumericSeries DMIMinus;
- NumericSeries DMI;
- NumericSeries ADX;
- NumericSeries ADXR;
- NumericSeries Volty;
- NumericSeries sDMI;
- NumericSeries sADX;
- NumericSeries sVolty;
- Numeric PlusDM;
- Numeric MinusDM;
- Numeric UpperMove;
- Numeric LowerMove;
- Numeric SumPlusDM(0);
- Numeric SumMinusDM(0);
- Numeric SumTR(0);
- NumericSeries AvgPlusDM;
- NumericSeries AvgMinusDM;
- Numeric SF; // smoothing factor
- Numeric Divisor;
- BoolSeries bStopLoop;
- Numeric i;
- Numeric j(0);
- Numeric K;
- Numeric nbHigh;
- NumericSeries TRValue;
- Numeric CumValue(0);
- Begin
- mtBarCnt = MtBar(TimeFrame,BarsBack,refCurBar,refOpen,refHigh,refLow,refClose,refVol,refOpenInt);
- mtHigh = refHigh;
- mtLow = refLow;
- mtClose = refClose;
-
- SF = 1/Length;
- TRValue = IIF(mtHigh >= mtClose[mtBarCnt],mtHigh,mtClose[mtBarCnt]) - IIF(mtLow <= mtClose[mtBarCnt],mtLow,mtClose[mtBarCnt]);
- bStopLoop = bStopLoop[1];
- If (bStopLoop!=true)
- {
- j = 0;
- For i = 0 to Length
- {
- If (mtHigh[j] == InvalidNumeric)
- {
- nbHigh = InvalidNumeric;
- i = Length + 1;
- }
- Else
- {
- nbHigh = mtHigh[j];
- j = j + mtBarCnt[j];
- }
- }
- if(nbHigh!=InvalidNumeric and mtHigh[j] != InvalidNumeric) bStopLoop = true;
- }
- If(nbHigh == InvalidNumeric)
- {
- DMIPlus = InvalidNumeric;
- DMIMinus = InvalidNumeric;
- DMI = InvalidNumeric;
- ADX = InvalidNumeric;
- ADXR = InvalidNumeric;
- Volty = InvalidNumeric;
- //RSIValue = InvalidNumeric;
- }Else
- {
- If(bStopLoop !=true)
- {
- j = 0;
- K = 0;
- For i = 1 To Length
- {
- j = K + mtBarCnt[K];
- PlusDM = 0 ;
- MinusDM = 0 ;
- UpperMove = High[K] - High[j] ;
- LowerMove = Low[j] - Low[K] ;
- if (UpperMove > LowerMove and UpperMove > 0 )
- {
- PlusDM = UpperMove;
- }else if (LowerMove > UpperMove and LowerMove > 0)
- {
- MinusDM = LowerMove ;
- }
- SumPlusDM = SumPlusDM + PlusDM ;
- SumMinusDM = SumMinusDM + MinusDM ;
- SumTR = SumTR + TRValue[K] ;
- K = j ;
- }
- AvgPlusDM = SumPlusDM / Length ;
- AvgMinusDM = SumMinusDM / Length ;
- sVolty = SumTR / Length ;
- }
- Else
- {
- PlusDM = 0 ;
- MinusDM = 0 ;
- UpperMove = mtHigh - mtHigh[mtBarCnt] ;
- LowerMove = mtLow[mtBarCnt] - mtLow ;
- if (UpperMove > LowerMove and UpperMove > 0 )
- {
- PlusDM = UpperMove;
- }else if (LowerMove > UpperMove and LowerMove > 0 )
- {
- MinusDM = LowerMove ;
- }
- AvgPlusDM = AvgPlusDM[mtBarCnt] + SF * ( PlusDM - AvgPlusDM[mtBarCnt] ) ;
- AvgMinusDM = AvgMinusDM[mtBarCnt] + SF * ( MinusDM - AvgMinusDM[mtBarCnt] ) ;
- sVolty = sVolty[mtBarCnt] + SF * ( TRValue - sVolty[mtBarCnt] ) ;
- }
- if (sVolty > 0)
- {
- DMIPlus = 100 * AvgPlusDM / sVolty ;
- DMIMinus = 100 * AvgMinusDM / sVolty ;
- }else
- {
- DMIPlus = 0 ;
- DMIMinus = 0 ;
- }
- Divisor = DMIPlus + DMIMinus ;
- if (Divisor > 0)
- {
- sDMI = 100 * Abs( DMIPlus - DMIMinus ) / Divisor;
- }else
- {
- sDMI = 0 ;
- }
- if (bStopLoop != true)
- {
- K = 0;
- j = 0;
- For i = 0 To Length - 1
- {
- CumValue = CumValue + sDMI[K];
- j = K + mtBarCnt[K];
- If (mtHigh[j] != InvalidNumeric) K = j;
- else
- {
- j = i;
- i = Length + 1;
- }
- }
- sADX = CumValue / j ;
- ADXR = ( sADX + sADX[ K-1 ] ) * 0.5 ;
- }else
- {
- sADX = sADX[mtBarCnt] + SF * ( sDMI - sADX[mtBarCnt] ) ;
- j = 0;
- For i = 0 To Length-1
- {
- j = j + mtBarCnt[j];
- }
- ADXR = ( sADX + sADX[ j ] ) * 0.5 ;
- }
- Volty = sVolty;
- DMI = sDMI;
- ADX = sADX;
- }
- oDMIPlus = DMIPlus;
- oDMIMinus = DMIMinus;
- oADX = ADX;
- return mtBarCnt;
- End
同樣舉個(gè)例子。公式應(yīng)用TestMtDMI
- Params
- Numeric TimeFrame(1440); // 目標(biāo)時(shí)間周期參數(shù),參數(shù)說明參見MtBar
- Numeric BarsBack(1); // 目標(biāo)時(shí)間周期BAR偏移參數(shù),說明見MtBar函數(shù)
- Numeric Length(20);
- Numeric Offset(2);
- Numeric Lots(1);
-
- Vars
- NumericSeries DMIPlus;
- NumericSeries DMIMinus;
- NumericSeries ADX;
- NumericSeries PreBar;
- Numeric DPlus;
- Numeric DMinus;
- Numeric tADX;
- Begin
- PreBar = MtDMI(TimeFrame,BarsBack,Length,DPLus,DMinus,tADX);
- DMIPlus = DPlus;
- DMIMinus = DMinus;
- ADX = tADX;
- PlotNumeric( "DMI+" ,DMIPlus);
- PlotNumeric( "DMI-" ,DMIMinus);
- PlotNumeric( "ADX" ,ADX);
-
- If ( MarketPosition != 1 and DMIPlus>DMIMinus and DMIPlus[PreBar]<DMIMinus[PreBar])
- {
- Buy(Lots,Open);
- }
- If ( MarketPosition !=-1 and DMIPlus<DMIMinus and DMIPlus[PreBar]>DMIMinus[PreBar])
- {
- SellShort(Lots,Open);
- }
- End
在小時(shí)圖上插入該公式應(yīng)用即可,參數(shù)默認(rèn)我就是日線1440,如需其他周期,直接改參數(shù)就可
按照你的意思,大致寫了一下,看看是否符合你的要求。
- //------------------------------------------------------------------------
- // 簡稱: TestMtMa_V2
- // 名稱: 周線加日線均線策略
- // 類別: 公式應(yīng)用
- // 類型: 用戶應(yīng)用
- // 輸出:
- //------------------------------------------------------------------------
- Params
- Numeric TimeFrame(10080); // 目標(biāo)時(shí)間周期:周線
- Numeric BarsBack(1); // 以前一周的均線來判斷
- Numeric WeekLength(5); // 周線的均線周期
- Numeric Length1(5); // 日線的短期均線周期
- Numeric Length2(10); // 日線的長期均線周期
- Numeric Lots(1); // 交易頭寸大小
- Vars
- NumericSeries WeekMA;
- Numeric oWeekMA;
- NumericSeries MA1;
- NumericSeries MA2;
- Begin
- MtMa(TimeFrame,BarsBack,WeekLength,oWeekMA);
- WeekMA = oWeekMA;
- PlotNumeric("WeekMA",WeekMA);
- MA1 = AverageFC(Close,Length1);
- MA2 = AverageFC(Close,Length2);
- PlotNumeric("MA1",MA1);
- PlotNumeric("MA2",MA2);
-
- If (MA1[1]>WeekMA and MA2[1]>WeekMA) // 日線短期均線和長期均線均再周線5周期均線之上
- {
- if (MarketPosition!=1 and MA1[1]>MA2[1])
- {
- Buy(Lots,Open);
- }
- if (MarketPosition==1 and MA1[1]<MA2[1])
- {
- Sell(Lots,Open);
- }
- }
- If (MA1[1]<WeekMA and MA2[1]<WeekMA) // 日線短期均線和長期均線均再周線5周期均線之下
- {
- if (MarketPosition!=-1 and MA1[1]<MA2[1])
- {
- SellShort(Lots,Open);
- }
- if (MarketPosition==-1 and MA1[1]>MA2[1])
- {
- BuyToCover(Lots,Open);
- }
- }
- End
拖了好幾天了,一直沒有把跨周期已經(jīng)完成的幾個(gè)指標(biāo)貼上來。致富網(wǎng)友,也提出了很高的要求,我試著來寫一寫吧。 首先把跨周期的KDJ指標(biāo)完成,新建函數(shù)MtKDJ,代碼如下:
- Params
- Numeric TimeFrame(1440); // 目標(biāo)時(shí)間周期參數(shù),參數(shù)說明參見MtBar
- Numeric BarsBack(1); // 目標(biāo)時(shí)間周期BAR偏移參數(shù),說明見MtBar函數(shù)
- Numeric Length(14);
- Numeric SlowLength(3);
- Numeric SmoothLength(3);
-
- NumericRef oKValue;
- NumericRef oDValue;
- Vars
- NumericSeries mtBarCnt;
- NumericSeries mtOpen;
- NumericSeries mtHigh;
- NumericSeries mtLow;
- NumericSeries mtClose;
- NumericSeries mtVol;
- NumericSeries mtOpenInt;
- Numeric refCurBar;
- Numeric refOpen;
- Numeric refHigh;
- Numeric refLow;
- Numeric refClose;
- Numeric refVol;
- Numeric refOpenInt;
- NumericSeries HighestValue;
- NumericSeries LowestValue;
- NumericSeries KValue;
- Numeric DValue;
- Numeric i;
- Numeric j(0);
- Numeric SumHLValue(0);
- Numeric SumCLValue(0);
- Numeric sumValue(0);
- Begin
- mtBarCnt = MtBar(TimeFrame,BarsBack,refCurBar,refOpen,refHigh,refLow,refClose,refVol,refOpenInt);
- mtHigh = refHigh;
- mtLow = refLow;
- mtClose = refClose;
-
- HighestValue = mtHigh;
- LowestValue = mtLow;
- For i = 1 to Length-1
- {
- j = j + mtBarCnt[j];
- If (mtHigh[j] == InvalidNumeric) HighestValue = InvalidNumeric;
- Else If ( mtHigh[j] > HighestValue) HighestValue = mtHigh[j];
- If (mtLow[j] == InvalidNumeric) LowestValue = InvalidNumeric;
- Else If ( mtLow[j] < LowestValue) LowestValue = mtLow[j];
- }
- j = 0;
- For i = 1 to SlowLength
- {
- If (mtClose[j] == InvalidNumeric)
- {
- SumHLValue = 0; //InvalidNumeric;
- SumCLValue = 0; //InvalidNumeric;
- }
- Else
- {
- SumCLValue = SumCLValue + mtClose[j] - LowestValue[j];
- SumHLValue = SumHLValue + HighestValue[j] - LowestValue[j];
- j = j + mtBarCnt[j];
- }
- }
- If (SumHLValue <> 0)
- {
- KValue = SumCLValue/SumHLValue*100;
- }Else
- {
- KValue = 0;
- }
- j = 0;
- For i = 1 to SmoothLength
- {
- If (KValue[j] == InvalidNumeric) SumValue = 0; //InvalidNumeric;
- Else
- {
- SumValue = SumValue + KValue[j];
- j = j + mtBarCnt[j];
- }
- }
- DValue = SumValue/SmoothLength;
- oKValue = KValue;
- oDValue = DValue;
- return mtBarCnt;
- End
復(fù)制代碼
然后和往常一樣,寫個(gè)調(diào)用的例子,新建公式應(yīng)用TestMtKdj,隨便在日線以下的時(shí)間周期中使用,參數(shù)為1440,表示求日線KDJ,根據(jù)日線KDJ,金叉買入,死叉賣出,不管交叉發(fā)生的位置。代碼如下:
- Params
- Numeric TimeFrame(1440); // 日線
- Numeric BarsBack(1); // 求前一日的值
- Numeric Length(14);
- Numeric SlowLength(3);
- Numeric SmoothLength(3);
- Numeric Lots(1);
- Vars
- NumericSeries KValue;
- NumericSeries DValue;
- Numeric ooKValue;
- Numeric ooDValue;
- Numeric PreBar;
- Begin
- PreBar = MtKDJ(TimeFrame,BarsBack,Length,SlowLength,SmoothLength,ooKValue,ooDValue);
- KValue = ooKValue;
- DValue = ooDValue;
- PlotNumeric("K",KValue);
- PlotNumeric("D",DValue);
- PlotNumeric("J",3*KValue - 2*DValue);
- PlotNumeric("Ref1",20);
- PlotNumeric("Ref2",80);
- If (KValue[PreBar] < DValue[PreBar] and KValue>DValue)
- {
- Buy(Lots,Open);
- }
- If (KValue[PreBar] > DValue[PreBar] and KValue<DValue)
- {
- SellShort(Lots,Open);
- }
- End
跨周期BOLL線指標(biāo),新建函數(shù)MtBoll,代碼如下:
- Params
- Numeric TimeFrame(1440); // 目標(biāo)時(shí)間周期參數(shù),參數(shù)說明參見MtBar
- Numeric BarsBack(1); // 目標(biāo)時(shí)間周期BAR偏移參數(shù),說明見MtBar函數(shù)
- Numeric Length(20);
- Numeric Offset(2);
- NumericRef oUpLine; // 上軌
- NumericRef oDownLine; // 下軌
- NumericRef oMidLine; // 中軌
- Vars
- NumericSeries mtBarCnt;
- NumericSeries mtClose;
- Numeric refCurBar;
- Numeric refOpen;
- Numeric refHigh;
- Numeric refLow;
- Numeric refClose;
- Numeric refVol;
- Numeric refOpenInt;
- Numeric tempBand;
- Numeric SumValue(0);
- Numeric i;
- Numeric j(0);
- Numeric SumSqr(0);
- Numeric VarPSValue;
-
- Begin
- mtBarCnt = MtBar(TimeFrame,BarsBack,refCurBar,refOpen,refHigh,refLow,refClose,refVol,refOpenInt);
- mtClose = refClose;
- For i = 1 to Length
- {
- If (mtClose[j] == InvalidNumeric) SumValue = InvalidNumeric;
- Else
- {
- SumValue = SumValue + mtClose[j];
- j = j + mtBarCnt[j];
- }
- }
- oMidLine = SumValue/Length;
- If(Length > 1)
- {
- j = 0;
- for i = 0 to Length - 1
- {
- SumSqr = SumSqr + Sqr( mtClose[j] - oMidLine ) ;
- j = j + mtBarCnt[j];
- }
- VarPSValue = SumSqr / (Length - 1) ;
- }Else
- {
- VarPSValue = 0;
- }
- tempBand = iif(VarPSValue > 0,Sqrt(VarPSValue),0);
- oUpLine = oMidLine + Offset * tempBand;
- oDownLine = oMidLine - Offset * tempBand;
- Return mtBarCnt;
- End
復(fù)制代碼
再寫個(gè)公式應(yīng)用的例子,新建公式應(yīng)用TestMtBoll,代碼如下:
- Params
- Numeric TimeFrame(1440); // 目標(biāo)時(shí)間周期參數(shù),參數(shù)說明參見MtBar
- Numeric BarsBack(1); // 目標(biāo)時(shí)間周期BAR偏移參數(shù),說明見MtBar函數(shù)
- Numeric Length(20);
- Numeric Offset(2);
- Numeric Lots(1);
- Vars
- NumericSeries UpLine; // 上軌
- NumericSeries DownLine; // 下軌
- NumericSeries MidLine; // 中軌
- NumericSeries PreBar;
- Numeric UpL;
- Numeric DnL;
- Numeric MdL;
- Begin
- PreBar = MtBOLL(TimeFrame,BarsBack,Length,Offset,UpL,DnL,MdL);
- UpLine = UpL;
- DownLine = DnL;
- MidLine = MdL;
- PlotNumeric("UpLine",UpLine);
- PlotNumeric("DownLine",DownLine);
- PlotNumeric("MidLine",MidLine);
-
- If ( MarketPosition != 1 and Close[1]>MidLine[1] and UpLine[1]>UpLine[PreBar] and MidLine[1]>MidLine[PreBar])
- {
- Buy(Lots,Open);
- }
- If ( MarketPosition !=-1 and Close[1]<MidLine[1] and DownLine[1]<DownLine[PreBar] and MidLine[1]<MidLine[PreBar])
- {
- SellShort(Lots,Open);
- }
- End
跨 周期RSI指標(biāo),新建函數(shù)MtRSI,代碼如下:
- Params
- Numeric TimeFrame(1440); // 目標(biāo)時(shí)間周期參數(shù),參數(shù)說明參見MtBar
- Numeric BarsBack(1); // 目標(biāo)時(shí)間周期BAR偏移參數(shù),說明見MtBar函數(shù)
- Numeric Length(14) ;
- NumericRef oRSIValue;
- Vars
- NumericSeries mtBarCnt;
- NumericSeries mtClose;
- Numeric refCurBar;
- Numeric refOpen;
- Numeric refHigh;
- Numeric refLow;
- Numeric refClose;
- Numeric refVol;
- Numeric refOpenInt;
- NumericSeries NetChgAvg( 0 );
- NumericSeries TotChgAvg( 0 );
- Numeric Change( 0 );
- Numeric SF( 0 );
- Numeric ChgRatio( 0 ) ;
- Numeric RSIValue;
- BoolSeries bStopLoop;
- Numeric i;
- Numeric j(0);
- Numeric K;
- Numeric nbClose;
- Numeric nbClose1;
- Begin
- mtBarCnt = MtBar(TimeFrame,BarsBack,refCurBar,refOpen,refHigh,refLow,refClose,refVol,refOpenInt);
- mtClose = refClose;
- SF = 1/Length;
- bStopLoop = bStopLoop[1];
- If (bStopLoop!=true)
- {
- j = 0;
- For i = 0 to Length
- {
- If (mtClose[j] == InvalidNumeric)
- {
- nbClose = InvalidNumeric;
- i = Length + 1;
- }
- Else
- {
- nbClose = mtClose[j];
- j = j + mtBarCnt[j];
- }
- }
- if(nbClose!=InvalidNumeric and mtClose[j] != InvalidNumeric) bStopLoop = true;
- }
- If(nbClose == InvalidNumeric)
- {
- RSIValue = InvalidNumeric;
- }Else
- {
- If(bStopLoop !=true)
- {
- NetChgAvg = ( mtClose - nbClose ) / Length ;
- j = 0;
- K = 0;
- For i = 1 To Length
- {
- j = K + mtBarCnt[K];
- TotChgAvg = TotChgAvg+Abs(mtClose[K] - mtClose[j]);
- K = j ;
- }
- TotChgAvg = TotChgAvg / Length;
- }
- Else
- {
- Change = mtClose - mtClose[mtBarCnt] ;
- NetChgAvg = NetChgAvg[mtBarCnt] + SF * ( Change - NetChgAvg[mtBarCnt] ) ;
- TotChgAvg = TotChgAvg[mtBarCnt] + SF * ( Abs( Change ) - TotChgAvg[mtBarCnt] ) ;
- }
-
- If( TotChgAvg <> 0 )
- {
- ChgRatio = NetChgAvg / TotChgAvg ;
- }else
- {
- ChgRatio = 0 ;
- }
-
- RSIValue = 50 * ( ChgRatio + 1 ) ;
- }
-
- oRSIValue = RSIValue;
- return mtBarCnt;
- End
復(fù)制代碼
再新建公式應(yīng)用TestMtRsi,把傳統(tǒng)的RSI使用方法改了一下,RSI大于50做多,小于50做空。
- Params
- Numeric TimeFrame(1440); // 日線
- Numeric BarsBack(1); // 求前一日的值
- Numeric Length(14) ;
- Numeric OverSold(30) ;
- Numeric OverBought(70) ;
- Numeric Lots(1);
- Vars
- NumericSeries RSIValue;
- Numeric oRSIValue;
- Numeric PreBar;
- Begin
- PreBar = MtRSI(TimeFrame,BarsBack,Length,oRSIValue);
- RSIValue = oRSIValue;
- PlotNumeric("RSI",RSIValue);
- PlotNumeric("超買",OverBought);
- PlotNumeric("超賣",OverSold);
- If (RSIValue>50)
- {
- Buy(Lots,Open);
- }
- If (RSIValue<50)
- {
- SellShort(Lots,Open);
- }
- End
至此,我已經(jīng)做好的幾個(gè)跨 周期的指標(biāo),已經(jīng)全部發(fā)表完畢。接下來,試著完成致富朋友的要求。其實(shí)有了前面的例子,要實(shí)現(xiàn)同時(shí)讀取多個(gè) 周期的MACD和KDJ值不是什么難事,就是同樣的代碼復(fù)制黏貼,多寫幾遍就是了。這里我只以MACD為例子,在1分鐘圖上使用,KDJ的部分致富可以自己完成。計(jì)算出來的指標(biāo)我就不顯示了,太多了。我只把提示買入賣出的信息作為注釋信息輸出了,供你參考吧。
- Params
- Numeric FastLength(12);
- Numeric SlowLength(26);
- Numeric MACDLength(9);
- Vars
- Numeric PreBar60;
- Numeric ooMACDValue60;
- Numeric ooAvgMACD60;
- NumericSeries MACDVAlue60;
- NumericSeries AvgMACD60;
- NumericSeries MACDDiff60;
-
- Numeric PreBar30;
- Numeric ooMACDValue30;
- Numeric ooAvgMACD30;
- NumericSeries MACDVAlue30;
- NumericSeries AvgMACD30;
- NumericSeries MACDDiff30;
- Numeric PreBar15;
- Numeric ooMACDValue15;
- Numeric ooAvgMACD15;
- NumericSeries MACDVAlue15;
- NumericSeries AvgMACD15;
- NumericSeries MACDDiff15;
-
- Numeric PreBar5;
- Numeric ooMACDValue5;
- Numeric ooAvgMACD5;
- NumericSeries MACDVAlue5;
- NumericSeries AvgMACD5;
- NumericSeries MACDDiff5;
- Numeric PreBar3;
- Numeric ooMACDValue3;
- Numeric ooAvgMACD3;
- NumericSeries MACDVAlue3;
- NumericSeries AvgMACD3;
- NumericSeries MACDDiff3;
- Begin
- PreBar60 = MtMACD(60,1,FastLength,SlowLength,MACDLength,ooMACDValue60,ooAvgMACD60);
- MACDVAlue60 = ooMACDValue60;
- AvgMACD60 = ooAvgMACD60;
- MACDDiff60 = MACDValue60 - AvgMACD60;
- If (MacdDiff60[PreBar60]<0 and MacdDiff60>0)
- Commentary("60分鐘MACD買入信號");
- If (MacdDiff60[PreBar60]>0 and MacdDiff60<0)
- Commentary("60分鐘MACD賣出信號");
- PreBar30 = MtMACD(30,1,FastLength,SlowLength,MACDLength,ooMACDValue30,ooAvgMACD30);
- MACDVAlue30 = ooMACDValue30;
- AvgMACD30 = ooAvgMACD30;
- MACDDiff30 = MACDValue30 - AvgMACD30;
- If (MacdDiff30[PreBar30]<0 and MacdDiff30>0)
- Commentary("30分鐘MACD買入信號");
- If (MacdDiff30[PreBar30]>0 and MacdDiff30<0)
- Commentary("30分鐘MACD賣出信號");
- PreBar15 = MtMACD(15,1,FastLength,SlowLength,MACDLength,ooMACDValue15,ooAvgMACD15);
- MACDVAlue15 = ooMACDValue15;
- AvgMACD15 = ooAvgMACD15;
- MACDDiff15 = MACDValue15 - AvgMACD15;
- If (MacdDiff15[PreBar15]<0 and MacdDiff15>0)
- Commentary("15分鐘MACD買入信號");
- If (MacdDiff15[PreBar15]>0 and MacdDiff15<0)
- Commentary("15分鐘MACD賣出信號");
- PreBar5 = MtMACD(5,1,FastLength,SlowLength,MACDLength,ooMACDValue5,ooAvgMACD5);
- MACDVAlue5 = ooMACDValue5;
- AvgMACD5 = ooAvgMACD5;
- MACDDiff5 = MACDValue5 - AvgMACD5;
- If (MacdDiff5[PreBar5]<0 and MacdDiff5>0)
- Commentary("5分鐘MACD買入信號");
- If (MacdDiff5[PreBar5]>0 and MacdDiff5<0)
- Commentary("5分鐘MACD賣出信號");
- PreBar3 = MtMACD(3,1,FastLength,SlowLength,MACDLength,ooMACDValue3,ooAvgMACD3);
- MACDVAlue3 = ooMACDValue3;
- AvgMACD3 = ooAvgMACD3;
- MACDDiff3 = MACDValue3 - AvgMACD3;
- If (MacdDiff3[PreBar3]<0 and MacdDiff3>0)
- Commentary("3分鐘MACD買入信號");
- If (MacdDiff3[PreBar3]>0 and MacdDiff3<0)
- Commentary("3分鐘MACD賣出信號");
- End
|