公式調(diào)用VBA函數(shù),參數(shù)返回問題 [金字塔]
- 咨詢內(nèi)容:
Function zbDate(Formula,ZB,XH)
'系統(tǒng)會(huì)在逐K線模式解釋公式時(shí)的每個(gè)周期都會(huì)調(diào)用此函數(shù)一遍,因此設(shè)計(jì)時(shí)應(yīng)該注重程序的執(zhí)行效率,不要重復(fù)的執(zhí)行一些沒必要的代碼dim resultMa
redim resultMa(2)
'msgbox ubound(resultMa)
resultMa(0)=-2
resultMa(1)=-2
zbDate=resultMa
'sgbox zbdate(1)'if Formula.workmode=0 then '如果是逐漸K模式,直接退出函數(shù)
' exit function
'end if
zbName="MACD"
seriseName="MACD1"
for i=0 to technic.GridCount-1 '循環(huán)獲得指標(biāo)所在的grid
Set Grid = Technic.GetGridByIndex(i)
for j=0 to Grid.formulaCount-1
set formula1=grid.getformulaByIndex(j)
if formula1.name=zbName then
exit for
end if
next
if j<> Grid.formulaCount then exit for
next
if i=technic.GridCount then '未找到對(duì)應(yīng)的指標(biāo)
exit function
end ifset history=grid.gethistorydata()
zb1=formula1.vardata(seriseName)
dataCount=UBound(zb1)
redim resultMa(dataCount)for i=formula1.dataSize-1 to 1 step -1
if zb1(i)>0 and zb1(i-1)<=0 then
a1=history.date(i)
a2=year(a1)*10000000000+month(a1)*100000000+day(a1)*1000000+hour(a1)*10000+minute(a1)*100+second(a1)'這里為了返回柱線的日期時(shí)間,特把其轉(zhuǎn)換為數(shù)值(公式只能返回?cái)?shù)值?),在VBA下用宏運(yùn)行是沒有問題的,但在公式中自定義函數(shù)調(diào)用傳遞回來(lái)時(shí)在小時(shí)、分鐘、秒上出錯(cuò),比如2013年9月2日11點(diǎn)30分,傳遞回'來(lái)的結(jié)果是20130901786624。請(qǐng)問高手們幫忙解決,急急……
msgbox a2
resultMa(i)=a2
EXIT FOR
else
resultMa(i)=-1
end if
nextzbDate=resultMa
End Function - 金字塔客服:
用cdbl試試
- 用戶回復(fù):
金字塔公式的數(shù)值最大是幾位的?似乎數(shù)值超過(guò)10位之后,數(shù)值計(jì)算就會(huì)出錯(cuò),如果是,這就是一個(gè)很大的bug,希望改進(jìn)
- 網(wǎng)友回復(fù):
32位的操作系統(tǒng).
2的31次方,值為21 4748 3648
您的日期值已經(jīng)遠(yuǎn)遠(yuǎn)超過(guò)這個(gè)值了,所以出現(xiàn)了異常顯示
分成兩個(gè)部分傳遞, 年月日 + 時(shí)分秒
- 網(wǎng)友回復(fù):
謝謝,但改成這樣還是有錯(cuò)
Function zbDate(Formula,ZB,XH)
'系統(tǒng)會(huì)在逐K線模式解釋公式時(shí)的每個(gè)周期都會(huì)調(diào)用此函數(shù)一遍,因此設(shè)計(jì)時(shí)應(yīng)該注重程序的執(zhí)行效率,不要重復(fù)的執(zhí)行一些沒必要的代碼dim resultMa
redim resultMa(2)
'msgbox ubound(resultMa)
resultMa(0)=-2
resultMa(1)=-2
zbDate=resultMa
'sgbox zbdate(1)'if Formula.workmode=0 then '如果是逐漸K模式,直接退出函數(shù)
' exit function
'end if
zbName="MACD"
seriseName="MACD1"
for i=0 to technic.GridCount-1 '循環(huán)獲得指標(biāo)所在的grid
Set Grid = Technic.GetGridByIndex(i)
for j=0 to Grid.formulaCount-1
set formula1=grid.getformulaByIndex(j)
if formula1.name=zbName then
exit for
end if
next
if j<> Grid.formulaCount then exit for
next
if i=technic.GridCount then '未找到對(duì)應(yīng)的指標(biāo)
exit function
end if
cycT=grid.cyctype
'msgbox cycT
set history=grid.gethistorydata()
zb1=formula1.vardata(seriseName)
dataCount=UBound(zb1)
redim resultMa(dataCount)for i=formula1.dataSize-1 to 1 step -1
if zb1(i)>0 and zb1(i-1)<=0 then
a1=history.date(i)
if cycT<=4 or (cycT>=10 and cycT<=14) or (cycT>=17 and cycT<=18) then '由于返回值超過(guò)10位后在公式調(diào)用中會(huì)出錯(cuò),所以這里在日頻度以下只返回時(shí)分秒,日頻度以上返回年月日
a2=cdbl(hour(a1))*10000+cdbl(minute(a1))*100+cdbl(second(a1))
else
a2=cdbl(year(a1))*10000+cdbl(month(a1))*100+cdbl(day(a1))
end if
'msgbox a1
resultMa(i)=a2
' EXIT FOR在日線先本來(lái)應(yīng)該返回20130903的,但在公式中顯示出來(lái)的是20130904?好多個(gè)日子都是這樣,請(qǐng)問是什么原因?你們可以拷貝代碼去測(cè)試,謝謝!
else
resultMa(i)=-1
end if
nextzbDate=resultMa
End Function
有思路,想編寫各種指標(biāo)公式,程序化交易模型,選股公式,預(yù)警公式的朋友
可聯(lián)系技術(shù)人員 QQ: 1145508240 進(jìn)行 有償 編寫!(不貴!點(diǎn)擊查看價(jià)格!)
相關(guān)文章
-
沒有相關(guān)內(nèi)容