'*******************************************************************************************
'该模块主要用来保存自定义公式函数主函数,不要拿做他用
'*******************************************************************************************
Function TriangleShape(Formula,Cyc,SCyc,ECyc)
'msgbox cyc & "-" & scyc & "-" & ecyc
'该函数计算当前位置图形是否是三角形突破,如果是则返回1,否则返回0。该函数只有在选股时才能使用。
TriangleShape=0
'如果是分时数据或者分笔成交那么直接返回
If Formula.ParentGrid.DataType = 0 or Formula.ParentGrid.CycType = 10 Then
Exit Function
End If
'得到K线数据对象
Set History = Formula.ParentGrid.GetHistoryData()
If History.Count < Cyc+3 Then
Exit Function
End If
'为了加快处理速度,只有公式在执行最后一个周期时使用。这就意味着只能在选股时使用该函数
'如果你在公式测试中使用,请注释掉以下语句
If Formula.IndexData < History.Count-1 Then Exit Function
'下面的代码判断当前图形是否可能为三角形态
'开始位置SCyc周期内的高低价格为三角形态的开始
StartPos = Formula.IndexData - (Cyc+2)
EndPos = Formula.IndexData-3
High = History.High(StartPos)
Low = History.Low(StartPos)
For I = StartPos To StartPos+SCyc
If History.High(I) > High Then
High = History.High(I)
End If
If History.Low(i) < Low Then
Low = History.Low(I)
End If
Next
'ECyc周期内的周期高低价格为三角的结束
High2 = History.High(EndPos - ECyc)
Low2 = History.Low(Endpos-ECyc)
for i = endpos - ECyc to endpos
If History.High(I) > High2 Then
High2 = History.High(I)
End If
If History.Low(i) < Low2 Then
Low2 = History.Low(I)
End If
next
'最后图形范围为开始图形的1/2的话,初步表明是可以做为三角形的结束
if high2-low2 <= 0 then
exit function
end if
If (High-Low) / (high2-low2) < 2 Then
Exit Function
End if
if high2 > high or low2 < low then
exit function
end if
'如果中间有超过三角形边界的地方,三角图形则不成立
'用斜率计算图形边界
'计算上边界
Slope = (high2-high) / (Endpos-StartPos)
b = high - slope * startpos
for i = startpos+SCyc to endpos
temp = slope * i + b
price = (history.open(i)+history.close(i))/2
if temp < price then
exit function
end if
next
'计算下边界
slope = (low2-low) / (endpos - startpos)
b = low - slope * startpos
for i = startpos to endpos-3
temp = slope * i + b
price = (history.open(i)+history.close(i))/2
if temp > price then
exit function
end if
next
’如果3日后的价格突破了三角型结束的上边线认为突破成功
if history.close(Formula.IndexData) > high2 then
TriangleShape = 1
end if
End Function
'计算逐周期模式下指定周期长度的收盘价均价
Function CU_MA2(Formula,cyc)
'得到K线数据对象
Set History = Formula.ParentGrid.GetHistoryData()
'若当前周期尚未到计算周期,不参与计算
if Formula.IndexData < cyc-1 then
CU_MA2 = 0
exit function
end if
DataCount = 0
for i = Formula.IndexData-cyc+1 to Formula.IndexData
'累加收盘价
DataCount = DataCount + history.close(i)
next
CU_MA2 = DataCount / cyc
End Function
'计算序列模式下指定周期长度的收盘价均价
Function CU_MA1(Formula,CLOSE,CYC)
CU_MA1=0
'防止公式逐周期模式时调用
If Formula.WorkMode = 0 Then
Exit Function
End If
'CLOSE数组数据长度一定会与Formula.DataSize-1相等
DataCount = UBound(CLOSE)
If DataCount <> Formula.DataSize-1 Then
Exit Function
End If
'定义一个计算返回的数组
Dim ResultMa
Redim ResultMa(DataCount)
For i = Cyc-1 To Formula.DataSize-1
Count = 0
For k = i-(Cyc-1) To i
Count = Count + CLOSE(k)
Next
ResultMa(i) = Count / Cyc
Next
'返回一个计算完毕的均线数组
CU_MA1 = ResultMa
End Function