舉例而言,有價格數據資料八筆,依大小排序為 : X(1)、X(2)、...、X(7)、X(8),定義全距(Range)=Max-Min=X(1)-X(8)=R(1,8)。
依此類推,R(3,6)就是X(3)-X(6)的距離,所以評估波動度的方法,又多了一個想法,就是R(1,8)/R(3,6)的比值,但因為運算便利考量,比值的檢定統計量改以R(1,8)的平方-R(3,6)的平方,透過標準常態分配亂數模擬,可以為此統計檢定量製表,以8筆資料為例,10萬次模擬的結果是,95%的顯著水準門檻值為16.85
MC程式碼構想如下
x=Average(High,8);
y=StdDev(High,8);
z[1]=(Highest(High,8)-x)/y;
z[2]=(Lowest(High,8)-x)/y;
z[3]=(NthHighest(3,High,8)-x)/y; z[4]=(NthHighest(6,High,8)-x)/y;
u=Power(z[1]-z[2],2);
v=Power(z[3]-z[4],2);
If u-v16.85 then Buy next bar at Highest(High,8) stop;
Excel VBA參考程式碼如下
Public RankSort(1 To 8) As Variant
Const FixConst As Variant = 0.398942284 '1/sqr(2*3.1415926)
Public Sub avgRank()
'模擬常態分配隨機變數的順序統計量期望值
Dim seed, seedCalc, nextSeed, NorValue As Variant
Dim ArrNorRandom(1 To 8), x(1 To 100000) As Variant
Dim iith, testNum, xTemp, yTemp As Long
For testNum = 1 To 100000
iith = 1
Do
seed = Int(Rnd() * 1000000)
seedCalc = ((seed - 500000) * 0.000001) * 8 '模擬4個標準差範圍的 N(0,1) 常態分配變數
NorValue = Format(FixConst * Exp(-0.5 * seedCalc * seedCalc), "0.000000") '常態分配機率函數的數值
nextSeed = 0
nextSeed = Int(0.001 * (seed * seed))
nextSeed = Format(0.000001 * nextSeed - Int(0.000001 * nextSeed), "0.000000")
nextSeed = Int(1000000 * nextSeed)
nextSeed = Int(((23 + iith * 0.01) * nextSeed + 1011 * iith)) Mod 999999
nextSeed = Format(nextSeed * 0.00001 * FixConst, "0.000000")
If (nextSeed) NorValue Then
ArrNorRandom(iith) = seedCalc
iith = iith + 1
End If
Loop While iith = 8
'泡沫排序法
Dim swapTemp, ii, jj As Variant
For jj = 0 To 6
For ii = 1 To 7 - jj
If ArrNorRandom(ii) ArrNorRandom(ii + 1) Then swapTemp = ""
swapTemp = ArrNorRandom(ii + 1)
ArrNorRandom(ii + 1) = ArrNorRandom(ii)
ArrNorRandom(ii) = swapTemp
End If
Next ii
Next jj
xTemp = (ArrNorRandom(8) - ArrNorRandom(1)) * (ArrNorRandom(8) - ArrNorRandom(1))
yTemp = (ArrNorRandom(6) - ArrNorRandom(3)) * (ArrNorRandom(6) - ArrNorRandom(3))
x(testNum) = xTemp - yTemp Next 'For testNum = 1 To 100000
For jj = 0 To 4999 '資料數據共計10萬筆,求解第95001序位
For ii = 1 To 100000 - 1 - jj
If x(ii) x(ii + 1) Then
xTemp = 0
xTemp = x(ii + 1)
x(ii + 1) = x(ii)
x(ii) = xTemp
End If
Next ii
Next jj
Debug.Print x(95001)
End Sub